📄 node.cc
字号:
static char rcsid [] = "$Id: node.cc,v 1.1 2001/08/07 22:04:12 suman Exp suman $";/* * $Log: node.cc,v $ * Revision 1.1 2001/08/07 22:04:12 suman * Initial revision * *//* * File: node.cc * Author: Suman Banerjee <suman@cs.umd.edu> * Date: July 31, 2001 * Terms: GPL * * myns simulator */#include <assert.h>#include <stdlib.h>#include <limits.h>#include <node.h>#include <agent.h>#include <packet.h>#undef LOG_PACKET_SEND#undef LOG_PACKET_RECV/* instant routing is true means that the packets do not see any link * delays on the path, and are routed instantly, hop to hop */bool global_instant_routing = false;extern Node ** ptr_global_node_array;Node::Node (void){ started = false; a = NULL; rt = NULL;}Node::~Node (void) { if (rt != NULL) delete rt; empty_neighbor_list();}/*void Node::InitRT (int num_nodes) { rt.num_nodes = num_nodes; rt.valid = new bool [num_nodes]; rt.next_hop_id = new int [num_nodes]; rt.cost = new double [num_nodes]; for (int i = 0; i < num_nodes; i++) rt.valid[i] = false; return;}*/void Node::start (void){#ifdef LOG_NODE_JUNK printf ("[Node] %d start at %8.5f\n",id, Scheduler::Clock());#endif // LOG_NODE_JUNK started = true; return;}void Node::stop (void){#ifdef LOG_NODE_JUNK printf ("[Node] %d stop at %8.5f\n",id, Scheduler::Clock());#endif // LOG_NODE_JUNK if (a != NULL) { if (a->started == true) a->stop(); } started = false; return;}void Node::display_packet (Packet *p, char *prefix){ printf ("[Pkt] %s node %d at %8.5f : Id %d : Src %d -> Dst %d\n", prefix, id, Scheduler::Clock(), p->id, p->src, p->dst); return;}void Node::send_pkt (Packet *p, int dst){ assert (started); p->process_count = 0; p->src = id; p->dst = dst; //p->id = pkt_id ++; p->send_time = Scheduler::Clock();#ifdef LOG_PACKET_SEND display_packet(p,"send");#endif // LOG_PACKET_SEND specific_rx_pkt_handler(p,true); if (id != dst) { /* i.e. this is not a loopback packet */ bool discard = forward_pkt(p); if (discard) delete p; } else { /* This is a loopback */ tx_pkt(p,this,0.0,global_instant_routing); } return;}/* Sends the packet to the next hop in the path *//* When no-delay is true, the packet is not held by the scheduler */void Node::tx_pkt (Packet *p, Node *next_hop, double link_delay, bool instant_route_pkt) { specific_tx_pkt(p,next_hop->id); if (instant_route_pkt) next_hop->rx_pkt_handler(p); else { NodeEvent *rcv_ev = new NodeEvent(RECV_PACKET,p); EventInfo * ev = new EventInfo (EVENT_NODE,(void*)next_hop,(void*)rcv_ev); p->process_count ++; Scheduler::AddRelativeEvent(link_delay,ev); } return;}/* Forward the packet to the next hop in the path towards the destination *//* This node should not be the destination */bool Node::forward_pkt (Packet *p) { int dst = p->dst; assert (id != dst); bool discard = false; int this_next_hop_id = rt->get_next_hop_id(dst); /* Note, index and id are same */ if (this_next_hop_id >= 0) { /* has a next hop path */ Node *next_hop = ptr_global_node_array[this_next_hop_id]; if (next_hop->started == true) { assert (neighbor_list.Locate(next_hop->id) != NULL); tx_pkt(p,next_hop,CONSTANT_LINK_DELAY,global_instant_routing); } else discard = true; } else { /* No next hop known, discard the packet */ discard = true; } return discard;}void Node::rx_pkt_handler (Packet *p){ if (! started) assert (0); p->process_count --; specific_rx_pkt_handler(p,false); if (p->dst == id) { /* Destination has been reached */#ifdef LOG_PACKET_RECV display_packet(p,"recv-local");#endif // LOG_PACKET_RECV /* Do app specific stuff here */ if (a != NULL) if ( (a->id == p->dst_agent) && (a->started == true) ) a->generic_rx_pkt_handler(p); if (! p->process_count) delete p; } else {#ifdef LOG_PACKET_RECV display_packet(p,"recv-non-local");#endif // LOG_PACKET_RECV bool discard = forward_pkt(p); if (discard && (! p->process_count)) delete p; } return;}void Node::add_neighbor (Node *n, double cost) { NeighborInfo *ni = new NeighborInfo(n,cost); neighbor_list.Add(ni,n->id); return;}void Node::empty_neighbor_list (void) { for (void *pos = neighbor_list.GetHeadPosition(); pos != NULL; neighbor_list.GetNext(pos) ) delete neighbor_list.GetAt(pos); neighbor_list.RemoveAll(); return;}void Node::EventHandler (NodeEvent *ne) { switch (ne->t) { case NODE_START : start(); /* { testing AppPacket *p = new AppPacket(); send_pkt(p,8); } */ break; case NODE_STOP : stop(); break; case RECV_PACKET : rx_pkt_handler(ne->p); break; default : printf ("[Err] Illegal node event\n"); exit(-1); } delete ne; return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -