MpduGen.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Copyright (C) 2015 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: Benjamin Seregi
  18. //
  19. #include "MpduGen.h"
  20. #include "inet/networklayer/common/L3AddressResolver.h"
  21. #include "inet/transportlayer/contract/udp/UDPControlInfo_m.h"
  22. namespace inet {
  23. Define_Module(MpduGen);
  24. simsignal_t MpduGen::sentPkSignal = registerSignal("sentPk");
  25. simsignal_t MpduGen::rcvdPkSignal = registerSignal("rcvdPk");
  26. void MpduGen::initialize(int stage) {
  27. ApplicationBase::initialize(stage);
  28. if (stage == INITSTAGE_LOCAL) {
  29. localPort = par("localPort");
  30. destPort = par("destPort");
  31. }
  32. else if (stage == INITSTAGE_LAST) {
  33. selfMsg = new cMessage("Self msg");
  34. scheduleAt(simTime() + par("startTime").doubleValue(), selfMsg);
  35. }
  36. }
  37. void MpduGen::processPacket(cPacket *pk) {
  38. emit(rcvdPkSignal, pk);
  39. EV_INFO << "Received packet: " << UDPSocket::getReceivedPacketInfo(pk) << endl;
  40. delete pk;
  41. numReceived++;
  42. }
  43. void MpduGen::sendPackets() {
  44. socket.setOutputGate(gate("udpOut"));
  45. const char *localAddress = par("localAddress");
  46. socket.bind(*localAddress ? L3AddressResolver().resolve(localAddress) : L3Address(), localPort);
  47. const char *destAddrStr = par("destAddress");
  48. const char *packets = par("packets").stringValue();
  49. L3Address destAddr = L3AddressResolver().resolve(destAddrStr);
  50. int len = strlen(packets);
  51. const char *packetName = par("packetName").stringValue();
  52. for (int i = 0; i < len; i++) {
  53. std::ostringstream str;
  54. str << packetName << "-" << i;
  55. cPacket *payload = new cPacket(str.str().c_str());
  56. if (packets[i] == 'L') {
  57. payload->setByteLength(par("longPacketSize").longValue());
  58. }
  59. else if (packets[i] == 'S') {
  60. payload->setByteLength(par("shortPacketSize").longValue());
  61. }
  62. else
  63. throw cRuntimeError("Unknown packet type = %c", packets[i]);
  64. emit(sentPkSignal, payload);
  65. socket.sendTo(payload, destAddr, destPort);
  66. numSent++;
  67. }
  68. socket.close();
  69. }
  70. void MpduGen::handleMessageWhenUp(cMessage *msg) {
  71. if (msg->isSelfMessage()) {
  72. ASSERT(msg == selfMsg);
  73. sendPackets();
  74. }
  75. else if (msg->getKind() == UDP_I_DATA) {
  76. processPacket(PK(msg));
  77. }
  78. else if (msg->getKind() == UDP_I_ERROR) {
  79. EV_WARN << "Ignoring UDP error report\n";
  80. delete msg;
  81. }
  82. else {
  83. throw cRuntimeError("Unrecognized message (%s)%s", msg->getClassName(), msg->getName());
  84. }
  85. if (hasGUI()) {
  86. char buf[40];
  87. sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
  88. getDisplayString().setTagArg("t", 0, buf);
  89. }
  90. }
  91. }