OverSim
I3Session.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 
24 #include "I3BaseApp.h"
25 #include "I3SessionMessage_m.h"
26 
27 using namespace std;
28 
29 #define DONT_REMOVE 0
30 #define REMOVE_AT_ONCE 1
31 #define WAIT_STATIC 2
32 #define WAIT_CONFIRMATION 3
33 
34 #define TYPE_CHANGE_SESSION 0
35 #define TYPE_REMOVE_TRIGGER 1
36 
37 enum Stats {
42 };
43 
44 class I3SessionServer : public I3BaseApp
45 {
46 public:
48 
51 
52  void initializeI3();
53  void deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg);
54  void finish();
55 };
56 
58 
60 {
61  numExchanged = 0;
62  clientIdentifier.createFromHash("Client");
63  myIdentifier.createFromHash("Server");
64  insertTrigger(myIdentifier);
65 }
66 
67 void I3SessionServer::deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg)
68 {
69  SessionMsg *smsg = check_and_cast<SessionMsg*>(msg);
70  smsg->setValue(smsg->getValue() + 1);
71  numExchanged++;
72  sendPacket(clientIdentifier, smsg);
73 }
74 
76  recordScalar("Server packets exchanged", numExchanged);
77 }
78 
79 
80 
81 
82 
83 
84 class I3SessionClient : public I3BaseApp
85 {
86 public:
87  cStdDev myStats[NUM_STATS];
88 
93  double actualValue;
97 
98  void initializeApp(int stage);
99  virtual void initializeI3();
100  void deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg);
101  void handleTimerEvent(cMessage *msg);
102  void finish();
103 };
104 
106 
107 
109 {
110  holdsSession = false;
111  numForeignPackets = 0;
112  numSessions = 0;
113  numExchanged = 0;
114  WATCH(numForeignPackets);
115  clientIdentifier.createFromHash("Client");
116  serverIdentifier.createFromHash("Server");
117 }
118 
120  poolIdentifier.createFromHash("Pool");
121  poolIdentifier.createRandomSuffix();
122  insertTrigger(poolIdentifier);
123 }
124 
125 void I3SessionClient::deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg)
126 {
127  SessionMsg *smsg = check_and_cast<SessionMsg*>(msg);
128 
129  if (smsg->getType() == PAYLOAD) {
130  if (holdsSession) {
131  //std::cout << "Got value " << smsg->getValue() << ", resending..." << endl;
132  numExchanged++;
133  actualValue = smsg->getValue();
134  sendPacket(serverIdentifier, msg);
135  } else {
136  numForeignPackets++;
137  //std::cout << "Foreign packet at " << nodeIPAddress << endl;
138  delete msg;
139  }
140 
141  } else if (smsg->getType() == CHANGE_SESSION) {
142 
143  //cout << "Insert new trigger" << nodeIPAddress << endl;
144  /* resume session */
145  insertTrigger(clientIdentifier, int(par("sessionMobilityType")) != DONT_REMOVE); // renew only if type != DONT_REMOVE
146  holdsSession = true;
147 
148  SessionMsg *newMsg = new SessionMsg();
149  newMsg->setType(PAYLOAD);
150  newMsg->setValue(smsg->getValue());
151  sendPacket(serverIdentifier, newMsg);
152 
153  if (int(par("sessionMobilityType")) == WAIT_CONFIRMATION) {
154  // send confirmation
155  SessionMsg *newMsg = new SessionMsg();
156  newMsg->setType(TRIGGER_CONFIRMATION);
157  newMsg->setValue(0);
158  newMsg->setSource(poolIdentifier);
159  sendPacket(smsg->getSource(), newMsg);
160  }
161  delete smsg;
162 
163  cMessage *msg = new cMessage();
164  msg->setKind(TYPE_CHANGE_SESSION);
165  scheduleAt(simTime() + int(par("sessionTime")), msg);
166  numSessions++;
167 
168  getParentModule()->bubble("Got session!");
169 
170  } else if (smsg->getType() == TRIGGER_CONFIRMATION) { // only for WAIT_CONFIRMATION
171  removeTrigger(clientIdentifier);
172  getParentModule()->bubble("Got confirmation for erase.");
173  delete smsg;
174 
175  } else {
176  // ??
177  delete smsg;
178  }
179 }
180 
182  recordScalar("Client packets received", numExchanged);
183  recordScalar("Client wrong received ", numForeignPackets);
184  recordScalar("Client session changed ", numSessions);
185 
186 }
187 
189  if (msg->getKind() == TYPE_CHANGE_SESSION) {
190  myStats[STAT_CHANGE].collect(simTime());
191  switch (int(par("sessionMobilityType"))) {
192  case DONT_REMOVE:
193  case WAIT_CONFIRMATION:
194  break;
195  case REMOVE_AT_ONCE:
196  removeTrigger(clientIdentifier);
197  break;
198  case WAIT_STATIC:
199  cMessage *msg = new cMessage();
200  msg->setKind(TYPE_REMOVE_TRIGGER);
201  scheduleAt(simTime() + int(par("sessionMobilityWait")), msg);
202  break;
203  }
204  holdsSession = false;
205 
206  /* cede session */
207  I3Identifier sessionId;
208 
209  sessionId.createFromHash("Pool");
210  sessionId.createRandomSuffix();
211 
212  SessionMsg *newMsg = new SessionMsg();
213  newMsg->setType(CHANGE_SESSION);
214  newMsg->setValue(actualValue);
215  newMsg->setSource(poolIdentifier);
216  sendPacket(sessionId, newMsg);
217 
218  getParentModule()->bubble("Ceding session...");
219  delete msg;
220 
221  } else if (msg->getKind() == TYPE_REMOVE_TRIGGER) { // for WAIT_STATIC only
222  getParentModule()->bubble("Timer ticked for erase.");
223  removeTrigger(clientIdentifier);
224  //cout << "Delete old trigger " << nodeIPAddress << endl;
225  delete msg;
226  }
227 }
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
239 {
240 public:
241  void initializeI3();
242 };
243 
245 
248 
249  /* start session */
250  insertTrigger(clientIdentifier, int(par("sessionMobilityType")) != DONT_REMOVE); // renew only if type != DONT_REMOVE
251  holdsSession = true;
252 
253  SessionMsg *newMsg = new SessionMsg();
254  newMsg->setType(PAYLOAD);
255  newMsg->setValue(0);
256  sendPacket(serverIdentifier, newMsg);
257 
258  cMessage *msg = new cMessage();
259  msg->setKind(TYPE_CHANGE_SESSION);
260 
261  std::cout << "Started starts" << endl;
262  scheduleAt(simTime() + int(par("sessionTime")), msg);
263 }