sensor-query.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:9k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * sensor-query.cc
  3.  * Copyright (C) 2000 by the University of Southern California
  4.  * $Id: sensor-query.cc,v 1.5 2005/08/25 18:58:12 johnh Exp $
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License,
  8.  * version 2, as published by the Free Software Foundation.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18.  *
  19.  *
  20.  * The copyright of this module includes the following
  21.  * linking-with-specific-other-licenses addition:
  22.  *
  23.  * In addition, as a special exception, the copyright holders of
  24.  * this module give you permission to combine (via static or
  25.  * dynamic linking) this module with free software programs or
  26.  * libraries that are released under the GNU LGPL and with code
  27.  * included in the standard release of ns-2 under the Apache 2.0
  28.  * license or under otherwise-compatible licenses with advertising
  29.  * requirements (or modified versions of such code, with unchanged
  30.  * license).  You may copy and distribute such a system following the
  31.  * terms of the GNU GPL for this module and the licenses of the
  32.  * other code concerned, provided that you include the source code of
  33.  * that other code when and as the GNU GPL requires distribution of
  34.  * source code.
  35.  *
  36.  * Note that people who make modified versions of this module
  37.  * are not obligated to grant this special exception for their
  38.  * modified versions; it is their choice whether to do so.  The GNU
  39.  * General Public License gives permission to release a modified
  40.  * version without this exception; this exception also makes it
  41.  * possible to release a modified version which carries forward this
  42.  * exception.
  43.  *
  44.  */
  45. // $Header: /cvsroot/nsnam/ns-2/sensor-nets/sensor-query.cc,v 1.5 2005/08/25 18:58:12 johnh Exp $
  46. #include <stdarg.h>
  47. #include <float.h>
  48. #include "sensor-query.h"
  49. #include "landmark.h"
  50. #include <random.h>
  51. #define CONST_INTERVAL 30
  52. static class SensorQueryClass:public TclClass
  53. {
  54.   public:
  55.   SensorQueryClass ():TclClass ("Agent/SensorQuery")
  56.   {
  57.   }
  58.   TclObject *create (int, const char *const *)
  59.   {
  60.     return (new SensorQueryAgent ());
  61.   }
  62. } class_sensor_query;
  63. void SensorQueryAgent::
  64. trace (char *fmt,...)
  65. {
  66.   va_list ap; // Define a variable ap that will refer to each argument in turn
  67.   if (!tracetarget_)
  68.     return;
  69.   // Initializes ap to first argument
  70.   va_start (ap, fmt); 
  71.   // Prints the elements in turn
  72.   vsprintf (tracetarget_->buffer (), fmt, ap); 
  73.   tracetarget_->dump ();
  74.   // Does the necessary clean-up before returning
  75.   va_end (ap); 
  76. }
  77. void
  78. SensorQueryAgent::stop()
  79. {
  80.   trace("Node %d: SensorQueryAgent going down at time %f",myaddr_,NOW);
  81.   // Event id > 0 implies presence on the event queue
  82.   if(gen_query_event_->uid_) {
  83.     Scheduler &s = Scheduler::instance();
  84.     s.cancel(gen_query_event_);
  85.   }
  86.   node_dead_ = 1;
  87. }
  88. int 
  89. SensorQueryAgent::command (int argc, const char *const *argv)
  90. {
  91.   if (argc == 2)
  92.     {
  93.       if (strcmp (argv[1], "start") == 0)
  94.         {
  95.           startUp();
  96.           return (TCL_OK);
  97.         }
  98.       if (strcmp (argv[1], "stop") == 0)
  99. {
  100.   stop();
  101.   return (TCL_OK);
  102. }
  103.       if (strcmp (argv[1], "generate-query") == 0)
  104.         {
  105.           generate_query(-1,-1,-1);
  106.           return (TCL_OK);
  107.         }
  108.     }
  109.   else if (argc == 3)
  110.     {
  111.       if (strcasecmp (argv[1], "tracetarget") == 0)
  112.         {
  113.           TclObject *obj;
  114.   if ((obj = TclObject::lookup (argv[2])) == 0)
  115.             {
  116.               fprintf (stderr, "%s: %s lookup of %s failedn", __FILE__, argv[1],
  117.                        argv[2]);
  118.               return TCL_ERROR;
  119.             }
  120.           tracetarget_ = (Trace *) obj;
  121.           return TCL_OK;
  122.         }
  123.       else if (strcasecmp (argv[1], "addr") == 0) {
  124.         int temp;
  125.         temp = Address::instance().str2addr(argv[2]);
  126.         myaddr_ = temp;
  127.         return TCL_OK;
  128.       }
  129.       else if (strcasecmp (argv[1], "attach-tag-dbase") == 0)
  130.         {
  131.           TclObject *obj;
  132.           if ((obj = TclObject::lookup (argv[2])) == 0)
  133.             {
  134.               fprintf (stderr, "%s: %s lookup of %s failedn", __FILE__, argv[1],
  135.                        argv[2]);
  136.               return TCL_ERROR;
  137.             }
  138.           tag_dbase_ = (tags_database *) obj;
  139.           return TCL_OK;
  140.         }
  141.     }
  142.   else if (argc == 5) {
  143.     if (strcmp (argv[1], "generate-query") == 0)
  144.       {
  145. int p1 = atoi(argv[2]);
  146. int p2 = atoi(argv[3]);
  147. int p3 = atoi(argv[4]);
  148. generate_query(p1,p2,p3);
  149. return (TCL_OK);
  150.       }
  151.   }
  152.   
  153.   return (Agent::command (argc, argv));
  154. }
  155. void
  156. SensorQueryAgent::startUp()
  157. {
  158.   //  Scheduler &s = Scheduler::instance();
  159.   //  double sched_time = CONST_INTERVAL + Random::uniform(query_interval_);
  160.   //  trace("Node %d: Scheduling query gen at %f from now %f",myaddr_,sched_time,s.clock());
  161.   //  s.schedule(query_handler_, gen_query_event_, Random::uniform(query_interval_));
  162.   trace("Node %d: SensorQueryAgent starting up at time %f",myaddr_,NOW);
  163.   node_dead_ = 0;
  164. }
  165. //void
  166. //SensorQueryAgent::handle(Event *) {
  167. //  Scheduler &s = Scheduler::instance();
  168. //  generate_query();
  169. //  double sched_time = Random::uniform(query_interval_);
  170. //  trace("Node %d: Scheduling query gen at %f from now %f",myaddr_,sched_time,s.clock());
  171. //  s.schedule(this, gen_query_event_, Random::uniform(query_interval_));
  172. //}
  173. void
  174. SensorQueryAgent::generate_query(int p1, int p2, int p3)
  175. {
  176.   Packet *p = allocpkt();
  177.   hdr_ip *iph = HDR_IP(p);
  178.   hdr_cmn *hdrc = HDR_CMN(p);
  179.   unsigned char *walk;
  180.   Scheduler &s = Scheduler::instance();
  181.   int obj_name;
  182.   int next_hop_level = 0, num_src_hops = 0;
  183.   int X = 65000, Y = 65000;
  184.   int action = QUERY_PKT;
  185.   if(node_dead_) {
  186.     trace("Node %d: node failed, cannot generate query");
  187.     return;
  188.   }
  189.   // Need to ask our routing module to direct the packet
  190.   hdrc->next_hop_ = myaddr_;
  191.   hdrc->addr_type_ = NS_AF_INET;
  192.   iph->daddr() = myaddr_;
  193.   iph->dport() = ROUTER_PORT;
  194.   iph->ttl_ = 300;   // since only 300 ids in source route in packet
  195.   iph->saddr() = myaddr_;
  196.   iph->sport() = 0;
  197.   // LM agent checks if the source port is 0 to identify a query packet  
  198.   // 2 bytes each for X and Y co-ords
  199.   // 1 byte for level - used in next hop lookup
  200.   // 4 bytes for object name; 2 byte for number of hops in src route
  201.   // 4 bytes for origin_time
  202.   // 90 bytes to store 30 source route ids and their levels (1 byte each)
  203.   // (assuming max_levels = 15) 
  204.   p->allocdata(105);
  205.   walk = p->accessdata();
  206.   (*walk++) = action & 0xFF;
  207.   // X coord = 65000 initially
  208.   (*walk++) = (X >> 8) & 0xFF;
  209.   (*walk++) = X & 0xFF;
  210.   // Y coord = 65000 initially
  211.   (*walk++) = (Y >> 8) & 0xFF;
  212.   (*walk++) = Y & 0xFF;
  213.   // Indicates next hop level
  214.   (*walk++) = next_hop_level & 0xFF;
  215.   if(p1 != -1 && p2 != -1 && p3 != -1)
  216.     obj_name = (int) ((p1 * pow(2,24)) + (p2 * pow(2,16)) + p3) ;
  217.   else
  218.     obj_name = tag_dbase_->get_random_tag();
  219.   //  p1 = Random::integer(10);
  220.   //  p2 = Random::integer(10);
  221.   //  p3 = Random::integer(50);
  222.   (*walk++) = (obj_name >> 24) & 0xFF;
  223.   (*walk++) = (obj_name >> 16) & 0xFF;
  224.   (*walk++) = (obj_name >> 8) & 0xFF;
  225.   (*walk++) = (obj_name) & 0xFF;
  226.   double now = Scheduler::instance().clock();
  227.   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);
  228.   
  229.   int origin_time = (int) now;
  230.   (*walk++) = (origin_time >> 24) & 0xFF;
  231.   (*walk++) = (origin_time >> 16) & 0xFF;
  232.   (*walk++) = (origin_time >> 8) & 0xFF;
  233.   (*walk++) = (origin_time) & 0xFF;
  234.   // Number of source route hops in packet (= 0); 2 bytes
  235.   (*walk++) = (num_src_hops >> 8) & 0xFF;
  236.   (*walk++) = (num_src_hops) & 0xFF;
  237.   // Above fields will be 4 bytes each. 20 bytes for the IP header will be
  238.   // added in LM agent. No source route hops on query creation.
  239.   hdrc->size_ = 24; 
  240.   hdrc->direction() = hdr_cmn::DOWN;
  241.   s.schedule(target_,p,0);
  242.   //  double sched_time = CONST_INTERVAL + Random::uniform(query_interval_);
  243.   //  trace("Node %d: Scheduling query gen at %f from now %f",myaddr_,sched_time,s.clock());
  244.   //  s.schedule(query_handler_, gen_query_event_, sched_time);
  245.   //  s.schedule(this, gen_query_event_, Random::uniform(query_interval_));
  246. }
  247. void
  248. SensorQueryAgent::recv(Packet *p, Handler *)
  249. {
  250.   unsigned char *walk = p->accessdata();
  251.   int X = 0, Y = 0, obj_name = -1;
  252.   if(node_dead_) {
  253.     Packet::free(p);
  254.     return;
  255.   }
  256.   ++walk;
  257.   X = *walk++;
  258.   X = (X << 8) | *walk++;
  259.   Y = *walk++;
  260.   Y = (Y << 8) | *walk++;
  261.   
  262.   ++walk;
  263.   obj_name = *walk++;
  264.   obj_name = (obj_name << 8) | *walk++;
  265.   obj_name = (obj_name << 8) | *walk++;
  266.   obj_name = (obj_name << 8) | *walk++;
  267.   double now = Scheduler::instance().clock();
  268.   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);
  269.   Packet::free(p);
  270. }
  271. SensorQueryAgent::SensorQueryAgent() : Agent(PT_MESSAGE) { 
  272.     query_interval_ = 120;
  273.     gen_query_event_ = new Event;
  274.     query_handler_ = new SensorQueryHandler(this);
  275.     node_dead_ = 0;
  276. }