Alex Bikfalvi
SimStream Documentation
LayerIpMcast.cpp
00001 #include "Headers.h" 00002 #include "LayerIpMcast.h" 00003 00004 CLayerIpMcast::CLayerIpMcast( 00005 CSimHandler* sim, 00006 __uint32 numGroups 00007 ) : CLayer(sim) 00008 { 00009 this->numGroups = numGroups; 00010 00011 // Groups 00012 this->groups = new CLayerIpMcastGroup[this->numGroups]; 00013 00014 // Memberships 00015 this->memberships = new CLayerIpMcastMembership[this->numGroups]; 00016 00017 // Delegates 00018 this->delegateRecv = new Delegate2<CLayerIpMcast, void, __uint32, CPacketIp*>(this, &CLayerIpMcast::Recv); 00019 this->delegateSend = new Delegate1<CLayerIpMcast, void, CPacketIp*>(this, &CLayerIpMcast::Send); 00020 this->delegateJoin = new Delegate3<CLayerIpMcast, void, __uint32, CAddress, CLayerIpMcastGroup*>(this, &CLayerIpMcast::Join); 00021 this->delegateLeave = new Delegate3<CLayerIpMcast, void, __uint32, CAddress, CLayerIpMcastGroup*>(this, &CLayerIpMcast::Leave); 00022 this->delegateLocalJoin = new Delegate2<CLayerIpMcast, void, __uint32, CAddress>(this, &CLayerIpMcast::LocalJoin); 00023 this->delegateLocalLeave = new Delegate2<CLayerIpMcast, void, __uint32, CAddress>(this, &CLayerIpMcast::LocalLeave); 00024 00025 // Events 00026 this->eventRecv = new Event2<void, __uint32, CPacketIp*>(); 00027 this->eventSend = new Event2<void, __uint32, CPacketIp*>(); 00028 } 00029 00030 CLayerIpMcast::~CLayerIpMcast() 00031 { 00032 delete[] this->groups; 00033 delete[] this->memberships; 00034 00035 // Delegates 00036 delete this->delegateRecv; 00037 delete this->delegateSend; 00038 delete this->delegateJoin; 00039 delete this->delegateLeave; 00040 delete this->delegateLocalJoin; 00041 delete this->delegateLocalLeave; 00042 00043 // Event 00044 delete this->eventRecv; 00045 delete this->eventSend; 00046 } 00047 00048 void CLayerIpMcast::Recv(__uint32 entry, CPacketIp* packet) 00049 { 00050 // Process only multicast packets addressed to a multicast group 00051 if(!packet->Dst().IsMulticastGroup()) return; 00052 00053 // Calculate the group 00054 __uint32 group = packet->Dst().MulticastGroup(); 00055 assert(group < this->numGroups); 00056 00057 /* 00058 * Local forwarding 00059 */ 00060 // If there is a local membership for this group send the packet payload to the upper layers 00061 if(this->memberships[group].State() == CLayerIpMcastMembership::MEMBER) 00062 { 00063 (*this->eventRecv)(entry, packet); 00064 } 00065 00066 /* 00067 * Multicast routing 00068 */ 00069 // Execute multicast forwarding only if TTL is greater than zero 00070 if(packet->Ttl() == 0) return; 00071 00072 // Forward a copy of the packet to all output interfaces for this group except the inbound interface 00073 for(CLayerIpMcastGroup::Iterator iter = this->groups[group].Out()->begin(); iter != this->groups[group].Out()->end(); iter++) 00074 { 00075 if(*iter != entry) 00076 { 00077 (*this->eventSend)(*iter, type_cast<CPacketIp*>(packet->Copy())); 00078 } 00079 } 00080 } 00081 00082 void CLayerIpMcast::Send(CPacketIp* packet) 00083 { 00084 // Process only packets addressed to a multicast group 00085 if(!packet->Dst().IsMulticastGroup()) return; 00086 00087 // Get the group 00088 __uint32 group = packet->Dst().MulticastGroup(); 00089 assert(group < this->numGroups); 00090 00091 // Forward the packet only if there is a membership for this group 00092 if(this->memberships[group].State() != CLayerIpMcastMembership::MEMBER) return; 00093 00094 // Send the packet to the lower layer on the membership interface 00095 (*this->eventSend)(this->memberships[group].Entry(), packet); 00096 } 00097 00098 void CLayerIpMcast::Join(__uint32 entry, CAddress address, CLayerIpMcastGroup* groupSender) 00099 { 00100 __uint32 group = address.MulticastGroup(); 00101 assert(group < this->numGroups); 00102 00103 // Add interface to multicast group 00104 this->groups[group].Out()->insert(entry); 00105 } 00106 00107 void CLayerIpMcast::Leave(__uint32 entry, CAddress address, CLayerIpMcastGroup* groupSender) 00108 { 00109 __uint32 group = address.MulticastGroup(); 00110 assert(group < this->numGroups); 00111 00112 // Remove interface from multicast group 00113 this->groups[group].Out()->erase(entry); 00114 } 00115 00116 void CLayerIpMcast::LocalJoin(__uint32 entry, CAddress address) 00117 { 00118 __uint32 group = address.MulticastGroup(); 00119 assert(group < this->numGroups); 00120 00121 // Add membership for this group and interface 00122 this->memberships[group].Join(entry); 00123 } 00124 00125 void CLayerIpMcast::LocalLeave(__uint32 entry, CAddress address) 00126 { 00127 __uint32 group = address.MulticastGroup(); 00128 assert(group < this->numGroups); 00129 00130 // Remove membership for this group and interface 00131 this->memberships[group].Leave(); 00132 } 00133
Last updated: February 8, 2011