OverSim
I3TriggerRoutingTime.cc
Go to the documentation of this file.
1 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 
23 #include "I3BaseApp.h"
24 #include "I3.h"
25 
26 using namespace std;
27 
28 static cStdDev stats;
29 static bool statsDumped = false;
30 
31 class I3TRTServer : public I3 {
32 
33  void initializeApp(int stage);
34  void deliver(OverlayKey &key, cMessage *msg);
35  void finish();
36 };
37 
39 
40 class I3TRTClient : public I3BaseApp {
41  void initializeI3();
42  void handleTimerEvent(cMessage *msg);
43 };
44 
46 
47 
48 void I3TRTServer::initializeApp(int stage) {
49  statsDumped = false;
50  I3::initializeApp(stage);
51 }
52 
53 void I3TRTServer::deliver(OverlayKey &key, cMessage *msg) {
55 
56  i3msg = dynamic_cast<I3InsertTriggerMessage*>(msg);
57  if (i3msg) {
58  simtime_t *pt = (simtime_t*)i3msg->getContextPointer();
59  if (pt) {
60  stats.collect(simTime() - *pt);
61  //cout << "Trigger reach time " << simTime() - *pt << endl;
62  delete pt;
63  i3msg->setContextPointer(0);
64  }
65  }
66  I3::deliver(key, msg);
67 }
68 
70  if (!statsDumped) {
71  statsDumped = true;
72  recordScalar("I3Sim Number of samples", stats.getCount());
73  recordScalar("I3Sim Min time", stats.getMin());
74  recordScalar("I3Sim Max time", stats.getMax());
75  recordScalar("I3Sim Mean time", stats.getMean());
76  recordScalar("I3Sim Stardard dev", stats.getStddev());
77  stats.clearResult();
78  }
79 }
80 
81 #define TRIGGER_TIMER 12345
82 
84  cMessage *msg = new cMessage();
85  msg->setKind(TRIGGER_TIMER);
86  scheduleAt(simTime() + int(par("triggerDelay")), msg);
87 }
88 
89 void I3TRTClient::handleTimerEvent(cMessage *msg) {
90  if (msg->getKind() == TRIGGER_TIMER) {
91 
92  I3Identifier id;
93  I3Trigger t;
95  I3IPAddress myAddress(nodeIPAddress, par("clientPort"));
96 
97  id.createRandomKey();
98  t.setIdentifier(id);
99  t.getIdentifierStack().push(myAddress);
100 
101 
102  imsg->setTrigger(t);
103  imsg->setSendReply(true);
104  imsg->setSource(myAddress);
105  imsg->setBitLength(INSERT_TRIGGER_L(imsg));
106  imsg->setContextPointer(new simtime_t(simTime()));
107 
108  sendThroughUDP(imsg, gateway.address);
109  scheduleAt(simTime() + int(par("triggerDelay")), msg);
110  }
111 }