📄 god.cc
字号:
return false;}bool God::IsPartition(){ int dtype, i, j, k; int *oif_map; for (dtype = 0; dtype < num_data_types; dtype ++) { for (i = 0; i < num_nodes; i++) { if (SRC_TAB(dtype,i) == NULL) continue; oif_map = SRC_TAB(dtype, i); for (j = 0; j < num_nodes; j++) { for (k = 0; k < num_nodes; k++) { if (oif_map[j*num_nodes + k] != 0) return false; } } } } return true;}void God::ComputeRoute() { if (active == false) { return; } floyd_warshall(); ComputeNextHop(); Rewrite_OIF_Map(); CountConnect(); CountAliveNode(); prev_time = NOW; num_compute++; if (allowTostop == false) return; if ( ExistSink() == true && ExistSource() == true && IsPartition() == true) StopSimulation();}void God::CountNewData(int *attr){ if (dtab.GetHash(attr) == NULL) { num_send[attr[0]]++; dtab.PutInHash(attr); }}void God::IncrRecv(){ num_recv++; // printf("God: num_connect %d, num_alive_node %d at recv pkt %d\n", // num_connect, num_alive_node, num_recv);}void God::StopSimulation() { Tcl& tcl=Tcl::instance(); printf("Network parition !! Exiting... at time %f\n", NOW); tcl.evalf("[Simulator instance] at %lf \"finish\"", (NOW)+0.000001); tcl.evalf("[Simulator instance] at %lf \"[Simulator instance] halt\"", (NOW)+0.000002);}// Modified from setdest.cc -- Chalermek 12/1/99void God::ComputeW(){ int i, j; int *W = min_hops; memset(W, '\xff', sizeof(int) * num_nodes * num_nodes); for(i = 0; i < num_nodes; i++) { W[i*num_nodes + i] = 0; for(j = i+1; j < num_nodes; j++) { W[i*num_nodes + j] = W[j*num_nodes + i] = IsNeighbor(i,j) ? 1 : INFINITY; } }}void God::floyd_warshall(){ assert(0);// int i, j, k;//// ComputeW(); // the connectivity matrix//// for(i = 0; i < num_nodes; i++) {// for(j = 0; j < num_nodes; j++) {// for(k = 0; k < num_nodes; k++) {// MIN_HOPS(j,k) = MIN(MIN_HOPS(j,k), MIN_HOPS(j,i) + MIN_HOPS(i,k));// }// }// }//////#ifdef SANITY_CHECKS//// for(i = 0; i < num_nodes; i++)// for(j = 0; j < num_nodes; j++) {// assert(MIN_HOPS(i,j) == MIN_HOPS(j,i));// assert(MIN_HOPS(i,j) <= INFINITY);// }//#endif}// --------------------------intGod::hops(int i, int j){ return min_hops[i * num_nodes + j];}voidGod::stampPacket(Packet *p){ hdr_cmn *ch = HDR_CMN(p); struct hdr_ip *ih = HDR_IP(p); nsaddr_t src = ih->saddr(); nsaddr_t dst = ih->daddr(); assert(min_hops); if (!packet_info.data_packet(ch->ptype())) return; if (dst > num_nodes || src > num_nodes) return; // broadcast pkt ch->opt_num_forwards() = min_hops[src * num_nodes + dst];}void God::recv(Packet *, Handler *){ abort();}intGod::load_grid(int x, int y, int size){ maxX = x; maxY = y; gridsize_ = size; // how many gridx in X direction gridX = (int)maxX/size; if (gridX * size < maxX) gridX ++; // how many grid in Y direcion gridY = (int)maxY/size; if (gridY * size < maxY) gridY ++; printf("Grid info:%d %d %d (%d %d)\n",maxX,maxY,gridsize_, gridX, gridY); return 0;} // return the grid that I am in// start from left bottom corner, // from left to right, 0, 1, ...intGod::getMyGrid(double x, double y){ int xloc, yloc; if (x > maxX || y >maxY) return(-1); xloc = (int) x/gridsize_; yloc = (int) y/gridsize_; return(yloc*gridX+xloc);}intGod::getMyLeftGrid(double x, double y){ int xloc, yloc; if (x > maxX || y >maxY) return(-1); xloc = (int) x/gridsize_; yloc = (int) y/gridsize_; xloc--; // no left grid if (xloc < 0) return (-2); return(yloc*gridX+xloc);}intGod::getMyRightGrid(double x, double y){ int xloc, yloc; if (x > maxX || y >maxY) return(-1); xloc = (int) x/gridsize_; yloc = (int) y/gridsize_; xloc++; // no left grid if (xloc > gridX) return (-2); return(yloc*gridX+xloc);}intGod::getMyTopGrid(double x, double y){ int xloc, yloc; if (x > maxX || y >maxY) return(-1); xloc = (int) x/gridsize_; yloc = (int) y/gridsize_; yloc++; // no top grid if (yloc > gridY) return (-2); return(yloc*gridX+xloc);}intGod::getMyBottomGrid(double x, double y){ int xloc, yloc; if (x > maxX || y >maxY) return(-1); xloc = (int) x/gridsize_; yloc = (int) y/gridsize_; yloc--; // no top grid if (yloc < 0 ) return (-2); return(yloc*gridX+xloc);}int God::command(int argc, const char* const* argv){ Tcl& tcl = Tcl::instance(); if ((instance_ == 0) || (instance_ != this)) instance_ = this; if (argc == 2) { if(strcmp(argv[1], "update_node_status") == 0) { UpdateNodeStatus(); return TCL_OK; } if(strcmp(argv[1], "compute_route") == 0) { ComputeRoute(); return TCL_OK; } if(strcmp(argv[1], "dump") == 0) { Dump(); return TCL_OK; } if (strcmp(argv[1], "dump_num_send") == 0) { DumpNumSend(); return TCL_OK; } if (strcmp(argv[1], "on") == 0) { active = true; return TCL_OK; } if (strcmp(argv[1], "off") == 0) { active = false; return TCL_OK; } if (strcmp(argv[1], "allow_to_stop") == 0) { allowTostop = true; return TCL_OK; } if (strcmp(argv[1], "not_allow_to_stop") == 0) { allowTostop = false; return TCL_OK; } /* if(strcmp(argv[1], "dump") == 0) { int i, j; for(i = 1; i < num_nodes; i++) { fprintf(stdout, "%2d) ", i); for(j = 1; j < num_nodes; j++) fprintf(stdout, "%2d ", min_hops[i * num_nodes + j]); fprintf(stdout, "\n"); } return TCL_OK; } */ if(strcmp(argv[1], "num_nodes") == 0) { tcl.resultf("%d", nodes()); return TCL_OK; } } else if(argc == 3) { if (strcasecmp(argv[1], "is_source") == 0) { int node_id = atoi(argv[2]); if (node_status[node_id].is_source_ == true) { tcl.result("1"); } else { tcl.result("0"); } return TCL_OK; } if (strcasecmp(argv[1], "is_sink") == 0) { int node_id = atoi(argv[2]); if (node_status[node_id].is_sink_ == true) { tcl.result("1"); } else { tcl.result("0"); } return TCL_OK; } if (strcasecmp(argv[1], "is_on_trees") == 0) { int node_id = atoi(argv[2]); if (node_status[node_id].is_on_trees_ == true) { tcl.result("1"); } else { tcl.result("0"); } return TCL_OK; } if (strcasecmp(argv[1], "num_nodes") == 0) { assert(num_nodes == 0); // index always starts from 0 num_nodes = atoi(argv[2]); assert(num_nodes > 0); printf("num_nodes is set %d\n", num_nodes); min_hops = new int[num_nodes * num_nodes]; mb_node = new MobileNode*[num_nodes]; node_status = new NodeStatus[num_nodes]; next_hop = new int[num_nodes * num_nodes]; bzero((char*) min_hops, sizeof(int) * num_nodes * num_nodes); bzero((char*) mb_node, sizeof(MobileNode*) * num_nodes); bzero((char*) next_hop, sizeof(int) * num_nodes * num_nodes); instance_ = this; return TCL_OK; } if (strcasecmp(argv[1], "num_data_types") == 0) { assert(num_data_types == 0); num_data_types = atoi(argv[2]); assert(num_nodes > 0); assert(num_data_types > 0); source_table = new int*[num_data_types * num_nodes]; sink_table = new int[num_data_types * num_nodes]; num_send = new int[num_data_types]; bzero((char*) source_table, sizeof(int *) * num_data_types * num_nodes); bzero((char*) sink_table, sizeof(int) * num_data_types * num_nodes); bzero((char*) num_send, sizeof(int) * num_data_types); return TCL_OK; } if (strcasecmp(argv[1], "new_node") == 0) { assert(num_nodes > 0); MobileNode *obj = (MobileNode *)TclObject::lookup(argv[2]); assert(obj != 0); assert(obj->address() < num_nodes); mb_node[obj->address()] = obj; return TCL_OK; } /* if (strcasecmp(argv[1], "num_nodes") == 0) { assert(num_nodes == 0); // allow for 0 based to 1 based conversion num_nodes = atoi(argv[2]) + 1; min_hops = new int[num_nodes * num_nodes]; bzero((char*) min_hops, sizeof(int) * num_nodes * num_nodes); instance_ = this; return TCL_OK; } */ } else if (argc == 4) { if (strcasecmp(argv[1], "is_reachable") == 0) { int n1 = atoi(argv[2]); int n2 = atoi(argv[3]); if (IsReachable(n1,n2) == true) { tcl.result("1"); } else { tcl.result("0"); } return TCL_OK; } // We can add source from tcl script or call AddSource directly. if (strcasecmp(argv[1], "add_source") == 0) { int dt = atoi(argv[2]); int srcid = atoi(argv[3]); AddSource(dt, srcid); return TCL_OK; } // We can add sink from tcl script or call AddSink directly. if (strcasecmp(argv[1], "add_sink") == 0) { int dt = atoi(argv[2]); int skid = atoi(argv[3]); AddSink(dt, skid); return TCL_OK; } } else if(argc == 5) { /* load for grid-based adaptive fidelity */ if (strcmp(argv[1], "load_grid") == 0) { if(load_grid(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]))) return TCL_ERROR; return TCL_OK; } if (strcasecmp(argv[1], "set-dist") == 0) { int i = atoi(argv[2]); int j = atoi(argv[3]); int d = atoi(argv[4]); assert(i >= 0 && i < num_nodes); assert(j >= 0 && j < num_nodes); if (active == true) { if (NOW > prev_time) { ComputeRoute(); } } else { min_hops[i*num_nodes+j] = d; min_hops[j*num_nodes+i] = d; } // The scenario file should set the node positions // before calling set-dist !! assert(min_hops[i * num_nodes + j] == d); assert(min_hops[j * num_nodes + i] == d); return TCL_OK; } /* if (strcasecmp(argv[1], "set-dist") == 0) { int i = atoi(argv[2]); int j = atoi(argv[3]); int d = atoi(argv[4]); assert(i >= 0 && i < num_nodes); assert(j >= 0 && j < num_nodes); min_hops[i * num_nodes + j] = d; min_hops[j * num_nodes + i] = d; return TCL_OK; } */ } return BiConnector::command(argc, argv);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -