OverSim
I3HostMobility.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 #define NUM_PARTNERS 5
27 
28 using namespace std;
29 
30 enum MsgType {
38 };
39 
41  int id;
43 };
44 
45 class I3HostMobility : public I3BaseApp {
47 
49  std::set<int> packets;
50  std::vector<I3Identifier> partners;
53 
54  void initializeApp(int stage);
55  void initializeI3();
56  void handleTimerEvent(cMessage *msg);
57  void handleUDPMessage(cMessage* msg);
58  void deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg);
59  void doMobilityEvent(I3MobilityStage stage);
60  void discoverPartners();
61  void finish();
62 };
63 
65 
66 
68  numSentPackets = 0;
69 
71 }
72 
74  recordScalar("Number of sent packets", numSentPackets);
75  recordScalar("Number of lost packets", packets.size());
76 }
77 
79  checkedPartners = false;
80 
81  poolId.createFromHash("HostMobility");
82  poolId.setName("HostMobility");
83  poolId.createRandomSuffix();
84  insertTrigger(poolId);
85 
86  closestId = retrieveClosestIdentifier();
87  insertTrigger(closestId);
88 
89  cMessage *msg = new cMessage();
90  msg->setKind(MSG_TIMER);
91  scheduleAt(simTime() + 5, msg);
92 
93  cMessage *nmsg = new cMessage();
94  msg->setKind(MSG_TIMER_REDISCOVER);
95  scheduleAt(simTime() + 60, nmsg);
96 }
97 
98 void I3HostMobility::handleUDPMessage(cMessage *msg) {
100 }
101 
102 void I3HostMobility::deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg)
103 {
104  MessageContent *mc = (MessageContent*)msg->getContextPointer();
105  switch (msg->getKind()) {
106  case MSG_QUERY_ID:
107  {
108  I3Identifier otherId = mc->source;
109  mc->source = closestId;
110  msg->setKind(MSG_REPLY_ID);
111  sendPacket(otherId, msg);
112  break;
113  }
114  case MSG_REPLY_ID:
115  partners.push_back(mc->source);
116  delete mc;
117  delete msg;
118  break;
119  case MSG_PING:
120  msg->setKind(MSG_REPLY);
121  sendPacket(mc->source, msg);
122  break;
123  case MSG_REPLY:
124  packets.erase(mc->id);
125  delete mc;
126  delete msg;
127  break;
128  default:
129  break;
130  }
131 }
132 
133 void I3HostMobility::handleTimerEvent(cMessage *msg) {
134  switch (msg->getKind()) {
135  case MSG_TIMER:
136  if (checkedPartners) {
137  // partners.size() != NUM_PARTNERS in the unlikely event
138  // that not all id queries have returned
139  //if (partners.size() == 0) {
140  // opp_error("Wtf?!");
141  //}
142  for (unsigned int i = 0; i < partners.size(); i++) {
143  cPacket *cmsg = new cPacket();
144  MessageContent *mc = new MessageContent();
145 
146  packets.insert(numSentPackets);
147  mc->id = numSentPackets++;
148  mc->source = closestId;
149  cmsg->setContextPointer(mc);
150  cmsg->setKind(MSG_PING);
151  cmsg->setBitLength((32 + intrand(512)) * 8);
152 
153  sendPacket(partners[i], cmsg, true);
154  }
155  scheduleAt(simTime() + truncnormal(0.5, 0.1), msg);
156  } else {
157  discoverPartners();
158  scheduleAt(simTime() + 10, msg);
159  }
160  break;
161  case MSG_TIMER_RESET_ID:
162  closestId = retrieveClosestIdentifier();
163  insertTrigger(closestId);
164  delete msg;
165  break;
167  checkedPartners = false;
168  scheduleAt(simTime() + 60, msg);
169  break;
170  default:
171  break;
172  }
173 }
174 
176 {
177  partners.clear();
178  for (int i = 0; i < NUM_PARTNERS; i++) {
179  cPacket *cmsg = new cPacket();
180  MessageContent *mc = new MessageContent();
181  I3Identifier partner;
182 
183  mc->source = closestId;
184  mc->id = -1;
185  cmsg->setContextPointer(mc);
186  cmsg->setKind(MSG_QUERY_ID);
187 
188  partner.createFromHash("HostMobility");
189  partner.createRandomSuffix();
190  sendPacket(partner, cmsg);
191  }
192  checkedPartners = true;
193 }
194 
196  if (stage == I3_MOBILITY_UPDATED) {
197  cMessage *msg = new cMessage();
198  msg->setKind(MSG_TIMER_RESET_ID);
199  scheduleAt(simTime() + 10, msg);
200  }
201 }
202