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

calcdest.cc File Reference

#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>
#include "../../../tools/rng.h"
#include "setdest.h"

Include dependency graph for calcdest.cc:

Include dependency graph

Go to the source code of this file.

Defines

#define SANITY_CHECKS
#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-9
#define M   2147483647L
#define INVERSE_M   ((double)4.656612875e-10)

Functions

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)
void OpenAndReadHeader (char *in_filename, char *out_filename)
void ReadInMovementPattern ()
int main (int argc, char **argv)

Variables

int count = 0
double RANGE = 250.0
double TIME = 0.0
double MAXTIME = 0.0
double 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
NodeNodeList = 0
u_int32_tD1 = 0
u_int32_tD2 = 0
FILE * in_file
FILE * out_file
char random_state [32]
RNGrng
char * optarg


Define Documentation

#define GOD_FORMAT   "$ns_ at %.12f \"$god_ set-dist %d %d %d\"\n"
 

Definition at line 22 of file calcdest.cc.

Referenced by show_diffs().

#define GOD_FORMAT2   "$god_ set-dist %d %d %d\n"
 

Definition at line 23 of file calcdest.cc.

Referenced by show_diffs().

#define INFINITY   0x00ffffff
 

Definition at line 29 of file calcdest.cc.

Referenced by RouteLogic::alloc(), JoBS::assignRateDropsADC(), JoBS::command(), RouteLogic::compute_routes(), God::ComputeW(), ComputeW(), JoBS::deque(), SatRouteObject::dump(), God::floyd_warshall(), floyd_warshall(), RouteLogic::hier_alloc(), RouteLogic::hier_compute_routes(), RouteLogic::hier_print_hadj(), RouteLogic::hier_reset(), JoBS::JoBS(), JoBS::minRatesNeeded(), SatRouteObject::node_compute_routes(), JoBS::pickDroppedRLC(), RouteLogic::reset(), and show_diffs().

#define INVERSE_M   ((double)4.656612875e-10)
 

Definition at line 80 of file calcdest.cc.

#define M   2147483647L
 

Definition at line 79 of file calcdest.cc.

Referenced by Friis(), and Propagation::Friis().

#define max x,
 )     ((x) > (y) ? (x) : (y))
 

Definition at line 31 of file calcdest.cc.

#define min x,
 )     ((x) < (y) ? (x) : (y))
 

Definition at line 30 of file calcdest.cc.

#define NODE_FORMAT   "$ns_ at %.12f \"$node_(%d) setdest %.12f %.12f %.12f\"\n"
 

Definition at line 24 of file calcdest.cc.

#define NODE_FORMAT2   "$node_(%d) setdest %.12f %.12f %.12f\n"
 

Definition at line 25 of file calcdest.cc.

#define NODE_FORMAT3   "$node_(%d) set %c_ %.12f\n"
 

Definition at line 26 of file calcdest.cc.

#define ROUND_ERROR   1e-9
 

Definition at line 32 of file calcdest.cc.

Referenced by main(), and Node::UpdateNeighbors().

#define SANITY_CHECKS
 

Definition at line 18 of file calcdest.cc.


Function Documentation

void ComputeW void   ) 
 

Definition at line 661 of file calcdest.cc.

References D2, INFINITY, Node::neighbor, NodeList, NODES, Neighbor::reachable, and u_int32_t.

Referenced by floyd_warshall().

00662 {
00663         u_int32_t i, j;
00664         u_int32_t *W = D2;
00665 
00666         memset(W, '\xff', sizeof(int) * NODES * NODES);
00667 
00668         for(i = 1; i < NODES; i++) {
00669                 for(j = i; j < NODES; j++) {
00670                         Neighbor *m = &NodeList[i].neighbor[j];
00671                         if(i == j)
00672                                 W[i*NODES + j] = W[j*NODES + i] = 0;
00673                         else
00674                                 W[i*NODES + j] = W[j*NODES + i] = m->reachable ? 1 : INFINITY;  
00675                 }
00676         }
00677 }

void dumpall void   ) 
 

Definition at line 649 of file calcdest.cc.

References Node::Dump(), NodeList, NODES, TIME, and u_int32_t.

Referenced by main().

00650 {
00651         u_int32_t i;
00652 
00653         fprintf(stdout, "\nTime: %.2f\n", TIME);
00654 
00655         for(i = 1; i < NODES; i++) {
00656                 NodeList[i].Dump();
00657         }
00658 }

