I3HostMobility.cc

Go to the documentation of this file.
00001 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016 //
00017 
00023 #include "I3BaseApp.h"
00024 #include "I3.h"
00025 
00026 #define NUM_PARTNERS 5
00027 
00028 using namespace std;
00029 
00030 enum MsgType {
00031     MSG_TIMER,
00032     MSG_TIMER_RESET_ID,
00033     MSG_TIMER_REDISCOVER,
00034     MSG_QUERY_ID,
00035     MSG_REPLY_ID,
00036     MSG_PING,
00037     MSG_REPLY
00038 };
00039 
00040 struct MessageContent {
00041     int id;
00042     I3Identifier source;
00043 };
00044 
00045 class I3HostMobility : public I3BaseApp {
00046     bool checkedPartners;
00047 
00048     int numSentPackets;
00049     std::set<int> packets;
00050     std::vector<I3Identifier> partners;
00051     I3Identifier poolId;
00052     I3Identifier closestId;
00053 
00054     void initializeApp(int stage);
00055     void initializeI3();
00056     void handleTimerEvent(cMessage *msg);
00057     void handleUDPMessage(cMessage* msg);
00058     void deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg);
00059     void doMobilityEvent(I3MobilityStage stage);
00060     void discoverPartners();
00061     void finish();
00062 };
00063 
00064 Define_Module(I3HostMobility);
00065 
00066 
00067 void I3HostMobility::initializeApp(int stage) {
00068     numSentPackets = 0;
00069 
00070     I3BaseApp::initializeApp(stage);
00071 }
00072 
00073 void I3HostMobility::finish() {
00074     recordScalar("Number of sent packets", numSentPackets);
00075     recordScalar("Number of lost packets", packets.size());
00076 }
00077 
00078 void I3HostMobility::initializeI3() {
00079     checkedPartners = false;
00080 
00081     poolId.createFromHash("HostMobility");
00082     poolId.setName("HostMobility");
00083     poolId.createRandomSuffix();
00084     insertTrigger(poolId);
00085 
00086     closestId = retrieveClosestIdentifier();
00087     insertTrigger(closestId);
00088 
00089     cMessage *msg = new cMessage();
00090     msg->setKind(MSG_TIMER);
00091     scheduleAt(simTime() + 5, msg);
00092 
00093     cMessage *nmsg = new cMessage();
00094     msg->setKind(MSG_TIMER_REDISCOVER);
00095     scheduleAt(simTime() + 60, nmsg);
00096 }
00097 
00098 void I3HostMobility::handleUDPMessage(cMessage *msg) {
00099     I3BaseApp::handleUDPMessage(msg);
00100 }
00101 
00102 void I3HostMobility::deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg)
00103 {
00104     MessageContent *mc = (MessageContent*)msg->getContextPointer();
00105     switch (msg->getKind()) {
00106     case MSG_QUERY_ID:
00107     {
00108         I3Identifier otherId = mc->source;
00109         mc->source = closestId;
00110         msg->setKind(MSG_REPLY_ID);
00111         sendPacket(otherId, msg);
00112         break;
00113     }
00114     case MSG_REPLY_ID:
00115         partners.push_back(mc->source);
00116         delete mc;
00117         delete msg;
00118         break;
00119     case MSG_PING:
00120         msg->setKind(MSG_REPLY);
00121         sendPacket(mc->source, msg);
00122         break;
00123     case MSG_REPLY:
00124         packets.erase(mc->id);
00125         delete mc;
00126         delete msg;
00127         break;
00128     default:
00129         break;
00130     }
00131 }
00132 
00133 void I3HostMobility::handleTimerEvent(cMessage *msg) {
00134     switch (msg->getKind()) {
00135     case MSG_TIMER:
00136         if (checkedPartners) {
00137             // partners.size() != NUM_PARTNERS in the unlikely event
00138             // that not all id queries have returned
00139             //if (partners.size() == 0) {
00140             //  opp_error("Wtf?!");
00141             //}
00142             for (unsigned int i = 0; i < partners.size(); i++) {
00143                 cPacket *cmsg = new cPacket();
00144                 MessageContent *mc = new MessageContent();
00145 
00146                 packets.insert(numSentPackets);
00147                 mc->id = numSentPackets++;
00148                 mc->source = closestId;
00149                 cmsg->setContextPointer(mc);
00150                 cmsg->setKind(MSG_PING);
00151                 cmsg->setBitLength((32 + intrand(512)) * 8);
00152 
00153                 sendPacket(partners[i], cmsg, true);
00154             }
00155             scheduleAt(simTime() + truncnormal(0.5, 0.1), msg);
00156         } else {
00157             discoverPartners();
00158             scheduleAt(simTime() + 10, msg);
00159         }
00160         break;
00161     case MSG_TIMER_RESET_ID:
00162         closestId = retrieveClosestIdentifier();
00163         insertTrigger(closestId);
00164         delete msg;
00165         break;
00166     case MSG_TIMER_REDISCOVER:
00167         checkedPartners = false;
00168         scheduleAt(simTime() + 60, msg);
00169     default:
00170         break;
00171     }
00172 }
00173 
00174 void I3HostMobility::discoverPartners()
00175 {
00176     partners.clear();
00177     for (int i = 0; i < NUM_PARTNERS; i++) {
00178         cPacket *cmsg = new cPacket();
00179         MessageContent *mc = new MessageContent();
00180         I3Identifier partner;
00181 
00182         mc->source = closestId;
00183         mc->id = -1;
00184         cmsg->setContextPointer(mc);
00185         cmsg->setKind(MSG_QUERY_ID);
00186 
00187         partner.createFromHash("HostMobility");
00188         partner.createRandomSuffix();
00189         sendPacket(partner, cmsg);
00190     }
00191     checkedPartners = true;
00192 }
00193 
00194 void I3HostMobility::doMobilityEvent(I3MobilityStage stage) {
00195     if (stage == I3_MOBILITY_UPDATED) {
00196         cMessage *msg = new cMessage();
00197         msg->setKind(MSG_TIMER_RESET_ID);
00198         scheduleAt(simTime() + 10, msg);
00199     }
00200 }
00201 
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3