?? god.cc
字號:
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- * * Copyright (c) 1997 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the Computer Systems * Engineering Group at Lawrence Berkeley Laboratory. * 4. Neither the name of the University nor of the Laboratory may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Header: /cvsroot/nsnam/ns-2/mobile/god.cc,v 1.20 2006/12/27 14:57:23 tom_henderson Exp $ *//* Ported from CMU/Monarch's code, nov'98 -Padma.*//* * god.cc * * General Operations Director * * perform operations requiring omnipotence in the simulation * * NOTE: Tcl node indexs are 0 based, NS C++ node IP addresses (and the * node->index() are 1 based. * */#include <object.h>#include <packet.h>#include <ip.h>#include <god.h>//#include <sys/param.h> /* for MIN/MAX */#include "diffusion/hash_table.h"#include "mobilenode.h"God* God::instance_;static class GodClass : public TclClass {public: GodClass() : TclClass("God") {} TclObject* create(int, const char*const*) { return (new God); }} class_God;God::God(){ min_hops = 0; num_nodes = 0; data_pkt_size = 64; mb_node = 0; next_hop = 0; prev_time = -1.0; num_alive_node = 0; num_connect = 0; num_recv = 0; num_compute = 0; num_data_types = 0; source_table = 0; sink_table = 0; num_send = 0; active = false; allowTostop = false;}// Added by Chalermek 12/1/99int God::NextHop(int from, int to){ if (active == false) { perror("God is off.\n"); exit(-1); } if (from >= num_nodes) { perror("index from higher than the maximum number of nodes.\n"); return -1; } if (to >= num_nodes) { perror("index to higher than the maximum number of nodes.\n"); return -1; } return NEXT_HOP(from,to);}void God::ComputeNextHop(){ if (active == false) { return; } int from, to, neighbor; for (from=0; from<num_nodes; from++) { for (to=0; to<num_nodes; to++) { NEXT_HOP(from,to) = UNREACHABLE; if (from==to) { NEXT_HOP(from,to) = from; // next hop is itself. } if (MIN_HOPS(from, to) == UNREACHABLE) { continue; } for (neighbor=0; neighbor<num_nodes; neighbor++){ if ( MIN_HOPS(from, neighbor) != 1) { continue; } if ( MIN_HOPS(from, to) == (MIN_HOPS(neighbor,to) +1) ) { NEXT_HOP(from, to) = neighbor; break; } } } }}void God::UpdateNodeStatus(){ int i,j; int count, cur, sk, srcid, dt; for (i=0; i<num_data_types; i++) { for (j=0; j<num_nodes; j++) { if (SRC_TAB(i,j) != NULL) { node_status[j].is_source_ = true; } } } for (i=0; i<num_data_types; i++) { for (j=0; j<num_nodes; j++) { if (SK_TAB(i,j) > 0) { node_status[j].is_sink_ = true; } } } for (dt=0; dt < num_data_types; dt++) { for (srcid=0; srcid < num_nodes; srcid++) { if (SRC_TAB(dt,srcid) == NULL) continue; for (sk = 0; sk < num_nodes; sk++) { if (SK_TAB(dt, sk) == 0) continue; cur = srcid; count = 0; node_status[cur].is_on_trees_ = true; while (cur != sk) { if (NextHop(cur, sk) == UNREACHABLE) break; assert(NextHop(cur,sk) >= 0 && NextHop(cur, sk) < num_nodes); cur = NextHop(cur, sk); node_status[cur].is_on_trees_ = true; count ++; assert(count < num_nodes); } } } } Dump(); DumpNodeStatus();}void God::DumpNodeStatus(){ for (int i=0; i < num_nodes; i++) { printf("Node %d status (sink %d, source %d, on_tree %d)\n", i, node_status[i].is_sink_, node_status[i].is_source_, node_status[i].is_on_trees_); }}void God::DumpNumSend(){#ifdef DEBUG_OUTPUT for (int i=0; i < num_data_types; i++) { fprintf(stdout, "God: data type %d distinct events %d\n", i, num_send[i]); }#endif}void God::Dump(){ int i, j, k, l; // Dump min_hops array fprintf(stdout,"Dump min_hops\n"); for(i = 0; i < num_nodes; i++) { fprintf(stdout, "%2d) ", i); for(j = 0; j < num_nodes; j++) fprintf(stdout, "%2d ", min_hops[i * num_nodes + j]); fprintf(stdout, "\n"); } // How many times the god compute routes ? fprintf(stdout, "God computes routes %d times.\n", num_compute); // The following information can be found only when god is active. if (active == false) { return; } // Dump next_hop array fprintf(stdout, "Dump next_hop\n"); for (i = 0; i < num_nodes; i++) { for (j = 0; j < num_nodes; j++) { fprintf(stdout,"NextHop(%d,%d):%d\n",i,j,NEXT_HOP(i,j)); } } // What is inside SRC_TAB ? fprintf(stdout, "Dump SRC_TAB\n"); for (i=0; i<num_data_types; i++) { fprintf(stdout,"%2d) ",i); for (j=0; j<num_nodes; j++) { fprintf(stdout,"%2d ", SRC_TAB(i,j) ? 1:0); } fprintf(stdout,"\n"); } // What is inside OIF_MAP ? int *oif_map; fprintf(stdout, "Dump OIF_MAP\n"); for (i=0; i<num_data_types; i++) { for (j=0; j<num_nodes; j++) { if (SRC_TAB(i,j)!=NULL) { oif_map = SRC_TAB(i,j); fprintf(stdout,"(%2d,%2d)\n",i,j); for (k=0; k<num_nodes; k++) { for (l=0; l<num_nodes; l++) { fprintf(stdout,"%2d ", oif_map[k*num_nodes +l]); } fprintf(stdout,"\n"); } } } } // What is inside SK_TAB ? fprintf(stdout, "Dump SK_TAB\n"); for (i=0; i<num_data_types; i++) { fprintf(stdout,"%2d) ",i); for (j=0; j<num_nodes; j++) { fprintf(stdout,"%2d ", SK_TAB(i,j)); } fprintf(stdout,"\n"); }}void God::AddSink(int dt, int skid){ if (active == false) { return; } assert(num_data_types > 0); assert(num_nodes > 0); assert(dt >= 0 && dt < num_data_types); assert(skid >= 0 && skid < num_nodes); if (SK_TAB(dt,skid) == 1) return; SK_TAB(dt,skid) = 1; Fill_for_Source(dt, skid);}void God::AddSource(int dt, int srcid){ if (active == false) { return; } assert(num_data_types > 0); assert(num_nodes > 0); assert(dt >= 0 && dt < num_data_types); assert(srcid >= 0 && srcid < num_nodes); if (SRC_TAB(dt,srcid) != 0) return; SRC_TAB(dt,srcid) = new int[num_nodes * num_nodes]; bzero((char*) SRC_TAB(dt, srcid), sizeof(int) * num_nodes * num_nodes); Fill_for_Sink(dt, srcid); // Dump();}void God::Fill_for_Sink(int dt, int srcid){ int sk, cur, count; int *oif_map = SRC_TAB(dt, srcid); assert(oif_map != NULL); for (sk = 0; sk < num_nodes; sk++) { if (SK_TAB(dt, sk) == 0) continue; cur = srcid; count = 0; while (cur != sk) { if (NextHop(cur, sk) == UNREACHABLE) break; assert(NextHop(cur,sk) >= 0 && NextHop(cur, sk) < num_nodes); oif_map[cur*num_nodes + NextHop(cur, sk)] = 1; cur = NextHop(cur, sk); count ++; assert(count < num_nodes); } }}void God::Fill_for_Source(int dt, int skid){ int src, cur, count; int *oif_map; for (src = 0; src < num_nodes; src++) { if (SRC_TAB(dt, src) == 0) continue; oif_map = SRC_TAB(dt, src); cur = src; count = 0; while (cur != skid) { if (NextHop(cur, skid) == UNREACHABLE) break; assert(NextHop(cur,skid) >= 0 && NextHop(cur, skid) < num_nodes); oif_map[cur*num_nodes + NextHop(cur, skid)] = 1; cur = NextHop(cur, skid); count ++; assert(count < num_nodes); } }}void God::Rewrite_OIF_Map(){ for (int dt = 0; dt < num_data_types; dt++) { for (int src = 0; src < num_nodes; src++) { if (SRC_TAB(dt, src) == NULL) continue; memset(SRC_TAB(dt,src),'\x00', sizeof(int) * num_nodes * num_nodes); Fill_for_Sink(dt, src); } }}int *God::NextOIFs(int dt, int srcid, int curid, int *ret_num_oif) { if (active == false) { perror("God is inactive.\n"); exit(-1); } int *oif_map = SRC_TAB(dt, srcid); int count=0; int i; for (i=0; i<num_nodes; i++) { if (oif_map[curid*num_nodes +i] == 1) count++; } *ret_num_oif = count; if (count == 0) return NULL; int *next_oifs = new int[count]; int j=0; for (i=0; i<num_nodes; i++) { if (oif_map[curid*num_nodes +i] == 1) { next_oifs[j] = i; j++; } } return next_oifs;}bool God::IsReachable(int i, int j){// if (MIN_HOPS(i,j) < UNREACHABLE && MIN_HOPS(i,j) >= 0) if (NextHop(i,j) != UNREACHABLE) return true; else return false;}bool God::IsNeighbor(int i, int j){ assert(i<num_nodes && j<num_nodes); //printf("i=%d, j=%d\n", i,j); if (mb_node[i]->energy_model()->node_on() == false || mb_node[j]->energy_model()->node_on() == false || mb_node[i]->energy_model()->energy() <= 0.0 || mb_node[j]->energy_model()->energy() <= 0.0 ) { return false; } vector_ns a(mb_node[i]->X(), mb_node[i]->Y(), mb_node[i]->Z()); vector_ns b(mb_node[j]->X(), mb_node[j]->Y(), mb_node[j]->Z()); vector_ns d = a - b; if (d.length() < RANGE) return true; else return false; }void God::CountConnect(){ int i,j; num_connect = 0; for (i=0; i<num_nodes; i++) { for (j=i+1; j<num_nodes; j++) { if (MIN_HOPS(i,j) != UNREACHABLE) { num_connect++; } } }}void God::CountAliveNode(){ int i; num_alive_node = 0; for (i=0; i<num_nodes; i++) { if (mb_node[i]->energy_model()->energy() > 0.0) { num_alive_node++; } }}bool God::ExistSource(){ int dtype, i; for (dtype = 0; dtype < num_data_types; dtype++) { for (i=0; i<num_nodes; i++) { if (SRC_TAB(dtype, i) != 0) return true; } } return false;}bool God::ExistSink(){ int dtype, i; for (dtype = 0; dtype < num_data_types; dtype++) { for (i=0; i<num_nodes; i++) { if (SK_TAB(dtype, i) != 0) return true; } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -