MeterTestApp.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Copyright (C) 2012 Opensim Ltd.
  3. // Author: Tamas Borbely
  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 <sstream>
  19. #include <fstream>
  20. #include <iomanip>
  21. #include "inet/common/INETDefs.h"
  22. #include "inet/networklayer/ipv4/IPv4Datagram.h"
  23. using namespace std;
  24. namespace inet {
  25. class MeterTestApp : public cSimpleModule
  26. {
  27. int numPackets;
  28. simtime_t stopTime;
  29. vector<string> colors;
  30. int counter;
  31. cMessage *timer;
  32. ofstream out;
  33. protected:
  34. virtual void initialize();
  35. virtual void finalize();
  36. virtual void handleMessage(cMessage *msg);
  37. };
  38. Define_Module(MeterTestApp);
  39. void MeterTestApp::initialize()
  40. {
  41. numPackets = par("numPackets");
  42. stopTime = par("stopTime");
  43. colors = cStringTokenizer(par("colors")).asVector();
  44. if ((int)colors.size() != gateSize("in"))
  45. throw cRuntimeError("Too %s colors are specified in the colors parameter.",
  46. (int)colors.size() < gateSize("in") ? "few" : "many");
  47. counter = 0;
  48. timer = new cMessage("Timer");
  49. const char *filename = par("resultFile");
  50. out.open(filename);
  51. if (out.fail())
  52. throw cRuntimeError("Can not open file %s", filename);
  53. out << left << setw(12) << "Packet" << setw(12) << "Conformance" << "\n";
  54. double startTime = par("startTime");
  55. if (stopTime < SIMTIME_ZERO || stopTime >= startTime)
  56. scheduleAt(startTime, timer);
  57. }
  58. void MeterTestApp::finalize()
  59. {
  60. cancelAndDelete(timer);
  61. out.close();
  62. }
  63. void MeterTestApp::handleMessage(cMessage *msg)
  64. {
  65. if (msg->isSelfMessage())
  66. {
  67. ostringstream packetName;
  68. packetName << "packet-" << (++counter);
  69. cPacket *packet = new IPv4Datagram(packetName.str().c_str());
  70. packet->setByteLength(par("packetSize").longValue());
  71. send(packet, "out");
  72. if ((numPackets == 0 || counter < numPackets) &&
  73. (stopTime < SIMTIME_ZERO || simTime() < stopTime))
  74. scheduleAt(simTime() + par("iaTime"), msg);
  75. else
  76. delete msg;
  77. }
  78. else
  79. {
  80. int gateIndex = msg->getArrivalGate()->getIndex();
  81. out << left << setw(12) << msg->getName() << setw(12) << colors[gateIndex] << "\n";
  82. delete msg;
  83. }
  84. }
  85. } // namespace inet