Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

sensor-query.cc

Go to the documentation of this file.
00001 // Copyright (c) 2000 by the University of Southern California
00002 // All rights reserved.
00003 //
00004 // Permission to use, copy, modify, and distribute this software and its
00005 // documentation in source and binary forms for non-commercial purposes
00006 // and without fee is hereby granted, provided that the above copyright
00007 // notice appear in all copies and that both the copyright notice and
00008 // this permission notice appear in supporting documentation. and that
00009 // any documentation, advertising materials, and other materials related
00010 // to such distribution and use acknowledge that the software was
00011 // developed by the University of Southern California, Information
00012 // Sciences Institute.  The name of the University may not be used to
00013 // endorse or promote products derived from this software without
00014 // specific prior written permission.
00015 //
00016 // THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about
00017 // the suitability of this software for any purpose.  THIS SOFTWARE IS
00018 // PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
00019 // INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00020 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 //
00022 // Other copyrights might apply to parts of this software and are so
00023 // noted when applicable.
00024 //
00025 // $Header: /nfs/jade/vint/CVSROOT/ns-2/sensor-nets/sensor-query.cc,v 1.4 2000/09/01 03:04:11 haoboy Exp $
00026 
00027 #include <stdarg.h>
00028 #include <float.h>
00029 
00030 #include "sensor-query.h"
00031 #include "landmark.h"
00032 #include <random.h>
00033 
00034 #define CONST_INTERVAL 30
00035 
00036 static class SensorQueryClass:public TclClass
00037 {
00038   public:
00039   SensorQueryClass ():TclClass ("Agent/SensorQuery")
00040   {
00041   }
00042   TclObject *create (int, const char *const *)
00043   {
00044     return (new SensorQueryAgent ());
00045   }
00046 } class_sensor_query;
00047 
00048 
00049 
00050 
00051 void SensorQueryAgent::
00052 trace (char *fmt,...)
00053 {
00054   va_list ap; // Define a variable ap that will refer to each argument in turn
00055 
00056   if (!tracetarget_)
00057     return;
00058 
00059   // Initializes ap to first argument
00060   va_start (ap, fmt); 
00061   // Prints the elements in turn
00062   vsprintf (tracetarget_->buffer (), fmt, ap); 
00063   tracetarget_->dump ();
00064   // Does the necessary clean-up before returning
00065   va_end (ap); 
00066 }
00067 
00068 
00069 
00070 
00071 void
00072 SensorQueryAgent::stop()
00073 {
00074   trace("Node %d: SensorQueryAgent going down at time %f",myaddr_,NOW);
00075 
00076   // Event id > 0 implies presence on the event queue
00077   if(gen_query_event_->uid_) {
00078     Scheduler &s = Scheduler::instance();
00079     s.cancel(gen_query_event_);
00080   }
00081   node_dead_ = 1;
00082 }
00083 
00084 
00085 
00086 
00087 int 
00088 SensorQueryAgent::command (int argc, const char *const *argv)
00089 {
00090   if (argc == 2)
00091     {
00092       if (strcmp (argv[1], "start") == 0)
00093         {
00094           startUp();
00095           return (TCL_OK);
00096         }
00097       if (strcmp (argv[1], "stop") == 0)
00098         {
00099           stop();
00100           return (TCL_OK);
00101         }
00102       if (strcmp (argv[1], "generate-query") == 0)
00103         {
00104           generate_query(-1,-1,-1);
00105           return (TCL_OK);
00106         }
00107     }
00108   else if (argc == 3)
00109     {
00110       if (strcasecmp (argv[1], "tracetarget") == 0)
00111         {
00112           TclObject *obj;
00113           if ((obj = TclObject::lookup (argv[2])) == 0)
00114             {
00115               fprintf (stderr, "%s: %s lookup of %s failed\n", __FILE__, argv[1],
00116                        argv[2]);
00117               return TCL_ERROR;
00118             }
00119           tracetarget_ = (Trace *) obj;
00120           return TCL_OK;
00121         }
00122       else if (strcasecmp (argv[1], "addr") == 0) {
00123         int temp;
00124         temp = Address::instance().str2addr(argv[2]);
00125         myaddr_ = temp;
00126         return TCL_OK;
00127       }
00128       else if (strcasecmp (argv[1], "attach-tag-dbase") == 0)
00129         {
00130           TclObject *obj;
00131           if ((obj = TclObject::lookup (argv[2])) == 0)
00132             {
00133               fprintf (stderr, "%s: %s lookup of %s failed\n", __FILE__, argv[1],
00134                        argv[2]);
00135               return TCL_ERROR;
00136             }
00137           tag_dbase_ = (tags_database *) obj;
00138           return TCL_OK;
00139         }
00140     }
00141   else if (argc == 5) {
00142     if (strcmp (argv[1], "generate-query") == 0)
00143       {
00144         int p1 = atoi(argv[2]);
00145         int p2 = atoi(argv[3]);
00146         int p3 = atoi(argv[4]);
00147         generate_query(p1,p2,p3);
00148         return (TCL_OK);
00149       }
00150   }
00151   
00152   return (Agent::command (argc, argv));
00153 }
00154 
00155 
00156 
00157 
00158 
00159 void
00160 SensorQueryAgent::startUp()
00161 {
00162   //  Scheduler &s = Scheduler::instance();
00163   //  double sched_time = CONST_INTERVAL + Random::uniform(query_interval_);
00164   //  trace("Node %d: Scheduling query gen at %f from now %f",myaddr_,sched_time,s.clock());
00165   //  s.schedule(query_handler_, gen_query_event_, Random::uniform(query_interval_));
00166   trace("Node %d: SensorQueryAgent starting up at time %f",myaddr_,NOW);
00167   node_dead_ = 0;
00168 }
00169 
00170 //void
00171 //SensorQueryAgent::handle(Event *) {
00172 //  Scheduler &s = Scheduler::instance();
00173 //  generate_query();
00174 //  double sched_time = Random::uniform(query_interval_);
00175 //  trace("Node %d: Scheduling query gen at %f from now %f",myaddr_,sched_time,s.clock());
00176 //  s.schedule(this, gen_query_event_, Random::uniform(query_interval_));
00177 //}
00178 
00179 void
00180 SensorQueryAgent::generate_query(int p1, int p2, int p3)
00181 {
00182   Packet *p = allocpkt();
00183   hdr_ip *iph = HDR_IP(p);
00184   hdr_cmn *hdrc = HDR_CMN(p);
00185   unsigned char *walk;
00186   Scheduler &s = Scheduler::instance();
00187   int obj_name;
00188   int next_hop_level = 0, num_src_hops = 0;
00189   int X = 65000, Y = 65000;
00190   int action = QUERY_PKT;
00191 
00192   if(node_dead_) {
00193     trace("Node %d: node failed, cannot generate query");
00194     return;
00195   }
00196 
00197   // Need to ask our routing module to direct the packet
00198   hdrc->next_hop_ = myaddr_;
00199   hdrc->addr_type_ = NS_AF_INET;
00200   iph->daddr() = myaddr_;
00201   iph->dport() = ROUTER_PORT;
00202   iph->ttl_ = 300;   // since only 300 ids in source route in packet
00203 
00204   iph->saddr() = myaddr_;
00205   iph->sport() = 0;
00206 
00207   // LM agent checks if the source port is 0 to identify a query packet  
00208   // 2 bytes each for X and Y co-ords
00209   // 1 byte for level - used in next hop lookup
00210   // 4 bytes for object name; 2 byte for number of hops in src route
00211   // 4 bytes for origin_time
00212   // 90 bytes to store 30 source route ids and their levels (1 byte each)
00213   // (assuming max_levels = 15) 
00214   p->allocdata(105);
00215   walk = p->accessdata();
00216 
00217   (*walk++) = action & 0xFF;
00218 
00219   // X coord = 65000 initially
00220   (*walk++) = (X >> 8) & 0xFF;
00221   (*walk++) = X & 0xFF;
00222 
00223   // Y coord = 65000 initially
00224   (*walk++) = (Y >> 8) & 0xFF;
00225   (*walk++) = Y & 0xFF;
00226 
00227   // Indicates next hop level
00228   (*walk++) = next_hop_level & 0xFF;
00229 
00230   if(p1 != -1 && p2 != -1 && p3 != -1)
00231     obj_name = (int) ((p1 * pow(2,24)) + (p2 * pow(2,16)) + p3) ;
00232   else
00233     obj_name = tag_dbase_->get_random_tag();
00234 
00235   //  p1 = Random::integer(10);
00236   //  p2 = Random::integer(10);
00237   //  p3 = Random::integer(50);
00238 
00239   (*walk++) = (obj_name >> 24) & 0xFF;
00240   (*walk++) = (obj_name >> 16) & 0xFF;
00241   (*walk++) = (obj_name >> 8) & 0xFF;
00242   (*walk++) = (obj_name) & 0xFF;
00243 
00244   double now = Scheduler::instance().clock();
00245   trace("Node %d: Generating query for object %d.%d.%d at time %f",myaddr_,(obj_name >> 24) & 0xFF,(obj_name >> 16) & 0xFF, obj_name & 0xFFFF,now);
00246   
00247 
00248   int origin_time = (int) now;
00249   (*walk++) = (origin_time >> 24) & 0xFF;
00250   (*walk++) = (origin_time >> 16) & 0xFF;
00251   (*walk++) = (origin_time >> 8) & 0xFF;
00252   (*walk++) = (origin_time) & 0xFF;
00253 
00254   // Number of source route hops in packet (= 0); 2 bytes
00255   (*walk++) = (num_src_hops >> 8) & 0xFF;
00256   (*walk++) = (num_src_hops) & 0xFF;
00257 
00258   // Above fields will be 4 bytes each. 20 bytes for the IP header will be
00259   // added in LM agent. No source route hops on query creation.
00260   hdrc->size_ = 24; 
00261   hdrc->direction() = hdr_cmn::DOWN;
00262 
00263   s.schedule(target_,p,0);
00264   //  double sched_time = CONST_INTERVAL + Random::uniform(query_interval_);
00265   //  trace("Node %d: Scheduling query gen at %f from now %f",myaddr_,sched_time,s.clock());
00266   //  s.schedule(query_handler_, gen_query_event_, sched_time);
00267   //  s.schedule(this, gen_query_event_, Random::uniform(query_interval_));
00268 }
00269 
00270 
00271 
00272 void
00273 SensorQueryAgent::recv(Packet *p, Handler *)
00274 {
00275   unsigned char *walk = p->accessdata();
00276   int X = 0, Y = 0, obj_name = -1;
00277 
00278   if(node_dead_) {
00279     Packet::free(p);
00280     return;
00281   }
00282 
00283   ++walk;
00284   X = *walk++;
00285   X = (X << 8) | *walk++;
00286 
00287   Y = *walk++;
00288   Y = (Y << 8) | *walk++;
00289   
00290   ++walk;
00291   obj_name = *walk++;
00292   obj_name = (obj_name << 8) | *walk++;
00293   obj_name = (obj_name << 8) | *walk++;
00294   obj_name = (obj_name << 8) | *walk++;
00295 
00296   double now = Scheduler::instance().clock();
00297   trace("Node %d: SensorQueryAgent Found object %d.%d.%d at (%d,%d) at time %f", myaddr_, (obj_name >> 24) & 0xFF, (obj_name >> 16) & 0xFF, obj_name & 0xFFFF,X,Y,now);
00298 
00299   Packet::free(p);
00300 }
00301 
00302 
00303 
00304 SensorQueryAgent::SensorQueryAgent() : Agent(PT_MESSAGE) { 
00305     query_interval_ = 120;
00306     gen_query_event_ = new Event;
00307     query_handler_ = new SensorQueryHandler(this);
00308     node_dead_ = 0;
00309 }
00310 
00311 

Generated on Tue Apr 20 12:14:32 2004 for NS2.26SourcesOriginal by doxygen 1.3.3