⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gear.cc

📁 跑leach需要的
💻 CC
📖 第 1 页 / 共 3 页
字号:
//// gear.cc        : GEAR Filter// authors        : Yan Yu and Fabio Silva//// Copyright (C) 2000-2003 by the University of Southern California// Copyright (C) 2000-2003 by the University of California// $Id: gear.cc,v 1.3 2003/09/24 17:45:13 haldar Exp $//// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License,// version 2, as published by the Free Software Foundation.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License along// with this program; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.////#include "gear.hh"#ifdef NS_DIFFUSIONstatic class GeoRoutingFilterClass : public TclClass {public:  GeoRoutingFilterClass() : TclClass("Application/DiffApp/GeoRoutingFilter") {}  TclObject * create(int argc, const char*const* argv) {    if (argc == 5)      return (new GeoRoutingFilter(argv[4]));    else       fprintf(stderr, "Insufficient number of args for creating GeoRoutingFilter");    return (NULL);  }} class_geo_routing_filter;int GeoRoutingFilter::command(int argc, const char*const* argv) {  if (argc == 2) {    if (strcmp(argv[1], "start") == 0) {      run();      return TCL_OK;    }  }  return DiffApp::command(argc, argv);}#endif // NS_DIFFUSIONvoid GeoFilterReceive::recv(Message *msg, handle h){  app_->recv(msg, h);}int GeoMessageSendTimer::expire(){  // Call timeout function  agent_->messageTimeout(msg_);  // Do not reschedule this timer  delete this;  return -1;}int GeoNeighborsTimer::expire(){  // Call timeout function  agent_->neighborTimeout();  // Reschedule this timer  return 0;}int GeoBeaconRequestTimer::expire(){  // Call the timeout function  agent_->beaconTimeout();  // Reschedule this timer  return 0;}void GeoRoutingFilter::beaconTimeout(){  NRAttrVec attrs;  Message *beacon_msg;  struct timeval tv;  GetTime(&tv);  DiffPrintWithTime(DEBUG_IMPORTANT, "Gear - Beacon Timeout !\n");  // We broadcast the request from time to time, in case a new  // neighbor joins in and never gets a chance to get its informaiton  if ((last_neighbor_request_tv_.tv_sec == 0) ||      ((tv.tv_sec - last_neighbor_request_tv_.tv_sec) >=       GEO_NEIGHBOR_REQUEST_PERIOD)){    // Update timestamp    GetTime(&last_neighbor_request_tv_);    // Sends a beacon to all neighbors    attrs.push_back(GeoBeaconTypeAttr.make(NRAttribute::IS, GEO_REQUEST));    attrs.push_back(GeoLatitudeAttr.make(NRAttribute::IS, geo_latitude_));    attrs.push_back(GeoLongitudeAttr.make(NRAttribute::IS, geo_longitude_));    attrs.push_back(GeoRemainingEnergyAttr.make(NRAttribute::IS,						remainingEnergy()));    beacon_msg = new Message(DIFFUSION_VERSION, DATA, 0, 0,			     attrs.size(), pkt_count_, rdm_id_,			     BROADCAST_ADDR, LOCALHOST_ADDR);    // Don't forget to increment pkt_count    pkt_count_++;    beacon_msg->msg_attr_vec_ = CopyAttrs(&attrs);    DiffPrintWithTime(DEBUG_IMPORTANT, "Gear - Sending Beacon Request...\n");    ((DiffusionRouting *)dr_)->sendMessage(beacon_msg, post_filter_handle_);    ClearAttrs(&attrs);    delete beacon_msg;  }}void GeoRoutingFilter::neighborTimeout(){  NeighborList::iterator neighbor_itr;  NeighborEntry *neighbor_entry;  struct timeval tv;  GetTime(&tv);  DiffPrintWithTime(DEBUG_IMPORTANT, "Gear - Neighbors Timeout !\n");  neighbor_itr = neighbors_list_.begin();  while (neighbor_itr != neighbors_list_.end()){    neighbor_entry = *neighbor_itr;    DiffPrintWithTime(DEBUG_IMPORTANT, "Gear - Check: %d: %d --> %d\n",		      neighbor_entry->id_, neighbor_entry->tv_.tv_sec,		      tv.tv_sec);    if ((tv.tv_sec - neighbor_entry->tv_.tv_sec) >= GEO_NEIGHBOR_EXPIRED){      // Delete timed-out neighbor      DiffPrintWithTime(DEBUG_IMPORTANT, "Gear - Deleting neighbor %d\n",			neighbor_entry->id_);      neighbor_itr = neighbors_list_.erase(neighbor_itr);      delete neighbor_entry;    }    else{      DiffPrintWithTime(DEBUG_IMPORTANT, "Gear - Neighbor %d, (%d,%d)\n",			neighbor_entry->id_, neighbor_entry->tv_.tv_sec,			neighbor_entry->tv_.tv_usec);      neighbor_itr++;    }  }}void GeoRoutingFilter::messageTimeout(Message *msg){  // Send message  DiffPrintWithTime(DEBUG_IMPORTANT, "Gear - Message Timeout !\n");  ((DiffusionRouting *)dr_)->sendMessage(msg, post_filter_handle_,					 GEOROUTING_POST_FILTER_PRIORITY);}void GeoRoutingFilter::recv(Message *msg, handle h){  if (h == pre_filter_handle_){    preProcessFilter(msg);    return;  }  if (h == post_filter_handle_){    postProcessFilter(msg);    return;  }  DiffPrintWithTime(DEBUG_ALWAYS,		    "Gear - Received message for an unknown handle %d !\n", h);}void GeoRoutingFilter::preProcessFilter(Message *msg){  NRSimpleAttribute<void *> *heuristic_value_attribute = NULL;  NRSimpleAttribute<int> *beacon_type_attribute = NULL;  NRSimpleAttribute<double> *beacon_info_attribute = NULL;  double neighbor_energy = 0.0;  double neighbor_longitude = 0.0;  double neighbor_latitude = 0.0;  double new_heuristic_value = 0.0;  int32_t neighbor_id;  int beacon_type;  struct timeval tv;   HeuristicValue *heuristic_value = NULL;  GeoLocation dst_location;  TimerCallback *beacon_timer = NULL;  NRAttrVec attrs;  Message *beacon_msg = NULL;  PktHeader *pkt_header = NULL;  switch (msg->msg_type_){  case INTEREST:  case EXPLORATORY_DATA:  case PUSH_EXPLORATORY_DATA:    if (msg->new_message_){      // Don't worry about old messages      // Pre-process the messages, extracting the geo-header attribute      // if it's present (or trying to create a new geo-header      // otherwise)      pkt_header = preProcessMessage(msg);      // If we have a packet header, add it to the message list      if (pkt_header){	sendNeighborRequest();      	message_list_.push_back(pkt_header);      }    }    ((DiffusionRouting *)dr_)->sendMessage(msg, pre_filter_handle_);    break;  case DATA:    // Look for a beacon type attribute in the data message    beacon_type_attribute = GeoBeaconTypeAttr.find(msg->msg_attr_vec_);    // If there is no beacon_type attribute, this is not a beacon data    // message. So, we just forward it to the next filter    if (!beacon_type_attribute){      ((DiffusionRouting *)dr_)->sendMessage(msg, pre_filter_handle_);      break;    }    // Get neighbor id    neighbor_id = msg->last_hop_;    if ((neighbor_id == BROADCAST_ADDR) ||	(neighbor_id == LOCALHOST_ADDR)){      DiffPrintWithTime(DEBUG_ALWAYS, "Gear - Neighbor ID Invalid !\n");      return;    }    // Look for the rest of the information    beacon_type = beacon_type_attribute->getVal();    beacon_info_attribute = GeoLongitudeAttr.find(msg->msg_attr_vec_);    if (beacon_info_attribute){      neighbor_longitude = beacon_info_attribute->getVal();    }    beacon_info_attribute = GeoLatitudeAttr.find(msg->msg_attr_vec_);    if (beacon_info_attribute){      neighbor_latitude = beacon_info_attribute->getVal();    }    beacon_info_attribute = GeoRemainingEnergyAttr.find(msg->msg_attr_vec_);    if (beacon_info_attribute){      neighbor_energy = beacon_info_attribute->getVal();    }    // Update our neighbor list with the beacon's information    updateNeighbor(neighbor_id, neighbor_longitude,		   neighbor_latitude, neighbor_energy);    // Get current time    GetTime(&tv);    DiffPrintWithTime(DEBUG_IMPORTANT,		      "Gear - Beacon Information id: %d - location: %f,%f - energy: %f\n",		      neighbor_id, neighbor_longitude,		      neighbor_latitude, neighbor_energy);    if (beacon_type == GEO_REQUEST){      DiffPrintWithTime(DEBUG_IMPORTANT,			"Gear - Received a beacon request...\n");      // If we received a neighbor request, we have to generate a      // reply and send it after some random time. But this has to be      // suppresed if we have recently done it already      if (last_beacon_reply_tv_.tv_sec > 0){	// Send at most one neighbor beacon every	// GEO_BEACON_REPLY_PERIOD seconds	if ((tv.tv_sec - last_beacon_reply_tv_.tv_sec)	    < GEO_BEACON_REPLY_PERIOD){	  DiffPrintWithTime(DEBUG_IMPORTANT,			    "Gear - Ignoring beacon request until %d.%06d!\n",			    (tv.tv_sec + GEO_BEACON_REPLY_PERIOD), tv.tv_usec);	  break;	}      }      // Create beacon reply message      attrs.push_back(GeoLongitudeAttr.make(NRAttribute::IS, geo_longitude_));      attrs.push_back(GeoLatitudeAttr.make(NRAttribute::IS, geo_latitude_));      attrs.push_back(GeoRemainingEnergyAttr.make(NRAttribute::IS,						  remainingEnergy()));      attrs.push_back(GeoBeaconTypeAttr.make(NRAttribute::IS, GEO_REPLY));      // Let's try to extract the beacon's heuristic_value      heuristic_value_attribute = GeoHeuristicValueAttr.find(msg->msg_attr_vec_);      if (heuristic_value_attribute){	// Heuristic Value attribute found ! Include information about	// this destination if we have anything in our table	heuristic_value = (HeuristicValue *) heuristic_value_attribute->getVal();	dst_location.longitude_ = heuristic_value->dst_longitude_;	dst_location.latitude_ = heuristic_value->dst_latitude_;	// Retrieve heuristic_value	DiffPrintWithTime(DEBUG_IMPORTANT,			  "Gear - Requesting h-value in (%lf,%lf)\n",			  dst_location.longitude_, dst_location.latitude_); 	new_heuristic_value = retrieveHeuristicValue(dst_location);	if (new_heuristic_value != INITIAL_HEURISTIC_VALUE){	  // Add the heuristic_value to the message if we have any	  // information about this particular destination 	  heuristic_value = new HeuristicValue(dst_location.longitude_,					       dst_location.latitude_,					       new_heuristic_value);	  DiffPrintWithTime(DEBUG_IMPORTANT,			    "Gear - Current h-value for (%lf,%lf):%lf\n",			    dst_location.longitude_, dst_location.latitude_,			    new_heuristic_value);	  attrs.push_back(GeoHeuristicValueAttr.make(NRAttribute::IS,						     (void *) heuristic_value,						     sizeof(HeuristicValue)));	  delete heuristic_value;	}      }      beacon_msg = new Message(DIFFUSION_VERSION, DATA, 0, 0, attrs.size(),			       pkt_count_, rdm_id_, BROADCAST_ADDR,			       LOCALHOST_ADDR);      // Don't forget to update pkt_count      pkt_count_++;      beacon_msg->msg_attr_vec_ = CopyAttrs(&attrs);      beacon_timer = new GeoMessageSendTimer(this, CopyMessage(beacon_msg));      ((DiffusionRouting *)dr_)->addTimer(GEO_BEACON_REPLY_DELAY +					  (int) ((GEO_BEACON_REPLY_JITTER *						  (GetRand() * 1.0 / RAND_MAX) -						  (GEO_BEACON_REPLY_JITTER / 2))),					  beacon_timer);      ClearAttrs(&attrs);      delete beacon_msg;      DiffPrintWithTime(DEBUG_IMPORTANT,			"Gear - Sending a beacon reply in response to request !\n");      DiffPrintWithTime(DEBUG_IMPORTANT,			"Gear - Local info: location: %f,%f - energy: %f\n",			geo_longitude_,	geo_latitude_, remainingEnergy());      // Update beacon timestamp      GetTime(&last_beacon_reply_tv_);    }    else{      if (beacon_type == GEO_REPLY){	DiffPrintWithTime(DEBUG_IMPORTANT, "Gear - Received beacon reply\n");      }      else{	if (beacon_type == GEO_UPDATE){	  DiffPrintWithTime(DEBUG_IMPORTANT,			    "Gear - Received beacon update\n");	}	else{	  DiffPrintWithTime(DEBUG_ALWAYS,			    "Gear - Received unknown message !\n");	  return;	}      }      // Extract the heuristic_value from the message      heuristic_value_attribute = GeoHeuristicValueAttr.find(msg->msg_attr_vec_);      if (heuristic_value_attribute){	heuristic_value = (HeuristicValue *) heuristic_value_attribute->getVal();	// Update learned cost value table	dst_location.longitude_ = heuristic_value->dst_longitude_;	dst_location.latitude_ = heuristic_value->dst_latitude_;	DiffPrintWithTime(DEBUG_IMPORTANT,		  "Gear - Received h-val update %d: (%lf,%lf):%f\n",		  neighbor_id, dst_location.longitude_,		  dst_location.latitude_,		  heuristic_value->heuristic_value_);	learned_cost_table_.updateEntry(neighbor_id, dst_location,					heuristic_value->heuristic_value_);      }    }    break;  default:    // If we do not know about this message, we just pass it to the    // next filter    ((DiffusionRouting *)dr_)->sendMessage(msg, pre_filter_handle_);    break;  }}void GeoRoutingFilter::postProcessFilter(Message *msg){  PktHeader *pkt_header = NULL;  GeoHeader *geo_header = NULL;  int action;  int32_t next_hop;  switch (msg->msg_type_){  case INTEREST:  case EXPLORATORY_DATA:  case PUSH_EXPLORATORY_DATA:    // Ignore messages with a local application as destination    if (msg->next_hop_ != LOCALHOST_ADDR){      // Retrieve packet header from previous stage      pkt_header = retrievePacketHeader(msg);      // If we have record of this packet, we restore the previous      // geo_header information before forwarding it further      if (pkt_header){	geo_header = restoreGeoHeader(pkt_header, msg);	// Increment path_len	geo_header->path_len_++;	// Check if we have to broadcast this message. We do not need	// to check if I have seen this broadcast packet before, since	// this is called by PostProcessFilter, so it must be a new	// packet. We only need to check if we are inside the target	// region	action = floodInsideRegion(geo_header);	// Add GeoHeader attribute to the message	msg->msg_attr_vec_->push_back(GeoHeaderAttr.make(NRAttribute::IS,							 (void *) geo_header,							 sizeof(GeoHeader)));	switch (action){	case BROADCAST:	  // We are inside the region and have at least a neighbor

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -