EthTestApp.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Copyright (C) 2013 OpenSim Ltd.
  3. // @author: Zoltan Bojthe
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. //
  18. #include "inet/common/INETDefs.h"
  19. #include "inet/linklayer/ethernet/EtherFrame_m.h"
  20. #include "inet/linklayer/common/MACAddress.h"
  21. namespace inet {
  22. class INET_API EthTestApp : public cSimpleModule
  23. {
  24. protected:
  25. MACAddress destAddr;
  26. public:
  27. EthTestApp() {}
  28. protected:
  29. void parseScript(const char *script);
  30. void processIncomingPacket(cMessage *msg);
  31. void createCommand(simtime_t t, int bytes);
  32. virtual void initialize();
  33. virtual void finish();
  34. virtual void handleMessage(cMessage *msg);
  35. };
  36. Define_Module(EthTestApp);
  37. void EthTestApp::initialize()
  38. {
  39. const char *addr = par("destAddr");
  40. destAddr = MACAddress(addr);
  41. const char *script = par("script");
  42. parseScript(script);
  43. }
  44. void EthTestApp::createCommand(simtime_t t, int bytes)
  45. {
  46. char name[100];
  47. sprintf(name, "PK at %s: %i Bytes", SIMTIME_STR(t), bytes);
  48. EtherFrame *packet = new EtherFrame(name);
  49. packet->setByteLength(bytes);
  50. packet->setDest(destAddr);
  51. //TODO set packet->destAddr
  52. scheduleAt(t, packet);
  53. }
  54. void EthTestApp::parseScript(const char *script)
  55. {
  56. const char *s = script;
  57. while (isspace(*s)) s++;
  58. while (*s)
  59. {
  60. // simtime in seconds
  61. char *os;
  62. double sendingTime = strtod(s,&os);
  63. if ((s == os) | (*os != ':'))
  64. throw cRuntimeError("syntax error in script: missing ':' after time");
  65. s = os;
  66. s++;
  67. while (isspace(*s)) s++;
  68. // length in bytes
  69. if (!isdigit(*s))
  70. throw cRuntimeError("syntax error in script: wrong bytelength spec");
  71. int bytes = atoi(s);
  72. while (isdigit(*s)) s++;
  73. // add command
  74. createCommand(sendingTime, bytes);
  75. // skip delimiter
  76. while (isspace(*s)) s++;
  77. }
  78. }
  79. void EthTestApp::handleMessage(cMessage *msg)
  80. {
  81. if (msg->isSelfMessage())
  82. send(msg, "out");
  83. else
  84. processIncomingPacket(msg);
  85. }
  86. void EthTestApp::processIncomingPacket(cMessage *msg)
  87. {
  88. delete msg;
  89. }
  90. void EthTestApp::finish()
  91. {
  92. }
  93. } // namespace inet