Here is the call graph for this function:

void floyd_warshall void   ) 
 

Definition at line 680 of file calcdest.cc.

References ComputeW(), D2, INFINITY, min, NODES, and u_int32_t.

Referenced by main().

00681 {
00682         u_int32_t i, j, k;
00683 
00684         ComputeW();     // the connectivity matrix
00685 
00686         for(i = 1; i < NODES; i++) {
00687                 for(j = 1; j < NODES; j++) {
00688                         for(k = 1; k < NODES; k++) {
00689                                 D2[j*NODES + k] = min(D2[j*NODES + k], D2[j*NODES + i] + D2[i*NODES + k]);
00690                         }
00691                 }
00692         }
00693 
00694 #ifdef SANITY_CHECKS
00695         for(i = 1; i < NODES; i++)
00696                 for(j = 1; j < NODES; j++) {
00697                         assert(D2[i*NODES + j] == D2[j*NODES + i]);
00698                         assert(D2[i*NODES + j] <= INFINITY);
00699                 }
00700 #endif
00701 }

Here is the call graph for this function:

void init void   ) 
 

Definition at line 112 of file calcdest.cc.

References D1, D2, NodeList, NODES, and u_int32_t.

Referenced by PolicyClassifier::addPolicyEntry(), main(), and Tcl_AppInit().

00113 {
00114         /*
00115          * Initialized the Random Number Generation
00116          */
00117 
00118         /*
00119          * Allocate memory for globals
00120          */
00121         NodeList = new Node[NODES];
00122         if(NodeList == 0) {
00123                 perror("new");
00124                 exit(1);
00125         }
00126 
00127         D1 = new u_int32_t[NODES * NODES];
00128         if(D1 == 0) {
00129                 perror("new");
00130                 exit(1);
00131         }
00132         memset(D1, '\xff', sizeof(u_int32_t) * NODES * NODES);
00133 
00134         D2 = new u_int32_t[NODES * NODES];
00135         if(D2 == 0) {
00136                 perror("new");
00137                 exit(1);
00138         }
00139         memset(D2, '\xff', sizeof(u_int32_t) * NODES * NODES);
00140 }

int main int  argc,
char **  argv
 

Definition at line 236 of file calcdest.cc.

References dumpall(), floyd_warshall(), RNG::HEURISTIC_SEED_SOURCE, init(), MAXTIME, min, NodeList, NODES, OpenAndReadHeader(), optarg, random_state, ReadInMovementPattern(), rng, ROUND_ERROR, RNG::set_seed(), show_counters(), show_diffs(), show_routes(), TIME, Node::time_arrival, Node::time_transition, u_int32_t, uniform(), Node::Update(), Node::UpdateNeighbors(), and usage().

