OverSim
I3Triggers.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 "I3TriggersMessage_m.h"
25 
26 using namespace std;
27 
28 class I3Triggers : public I3BaseApp
29 {
30 private:
31  static int index; // HACK Change to use index module when it's done
32 public:
33  int myIndex;
34 
35  // for both
37 
38  // for server
39  struct Client {
42  int sentValue;
43  };
44  map<I3Identifier, Client> clients;
45 
46  // for client
49 
50  cMessage *handShakeTimer;
51  cMessage *sendPacketTimer;
52 
53  void initializeApp(int stage);
54  void initializeI3();
55  void deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg);
56  void handleTimerEvent(cMessage *msg);
57  void createMessage();
58 };
59 
60 int I3Triggers::index = 0;
61 
63 {
64  myIndex = index++;
65 
66  WATCH(myIndex);
67  WATCH(myIdentifier);
68 }
69 
71  publicIdentifier.createFromHash("Triggers 0");
72 
73  ostringstream os;
74  os << "Triggers " << myIndex;
75 
76  myIdentifier.createFromHash(os.str());
77  insertTrigger(myIdentifier);
78 
79  // handshake timer must be set before the packet timer!
80  handShakeTimer = new cMessage("handshake timer");
81  scheduleAt(simTime() + 5, handShakeTimer);
82 
83  sendPacketTimer = new cMessage("packet timer");
84  scheduleAt(simTime() + 10, sendPacketTimer);
85 }
86 
87 void I3Triggers::handleTimerEvent(cMessage *msg)
88 {
89  if (myIndex != 0) {
90  if (msg == handShakeTimer) {
91 
92  // start handshake
94 
95  msg->setValue(myIndex);
96  msg->setTriggerId(myIdentifier);
97  sendPacket(publicIdentifier, msg);
98  getParentModule()->bubble("Started handshake");
99 
100  } else if (msg == sendPacketTimer) {
101 
102  //send a packet
103  TriggersMsg *msg = new TriggersMsg();
104  msg->setValue(0);
105  sendPacket(privateIdentifier, msg);
106 
107  // reset timer
108  sendPacketTimer = new cMessage("packet timer");
109  scheduleAt(simTime() + 5, sendPacketTimer);
110 
111  getParentModule()->bubble("Sending packet");
112  }
113  }
114  delete msg;
115 }
116 
117 void I3Triggers::deliver(I3Trigger &matchingTrigger, I3IdentifierStack &stack, cPacket *msg)
118 {
119  TriggersHandshakeMsg *hmsg = dynamic_cast<TriggersHandshakeMsg*>(msg);
120  TriggersMsg *tmsg = NULL;
121 
122  if (!hmsg) tmsg = dynamic_cast<TriggersMsg*>(msg);
123 
124  if (myIndex == 0) {
125  // act as server
126 
127  if (hmsg) {
128  getParentModule()->bubble("Got handshake!");
129 
130  // this is a handshake message
132 
133  // create a new private trigger
134  I3Identifier privateId;
135  privateId.createRandomKey();
136 
137  // insert it into i3
138  insertTrigger(privateId);
139 
140  // store the client's value
141  Client client;
142  client.clientId = hmsg->getTriggerId();
143  client.privateId = privateId;
144  client.sentValue = hmsg->getValue();
145  clients[privateId] = client;
146 
147  // notify the client back
148  newMsg->setValue(0);
149  newMsg->setTriggerId(privateId);
150  sendPacket(hmsg->getTriggerId(), newMsg);
151 
152 
153 
154  } else if (tmsg) {
155 
156  getParentModule()->bubble("Got normal message!");
157 
158  // this is a normal message. just reply with sent value
159  TriggersMsg *newMsg = new TriggersMsg();
160 
161  Client &client = clients[matchingTrigger.getIdentifier()];
162  newMsg->setValue(client.sentValue);
163  sendPacket(client.clientId, newMsg);
164  }
165 
166  } else {
167  //act as client
168 
169  if (hmsg) {
170 
171  getParentModule()->bubble("Finished handshaking!");
172 
173  // store the private trigger
174  privateIdentifier = hmsg->getTriggerId();
175  WATCH(privateIdentifier);
176 
177  } else {
178 
179  // check if the value is valid
180  if (tmsg->getValue() == myIndex) {
181  getParentModule()->bubble("Got packet - Got my id!");
182  } else {
183  getParentModule()->bubble("Got packet - Got an unknown id");
184  }
185 
186  }
187  }
188 }
189 
190 
192 
193