00001
00002 #include <iostream>
00003 #include <vector>
00004 using namespace std;
00005 #include <boost/random.hpp>
00006 #include <boost/shared_ptr.hpp>
00007 #include <boost/weak_ptr.hpp>
00008
00009 #include "simulator.hpp"
00010 #include "utility.hpp"
00011
00012 #include "fading.hpp"
00013 #include "path_loss.hpp"
00014 #include "wireless_channel.hpp"
00015 #include "link_layer.hpp"
00016 #include "rfid_reader_phy.hpp"
00017 #include "rfid_reader_mac.hpp"
00018 #include "rfid_reader_app.hpp"
00019 #include "rfid_tag_phy.hpp"
00020 #include "rfid_tag_mac.hpp"
00021 #include "rfid_tag_app.hpp"
00022 #include "packet.hpp"
00023 #include "rand_num_generator.hpp"
00024
00025
00026
00027
00028
00029 void unitTestEventQueue(SimulatorPtr sim);
00030
00031 void packetSendTest();
00032
00033 void randomTest();
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 int main(int argc, char *argv[])
00061 {
00062 SimulatorPtr s = Simulator::instance();
00063 DummyEventPtr e1 = DummyEvent::create();
00064 SimTime st(2.0);
00065 s->scheduleEvent(e1, st);
00066
00067 s->reset();
00068
00069 packetSendTest();
00070
00071
00072
00073
00074
00075
00076 }
00077
00078 void packetSendTest()
00079 {
00080
00081 t_uint currentPowerLevel = 2;
00082 t_uint numTags = 50;
00083 t_uint numReaders = 1;
00084 t_uint numChannels = (numReaders + 1);
00085 bool doCollocation = false;
00086 t_uint allChannelId = 0;
00087 WirelessChannelPtr channels[numChannels];
00088
00089 if(doCollocation)
00090 numReaders *= 2;
00091
00092 ostringstream outputFileName;
00093 outputFileName << "out" << currentPowerLevel << ".txt";
00094 ostreamPtr outputStream(new ofstream(outputFileName.str().c_str()));
00095 LogStreamManager::instance()->setAllStreams(outputStream);
00096
00097 ostringstream statsFileName;
00098 statsFileName << "stats" << currentPowerLevel << ".txt";
00099 ostreamPtr statsStream(new ofstream(statsFileName.str().c_str()));
00100 LogStreamManager::instance()->setStatsStream(statsStream);
00101
00102 WirelessChannelManagerPtr channelManager =
00103 WirelessChannelManager::create();
00104 for(t_uint i = 0; i < numChannels; ++i) {
00105 TwoRayPtr twoRay = TwoRay::create();
00106 RiceanPtr ricean = Ricean::create();
00107
00108 channels[i] = WirelessChannel::create(twoRay);
00109 channelManager->addChannel(i, channels[i]);
00110 }
00111
00112 RandNumGeneratorPtr rand =
00113 Simulator::instance()->getRandNumGenerator();
00114
00115 vector<RfidReaderAppPtr> readerAppVector;
00116 for(t_uint i = 0; i < numReaders; ++i) {
00117 double locationStep = 4.8;
00118
00119
00120
00121 double epsilon = 0.000001;
00122 Location location((locationStep * i),0,0);
00123 if(doCollocation) {
00124 location.setCoordinates(
00125 (epsilon * i) + (locationStep * floor(i / 2.0)),0,0);
00126 }
00127 NodePtr readerNode = Node::create(location, NodeId(i));
00128
00129 ostringstream userDefinedStream;
00130 userDefinedStream << "Reader ID: " << readerNode->getNodeId() <<
00131 " Location: " << readerNode->getLocation();
00132 LogStreamManager::instance()->logUserDefinedItem(
00133 userDefinedStream.str());
00134
00135 RfidReaderPhyPtr readerPhy =
00136 RfidReaderPhy::create(readerNode, channelManager);
00137 readerPhy->setAllSendersChannel(allChannelId);
00138 t_uint channelId = (i + 1);
00139
00140 if(doCollocation)
00141 channelId = static_cast<t_uint>(floor(i / 2.0) + 1);
00142 assert(channelId < numChannels);
00143 readerPhy->setRegularChannel(channelId);
00144
00145 RfidReaderAppPtr readerApp = RfidReaderApp::create(
00146 readerNode, readerPhy);
00147
00148
00149
00150 if(doCollocation && ((i % 2) != 0)) {
00151 readerApp->setDoReset(false);
00152 }
00153
00154 RfidReaderMacPtr readerMac = RfidReaderMac::create(readerNode,
00155 readerApp);
00156 LinkLayerPtr readerLink = LinkLayer::create(readerNode, readerMac);
00157
00158 readerApp->insertLowerLayer(readerLink);
00159 readerLink->insertLowerLayer(readerPhy);
00160
00161 readerApp->setNumPowerControlLevels(currentPowerLevel);
00162 double startTime = 2.5;
00163 SimTime readerAppStartTime(startTime);
00164
00165
00166
00167 if(doCollocation && ((i % 2) != 0)) {
00168 double staggerStep = floor(i / 2.0);
00169 readerAppStartTime.setTime((startTime * 4.0) +
00170 (staggerStep * startTime));
00171 }
00172 readerApp->start(readerAppStartTime);
00173 }
00174
00175 vector<RfidTagAppPtr> tagAppVector;
00176 for(t_uint i = 0; i < numTags; ++i) {
00177 double locationFactor = numReaders;
00178 if(doCollocation)
00179 locationFactor /= 2;
00180 Location location(rand->uniformReal(0.0, (locationFactor * 2.4)),0,0);
00181
00182
00183
00184
00185
00186 NodePtr tagNode = Node::create(location, NodeId(numReaders+i));
00187
00188 ostringstream userDefinedStream;
00189 userDefinedStream << "Tag ID: " << tagNode->getNodeId() <<
00190 " Location: " << tagNode->getLocation();
00191 LogStreamManager::instance()->logUserDefinedItem(
00192 userDefinedStream.str());
00193
00194 RfidTagPhyPtr tagPhy = RfidTagPhy::create(tagNode, channelManager);
00195 tagPhy->setAllListenersChannel(allChannelId);
00196
00197 RfidTagAppPtr tagApp = RfidTagApp::create(tagNode);
00198
00199 RfidTagMacPtr tagMac = RfidTagMac::create(tagNode, tagApp);
00200 LinkLayerPtr tagLink = LinkLayer::create(tagNode, tagMac);
00201
00202 tagApp->insertLowerLayer(tagLink);
00203 tagLink->insertLowerLayer(tagPhy);
00204
00205 tagApp->start(SimTime(0.0));
00206 }
00207
00208 Simulator::instance()->runSimulation(SimTime(20.0));
00209
00210 }
00211
00212 void unitTestEventQueue(SimulatorPtr sim)
00213 {
00214
00215 vector<double> timeVals;
00216 timeVals.push_back(0.0);
00217 timeVals.push_back(2.0);
00218 timeVals.push_back(5.0);
00219 timeVals.push_back(1.5);
00220 timeVals.push_back(1.5);
00221 timeVals.push_back(1.5);
00222 timeVals.push_back(5.0);
00223 timeVals.push_back(10.0);
00224 timeVals.push_back(1.0);
00225
00226 cout << "\n";
00227 vector<EventPtr> eventVals;
00228 for(t_uint i = 0; i < timeVals.size(); i++) {
00229 EventPtr newEvent = DummyEvent::create();
00230 eventVals.push_back(newEvent);
00231 SimTimePtr newSimTime(new SimTime(timeVals[i]));
00232 cout << "Adding event " << newEvent << " to fire at time " <<
00233 *newSimTime << ".\n";
00234 bool didSchedule = sim->scheduleEvent(newEvent, *newSimTime);
00235 assert(didSchedule);
00236 }
00237
00238 typedef boost::lagged_fibonacci607 BaseGenerator;
00239 BaseGenerator baseGenerator(2004u);
00240 boost::uniform_int<> uni_int(0,(eventVals.size() - 1));
00241 boost::variate_generator<BaseGenerator&,
00242 boost::uniform_int<> > rng(baseGenerator, uni_int);
00243
00244
00245 cout << "\n";
00246 EventPtr eventToRemove(eventVals[rng()]);
00247 cout << "Cancelling event " << eventToRemove << "\n";
00248 bool didRemove = sim->cancelEvent(eventToRemove);
00249 assert(didRemove);
00250
00251 eventToRemove = eventVals[rng()];
00252 cout << "Cancelling event " << eventToRemove << "\n";
00253 didRemove = sim->cancelEvent(eventToRemove);
00254 assert(didRemove);
00255
00256
00257
00258
00259
00260
00261
00262
00263 cout << "\n";
00264 sim->runSimulation(SimTime(100.0));
00265 sim->reset();
00266 cout << "\n";
00267
00268 }
00269
00270 void randomTest()
00271 {
00272
00273 RfidTagAppDataPtr tagAppData = RfidTagAppData::create();
00274 NodeId testNodeId(1460502);
00275 tagAppData->setTagId(testNodeId);
00276 cout << "Set ID: " << testNodeId << ", Retrieved ID: " <<
00277 tagAppData->getTagId() << endl;
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333