00237 {
00238         char ch;
00239         char *in_filename = NULL;
00240         char *out_filename = NULL;
00241 
00242         while ((ch = getopt(argc, argv, "n:t:i:o:")) != EOF) {       
00243 
00244                 switch (ch) { 
00245 
00246                 case 'n':
00247                         NODES = atoi(optarg) + 1;
00248                         break;
00249 
00250                 case 't':
00251                         MAXTIME = atof(optarg);
00252                         break;
00253         
00254                 case 'i':
00255                         in_filename = optarg;
00256                         break;
00257 
00258                 case 'o':
00259                         out_filename = optarg;
00260                         break;
00261 
00262                 default:
00263                         usage(argv);
00264                         exit(1);
00265                 }
00266         }
00267 
00268         if (NULL == in_filename || NULL == out_filename) {
00269                 usage(argv);
00270                 exit(1);
00271         }
00272 
00273         OpenAndReadHeader(in_filename, out_filename);
00274 
00275         if(NODES == 0 || MAXTIME == 0.0) {
00276                 usage(argv);
00277                 exit(1);
00278         }
00279         // A more portable solution for random number generation
00280         rng = new RNG;
00281         rng->set_seed(RNG::HEURISTIC_SEED_SOURCE); 
00282         init();
00283 
00284         ReadInMovementPattern();
00285 
00286         while(TIME <= MAXTIME) {
00287                 double nexttime = 0.0;
00288                 u_int32_t i;
00289 
00290                 for(i = 1; i < NODES; i++) {
00291                         NodeList[i].Update();
00292                 }
00293 
00294                 for(i = 1; i < NODES; i++) {
00295                         NodeList[i].UpdateNeighbors();
00296                 }
00297 
00298                 for(i = 1; i < NODES; i++) {
00299                         Node *n = &NodeList[i];
00300         
00301                         if(n->time_transition > 0.0) {
00302                                 if(nexttime == 0.0)
00303                                         nexttime = n->time_transition;
00304                                 else
00305                                         nexttime = min(nexttime, n->time_transition);
00306                         }
00307 
00308                         if(n->time_arrival > 0.0) {
00309                                 if(nexttime == 0.0)
00310                                         nexttime = n->time_arrival;
00311                                 else
00312                                         nexttime = min(nexttime, n->time_arrival);
00313                         }
00314                 }
00315 
00316                 floyd_warshall();
00317 
00318 #ifdef DEBUG
00319                 show_routes();
00320 #endif
00321 
00322                 show_diffs();
00323 
00324 #ifdef DEBUG
00325                 dumpall();
00326 #endif
00327 
00328                 if (nexttime <= TIME + ROUND_ERROR) 
00329                   TIME = MAXTIME + 1;   /* we're done */
00330                 else 
00331                   TIME = nexttime;
00332 #ifdef OLD
00333                 assert(nexttime > TIME + ROUND_ERROR);
00334                 TIME = nexttime;
00335 #endif
00336 
00337         }
00338 
00339         show_counters();
00340 
00341         int of;
00342         if ((of = open(".rand_state",O_WRONLY | O_TRUNC | O_CREAT, 0777)) < 0)
00343           fprintf(stderr,"open rand state");
00344         for (unsigned int i = 0; i < sizeof(random_state); i++)
00345           random_state[i] = 0xff & (int) (uniform() * 256);
00346         if (write(of,random_state, sizeof(random_state)) < 0)
00347           fprintf(stderr,"writing rand state");
00348         close(of);
00349 }

Here is the call graph for this function:

void OpenAndReadHeader char *  in_filename,
char *  out_filename
 

Definition at line 143 of file calcdest.cc.

References in_file, MAXTIME, NODES, out_file, and RANGE.

Referenced by main().

00146 {
00147   char buf[256];
00148   
00149   in_file = fopen(in_filename,"r");
00150   out_file = fopen(out_filename,"w");
00151   
00152   if (NULL == in_file) 
00153         fprintf(stderr, "*** can't open inputfile %s",in_filename);
00154   if (NULL == out_file) 
00155         fprintf(stderr,"can't open outputfile %s",out_filename);
00156   
00157   while (!feof(in_file)) {
00158 
00159     *buf = fgetc(in_file);
00160     ungetc(*buf,in_file);
00161     if (*buf != '#') break;
00162 
00163     fgets(buf, sizeof(buf), in_file);
00164 
00165     /* check to see if we need data from the line */
00166     sscanf(buf, "# nodes: %d, max time: %lf", &NODES, &MAXTIME);
00167     sscanf(buf, "# nominal range: %lf", &RANGE);
00168     
00169     fprintf(out_file, "%s", buf);
00170   }
00171   NODES += 1;                   // correct for 1-based indexs
00172   fflush(out_file);
00173 }

void ReadInMovementPattern void   ) 
 

Definition at line 176 of file calcdest.cc.

References in_file, LIST_INSERT_HEAD, MAXTIME, NodeList, NODES, out_file, Node::position, vector::X, vector::Y, and vector::Z.

Referenced by main().

