Alex Bikfalvi
SimStream Documentation
Node.cpp
00001 #include "Headers.h" 00002 #include "Node.h" 00003 00004 CNode::CNode( 00005 __uint32 id, 00006 CSimHandler* sim, 00007 CAddress address, 00008 __uint32 numLinks, 00009 CRoute* route, 00010 CInfo* info 00011 ) : CObject(sim) 00012 { 00013 // Global 00014 this->id = id; 00015 this->address = address; 00016 this->info = info; 00017 this->route = route; 00018 00019 // Links 00020 this->numLinks = numLinks; 00021 this->currentLink = 0; 00022 00023 // Links 00024 this->links = new CLink*[this->numLinks]; 00025 this->linkEntries = new __uint32[this->numLinks]; 00026 00027 // Base layer delegate and event 00028 this->delegateSend = new Delegate2<CNode, void, __uint32, CPacket*>(this, &CNode::Send); 00029 this->eventRecv = new Event2<void, __uint32, CPacket*>(); 00030 00031 // Initialize layers 00032 this->layerIp = new CLayerIp(this->sim); 00033 this->layerIpLocal = new CLayerIpLocal(this->sim, this->address, this->route); 00034 this->layerIpMcast = new CLayerIpMcast(this->sim, this->info->NumGroups()); 00035 this->layerUdp = new CLayerUdp(this->sim); 00036 00037 // Create layer connections 00038 // Base layer <-> IP layer 00039 (*this->eventRecv) += this->layerIp->DelegateRecv(); 00040 (*this->layerIp->EventSend()) += this->delegateSend; 00041 // IP layer <-> IP local layer 00042 (*this->layerIp->EventRecv()) += this->layerIpLocal->DelegateRecv(); 00043 (*this->layerIpLocal->EventSend()) += this->layerIp->DelegateSend(); 00044 // IP local layer <-> UDP layer 00045 (*this->layerIpLocal->EventRecv()) += this->layerUdp->DelegateRecv(); 00046 (*this->layerUdp->EventSend()) += this->layerIpLocal->DelegateSend(); 00047 // IP layer <-> IP multicast layer 00048 (*this->layerIp->EventRecv()) += this->layerIpMcast->DelegateRecv(); 00049 (*this->layerIpMcast->EventSend()) += this->layerIp->DelegateSend(); 00050 // IP local layer <-> IP multicast layer 00051 (*this->layerIpMcast->EventRecv()) += this->layerIpLocal->DelegateRecvMcast(); 00052 (*this->layerIpLocal->EventSendMcast()) += this->layerIpMcast->DelegateSend(); 00053 00054 // Statistics 00055 this->statPacketsRead = 0; 00056 this->statPacketsWrite = 0; 00057 00058 this->statDataRead = 0; 00059 this->statDataWrite = 0; 00060 } 00061 00062 CNode::~CNode() 00063 { 00064 // Delete base delegate and event 00065 delete this->delegateSend; 00066 delete this->eventRecv; 00067 00068 // Delete layers 00069 delete this->layerIp; 00070 delete this->layerIpLocal; 00071 delete this->layerIpMcast; 00072 delete this->layerUdp; 00073 00074 // Delete links 00075 delete[] this->links; 00076 delete[] this->linkEntries; 00077 } 00078 00079 void CNode::AddLink(CLink* link) 00080 { 00081 assert(link); 00082 assert(this->currentLink < this->numLinks); 00083 00084 // Add link to the node 00085 this->links[this->currentLink] = link; 00086 00087 // Add node to the link 00088 this->linkEntries[this->currentLink] = link->AddNode(this, this->currentLink); 00089 00090 // Add link index 00091 this->linkIndices.insert(pair<__uint32, __uint32>(link->Id(), this->currentLink)); 00092 00093 this->currentLink++; 00094 } 00095 00096 ENetworkCode CNode::Recv(CObject* sender, __uint32 entry, CPacket* packet) 00097 { 00098 assert(sender); 00099 assert(packet); 00100 assert(sender == this->links[entry]); 00101 00102 #ifdef LOG_LAYER 00103 printf("\nT = %7.3lf NODE RECV id=%u entry=%u bytes=%u", this->sim->Time(), this->id, entry, packet->Size()); 00104 #endif 00105 00106 // Send the packet to the upper layers 00107 (*this->eventRecv)(entry, packet); 00108 00109 // Stat 00110 this->statPacketsRead++; 00111 this->statDataRead += packet->Size(); 00112 00113 // Delete the packet 00114 packet->Delete(); 00115 delete packet; 00116 00117 return NETWORK_SUCCESS; 00118 } 00119 00120 void CNode::Send(__uint32 entry, CPacket* packet) 00121 { 00122 assert(packet); 00123 assert(entry < this->numLinks); 00124 00125 this->statPacketsWrite++; 00126 this->statDataWrite += packet->Size(); 00127 00128 #ifdef LOG_LAYER 00129 printf("\nT = %7.3lf NODE SEND id=%u entry=%u bytes=%u", this->sim->Time(), this->id, entry, packet->Size()); 00130 #endif 00131 00132 this->links[entry]->Recv(this, this->linkEntries[entry], packet); 00133 } 00134 00135 void CNode::Finalize() 00136 { 00137 // Call the finalizer for all layers 00138 this->layerIp->Finalize(); 00139 this->layerIpLocal->Finalize(); 00140 this->layerIpMcast->Finalize(); 00141 this->layerUdp->Finalize(); 00142 }
Last updated: February 8, 2011