TestRadio.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "TestRadio.h"
  20. #include "TestOperation.h"
  21. namespace inet {
  22. Define_Module(TestRadio);
  23. bool TestRadio::handleOperationStage(LifecycleOperation *operation, int stage, IDoneCallback *doneCallback)
  24. {
  25. Enter_Method_Silent();
  26. if (dynamic_cast<TestNodeStartOperation *>(operation)) {
  27. if (stage == 0) {
  28. scheduleAt(simTime() + 1, &turnOnTransmitter);
  29. EV << getFullPath() << " turning on transmitter" << endl;
  30. }
  31. else if (stage == 1) {
  32. scheduleAt(simTime() + 2, &turnOnReceiver);
  33. EV << getFullPath() << " turning on receiver" << endl;
  34. }
  35. else if (stage == 2 || stage == 3)
  36. return true;
  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 || stage == 3)
  44. return true;
  45. else if (stage == 1) {
  46. scheduleAt(simTime() + 2, &turnOffReceiver);
  47. EV << getFullPath() << " turning off receiver" << endl;
  48. }
  49. else if (stage == 2) {
  50. scheduleAt(simTime() + 1, &turnOffTransmitter);
  51. EV << getFullPath() << " turning off transmitter" << endl;
  52. }
  53. else
  54. throw cRuntimeError("Unknown stage");
  55. this->doneCallback = doneCallback;
  56. return false;
  57. }
  58. else
  59. return true;
  60. }
  61. void TestRadio::initialize(int stage)
  62. {
  63. receiverTurnedOn = false;
  64. transmitterTurnedOn = false;
  65. }
  66. void TestRadio::handleMessage(cMessage * message)
  67. {
  68. if (message == &turnOnTransmitter) {
  69. transmitterTurnedOn = true;
  70. EV << getFullPath() << " transmitter turned on" << endl;
  71. doneCallback->invoke();
  72. }
  73. else if (message == &turnOnReceiver) {
  74. receiverTurnedOn = true;
  75. EV << getFullPath() << " receiver turned on" << endl;
  76. doneCallback->invoke();
  77. }
  78. else if (message == &turnOffTransmitter) {
  79. transmitterTurnedOn = false;
  80. EV << getFullPath() << " transmitter turned off" << endl;
  81. doneCallback->invoke();
  82. }
  83. else if (message == &turnOffReceiver) {
  84. receiverTurnedOn = false;
  85. EV << getFullPath() << " receiver turned off" << endl;
  86. doneCallback->invoke();
  87. }
  88. else
  89. throw cRuntimeError("Unknown message");
  90. }
  91. }