TestProtocol.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Copyright (C) 2013 Opensim Ltd.
  3. //
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public License
  6. // as published by the Free Software Foundation; either version 2
  7. // of the License, or (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. //
  17. // author: Levente Meszaros (levy@omnetpp.org)
  18. //
  19. #include "TestProtocol.h"
  20. #include "TestOperation.h"
  21. namespace inet {
  22. Define_Module(TestProtocol);
  23. bool TestProtocol::handleOperationStage(LifecycleOperation *operation, int stage, IDoneCallback *doneCallback)
  24. {
  25. Enter_Method_Silent();
  26. if (dynamic_cast<TestNodeStartOperation *>(operation)) {
  27. if (stage == 0 || stage == 3)
  28. return true;
  29. else if (stage == 1) {
  30. scheduleAt(simTime() + 3, &sendOpen);
  31. EV << getFullPath() << " opening connection" << endl;
  32. }
  33. else if (stage == 2) {
  34. scheduleAt(simTime() + 2, &sendData);
  35. EV << getFullPath() << " sending initial data" << endl;
  36. }
  37. else
  38. throw cRuntimeError("Unknown stage");
  39. this->doneCallback = doneCallback;
  40. return false;
  41. }
  42. else if (dynamic_cast<TestNodeShutdownOperation *>(operation)) {
  43. if (stage == 0) {
  44. scheduleAt(simTime() + 2, &sendData);
  45. EV << getFullPath() << " sending final data" << endl;
  46. }
  47. else if (stage == 1) {
  48. scheduleAt(simTime() + 3, &sendClose);
  49. EV << getFullPath() << " closing connection" << endl;
  50. }
  51. else if (stage == 2 || stage == 3)
  52. return true;
  53. else
  54. throw cRuntimeError("Unknown stage");
  55. this->doneCallback = doneCallback;
  56. return false;
  57. }
  58. else
  59. return true;
  60. }
  61. void TestProtocol::initialize(int stage)
  62. {
  63. connectionOpen = false;
  64. dataSent = false;
  65. }
  66. void TestProtocol::handleMessage(cMessage * message)
  67. {
  68. if (message == &sendOpen) {
  69. connectionOpen = true;
  70. EV << getFullPath() << " connection open" << endl;
  71. doneCallback->invoke();
  72. }
  73. else if (message == &sendData) {
  74. dataSent = true;
  75. EV << getFullPath() << " data sent" << endl;
  76. doneCallback->invoke();
  77. }
  78. else if (message == &sendClose) {
  79. connectionOpen = false;
  80. EV << getFullPath() << " connection closed" << endl;
  81. doneCallback->invoke();
  82. }
  83. else
  84. throw cRuntimeError("Unknown message");
  85. }
  86. }