📄 setdest.cc
字号:
extern "C" {#include <assert.h>#include <fcntl.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/time.h>#include <sys/types.h>#include <sys/uio.h>#include <unistd.h>#if !defined(sun)#include <err.h>#endif};#include "../../../rng.h"#include "setdest.h"// #define DEBUG#define SANITY_CHECKS//#define SHOW_SYMMETRIC_PAIRS#define GOD_FORMAT "$ns_ at %.12f \"$god_ set-dist %d %d %d\"\n"#define GOD_FORMAT2 "$god_ set-dist %d %d %d\n"#define NODE_FORMAT "$ns_ at %.12f \"$node_(%d) setdest %.12f %.12f %.12f\"\n"#define NODE_FORMAT2 "$node_(%d) setdest %.12f %.12f %.12f\n"#define NODE_FORMAT3 "$node_(%d) set %c_ %.12f\n"#define INFINITY 0x00ffffff#define min(x,y) ((x) < (y) ? (x) : (y))#define max(x,y) ((x) > (y) ? (x) : (y))#define ROUND_ERROR 1e-9static int count = 0;/* ====================================================================== Function Prototypes ====================================================================== */void usage(char**);void init(void);double uniform(void);void dumpall(void);void ComputeW(void);void floyd_warshall(void);void show_diffs(void);void show_routes(void);void show_counters(void);/* ====================================================================== Global Variables ====================================================================== */const double RANGE = 250.0; // transmitter range in metersdouble TIME = 0.0; // my clock;double MAXTIME = 0.0; // duration of simulationdouble MAXX = 0.0;double MAXY = 0.0;double MAXSPEED = 0.0;double PAUSE = 0.0;u_int32_t NODES = 0;u_int32_t RouteChangeCount = 0;u_int32_t LinkChangeCount = 0;u_int32_t DestUnreachableCount = 0;Node *NodeList = 0;u_int32_t *D1 = 0;u_int32_t *D2 = 0;/* ====================================================================== Random Number Generation ====================================================================== */#define M 2147483647L#define INVERSE_M ((double)4.656612875e-10)char random_state[32];RNG *rng;doubleuniform(){ count++; return rng->uniform_double() ;} /* ====================================================================== Misc Functions... ====================================================================== */voidusage(char **argv){ fprintf(stderr, "\nusage: %s\t-n <nodes> -p <pause time> -s <max speed>\n", argv[0]); fprintf(stderr, "\t\t-t <simulation time> -x <max X> -y <max Y>\n\n");}voidinit(){ /* * Initialized the Random Number Generation */ /* This part of init() is commented out and is replaced by more portable RNG (random number generator class of ns) functions. struct timeval tp; int fd, seed, bytes; if((fd = open("/dev/random", O_RDONLY)) < 0) { perror("open"); exit(1); } if((bytes = read(fd, random_state, sizeof(random_state))) < 0) { perror("read"); exit(1); } close(fd); fprintf(stderr, "*** read %d bytes from /dev/random\n", bytes); if(bytes != sizeof(random_state)) { fprintf(stderr,"Not enough randomness. Reading `.rand_state'\n"); if((fd = open(".rand_state", O_RDONLY)) < 0) { perror("open .rand_state"); exit(1); } if((bytes = read(fd, random_state, sizeof(random_state))) < 0) { perror("reading .rand_state"); exit(1); } close(fd); } if(gettimeofday(&tp, 0) < 0) { perror("gettimeofday"); exit(1); } seed = (tp.tv_sec >> 12 ) ^ tp.tv_usec; (void) initstate(seed, random_state, bytes & 0xf8);*/ /* * Allocate memory for globals */ NodeList = new Node[NODES]; if(NodeList == 0) { perror("new"); exit(1); } D1 = new u_int32_t[NODES * NODES]; if(D1 == 0) { perror("new"); exit(1); } memset(D1, '\xff', sizeof(u_int32_t) * NODES * NODES); D2 = new u_int32_t[NODES * NODES]; if(D2 == 0) { perror("new"); exit(1); } memset(D2, '\xff', sizeof(u_int32_t) * NODES * NODES);}extern "C" char *optarg;intmain(int argc, char **argv){ char ch; while ((ch = getopt(argc, argv, "n:p:s:t:x:y:i:o:")) != EOF) { switch (ch) { case 'n': NODES = atoi(optarg); break; case 'p': PAUSE = atof(optarg); break; case 's': MAXSPEED = atof(optarg); break; case 't': MAXTIME = atof(optarg); break; case 'x': MAXX = atof(optarg); break; case 'y': MAXY = atof(optarg); break; default: usage(argv); exit(1); } } if(MAXX == 0.0 || MAXY == 0.0 || NODES == 0 || MAXTIME == 0.0) { usage(argv); exit(1); } fprintf(stdout, "#\n# nodes: %d, pause: %.2f, max speed: %.2f max x = %.2f, max y: %.2f\n#\n", NODES , PAUSE, MAXSPEED, MAXX, MAXY); // The more portable solution for random number generation rng = new RNG; rng->set_seed(RNG::HEURISTIC_SEED_SOURCE); init(); while(TIME <= MAXTIME) { double nexttime = 0.0; u_int32_t i; for(i = 0; i < NODES; i++) { NodeList[i].Update(); } for(i = 0; i < NODES; i++) { NodeList[i].UpdateNeighbors(); } for(i = 0; i < NODES; i++) { Node *n = &NodeList[i]; if(n->time_transition > 0.0) { if(nexttime == 0.0) nexttime = n->time_transition; else nexttime = min(nexttime, n->time_transition); } if(n->time_arrival > 0.0) { if(nexttime == 0.0) nexttime = n->time_arrival; else nexttime = min(nexttime, n->time_arrival); } } floyd_warshall();#ifdef DEBUG show_routes();#endif show_diffs();#ifdef DEBUG dumpall();#endif assert(nexttime > TIME + ROUND_ERROR); TIME = nexttime; } show_counters(); int of; if ((of = open(".rand_state",O_WRONLY | O_TRUNC | O_CREAT, 0777)) < 0) { fprintf(stderr, "open rand state\n"); exit(-1); } for (unsigned int i = 0; i < sizeof(random_state); i++) random_state[i] = 0xff & (int) (uniform() * 256); if (write(of,random_state, sizeof(random_state)) < 0) { fprintf(stderr, "writing rand state\n"); exit(-1); } close(of);}/* ====================================================================== Node Class Functions ====================================================================== */u_int32_t Node::NodeIndex = 0;Node::Node(){ u_int32_t i; index = NodeIndex++; //if(index == 0) // return; route_changes = 0; link_changes = 0; /* * For the first PAUSE seconds of the simulation, all nodes * are stationary. */ time_arrival = TIME + PAUSE; time_update = TIME; time_transition = 0.0; position.X = position.Y = position.Z = 0.0; destination.X = destination.Y = destination.Z = 0.0; direction.X = direction.Y = direction.Z = 0.0; speed = 0.0; RandomPosition(); fprintf(stdout, NODE_FORMAT3, index, 'X', position.X); fprintf(stdout, NODE_FORMAT3, index, 'Y', position.Y); fprintf(stdout, NODE_FORMAT3, index, 'Z', position.Z); neighbor = new Neighbor[NODES]; if(neighbor == 0) { perror("new"); exit(1); } for(i = 0; i < NODES; i++) { neighbor[i].index = i; neighbor[i].reachable = (index == i) ? 1 : 0; neighbor[i].time_transition = 0.0; }}voidNode::RandomPosition(){ position.X = uniform() * MAXX; position.Y = uniform() * MAXY; position.Z = 0.0;}voidNode::RandomDestination(){ destination.X = uniform() * MAXX; destination.Y = uniform() * MAXY; destination.Z = 0.0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -