OverSim
GIASearchApp.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include <IPAddressResolver.h>
25 
26 #include <InitStages.h>
27 #include <CommonMessages_m.h>
28 #include <ExtAPIMessages_m.h>
29 #include <GiaMessage_m.h>
30 #include <GlobalStatistics.h>
31 
32 #include "GIASearchApp.h"
33 
34 
36 
38 {
39  search_timer = keyList_timer = NULL;
40  srMsgBook = NULL;
41 }
42 
44 {
45  cancelAndDelete(search_timer);
46  cancelAndDelete(keyList_timer);
47  if (srMsgBook != NULL) {
48  delete srMsgBook;
49  srMsgBook = NULL;
50  }
51 }
52 
54 {
55  if (stage != MIN_STAGE_APP)
56  return;
57 
58  // fetch parameters
59  mean = par("messageDelay");
60  deviation = mean / 3;
61  randomNodes = par("randomNodes");
62  maxResponses = par("maxResponses");
63 
65 
66  // statistics
73 
74  // initiate test message emision
75  search_timer = new cMessage("search_timer");
76  scheduleAt(simTime() + truncnormal(mean, deviation),
77  search_timer);
78 
79  keyList_timer = new cMessage("keyList_timer");
80  scheduleAt(simTime() + uniform(0.0, 10.0), keyList_timer);
81 }
82 
83 void GIASearchApp::handleTimerEvent(cMessage *msg)
84 {
85  if(msg == keyList_timer) {
86  keyList = globalNodeList->getKeyList(par("maximumKeys"));
87  WATCH_VECTOR(*keyList);
88 
89  // create message
90  GIAput* putMsg = new GIAput("GIA-Keylist");
91  putMsg->setCommand(GIA_PUT);
92 
93  putMsg->setKeysArraySize(keyList->size());
94 
95  std::vector<OverlayKey>::iterator it;
96  int k;
97  for(it = keyList->begin(), k = 0; it != keyList->end(); it++, k++)
98  putMsg->setKeys(k, *it);
99 
100  putMsg->setBitLength(GIAPUT_L(putMsg));
101 
102  sendMessageToLowerTier(putMsg);
103 
104  if (debugOutput)
105  EV << "[GIASearchApp::handleTimerEvent() @ " << overlay->getThisNode().getIp()<< "]\n"
106  << " Node sent keylist to overlay."
107  << endl;
108 
110  stat_keyListBytesSent += putMsg->getByteLength();
111  }
112  else if(msg == search_timer) {
113  // schedule next search-message
114  scheduleAt(simTime() + truncnormal(mean, deviation), msg);
115 
116  // do nothing, if the network is still in the initiaization phase
117  if((!par("activeNetwInitPhase")) && (underlayConfigurator->isInInitPhase()))
118  return;
119 
120  OverlayKey keyListItem;
121  uint32_t maximumTries = 20;
122  // pic a search key we are not already searching
123  do {
124  if (maximumTries-- == 0)
125  break;
126  keyListItem = globalNodeList->getRandomKeyListItem();
127  } while ((keyListItem.isUnspecified())
128  && ((srMsgBook->contains(keyListItem))));
129 
130  if (!keyListItem.isUnspecified()) {
131  // create message
132  GIAsearch* getMsg = new GIAsearch("GIA-Search");
133  getMsg->setCommand(GIA_SEARCH);
134  getMsg->setSearchKey(keyListItem);
135  getMsg->setMaxResponses(maxResponses);
136  getMsg->setBitLength(GIAGET_L(getMsg));
137 
138  sendMessageToLowerTier(getMsg);
139 
140  // add search key to our bookkeeping list
141  srMsgBook->addMessage(keyListItem);
142 
143  if (debugOutput)
144  EV << "[GIASearchApp::handleTimerEvent() @ " << overlay->getThisNode().getIp()<< "]\n"
145  << " Node sent get-message to overlay."
146  << endl;
147 
149  stat_searchBytesSent += getMsg->getByteLength();
150  }
151  }
152 }
153 
155 {
156  GIAanswer* answer = check_and_cast<GIAanswer*>(msg);
157  OverlayCtrlInfo* overlayCtrlInfo =
158  check_and_cast<OverlayCtrlInfo*>(answer->removeControlInfo());
159 
160  OverlayKey searchKey = answer->getSearchKey();
161 
162  if (debugOutput)
163  EV << "[GIASearchApp::handleLowerMessage() @ " << overlay->getThisNode().getIp()<< "]\n"
164  << " Node received answer-message from overlay:"
165  << " (key: " << searchKey
166  << " at node " << answer->getNode() << ")"
167  << endl;
168 
170  stat_searchResponseBytes += answer->getByteLength();
171 
172  if (srMsgBook->contains(searchKey))
173  srMsgBook->updateItem(searchKey, overlayCtrlInfo->getHopCount());
174 
175  delete answer;
176 }
177 
179 {
180  // record scalar data
182 
183  if (stats.minDelay == 0 &&
184  stats.maxDelay == 0 &&
185  stats.minHopCount == 0 &&
186  stats.maxHopCount == 0 &&
187  stats.responseCount == 0) return;
188 
189  globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. min delay",
190  stats.minDelay);
191  globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. max delay",
192  stats.maxDelay);
193  globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. min hops",
194  stats.minHopCount);
195  globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. max hops",
196  stats.maxHopCount);
197  globalStatistics->addStdDev("GIASearchApp: SearchMsg avg. response count",
198  stats.responseCount);
199 }