I3Triggers.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 "I3TriggersMessage_m.h"
00025 
00026 using namespace std;
00027 
00028 class I3Triggers : public I3BaseApp
00029 {
00030 private:
00031     static int index; // HACK Change to use index module when it's done
00032 public:
00033     int myIndex;
00034 
00035     // for both
00036     I3Identifier myIdentifier;
00037 
00038     // for server
00039     struct Client {
00040         I3Identifier clientId;
00041         I3Identifier privateId;
00042         int sentValue;
00043     };
00044     map<I3Identifier, Client> clients;
00045 
00046     // for client
00047     I3Identifier publicIdentifier;
00048     I3Identifier privateIdentifier;
00049 
00050     cMessage *handShakeTimer;
00051     cMessage *sendPacketTimer;
00052 
00053     void initializeApp(int stage);
00054     void initializeI3();
00055     void deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg);
00056     void handleTimerEvent(cMessage *msg);
00057     void createMessage();
00058 };
00059 
00060 int I3Triggers::index = 0;
00061 
00062 void I3Triggers::initializeApp(int stage)
00063 {
00064     myIndex = index++;
00065 
00066     WATCH(myIndex);
00067     WATCH(myIdentifier);
00068 }
00069 
00070 void I3Triggers::initializeI3() {
00071     publicIdentifier.createFromHash("Triggers 0");
00072 
00073     ostringstream os;
00074     os << "Triggers " << myIndex;
00075 
00076     myIdentifier.createFromHash(os.str());
00077     insertTrigger(myIdentifier);
00078 
00079     // handshake timer must be set before the packet timer!
00080     handShakeTimer = new cMessage("handshake timer");
00081     scheduleAt(simTime() + 5, handShakeTimer);
00082 
00083     sendPacketTimer = new cMessage("packet timer");
00084     scheduleAt(simTime() + 10, sendPacketTimer);
00085 }
00086 
00087 void I3Triggers::handleTimerEvent(cMessage *msg)
00088 {
00089     if (myIndex != 0) {
00090         if (msg == handShakeTimer) {
00091 
00092             // start handshake
00093             TriggersHandshakeMsg *msg = new TriggersHandshakeMsg();
00094 
00095             msg->setValue(myIndex);
00096             msg->setTriggerId(myIdentifier);
00097             sendPacket(publicIdentifier, msg);
00098             getParentModule()->bubble("Started handshake");
00099 
00100         } else if (msg == sendPacketTimer) {
00101 
00102             //send a packet
00103             TriggersMsg *msg = new TriggersMsg();
00104             msg->setValue(0);
00105             sendPacket(privateIdentifier, msg);
00106 
00107             // reset timer
00108             sendPacketTimer = new cMessage("packet timer");
00109             scheduleAt(simTime() + 5, sendPacketTimer);
00110 
00111             getParentModule()->bubble("Sending packet");
00112         }
00113     }
00114     delete msg;
00115 }
00116 
00117 void I3Triggers::deliver(I3Trigger &matchingTrigger, I3IdentifierStack &stack, cPacket *msg)
00118 {
00119     TriggersHandshakeMsg *hmsg = dynamic_cast<TriggersHandshakeMsg*>(msg);
00120     TriggersMsg *tmsg = NULL;
00121 
00122     if (!hmsg) tmsg = dynamic_cast<TriggersMsg*>(msg);
00123 
00124     if (myIndex == 0) {
00125         // act as server
00126 
00127         if (hmsg) {
00128             getParentModule()->bubble("Got handshake!");
00129 
00130             // this is a handshake message
00131             TriggersHandshakeMsg *newMsg = new TriggersHandshakeMsg();
00132 
00133             // create a new private trigger
00134             I3Identifier privateId;
00135             privateId.createRandomKey();
00136 
00137             // insert it into i3
00138             insertTrigger(privateId);
00139 
00140             // store the client's value
00141             Client client;
00142             client.clientId = hmsg->getTriggerId();
00143             client.privateId = privateId;
00144             client.sentValue = hmsg->getValue();
00145             clients[privateId] = client;
00146 
00147             // notify the client back
00148             newMsg->setValue(0);
00149             newMsg->setTriggerId(privateId);
00150             sendPacket(hmsg->getTriggerId(), newMsg);
00151 
00152 
00153 
00154         } else if (tmsg) {
00155 
00156             getParentModule()->bubble("Got normal message!");
00157 
00158             // this is a normal message. just reply with sent value
00159             TriggersMsg *newMsg = new TriggersMsg();
00160 
00161             Client &client = clients[matchingTrigger.getIdentifier()];
00162             newMsg->setValue(client.sentValue);
00163             sendPacket(client.clientId, newMsg);
00164         }
00165 
00166     } else {
00167         //act as client
00168 
00169         if (hmsg) {
00170 
00171             getParentModule()->bubble("Finished handshaking!");
00172 
00173             // store the private trigger
00174             privateIdentifier = hmsg->getTriggerId();
00175             WATCH(privateIdentifier);
00176 
00177         } else {
00178 
00179             // check if the value is valid
00180             if (tmsg->getValue() == myIndex) {
00181                 getParentModule()->bubble("Got packet - Got my id!");
00182             } else {
00183                 getParentModule()->bubble("Got packet - Got an unknown id");
00184             }
00185 
00186         }
00187     }
00188 }
00189 
00190 
00191 Define_Module(I3Triggers);
00192 
00193 
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3