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

📄 test_source.cc

📁 topo_gen基于NS2的场景自动生成软件源代码
💻 CC
字号:
//// test_source.cc : Test Source Main File// author         : Fabio Silva//// Copyright (C) 2000-2003 by the University of Southern California// $Id: test_source.cc,v 1.1 2005/04/22 06:08:36 fstann 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 "test_source.hh"#include <unistd.h>TestSourceApp *test_source_app;void signal_handler(int p){  test_source_app->stop();  exit(0);}void TestSourceApp::stop(){  FILE *outfile = NULL;  char outfile_name[100];  if (my_id_ != -1)    sprintf(outfile_name, "/tmp/source-%d.out", my_id_);  else    sprintf(outfile_name, "/tmp/source.out");  outfile = fopen(outfile_name, "a");  if (outfile){    fprintf(outfile, "Source %d sent %d messages !\n",	    my_id_, num_msg_sent_);    fclose(outfile);  }  DiffPrint(DEBUG_ALWAYS,	    "Stopping... source sent %d messages !\n",	    num_msg_sent_);}handle TestSourceApp::setupPublication(){  NRAttrVec attrs;  attrs.push_back(NRClassAttr.make(NRAttribute::IS,				   NRAttribute::DATA_CLASS));  // Use push or pull semantics  if (using_push_){    attrs.push_back(NRScopeAttr.make(NRAttribute::IS,				     NRAttribute::GLOBAL_SCOPE));    if (using_gear_){      attrs.push_back(LatitudeAttr.make(NRAttribute::GE, lat_min_));      attrs.push_back(LatitudeAttr.make(NRAttribute::LE, lat_max_));      attrs.push_back(LongitudeAttr.make(NRAttribute::GE, long_min_));      attrs.push_back(LongitudeAttr.make(NRAttribute::LE, long_max_));    }  }  else{    attrs.push_back(NRScopeAttr.make(NRAttribute::IS,				     NRAttribute::NODE_LOCAL_SCOPE));    attrs.push_back(NRProtocolAttr.make(NRAttribute::IS,					NRAttribute::ONE_PHASE_PULL_PROTOCOL));    if (using_gear_){      attrs.push_back(LatitudeAttr.make(NRAttribute::IS, lat_pt_));      attrs.push_back(LongitudeAttr.make(NRAttribute::IS, long_pt_));    }  }  attrs.push_back(TargetAttr.make(NRAttribute::IS, "Elephant"));  attrs.push_back(NodeAttr.make(NRAttribute::IS, my_id_));  handle h = dr_->publish(&attrs);  ClearAttrs(&attrs);  return h;}void TestSourceApp::run(){  struct timeval tmv;  int retval;  double sleep_time;  // Setup publication and subscription  pubHandle_ = setupPublication();  // Create time attribute  GetTime(&tmv);  lastEventTime_ = new EventTime;  lastEventTime_->seconds_ = tmv.tv_sec;  lastEventTime_->useconds_ = tmv.tv_usec;  timeAttr_ = TimeAttr.make(NRAttribute::IS, (void *) &lastEventTime_,			    sizeof(EventTime));  data_attr_.push_back(timeAttr_);  // Change pointer to point to attribute's payload  delete lastEventTime_;  lastEventTime_ = (EventTime *) timeAttr_->getVal();   // Main thread will send ping probes  while(1){    sleep_time = -1 * (log(1 - (GetRand() * 1.0 / RAND_MAX))) / send_rate_lambda_;    //    DiffPrint(DEBUG_ALWAYS, "Source: Sleeping for %.62f seconds !\n",    //      	      sleep_time);    tmv.tv_sec = (int) floor(sleep_time);    tmv.tv_usec = (int) floor((sleep_time - tmv.tv_sec) * 1000000);    select(0, NULL, NULL, NULL, &tmv);    // Update time and send packet     GetTime(&tmv);    lastEventTime_->seconds_ = tmv.tv_sec;    lastEventTime_->useconds_ = tmv.tv_usec;    // Send data probe    DiffPrint(DEBUG_DETAILS, "Sending Data !\n");    retval = dr_->send(pubHandle_, &data_attr_);    if (tmv.tv_sec > start_time_.tv_sec + EXPERIMENT_WARM_UP_TIME){      // Count this message      num_msg_sent_++;    }  }}void TestSourceApp::usage(char *s){  DiffPrint(DEBUG_ALWAYS, "Usage: %s [-d debug] [-p port] [-s] [-g] [-r] [-h]\n\n", s);  DiffPrint(DEBUG_ALWAYS, "\t-d - Sets debug level (0-10)\n");  DiffPrint(DEBUG_ALWAYS, "\t-p - Uses port 'port' to talk to diffusion\n");  DiffPrint(DEBUG_ALWAYS, "\t-r - Sets send rate\n");  DiffPrint(DEBUG_ALWAYS,	    "\t-s - Uses push semantics (default: one-phase-pull)\n");  DiffPrint(DEBUG_ALWAYS, "\t-g - Uses gear\n");  DiffPrint(DEBUG_ALWAYS, "\t-h - Prints this information\n");  DiffPrint(DEBUG_ALWAYS, "\n");  exit(0);}void TestSourceApp::parseCommandLine(int argc, char **argv){  u_int16_t diff_port = DEFAULT_DIFFUSION_PORT;  int debug_level;  int opt;  config_file_ = NULL;  using_push_ = false;  using_gear_ = false;  opterr = 0;  lat_min_ = -1;  lat_max_ = -1;  long_min_ = -1;  long_max_ = -1;  lat_pt_ = -1;  long_pt_ = -1;  while (1){    opt = getopt(argc, argv, "sgr:hd:p:i:j:k:l:x:y:");    switch (opt){    case 'p':      diff_port = (u_int16_t) atoi(optarg);      if ((diff_port < 1024) || (diff_port >= 65535)){	DiffPrint(DEBUG_ALWAYS, "Error: Diffusion port must be between 1024 and 65535 !\n");	exit(-1);      }      break;    case 'r':      send_rate_lambda_ = (float) atof(optarg);      if ((send_rate_lambda_ < 0.000000001 || send_rate_lambda_ > 20)){	DiffPrint(DEBUG_ALWAYS, "Error: Invalid send rate !\n");	exit(-1);      }      break;    case 'i':      long_min_ = (float) atof(optarg);      break;    case 'j':      long_max_ = (float) atof(optarg);      break;    case 'k':      lat_min_ = (float) atof(optarg);      break;    case 'l':      lat_max_ = (float) atof(optarg);      break;    case 'x':      long_pt_ = (float) atof(optarg);      break;    case 'y':      lat_pt_ = (float) atof(optarg);      break;    case 'h':      usage(argv[0]);      break;    case 'd':      debug_level = atoi(optarg);      if (debug_level < 1 || debug_level > 10){	DiffPrint(DEBUG_ALWAYS, "Error: Debug level outside range or missing !\n");	usage(argv[0]);      }      global_debug_level = debug_level;      break;    case 's':      using_push_ = true;      break;    case 'g':      using_gear_ = true;      break;    case '?':      DiffPrint(DEBUG_ALWAYS,		"Error: %c isn't a valid option or its parameter is missing !\n", optopt);      usage(argv[0]);      break;    case ':':      DiffPrint(DEBUG_ALWAYS, "Parameter missing !\n");      usage(argv[0]);      break;    }    if (opt == -1)      break;  }  diffusion_port_ = diff_port;  // Check for gear coordinates input  if (using_gear_){    if (using_push_){      // For push, we must have a region      if (lat_min_ == -1 || lat_max_ == -1 ||	  long_min_ == -1 || long_max_ == 1){	DiffPrint(DEBUG_ALWAYS, "Using push and gear but region not set !\n");	exit(-1);      }    }    else{      if (lat_pt_ == -1 || long_pt_ == -1){	DiffPrint(DEBUG_ALWAYS, "Using pull and gear but not point set !\n");	exit(-1);      }    }  }}TestSourceApp::TestSourceApp(int argc, char **argv){  struct timeval tmv;  int sleep_time;  char *sim_id;  num_msg_sent_ = 0;  send_rate_lambda_ = SEND_RATE;  parseCommandLine(argc, argv);  dr_ = NR::createNR(diffusion_port_);  // Initialize Experiment  GetTime(&start_time_);  sim_id = getenv("SIM_ID");  if (sim_id)    my_id_ = atoi(sim_id);  else    my_id_ = -1;  GetTime(&tmv);  SetSeed(&tmv);  sleep_time = STARTUP_DELAY_TIME +    (int) ((STARTUP_DELAY_JITTER * (GetRand() * 1.0 / RAND_MAX)	    - (STARTUP_DELAY_JITTER / 2)));  //  DiffPrint(DEBUG_ALWAYS, "Source: Sleeping for %d seconds !\n",  //	    sleep_time);  sleep(sleep_time);  DiffPrint(DEBUG_ALWAYS, "Source Initialized !\n");  //  DiffPrint(DEBUG_ALWAYS, "Rate is %f\n", send_rate_lambda_);}#ifndef USE_SINGLE_ADDRESS_SPACEint main(int argc, char **argv){  TestSourceApp *app;  app = new TestSourceApp(argc, argv);  test_source_app = app;  signal(SIGINT, signal_handler);  app->run();  return 0;}#endif //!USE_SINGLE_ADDRESS_SPACE

⌨️ 快捷键说明

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