00177 {
00178   char buf[256];
00179   u_int n;
00180   double x,y,z,t,s;
00181   struct setdest *setdest;
00182 
00183   while (!feof(in_file)) {
00184 
00185     fgets(buf, sizeof(buf), in_file);
00186     fprintf(out_file, "%s", buf);
00187     if (*buf == '#') continue;
00188     if (*buf == '\n') continue;
00189 
00190     /* check to see if we need data from the line */
00191     if (2 == sscanf(buf,"$node_(%d) set Z_ %lf", &n, &z)) 
00192       {
00193         assert(n < NODES);
00194         NodeList[n].position.Z = z;
00195       }
00196     else if (2 == sscanf(buf,"$node_(%d) set X_ %lf", &n, &x)) 
00197       {
00198         assert(n < NODES);
00199         NodeList[n].position.X = x;
00200       }
00201     else if (2 == sscanf(buf,"$node_(%d) set Y_ %lf", &n, &y)) 
00202       {
00203         assert(n < NODES);
00204         NodeList[n].position.Y = y;
00205       }
00206     else if (5 == sscanf(buf,"$ns_ at %lf \"$node_(%d) setdest %lf %lf %lf\"", 
00207                         &t, &n, &x, &y, &s)) 
00208       {
00209         assert(n < NODES);
00210         assert(t <= MAXTIME);
00211         setdest = (struct setdest *)malloc(sizeof(*setdest));
00212         assert(setdest);
00213         setdest->X = x; setdest->Y = y; setdest->Z = 0;
00214         setdest->time = t;
00215         setdest->speed = s;
00216         if (NodeList[n].traj.lh_first 
00217             && t > NodeList[n].traj.lh_first->time)
00218           {
00219             printf("setdest's must be in anti-chronological order in input file!\n");
00220             printf("failed on node %d\n",n);
00221             exit(-1);
00222           }
00223         LIST_INSERT_HEAD(&NodeList[n].traj,setdest,traj);
00224       }
00225     else 
00226       {
00227         printf("unparsable line: '%s'", buf);
00228         continue;
00229       }
00230   }  
00231   fflush(out_file);  
00232 }

void show_counters void   ) 
 

Definition at line 765 of file calcdest.cc.

References DestUnreachableCount, LinkChangeCount, NodeList, NODES, out_file, RouteChangeCount, and u_int32_t.

Referenced by main().

00766 {
00767         u_int32_t i;
00768 
00769         fprintf(out_file, "#\n# Destination Unreachables: %d\n#\n",
00770                 DestUnreachableCount);
00771         fprintf(out_file, "# Route Changes: %d\n#\n", RouteChangeCount);
00772         fprintf(out_file, "# Link Changes: %d\n#\n", LinkChangeCount);
00773         fprintf(out_file, "# Node | Route Changes | Link Changes\n");
00774         for(i = 1; i < NODES; i++)
00775                 fprintf(out_file, "# %4d |          %4d |         %4d\n",
00776                         i, NodeList[i].route_changes,
00777                         NodeList[i].link_changes);
00778         fprintf(out_file, "#\n");
00779 }

void show_diffs void   ) 
 

Definition at line 708 of file calcdest.cc.

References D1, D2, DestUnreachableCount, GOD_FORMAT, GOD_FORMAT2, INFINITY, NodeList, NODES, out_file, Node::route_changes, RouteChangeCount, TIME, and u_int32_t.

Referenced by main().

00709 {
00710         u_int32_t i, j;
00711 
00712         for(i = 1; i < NODES; i++) {
00713                 for(j = i + 1; j < NODES; j++) {
00714                         if(D1[i*NODES + j] != D2[i*NODES + j]) {
00715 
00716                                 if(D2[i*NODES + j] == INFINITY)
00717                                         DestUnreachableCount++;
00718 
00719                                 if(TIME > 0.0) {
00720                                         RouteChangeCount++;
00721                                         NodeList[i].route_changes++;
00722                                         NodeList[j].route_changes++;
00723                                 }
00724 
00725                                 if(TIME == 0.0) {
00726                                         fprintf(out_file, GOD_FORMAT2,
00727                                                 i, j, D2[i*NODES + j]);
00728 #ifdef SHOW_SYMMETRIC_PAIRS
00729                                         fprintf(out_file, GOD_FORMAT2,
00730                                                 j, i, D2[j*NODES + i]);
00731 #endif
00732                                 }
00733                                 else {
00734                                         fprintf(out_file, GOD_FORMAT, 
00735                                                 TIME, i, j, D2[i*NODES + j]);
00736 #ifdef SHOW_SYMMETRIC_PAIRS
00737                                         fprintf(out_file, GOD_FORMAT, 
00738                                                 TIME, j, i, D2[j*NODES + i]);
00739 #endif
00740                                 }
00741                         }
00742                 }
00743         }
00744 
00745         memcpy(D1, D2, sizeof(int) * NODES * NODES);
00746 }

void show_routes void   ) 
 

Definition at line 750 of file calcdest.cc.

References D2, NODES, TIME, and u_int32_t.

Referenced by main().

00751 {
00752         u_int32_t i, j;
00753 
00754         fprintf(stdout, "#\n# TIME: %.12f\n#\n", TIME);
00755         for(i = 1; i < NODES; i++) {
00756                 fprintf(stdout, "# %2d) ", i);
00757                 for(j = 1; j < NODES; j++)
00758                         fprintf(stdout, "%3d ", D2[i*NODES + j] & 0xff);
00759                 fprintf(stdout, "\n");
00760         }
00761         fprintf(stdout, "#\n");
00762 }

