main.cpp

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 //#include "log_stream_manager.hpp"
00025 //#include "signal.hpp"
00026 //#include "wireless_comm_signal.hpp"
00027 //#include "packet.hpp"
00028 
00029 void unitTestEventQueue(SimulatorPtr sim);
00030 
00031 void packetSendTest();
00032 
00033 void randomTest();
00034 
00035 //void copyTest(WirelessCommSignal sig);
00036 
00037 /*
00038 class Foo {
00039 public:
00040 
00041    static boost::shared_ptr<Foo> create() {
00042       boost::shared_ptr<Foo> p(new Foo);
00043       p->m_internal_weak_this = p;
00044       return p;
00045    }
00046 
00047    boost::shared_ptr<Foo> get_this()
00048    {
00049       boost::shared_ptr<Foo> p(m_internal_weak_this);
00050       return p;
00051    }
00052 
00053 private:
00054    Foo() {}
00055    boost::weak_ptr<Foo> m_internal_weak_this;
00056 };
00057 typedef boost::shared_ptr<Foo> FooPtr;
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    //s->runSimulation();
00067    s->reset();
00068 
00069    packetSendTest();
00070    //randomTest();
00071 
00072    /*
00073    unitTestEventQueue(s);
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       //channels[i] = WirelessChannel::create(twoRay, ricean);
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       // Nodes cannot be collocated exactly or else the path loss
00119       // model will end up with a zero in its denominator.  Thus,
00120       // we add a small epsilon to "collocated" nodes.
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       // Keep the same channel id for collocated nodes.
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       // The second collocated nodes should *not* send
00148       // a reset packet since it is reading the tags
00149       // that the first collocated node missed.
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       // Start the collocated nodes a sufficiently long time
00165       // after the original nodes and stagger the collocated
00166       // nodes so that they are not reading at the same time.
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       Location location(0,0,0.01);
00183       if(i == 1)
00184          location.setCoordinates(0,0,0.1);
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    eventToRemove.reset(new TestEvent());
00258    didRemove = sim->cancelEvent(eventToRemove);
00259    // should fail since event was never scheduled
00260    assert(didRemove);
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    int i = 2;
00281    double d = 6.8;
00282    d = d - i * floor(d/i);
00283    d = d/i;
00284    cout << d << "\n";
00285    */
00286 
00287    /*
00288    RandNumGeneratorPtr r = RandNumGenerator::create();
00289    cout << r.use_count() << "\n";
00290    */
00291 
00292    /*
00293    FooPtr f = Foo::create();
00294    FooPtr f2 = f->get_this();
00295    cout << "f: " << f << ", use_count: " << f.use_count() << "\n";
00296    cout << "f2: " << f2 << ", use_count: " << f2.use_count() << "\n";
00297    */
00298 
00299    /*
00300    PacketPtr pkt(new Packet);
00301    Location loc(1, 2, 3);
00302    WirelessCommSignalPtr sig(new WirelessCommSignal(loc, 1.0, pkt));
00303    cout << "sig: " << *sig << "\n\n";
00304    copyTest(*sig);
00305    */
00306 
00307    /*
00308    Location loc;
00309    Signal sig(loc1, 5.0);
00310    Location loc2 = sig.getLocation();
00311    Location loc3(1, 2, 3);
00312    cout << *loc2 << "\n";
00313    loc2 = loc3;
00314    cout << *loc2 << "\n";
00315    SignalPtr sigp(&sig);
00316    */
00317 
00318    /*
00319    LogStreamManagerPtr lsm = LogStreamManager::instance();
00320    lsm->setSimulator(s);
00321    lsm->logUserDefinedItem("Foo bar");
00322    */
00323 
00324 }
00325 
00326 /*
00327 void copyTest(WirelessCommSignal sig)
00328 {
00329    cout << "foo sig: " << sig << "\n\n";
00330 }
00331 */
00332 
00333 

Generated on Tue Dec 12 17:04:38 2006 for rfidsim by  doxygen 1.4.7