SomeUDPApp.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // Copyright (C) 2000 Institut fuer Telematik, Universitaet Karlsruhe
  3. //
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. //
  18. #include "inet/common/INETDefs.h"
  19. #include "SomeUDPApp.h"
  20. #include "inet/transportlayer/contract/udp/UDPControlInfo_m.h"
  21. #include "AddressResolver.h"
  22. Define_Module(SomeUDPApp);
  23. int SomeUDPApp::counter;
  24. void SomeUDPApp::initialize(int stage)
  25. {
  26. // because of AddressResolver, we need to wait until interfaces are registered,
  27. // address auto-assignment takes place etc.
  28. if (stage != INITSTAGE_APPLICATION_LAYER)
  29. return;
  30. counter = 0;
  31. numSent = 0;
  32. numReceived = 0;
  33. WATCH(numSent);
  34. WATCH(numReceived);
  35. localPort = par("localPort");
  36. destPort = par("destPort");
  37. msgLength = par("messageLength");
  38. const char *destAddrs = par("destAddresses");
  39. cStringTokenizer tokenizer(destAddrs);
  40. const char *token;
  41. while ((token = tokenizer.nextToken())!=NULL)
  42. destAddresses.push_back(AddressResolver().resolve(token));
  43. if (destAddresses.empty())
  44. return;
  45. bindToPort(localPort);
  46. cMessage *timer = new cMessage("sendTimer");
  47. scheduleAt((double)par("sendInterval"), timer);
  48. }
  49. Address SomeUDPApp::chooseDestAddr()
  50. {
  51. int k = intrand(destAddresses.size());
  52. return destAddresses[k];
  53. }
  54. void SomeUDPApp::sendPacket()
  55. {
  56. char msgName[32];
  57. sprintf(msgName,"SomeUDPAppData-%d", counter++);
  58. cMessage *payload = new cMessage(msgName);
  59. payload->setLength(msgLength);
  60. Address destAddr = chooseDestAddr();
  61. sendToUDP(payload, localPort, destAddr, destPort);
  62. numSent++;
  63. }
  64. void SomeUDPApp::handleMessage(cMessage *msg)
  65. {
  66. if (msg->isSelfMessage())
  67. {
  68. // send, then reschedule next sending
  69. sendPacket();
  70. scheduleAt(simTime()+(double)par("sendInterval"), msg);
  71. }
  72. else
  73. {
  74. // process incoming packet
  75. processPacket(msg);
  76. }
  77. }
  78. void SomeUDPApp::refreshDisplay() const
  79. {
  80. char buf[40];
  81. sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
  82. displayString().setTagArg("t",0,buf);
  83. }
  84. void SomeUDPApp::processPacket(cMessage *msg)
  85. {
  86. EV << "Received packet: ";
  87. printPacket(msg);
  88. delete msg;
  89. numReceived++;
  90. }