double uniform void   ) 
 

Definition at line 86 of file calcdest.cc.

References count, rng, and RNG::uniform_double().

Referenced by main(), Node::RandomDestination(), Node::RandomPosition(), and Node::RandomSpeed().

00087 {
00088         count++;
00089         return rng->uniform_double();
00090 }

Here is the call graph for this function:

void usage char **   ) 
 

Definition at line 97 of file calcdest.cc.

00098 {
00099         fprintf(stderr,
00100                 "\nusage: %s\t[-n <nodes>] \n",
00101                 argv[0]);
00102         fprintf(stderr,
00103                 "\t\t[-t <simulation time>]\n");
00104         fprintf(stderr,
00105                 "\t\t-i <input_file> -o <output file>\n");
00106         fprintf(stderr,
00107 "\t\t #nodes, max time, and range read from scenario file if possible\n\n");
00108         
00109 }


Variable Documentation

int count = 0 [static]
 

Definition at line 34 of file calcdest.cc.

Referenced by uniform().

u_int32_t* D1 = 0
 

Definition at line 69 of file calcdest.cc.

Referenced by init(), and show_diffs().

u_int32_t* D2 = 0
 

Definition at line 70 of file calcdest.cc.

Referenced by ComputeW(), floyd_warshall(), init(), show_diffs(), and show_routes().

u_int32_t DestUnreachableCount = 0
 

Definition at line 66 of file calcdest.cc.

Referenced by show_counters(), and show_diffs().

FILE* in_file
 

Definition at line 72 of file calcdest.cc.

Referenced by main(), OpenAndReadHeader(), ReadEntry(), ReadEntryV1(), ReadHeader(), and ReadInMovementPattern().

u_int32_t LinkChangeCount = 0
 

Definition at line 65 of file calcdest.cc.

Referenced by show_counters(), and Node::UpdateNeighbors().

double MAXSPEED = 0.0
 

Definition at line 61 of file calcdest.cc.

Referenced by main(), and Node::RandomSpeed().

double MAXTIME = 0.0
 

Definition at line 57 of file calcdest.cc.

Referenced by main(), OpenAndReadHeader(), ReadInMovementPattern(), and Node::Update().

double MAXX = 0.0
 

Definition at line 59 of file calcdest.cc.

Referenced by main(), Node::RandomDestination(), and Node::RandomPosition().

double MAXY = 0.0
 

Definition at line 60 of file calcdest.cc.

Referenced by main(), Node::RandomDestination(), and Node::RandomPosition().

Node* NodeList = 0
 

Definition at line 68 of file calcdest.cc.

Referenced by ComputeW(), dumpall(), init(), main(), ReadInMovementPattern(), show_counters(), show_diffs(), and Node::UpdateNeighbors().

u_int32_t NODES = 0
 

Definition at line 63 of file calcdest.cc.

Referenced by ComputeW(), Node::Dump(), dumpall(), floyd_warshall(), init(), main(), OpenAndReadHeader(), ReadInMovementPattern(), show_counters(), show_diffs(), show_routes(), and Node::UpdateNeighbors().

char* optarg
 

Definition at line 234 of file calcdest.cc.

Referenced by DiffusionCoreAgent::DiffusionCoreAgent(), main(), and DiffApp::parseCommandLine().

FILE* out_file
 

Definition at line 73 of file calcdest.cc.

Referenced by OpenAndReadHeader(), PrintEntry_Text(), PrintHeader(), ReadInMovementPattern(), show_counters(), and show_diffs().

double PAUSE = 0.0
 

Definition at line 62 of file calcdest.cc.

Referenced by main().

char random_state[32]
 

Definition at line 82 of file calcdest.cc.

Referenced by main().

double RANGE = 250.0
 

Definition at line 55 of file calcdest.cc.

RNG* rng
 

Definition at line 83 of file calcdest.cc.

Referenced by main(), and uniform().

u_int32_t RouteChangeCount = 0
 

Definition at line 64 of file calcdest.cc.

Referenced by show_counters(), and show_diffs().

double TIME = 0.0
 

Definition at line 56 of file calcdest.cc.

Referenced by dumpall(), main(), show_diffs(), show_routes(), Node::Update(), and Node::UpdateNeighbors().


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