wirelessch-module.cc

00001 /*
00002  * Copyright (c) 2007 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "wirelessch-module.h"
00031 
00032 static class WirelessChModuleClass : public TclClass {
00033 public:
00034         WirelessChModuleClass() : TclClass("Module/WirelessCh") {}
00035         TclObject* create(int, const char*const*) {
00036         return (new WirelessChModule());
00037 
00038 }
00039 } class_wirelesschmodule;
00040 
00041 WirelessChModule::WirelessChModule() : ChannelModule(), sorted_(0)
00042 {
00043         bind("CSThresh_", &CSThresh_);
00044         bind("freq_", &freq_);
00045         bind("L_", &L_);
00046         lambda_ = SPEED_OF_LIGHT / freq_;
00047 
00048 }
00049 
00050 WirelessChModule::~WirelessChModule()
00051 {
00052 }
00053 
00054 
00055 
00056 int WirelessChModule::command(int argc, const char*const* argv)
00057 {
00058         //Tcl& tcl = Tcl::instance();
00059         
00060 //      if (argc==3)
00061 //      {
00062 //              // Over-ride of a channel-module tcl command:
00063 //              // 1) and set up the array of the nodes used for reduce complexity in packet trasmission
00064 //              // 2) install a SAP to an above module (call channel-module tcl command)
00065 //              if (strcasecmp(argv[1],"addsap")==0)
00066 //              {
00067 //                      ChSAP *chsap = dynamic_cast<ChSAP*>(TclObject::lookup(argv[2]));
00068 //                      if (!chsap)
00069 //                              return TCL_ERROR;
00070 //                      addNode(chsap);
00071 //                      Module::command(argc, argv);
00072 //              }
00073 //              
00074 //      }
00075         return ChannelModule::command(argc, argv);
00076 }
00077 
00078 
00079 void WirelessChModule::sortChSAPList()
00080 {
00081         if (!getChSAPnum())
00082                 return;
00083 
00084         sorted_ = true;
00085         
00086         int n = getChSAPnum();
00087         // Bubble Sort
00088         for (int i = 0; i < n - 1; i++) {
00089                 for (int j = 0; j < (n - 1 - i); j++)
00090                 {
00091                   if ( ( ((ChSAP *)getChSAP(j+1))->getPosition())->getX() < (((ChSAP *)getChSAP(j))->getPosition())->getX() )
00092                         {
00093                                 // swap i-el and i-el
00094                                 swapChSAP(j+1,j);
00095                         }
00096                 }
00097         }
00098 
00099         
00100         // BUBBLE SORT ORIGINAL VERSION (from www.cs.princeton.edu/~ah/alg_anim/gawain-4.0/BubbleSort.html)
00101 //      for (i=0; i<n-1; i++) {
00102 //   for (j=0; j<n-1-i; j++)
00103 //     if (a[j+1] < a[j]) {  /* compare the two neighbors */
00104 //       tmp = a[j];         /* swap a[j] and a[j+1]      */
00105 //       a[j] = a[j+1];
00106 //       a[j+1] = tmp;
00107 //   }
00108 }
00109 
00110 
00111 
00112 int* WirelessChModule::getInfluencedNodes(Position *p, double radius,  int *numInfluencedNodes)
00113 {
00114         double xmin, xmax, ymin, ymax;
00115         int n = 0;
00116         
00117         int nodesNum = getChSAPnum();
00118         
00119         if (!nodesNum) {
00120                 *numInfluencedNodes=-1;
00121                 fprintf(stderr, "WirelessChModule::getInfluencedNodes, no ChSAP installed when trying to send!!!\n");
00122                 return NULL;
00123         }
00124         
00125         xmin = p->getX() - radius;
00126         xmax = p->getX() + radius;
00127         ymin = p->getY() - radius;
00128         ymax = p->getY() + radius;
00129         
00130         double xpos,ypos;
00131         double yprec, xprec = 0;
00132         
00133         // First allocate as much as possible needed
00134         int* tmpList = new int[nodesNum];
00135         
00136         for(int i = 0; i < nodesNum; i++)
00137         {
00138                 xpos = ( ((ChSAP *)getChSAP(i))->getPosition() )->getX();
00139                 ypos = ( ((ChSAP *)getChSAP(i))->getPosition() )->getY();
00140                 //printf("Node from ChSAP %p has position (%f,%f)\n",((ChSAP *)getChSAP(i)),xpos,ypos);
00141                 
00142                 if (xpos < xprec)
00143                 {
00144                         delete [] tmpList;
00145                         // re-sort the chsap list by x-pos and re-do this function
00146                         sortChSAPList();
00147                         return (getInfluencedNodes(p, radius, numInfluencedNodes));
00148                 }
00149                 
00150                 if (    (xpos >= xmin) &&  (xpos <= xmax) )
00151                 {
00152                         if (( ypos >= ymin) && ( ypos <= ymax) )
00153                         {
00154                                 tmpList[n++] = i;
00155                         }
00156                 }
00157                 if (xpos > xmax)
00158                         break;                          // end of the inteference area
00159 
00160                 xprec = xpos;
00161                 yprec = xpos;
00162         }
00163         
00164         
00165         int* list = new int[n];
00166         memcpy(list, tmpList, n * sizeof(int));
00167         delete [] tmpList;
00168         *numInfluencedNodes = n;
00169         return list;
00170 }
00171 
00172 
00173 double WirelessChModule::getPropDelay(Position *source, Position* dest)
00174 {
00175         return  (sqrt( ((source->getX()-dest->getX())*(source->getX()-dest->getX())) + 
00176                                 ((source->getY()-dest->getY())*(source->getY()-dest->getY())) ) / SPEED_OF_LIGHT);
00177 }
00178 
00179 void WirelessChModule::sendUpPhy(Packet *p,ChSAP *chsap)
00180 {
00181 
00182         
00183         Scheduler &s = Scheduler::instance();
00184         struct hdr_cmn *hdr = HDR_CMN(p);
00185         
00186         hdr->direction() = hdr_cmn::UP;
00187         
00188         double Pt = p->txinfo_.getTxPr();
00189         // calculate max radius
00190         double radius = sqrt(( Pt * lambda_ * lambda_) / (L_ * CSThresh_)) /  (4 * PI);
00191 
00192         if(!sorted_){
00193                 sortChSAPList();                // first packet trasmission sorting
00194         }
00195         
00196         Position *sourcePos = chsap->getPosition();
00197         ChSAP *dest;
00198         
00199         int *affectedNodes;
00200         int numInfluencedNodes = -1, i;
00201         affectedNodes = getInfluencedNodes(chsap->getPosition(), radius + /* safety */ 5, &numInfluencedNodes);
00202         
00203         for (i=0; i < numInfluencedNodes; i++) {
00204                 
00205                 dest = (ChSAP*)getChSAP(affectedNodes[i]);
00206                 if (chsap == dest)              // it's the source node -> skip it
00207                         continue;
00208                 
00209                 s.schedule(dest, p->copy(), getPropDelay(sourcePos, dest->getPosition()));
00210         }
00211         
00212         delete [] affectedNodes;
00213 
00214         Packet::free(p);
00215 }
00216 
00217 void WirelessChModule::recv(Packet *p, ChSAP* chsap)
00218 {
00219         sendUpPhy(p, chsap);
00220 }

Generated on Wed Nov 26 15:47:29 2008 for NS-MIRACLE library by  doxygen 1.5.2