QQ.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // Copyright 2012 OpenSim Ltd.
  3. //
  4. // This library is free software, you can redistribute it
  5. // and/or modify
  6. // it under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation;
  8. // either version 2 of the License, or any later version.
  9. // The library 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.
  12. // See the GNU Lesser General Public License for more details.
  13. //
  14. // @author Zoltan Bojthe
  15. //
  16. #include "inet/common/INETDefs.h"
  17. namespace inet {
  18. class INET_API QQ : public cSimpleModule
  19. {
  20. protected:
  21. cQueue queue;
  22. cGate *outGate;
  23. cMessage *timer;
  24. public:
  25. QQ() : outGate(0), timer(0) {}
  26. ~QQ();
  27. protected:
  28. virtual void initialize();
  29. virtual void handleMessage(cMessage *msg);
  30. };
  31. Define_Module(QQ);
  32. void QQ::initialize()
  33. {
  34. outGate = gate("out");
  35. timer = new cMessage("TIMER");
  36. }
  37. void QQ::handleMessage(cMessage *msg)
  38. {
  39. if (msg != timer)
  40. queue.insert(msg);
  41. cChannel *ch = outGate->findTransmissionChannel();
  42. if (outGate->isConnected())
  43. {
  44. if (ch && ch->isBusy())
  45. {
  46. cancelEvent(timer);
  47. scheduleAt(ch->getTransmissionFinishTime(), timer);
  48. }
  49. else
  50. {
  51. msg = check_and_cast<cMessage *>(queue.pop());
  52. send(msg, outGate);
  53. }
  54. }
  55. }
  56. QQ::~QQ()
  57. {
  58. cancelAndDelete(timer);
  59. }
  60. } // namespace inet