Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2013 Mohammed J.F. Alenazi | 3 * Copyright (c) 2013 Mohammed J.F. Alenazi |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 5 * This program is free software; you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 2 as | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with this program; if not, write to the Free Software | 15 * along with this program; if not, write to the Free Software |
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 * | 17 * |
18 * Author: Mohammed J.F. Alenazi <malenazi@ittc.ku.edu> | 18 * Author: Mohammed J.F. Alenazi <malenazi@ittc.ku.edu> |
19 * | 19 * |
20 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director | 20 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director |
21 * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets | 21 * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets |
22 * Information and Telecommunication Technology Center (ITTC) | 22 * Information and Telecommunication Technology Center (ITTC) |
23 * and Department of Electrical Engineering and Computer Science | 23 * and Department of Electrical Engineering and Computer Science |
24 * The University of Kansas Lawrence, KS USA. | 24 * The University of Kansas Lawrence, KS USA. |
25 * | 25 * |
26 * Work supported by King Saud University and the ITTC at The University of Kans as. | 26 * Work supported by King Saud University and |
27 * the ITTC at The University of Kansas. | |
27 */ | 28 */ |
28 | 29 |
29 | 30 |
30 #include "epidemic-routing-protocol.h" | 31 #include "epidemic-routing-protocol.h" |
31 #include <vector> | 32 #include <vector> |
32 #include "ns3/boolean.h" | 33 #include "ns3/boolean.h" |
33 #include "ns3/config.h" | 34 #include "ns3/config.h" |
34 #include "ns3/log.h" | 35 #include "ns3/log.h" |
35 #include "ns3/inet-socket-address.h" | 36 #include "ns3/inet-socket-address.h" |
36 #include "ns3/random-variable-stream.h" | 37 #include "ns3/random-variable-stream.h" |
37 #include "ns3/udp-socket-factory.h" | 38 #include "ns3/udp-socket-factory.h" |
38 #include "ns3/boolean.h" | 39 #include "ns3/boolean.h" |
39 #include "ns3/double.h" | 40 #include "ns3/double.h" |
40 #include "ns3/uinteger.h" | 41 #include "ns3/uinteger.h" |
41 #include "ns3/udp-header.h" | 42 #include "ns3/udp-header.h" |
42 #include <iostream> | 43 #include <iostream> |
43 #include <algorithm> | 44 #include <algorithm> |
44 #include <functional> | 45 #include <functional> |
45 #include "ns3/ipv4-route.h" | 46 #include "ns3/ipv4-route.h" |
46 #include "ns3/socket.h" | 47 #include "ns3/socket.h" |
47 #include "ns3/log.h" | 48 #include "ns3/log.h" |
48 | 49 |
49 | 50 |
51 /** | |
52 * \file | |
53 * \ingroup epidemic | |
54 * ns3::Epidemic::RoutingProtocol implementation. | |
55 */ | |
56 | |
57 | |
50 using namespace std; | 58 using namespace std; |
51 | 59 |
52 namespace ns3 { | 60 namespace ns3 { |
61 | |
62 NS_LOG_COMPONENT_DEFINE ("EpidemicRoutingProtocol"); | |
63 | |
53 namespace Epidemic { | 64 namespace Epidemic { |
54 NS_OBJECT_ENSURE_REGISTERED (RoutingProtocol); | 65 NS_OBJECT_ENSURE_REGISTERED (RoutingProtocol); |
55 NS_LOG_COMPONENT_DEFINE ("EpidemicRoutingProtocol"); | 66 |
56 | 67 |
57 | 68 |
58 TypeId RoutingProtocol::GetTypeId (void) | 69 TypeId RoutingProtocol::GetTypeId (void) |
59 { | 70 { |
60 static TypeId tid = TypeId ("ns3::epidemic::RoutingProtocol") | 71 static TypeId tid = TypeId ("ns3::Epidemic::RoutingProtocol") |
61 .SetParent<Ipv4RoutingProtocol> () | 72 .SetParent<Ipv4RoutingProtocol> () |
62 .AddConstructor<RoutingProtocol> () | 73 .AddConstructor<RoutingProtocol> () |
63 .AddAttribute ("HopCount","Maximum number of times a packet will be flooded. ", | 74 .AddAttribute ("HopCount","Maximum number of times " |
75 "a packet will be flooded.", | |
64 UintegerValue (64), | 76 UintegerValue (64), |
65 MakeUintegerAccessor (&RoutingProtocol::m_hopCount), | 77 MakeUintegerAccessor (&RoutingProtocol::m_hopCount), |
66 MakeUintegerChecker<uint32_t> ()) | 78 MakeUintegerChecker<uint32_t> ()) |
67 .AddAttribute ("QueueLength","Maximum number of packets that a queue can hol d.", | 79 .AddAttribute ("QueueLength","Maximum number of " |
80 "packets that a queue can hold.", | |
68 UintegerValue (64), | 81 UintegerValue (64), |
69 MakeUintegerAccessor (&RoutingProtocol::m_maxQueueLen), | 82 MakeUintegerAccessor (&RoutingProtocol::m_maxQueueLen), |
70 MakeUintegerChecker<uint32_t> ()) | 83 MakeUintegerChecker<uint32_t> ()) |
71 .AddAttribute ("QueueEntryExpireTime","Maximum time a packet can live in " | 84 .AddAttribute ("QueueEntryExpireTime","Maximum time a packet can live in " |
72 "the epidemic queues since it's generated at the source.", | 85 "the epidemic queues since it's generated at the source.", |
73 TimeValue (Seconds (100)), | 86 TimeValue (Seconds (100)), |
74 MakeTimeAccessor (&RoutingProtocol::m_queueEntryExpireTime), | 87 MakeTimeAccessor (&RoutingProtocol::m_queueEntryExpireTime), |
75 MakeTimeChecker ()) | 88 MakeTimeChecker ()) |
76 .AddAttribute ("BeaconInterval","Time in seconds after which a beacon packet is broadcast.", | 89 .AddAttribute ("HostRecentPeriod","Time in seconds for host recent period" |
90 ", in which hosts can not re-exchange summary vectors.", | |
91 TimeValue (Seconds (10)), | |
92 MakeTimeAccessor (&RoutingProtocol::m_hostRecentPeriod), | |
93 MakeTimeChecker ()) | |
94 .AddAttribute ("BeaconInterval","Time in seconds after which a " | |
95 "beacon packet is broadcast.", | |
77 TimeValue (Seconds (1)), | 96 TimeValue (Seconds (1)), |
78 MakeTimeAccessor (&RoutingProtocol::m_beaconInterval), | 97 MakeTimeAccessor (&RoutingProtocol::m_beaconInterval), |
79 MakeTimeChecker ()) | 98 MakeTimeChecker ()) |
80 .AddAttribute ("BeaconRandomness","Upper bond of the uniform distribution ra ndom time " | 99 .AddAttribute ("BeaconRandomness","Upper bound of the uniform distribution" |
Peter Barnes
2014/09/12 20:33:49
"bound"
mjf.alenazi
2014/09/18 00:34:25
Done.
| |
81 "added to avoid collisions. Measured in milliseconds", | 100 " random time added to avoid collisions. Measured in millisec onds", |
82 UintegerValue (100), | 101 UintegerValue (100), |
83 MakeUintegerAccessor (&RoutingProtocol::m_beaconRandomness), | 102 MakeUintegerAccessor (&RoutingProtocol::m_beaconMaxJitterMs), |
84 MakeUintegerChecker<uint32_t> ()); | 103 MakeUintegerChecker<uint32_t> ()); |
85 | 104 |
86 return tid; | 105 return tid; |
87 } | 106 } |
88 | 107 |
89 | 108 |
90 | 109 |
91 | 110 |
92 RoutingProtocol::RoutingProtocol () | 111 RoutingProtocol::RoutingProtocol () |
93 : m_hopCount (0), | 112 : m_hopCount (0), |
94 m_maxQueueLen (0), | 113 m_maxQueueLen (0), |
95 m_queueEntryExpireTime (Seconds (0)), | 114 m_queueEntryExpireTime (Seconds (0)), |
96 m_beaconInterval (Seconds (0)), | 115 m_beaconInterval (Seconds (0)), |
97 m_beaconRandomness (0), | 116 m_hostRecentPeriod (Seconds (0)), |
117 m_beaconMaxJitterMs (0), | |
98 m_dataPacketCounter (0), | 118 m_dataPacketCounter (0), |
99 m_queue (m_maxQueueLen) | 119 m_queue (m_maxQueueLen) |
100 { | 120 { |
121 NS_LOG_FUNCTION (this); | |
101 } | 122 } |
102 | 123 |
103 RoutingProtocol::~RoutingProtocol () | 124 RoutingProtocol::~RoutingProtocol () |
104 { | 125 { |
126 NS_LOG_FUNCTION (this); | |
105 } | 127 } |
106 | 128 |
107 void | 129 void |
108 RoutingProtocol::DoDispose () | 130 RoutingProtocol::DoDispose () |
109 { | 131 { |
132 NS_LOG_FUNCTION (this); | |
110 m_ipv4 = 0; | 133 m_ipv4 = 0; |
111 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::iterator iter = m_socketAddr esses.begin (); iter | 134 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::iterator |
135 iter = m_socketAddresses.begin (); iter | |
112 != m_socketAddresses.end (); iter++) | 136 != m_socketAddresses.end (); iter++) |
113 { | 137 { |
114 iter->first->Close (); | 138 iter->first->Close (); |
115 } | 139 } |
116 m_socketAddresses.clear (); | 140 m_socketAddresses.clear (); |
117 Ipv4RoutingProtocol::DoDispose (); | 141 Ipv4RoutingProtocol::DoDispose (); |
118 } | 142 } |
119 | 143 |
120 void | 144 void |
121 RoutingProtocol::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const | 145 RoutingProtocol::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const |
122 { | 146 { |
123 /* | 147 /* |
124 * There is no routing table | 148 * There is no routing table |
125 */ | 149 */ |
126 *stream->GetStream () << "No Routing table "; | 150 *stream->GetStream () << "No Routing table "; |
127 } | 151 } |
128 | 152 |
129 | 153 |
130 | 154 |
131 | 155 |
132 void | 156 void |
133 RoutingProtocol::Start () | 157 RoutingProtocol::Start () |
134 { | 158 { |
135 NS_LOG_FUNCTION (this ); | 159 NS_LOG_FUNCTION (this ); |
136 m_queue.SetMaxQueueLen (m_maxQueueLen); | 160 m_queue.SetMaxQueueLen (m_maxQueueLen); |
137 m_beaconTimer.SetFunction (&RoutingProtocol::SendBeacons,this); | 161 m_beaconTimer.SetFunction (&RoutingProtocol::SendBeacons,this); |
138 m_beaconRandomnessDistro = CreateObject<UniformRandomVariable> (); | 162 m_beaconJitter = CreateObject<UniformRandomVariable> (); |
Peter Barnes
2014/09/12 20:33:49
Add
m_beaconRandomnessDistro->SetAttribute ("Max"
mjf.alenazi
2014/09/18 00:34:25
Done.
| |
139 m_beaconTimer.Schedule (m_beaconInterval + MilliSeconds (m_beaconRandomnessDis tro->GetValue () * m_beaconRandomness)); | 163 m_beaconJitter->SetAttribute ("Max", DoubleValue (m_beaconMaxJitterMs)); |
Peter Barnes
2014/09/12 20:33:49
(With added line above):
… m_beaconInterval + Mill
mjf.alenazi
2014/09/18 00:34:25
Done.
| |
140 } | 164 m_beaconTimer.Schedule (m_beaconInterval + MilliSeconds |
141 | 165 (m_beaconJitter->GetValue ())); |
142 /* | 166 } |
143 a function used to send a packet for a given address | 167 |
144 */ | 168 bool |
169 RoutingProtocol::IsHostContactedRecently (Ipv4Address hostID) | |
170 { | |
171 NS_LOG_FUNCTION (this << hostID); | |
172 // If host is not in has table, record current time and return false | |
173 HostContactMap::iterator hostID_pair = m_hostContactTime.find (hostID); | |
174 if (hostID_pair == m_hostContactTime.end ()) | |
175 { | |
176 m_hostContactTime.insert (std::make_pair (hostID,Now ())); | |
177 return false; | |
178 } | |
179 | |
180 //if host is in hash table check time is less than the recent_period: | |
181 if (Now () < (hostID_pair->second + m_hostRecentPeriod )) | |
182 { | |
183 // it means the host is recently contacted | |
184 return true; | |
185 } | |
186 else | |
187 { | |
188 // update the recent contact value | |
189 hostID_pair->second = Now (); | |
190 // return false since it has exceeded the recent contact period | |
191 return false; | |
192 } | |
193 | |
194 } | |
145 | 195 |
146 void | 196 void |
147 RoutingProtocol::SendPacket (Ptr<Packet> p,InetSocketAddress addr) | 197 RoutingProtocol::SendPacket (Ptr<Packet> p,InetSocketAddress addr) |
148 { | 198 { |
149 NS_LOG_FUNCTION (this << *p); | 199 NS_LOG_FUNCTION (this << p << addr); |
150 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = m_socketA ddresses.begin (); | 200 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator |
201 j = m_socketAddresses.begin (); | |
151 j != m_socketAddresses.end (); ++j) | 202 j != m_socketAddresses.end (); ++j) |
152 { | 203 { |
153 Ipv4InterfaceAddress iface = j->second; | 204 Ipv4InterfaceAddress iface = j->second; |
154 if (iface.GetLocal () == m_mainAddress) | 205 if (iface.GetLocal () == m_mainAddress) |
155 { | 206 { |
156 Ptr<Socket> socket = j->first; | 207 Ptr<Socket> socket = j->first; |
157 NS_LOG_LOGIC ("Packet " << p << " is sent to" << addr ); | 208 NS_LOG_LOGIC ("Packet " << p << " is sent to" << addr ); |
158 socket->SendTo (p,0, addr); | 209 socket->SendTo (p,0, addr); |
159 } | 210 } |
160 } | 211 } |
161 NS_LOG_FUNCTION (this << *p); | 212 NS_LOG_FUNCTION (this << *p); |
162 } | 213 } |
163 | 214 |
164 | 215 |
165 | 216 |
166 void | 217 void |
167 RoutingProtocol::BroadcastPacket (Ptr<Packet> p) | 218 RoutingProtocol::BroadcastPacket (Ptr<Packet> p) |
168 { | 219 { |
169 NS_LOG_FUNCTION (this); | 220 NS_LOG_FUNCTION (this << p); |
170 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = m_socketA ddresses.begin (); | 221 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator |
222 j = m_socketAddresses.begin (); | |
171 j != m_socketAddresses.end (); ++j) | 223 j != m_socketAddresses.end (); ++j) |
172 { | 224 { |
173 Ptr<Socket> socket = j->first; | 225 Ptr<Socket> socket = j->first; |
174 Ipv4InterfaceAddress iface = j->second; | 226 Ipv4InterfaceAddress iface = j->second; |
175 // Send to all-hosts broadcast if on /32 addr, subnet-directed otherwise | 227 // Send to all-hosts broadcast if on /32 addr, subnet-directed otherwise |
176 Ipv4Address destination; | 228 Ipv4Address destination; |
177 if (iface.GetMask () == Ipv4Mask::GetOnes ()) | 229 if (iface.GetMask () == Ipv4Mask::GetOnes ()) |
178 { | 230 { |
179 destination = Ipv4Address ("255.255.255.255"); | 231 destination = Ipv4Address ("255.255.255.255"); |
180 } | 232 } |
181 else | 233 else |
182 { | 234 { |
183 destination = iface.GetBroadcast (); | 235 destination = iface.GetBroadcast (); |
184 } | 236 } |
185 NS_LOG_LOGIC ("Packet " << p << " is sent to" << destination ); | 237 NS_LOG_LOGIC ("Packet " << p << " is sent to" << destination ); |
186 socket->SendTo (p, 0, InetSocketAddress (destination, EPIDEMIC_PORT)); | 238 socket->SendTo (p, 0, InetSocketAddress (destination, EPIDEMIC_PORT)); |
187 } | 239 } |
188 } | 240 } |
189 | 241 |
190 | 242 |
191 | 243 |
192 void | 244 void |
193 RoutingProtocol::SendPacketFromQueue (Ipv4Address dst,QueueEntry queueEntry) | 245 RoutingProtocol::SendPacketFromQueue (Ipv4Address dst,QueueEntry queueEntry) |
194 { | 246 { |
195 NS_LOG_FUNCTION (this << "Queue Sending Packet " << dst ); | 247 NS_LOG_FUNCTION (this << dst << queueEntry.GetPacketID ()); |
196 Ptr<Packet> p = ConstCast<Packet> (queueEntry.GetPacket ()); | 248 Ptr<Packet> p = ConstCast<Packet> (queueEntry.GetPacket ()); |
197 UnicastForwardCallback ucb = queueEntry.GetUnicastForwardCallback (); | 249 UnicastForwardCallback ucb = queueEntry.GetUnicastForwardCallback (); |
198 Ipv4Header header = queueEntry.GetIpv4Header (); | 250 Ipv4Header header = queueEntry.GetIpv4Header (); |
199 /* | 251 /* |
200 * Since Epidemic routing has a control mechanism to drop packets based on ho p count, | 252 * Since Epidemic routing has a control mechanism to drop packets based |
201 * IP TTL dropping mechanism is avoided by incrementing TTL. | 253 * on hop count, IP TTL dropping mechanism is avoided by incrementing TTL. |
202 */ | 254 */ |
203 header.SetTtl (header.GetTtl () + 1); | 255 header.SetTtl (header.GetTtl () + 1); |
204 header.SetPayloadSize (p->GetSize ()); | 256 header.SetPayloadSize (p->GetSize ()); |
205 Ptr<Ipv4Route> rt = Create<Ipv4Route> (); | 257 Ptr<Ipv4Route> rt = Create<Ipv4Route> (); |
206 rt->SetSource (header.GetSource ()); | 258 rt->SetSource (header.GetSource ()); |
207 rt->SetDestination (header.GetDestination ()); | 259 rt->SetDestination (header.GetDestination ()); |
208 rt->SetGateway (dst); | 260 rt->SetGateway (dst); |
209 if (m_ipv4->GetInterfaceForAddress (m_mainAddress) != -1) | 261 if (m_ipv4->GetInterfaceForAddress (m_mainAddress) != -1) |
210 { | 262 { |
211 Ptr<Node> node = m_ipv4->GetObject<Node> (); | 263 Ptr<Node> node = m_ipv4->GetObject<Node> (); |
212 rt->SetOutputDevice (m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (m_mainAddress))); | 264 rt->SetOutputDevice (m_ipv4->GetNetDevice |
265 (m_ipv4->GetInterfaceForAddress (m_mainAddress))); | |
213 | 266 |
214 } | 267 } |
215 | 268 |
216 Ptr<Packet> copy = p->Copy (); | 269 Ptr<Packet> copy = p->Copy (); |
217 ucb (rt, copy, header); | 270 /* |
218 | 271 * The packet will not be sent if: |
272 * The forward address is the source address of the packet. | |
273 * The forward address is the destination address of the packet. | |
274 */ | |
275 if (dst != header.GetSource () && !IsMyOwnAddress (header.GetDestination ())) | |
276 { | |
277 ucb (rt, copy, header); | |
278 } | |
219 } | 279 } |
220 | 280 |
221 void | 281 void |
222 RoutingProtocol::SendBeacons () | 282 RoutingProtocol::SendBeacons () |
223 { | 283 { |
224 NS_LOG_FUNCTION (this << "Broadcasting Beacons"); | 284 NS_LOG_FUNCTION (this); |
225 Ptr<Packet> packet = Create<Packet> (); | 285 Ptr<Packet> packet = Create<Packet> (); |
226 EpidemicHeader header; | 286 EpidemicHeader header; |
227 // This number does not have any effect but it has to be more than 1 to avoid dropping at the receiver | 287 // This number does not have any effect but it has to be more than |
288 // 1 to avoid dropping at the receiver | |
228 header.SetHopCount (m_hopCount); | 289 header.SetHopCount (m_hopCount); |
229 packet->AddHeader (header); | 290 packet->AddHeader (header); |
230 TypeHeader tHeader (EPIDEMIC_TYPE_BEACON); | 291 TypeHeader tHeader (TypeHeader::BEACON); |
231 packet->AddHeader (tHeader); | 292 packet->AddHeader (tHeader); |
232 EpidemicTag tempTag (1); | 293 ControlTag tempTag (ControlTag::CONTROL); |
Peter Barnes
2014/09/12 20:33:49
The magic value 1 should be an enum constant in cl
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
233 // Packet tag is added and will be removed before local delivery | 294 // Packet tag is added and will be removed before local delivery in |
295 // RouteInput function | |
234 packet->AddPacketTag (tempTag); | 296 packet->AddPacketTag (tempTag); |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
What do you mean by "before
mjf.alenazi
2014/09/18 00:34:26
Before the data packet is sent to transport layer
| |
235 | 297 |
236 Simulator::Schedule (MilliSeconds (m_beaconRandomnessDistro->GetValue () * m_b eaconRandomness),&RoutingProtocol::BroadcastPacket,this, packet); | 298 |
Peter Barnes
2014/09/12 20:33:49
(See comment line 139)
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
237 m_beaconTimer.Schedule (m_beaconInterval + MilliSeconds (m_beaconRandomnessDis tro->GetValue () * m_beaconRandomness)); | 299 BroadcastPacket (packet); |
Peter Barnes
2014/09/12 20:33:49
(See comment line 139)
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
300 m_beaconTimer.Schedule (m_beaconInterval + MilliSeconds | |
301 (m_beaconJitter->GetValue ())); | |
238 } | 302 } |
239 | 303 |
240 | 304 |
241 uint32_t | 305 uint32_t |
242 RoutingProtocol::FindOutputDeviceForAddress (Ipv4Address dst) | 306 RoutingProtocol::FindOutputDeviceForAddress (Ipv4Address dst) |
243 { | 307 { |
308 NS_LOG_FUNCTION (this << dst); | |
244 Ptr<Node> mynode = m_ipv4->GetObject<Node> (); | 309 Ptr<Node> mynode = m_ipv4->GetObject<Node> (); |
245 for (uint32_t i = 0; i < mynode->GetNDevices (); i++) | 310 for (uint32_t i = 0; i < mynode->GetNDevices (); i++) |
246 { | 311 { |
247 Ipv4InterfaceAddress iface = m_ipv4->GetAddress (m_ipv4->GetInterfaceForDe vice (mynode->GetDevice (i)),0); | 312 Ipv4InterfaceAddress iface = m_ipv4->GetAddress ( |
248 if (dst.CombineMask (iface.GetMask ()) == iface.GetLocal ().CombineMask (i face.GetMask ())) | 313 m_ipv4->GetInterfaceForDevice (mynode->GetDevice (i)),0); |
314 if (dst.CombineMask (iface.GetMask ()) == | |
315 iface.GetLocal ().CombineMask (iface.GetMask ())) | |
249 { | 316 { |
250 return i; | 317 return i; |
251 } | 318 } |
252 } | 319 } |
253 return -1; | 320 return -1; |
254 } | 321 } |
255 | 322 |
256 | 323 |
257 uint32_t | 324 uint32_t |
258 RoutingProtocol::FindLoopbackDevice () | 325 RoutingProtocol::FindLoopbackDevice () |
259 { | 326 { |
327 NS_LOG_FUNCTION (this); | |
260 Ptr<Node> mynode = m_ipv4->GetObject<Node> (); | 328 Ptr<Node> mynode = m_ipv4->GetObject<Node> (); |
261 for (uint32_t i = 0; i < mynode->GetNDevices (); i++) | 329 for (uint32_t i = 0; i < mynode->GetNDevices (); i++) |
262 { | 330 { |
263 Ipv4InterfaceAddress iface = m_ipv4->GetAddress (m_ipv4->GetInterfaceForDe vice (mynode->GetDevice (i)),0); | 331 Ipv4InterfaceAddress iface = m_ipv4->GetAddress ( |
332 m_ipv4->GetInterfaceForDevice (mynode->GetDevice (i)),0); | |
264 if (iface.GetLocal () == Ipv4Address ("127.0.0.1") ) | 333 if (iface.GetLocal () == Ipv4Address ("127.0.0.1") ) |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
mjf.alenazi
2014/09/18 00:34:25
I could not do it. Can you help me with this one?
| |
265 { | 334 { |
266 return i; | 335 return i; |
267 } | 336 } |
268 } | 337 } |
269 return -1; | 338 return -1; |
270 } | 339 } |
271 | 340 |
272 | 341 |
273 bool | 342 bool |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
mjf.alenazi
2014/09/18 00:34:26
I tried pv4::IsDestinationAddress() but it did not
| |
274 RoutingProtocol::IsMyOwnAddress (Ipv4Address src) | 343 RoutingProtocol::IsMyOwnAddress (Ipv4Address src) |
275 { | 344 { |
276 NS_LOG_FUNCTION (this << src); | 345 NS_LOG_FUNCTION (this << src); |
277 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = | 346 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = |
278 m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) | 347 m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) |
279 { | 348 { |
280 Ipv4InterfaceAddress iface = j->second; | 349 Ipv4InterfaceAddress iface = j->second; |
281 if (src == iface.GetLocal ()) | 350 if (src == iface.GetLocal ()) |
282 { | 351 { |
283 return true; | 352 return true; |
284 } | 353 } |
285 } | 354 } |
286 return false; | 355 return false; |
287 } | 356 } |
288 | 357 |
289 | 358 |
290 Ptr<Ipv4Route> | 359 Ptr<Ipv4Route> |
291 RoutingProtocol::RouteOutput (Ptr<Packet> p, | 360 RoutingProtocol::RouteOutput (Ptr<Packet> p, |
292 const Ipv4Header &header, | 361 const Ipv4Header &header, |
293 Ptr<NetDevice> oif, | 362 Ptr<NetDevice> oif, |
294 Socket::SocketErrno &sockerr) | 363 Socket::SocketErrno &sockerr) |
295 { | 364 { |
296 | 365 |
297 NS_LOG_FUNCTION (this << "Packet Size" << p->GetSize () << " Packet " << p->G etUid () | 366 NS_LOG_FUNCTION (this << p << header << oif << sockerr); |
298 << " reached node " << m_mainAddress << " source " << h eader.GetSource () | 367 NS_LOG_LOGIC (this << "Packet Size" << p->GetSize () |
299 << " going to " << header.GetDestination ()); | 368 << " Packet " << p->GetUid () |
300 | 369 << " reached node " << m_mainAddress << " source " << head er.GetSource () |
301 | 370 << " going to " << header.GetDestination ()); |
302 | |
303 | 371 |
304 | 372 |
305 if (IsMyOwnAddress (header.GetDestination ())) | 373 if (IsMyOwnAddress (header.GetDestination ())) |
306 { | 374 { |
307 NS_LOG_LOGIC ("Local delivery a packet" << p->GetUid () << " has arrived d estination " | 375 NS_LOG_LOGIC ("Local delivery a packet" << p->GetUid () |
376 << " has arrived destination " | |
308 << " At node " << m_mainAddress << " " << header); | 377 << " At node " << m_mainAddress << " " << header); |
309 Ptr<Ipv4Route> rt = Create<Ipv4Route> (); | 378 Ptr<Ipv4Route> rt = Create<Ipv4Route> (); |
310 rt->SetSource (m_mainAddress); | 379 rt->SetSource (m_mainAddress); |
311 rt->SetDestination (header.GetDestination ()); | 380 rt->SetDestination (header.GetDestination ()); |
312 return rt; | 381 return rt; |
313 } | 382 } |
314 else | 383 else |
315 { | 384 { |
316 | |
317 /* | |
318 * Control packets generated at this node, are tagged with EpidemicTag. | |
319 */ | |
320 EpidemicTag tag; | |
321 p->PeekPacketTag (tag); | |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
mjf.alenazi
2014/09/18 00:34:25
Before the data packet is sent to transport layer
| |
322 Ptr<Ipv4Route> rt = Create<Ipv4Route> (); | 385 Ptr<Ipv4Route> rt = Create<Ipv4Route> (); |
323 rt->SetSource (m_mainAddress); | 386 rt->SetSource (m_mainAddress); |
324 rt->SetDestination (header.GetDestination ()); | 387 rt->SetDestination (header.GetDestination ()); |
325 rt->SetGateway (header.GetDestination ()); | 388 rt->SetGateway (header.GetDestination ()); |
326 | 389 |
327 if (m_ipv4->GetInterfaceForAddress (m_mainAddress) != -1) | 390 if (m_ipv4->GetInterfaceForAddress (m_mainAddress) != -1) |
328 { | 391 { |
329 // NS_LOG_DEBUG ( "Returning a route with a destination:" << header.Ge tDestination () ); | 392 /* |
330 | 393 * Control packets generated at this node, are |
331 if ( tag.GetEpidemicTag () != -1) | 394 * tagged with ControlTag. |
Peter Barnes
2014/09/12 20:33:49
(See patch-4 comment)
The magic value 1 should be
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
395 * They are removed before local delivery in RouteInput function. | |
396 */ | |
397 ControlTag tag; | |
398 p->PeekPacketTag (tag); | |
399 if ( tag.GetTagType () == ControlTag::CONTROL) | |
332 { | 400 { |
333 /* | 401 /* |
334 * if the packet is not tagged, it means a data packet | 402 * if the packet is not control, it means a data packet |
Peter Barnes
2014/09/12 20:33:49
(See patch-4 comment)
mjf.alenazi
2014/09/18 00:34:25
I am just trying to see if the packet is generated
| |
335 * Thus, data packet is supposed to be looped back to store it in the epidemic queue. | 403 * Thus, data packet is supposed to be looped back to |
336 */ | 404 * store it in the epidemic queue. |
337 //NS_LOG_DEBUG ( "New Data packet looped back " << p->GetSize () << " queue size " << m_queue.GetSize ()); | 405 */ |
338 rt->SetOutputDevice (m_ipv4->GetNetDevice (FindLoopbackDevice ())) ; | 406 rt->SetOutputDevice (m_ipv4->GetNetDevice (FindLoopbackDevice ())) ; |
339 } | 407 } |
340 else | 408 else |
341 { | 409 { |
342 /* | 410 /* |
343 * if the packet is tagged, it is an Epidemic control packet | 411 * if the packet is control packet |
344 * Thus, find the corresponding output device | 412 * Thus, find the corresponding output device |
345 */ | 413 */ |
346 NS_LOG_DEBUG ( "Epidemic triggered packets :" << header.GetDestina tion () << | 414 NS_LOG_DEBUG ( "Epidemic triggered packets :" << |
347 " found " << FindOutputDeviceForAddress (header.Ge tDestination ())); | 415 header.GetDestination () << " found " << |
348 rt->SetOutputDevice (m_ipv4->GetNetDevice (FindOutputDeviceForAddr ess (header.GetDestination ()))); | 416 FindOutputDeviceForAddress (header.GetDestination ( ))); |
417 rt->SetOutputDevice (m_ipv4->GetNetDevice ( | |
418 FindOutputDeviceForAddress (header.GetDesti nation ()))); | |
349 } | 419 } |
350 } | 420 } |
351 return rt; | 421 return rt; |
352 } | 422 } |
353 | 423 |
354 } | 424 } |
355 | 425 |
356 | 426 |
357 bool | 427 bool |
358 RoutingProtocol::RouteInput (Ptr<const Packet> p, | 428 RoutingProtocol::RouteInput (Ptr<const Packet> p, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 * Check all the interfaces local addresses for local delivery | 468 * Check all the interfaces local addresses for local delivery |
399 */ | 469 */ |
400 | 470 |
401 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = | 471 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = |
402 m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) | 472 m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) |
403 { | 473 { |
404 Ipv4InterfaceAddress iface = j->second; | 474 Ipv4InterfaceAddress iface = j->second; |
405 int32_t iif = m_ipv4->GetInterfaceForDevice (idev); | 475 int32_t iif = m_ipv4->GetInterfaceForDevice (idev); |
406 if (m_ipv4->GetInterfaceForAddress (iface.GetLocal ()) == iif) | 476 if (m_ipv4->GetInterfaceForAddress (iface.GetLocal ()) == iif) |
407 { | 477 { |
408 if (header.GetDestination () == iface.GetBroadcast () || header.GetDes tination () == m_mainAddress ) | 478 if (header.GetDestination () == iface.GetBroadcast () |
479 || header.GetDestination () == m_mainAddress ) | |
409 { | 480 { |
410 EpidemicTag tag; | 481 ControlTag tag; |
411 p->PeekPacketTag (tag); | 482 p->PeekPacketTag (tag); |
412 Ptr<Packet> local_copy = p->Copy (); | 483 Ptr<Packet> local_copy = p->Copy (); |
413 bool duplicatePacket = false; | 484 bool duplicatePacket = false; |
414 /* | 485 /* |
415 * If this is a data packet, add it to the epidemic queue in order to avoid | 486 * If this is a data packet, add it to the epidemic |
487 * queue in order to avoid | |
416 * receiving duplicates of the same packet. | 488 * receiving duplicates of the same packet. |
417 */ | 489 */ |
418 if (tag.GetEpidemicTag () < 1) | 490 if (tag.GetTagType () == ControlTag::NOT_SET) |
419 { | 491 { |
420 Ptr<Packet> copy = p->Copy (); | 492 Ptr<Packet> copy = p->Copy (); |
421 QueueEntry newEntry (copy, header, ucb, ecb); | 493 QueueEntry newEntry (copy, header, ucb, ecb); |
422 EpidemicHeader current_epidemicHeader; | 494 EpidemicHeader current_epidemicHeader; |
423 copy->PeekHeader (current_epidemicHeader); | 495 copy->PeekHeader (current_epidemicHeader); |
424 newEntry.SetExpireTime (m_queueEntryExpireTime + current_epide micHeader.GetTimeStamp ()); | 496 newEntry.SetExpireTime (m_queueEntryExpireTime + |
497 current_epidemicHeader.GetTimeStamp () ); | |
425 newEntry.SetPacketID (current_epidemicHeader.GetPacketID ()); | 498 newEntry.SetPacketID (current_epidemicHeader.GetPacketID ()); |
426 EpidemicHeader epidemicHeader; | 499 EpidemicHeader epidemicHeader; |
427 local_copy->RemoveHeader (epidemicHeader); | 500 local_copy->RemoveHeader (epidemicHeader); |
428 // Try to see the packet has been delivered i.e. in the epidem ic buffer | 501 // Try to see the packet has been |
429 if (m_queue.Find (current_epidemicHeader.GetPacketID ()).GetPa cketID () == 0) | 502 // delivered i.e. in the epidemic buffer |
503 if (m_queue.Find ( | |
504 current_epidemicHeader.GetPacketID ()).GetPacketID () | |
505 == 0) | |
430 { | 506 { |
431 m_queue.Enqueue (newEntry); | 507 m_queue.Enqueue (newEntry); |
432 // Remove the packet header for delivery | |
433 } | 508 } |
434 else | 509 else |
435 { | 510 { |
436 duplicatePacket = true; | 511 duplicatePacket = true; |
437 } | 512 } |
438 } | 513 } |
439 /* | 514 /* |
440 Deliver the packet locally | 515 Deliver the packet locally |
441 */ | 516 */ |
442 if (!duplicatePacket) | 517 if (!duplicatePacket) |
443 { | 518 { |
444 local_copy->RemoveAllPacketTags (); | 519 local_copy->RemovePacketTag (tag); |
445 lcb (local_copy, header, iif); | 520 lcb (local_copy, header, iif); |
446 } | 521 } |
447 return true; | 522 return true; |
448 | 523 |
449 } | 524 } |
450 } | 525 } |
451 } | 526 } |
452 | 527 |
453 /* | 528 /* |
454 If the packet does not have an epidemic header, create one and attach it to th e packet. | 529 If the packet does not have an epidemic header, |
455 This condition is called one the packet is originated locally and does not hav e an epidemic header | 530 create one and attach it to the packet. |
531 This condition occurs when the packet is originated locally and does not have | |
532 an Epidemic header. | |
456 */ | 533 */ |
457 | 534 |
458 Ptr<Packet> copy = p->Copy (); | 535 Ptr<Packet> copy = p->Copy (); |
459 NS_LOG_LOGIC ("Creating Epidemic packet " << p->GetUid () << " Src " << head er.GetSource () << " Dest " << | 536 NS_LOG_LOGIC ("Creating Epidemic packet " << p->GetUid () << " Src " << |
537 header.GetSource () << " Dest " << | |
460 header.GetDestination () << " Size before" << copy->GetSize ()); | 538 header.GetDestination () << " Size before" << copy->GetSize ()); |
461 /* | 539 /* |
462 * The global packet id format: 16 bit(Source IP):16bit(source packet counter) | 540 * The global packet id format: 16bit(Source IP):16bit(source packet counter) |
463 */ | 541 */ |
464 uint16_t hostID = header.GetSource ().Get () & 0xFFFF; | 542 uint16_t hostID = header.GetSource ().Get () & 0xFFFF; |
465 m_dataPacketCounter++; | 543 m_dataPacketCounter++; |
466 uint32_t global_packet_ID = hostID; | 544 uint32_t global_packet_ID = hostID; |
467 global_packet_ID = global_packet_ID << 16 | m_dataPacketCounter; | 545 global_packet_ID = global_packet_ID << 16 | m_dataPacketCounter; |
468 | 546 |
469 | 547 |
470 | 548 |
471 NS_LOG_LOGIC ("Adding Epidemic packet header " << p->GetUid () ); | 549 |
472 //ADD EPIDEMIC HEADER | 550 // Adding the data packet to the queue |
Peter Barnes
2014/09/12 20:33:50
(See patch-3 comment)
mjf.alenazi
2014/09/18 00:34:25
Done.
| |
473 EpidemicHeader new_epidemicHeader; | |
474 new_epidemicHeader.SetPacketID (global_packet_ID); | |
475 new_epidemicHeader.SetTimeStamp (Simulator::Now ()); | |
476 new_epidemicHeader.SetHopCount (m_hopCount); | |
477 | |
478 // Adding the data packet to the queue | |
479 QueueEntry newEntry (copy, header, ucb, ecb); | 551 QueueEntry newEntry (copy, header, ucb, ecb); |
480 newEntry.SetPacketID (global_packet_ID); | 552 newEntry.SetPacketID (global_packet_ID); |
481 | 553 |
482 if (IsMyOwnAddress (header.GetSource ())) | 554 if (IsMyOwnAddress (header.GetSource ())) |
483 { | 555 { |
556 NS_LOG_DEBUG ("Adding Epidemic packet header " << p->GetUid () ); | |
557 //ADD EPIDEMIC HEADER | |
558 EpidemicHeader new_epidemicHeader; | |
559 new_epidemicHeader.SetPacketID (global_packet_ID); | |
560 new_epidemicHeader.SetTimeStamp (Simulator::Now ()); | |
561 new_epidemicHeader.SetHopCount (m_hopCount); | |
484 // If the packet is generated in this node, add the epidemic header | 562 // If the packet is generated in this node, add the epidemic header |
485 copy->AddHeader (new_epidemicHeader); | 563 copy->AddHeader (new_epidemicHeader); |
486 // If the packet is generated in this node, make the Expire time start fro m now + the user specified period | 564 // If the packet is generated in this node, |
565 // make the Expire time start from now + the user specified period | |
487 newEntry.SetExpireTime (m_queueEntryExpireTime + Simulator::Now ()); | 566 newEntry.SetExpireTime (m_queueEntryExpireTime + Simulator::Now ()); |
488 | 567 |
489 } | 568 } |
490 else | 569 else |
491 { | 570 { |
492 // If the packet is generated in another node, read the epidemic header | 571 // If the packet is generated in another node, read the epidemic header |
493 EpidemicHeader current_epidemicHeader; | 572 EpidemicHeader current_epidemicHeader; |
494 copy->RemoveHeader (current_epidemicHeader); | 573 copy->RemoveHeader (current_epidemicHeader); |
495 if (current_epidemicHeader.GetHopCount () <= 1 | 574 if (current_epidemicHeader.GetHopCount () <= 1 |
496 || (current_epidemicHeader.GetTimeStamp () + m_queueEntryExpireTime) < Simulator::Now () ) | 575 || (current_epidemicHeader.GetTimeStamp () |
497 { | 576 + m_queueEntryExpireTime) < Simulator::Now () ) |
498 // Exit the function to not add the packet to the queue since the flo od count limit is reached | 577 { |
499 NS_LOG_DEBUG ("Exit the function and not add the packet to the queue since the flood count limit is reached"); | 578 // Exit the function to not add the packet to the queue |
579 // since the flood count limit is reached | |
580 NS_LOG_DEBUG ("Exit the function and not add the " | |
581 "packet to the queue since the flood count limit is reac hed"); | |
500 return true; | 582 return true; |
501 } | 583 } |
502 // If the packet is generated in another node, use the timestamp from the epidemic header | 584 // If the packet is generated in another node, |
503 newEntry.SetExpireTime (m_queueEntryExpireTime + current_epidemicHeader.Ge tTimeStamp ()); | 585 // use the timestamp from the epidemic header |
504 // If the packet is generated in another node, use the PacketID from the e pidemic header | 586 newEntry.SetExpireTime (m_queueEntryExpireTime + |
587 current_epidemicHeader.GetTimeStamp ()); | |
588 // If the packet is generated in another node, | |
589 // use the PacketID from the epidemic header | |
505 newEntry.SetPacketID (current_epidemicHeader.GetPacketID ()); | 590 newEntry.SetPacketID (current_epidemicHeader.GetPacketID ()); |
506 //Decrease the packet flood counter | 591 //Decrease the packet flood counter |
507 current_epidemicHeader.SetHopCount (current_epidemicHeader.GetHopCount () - 1); | 592 current_epidemicHeader.SetHopCount ( |
593 current_epidemicHeader.GetHopCount () - 1); | |
508 // Add the updated header | 594 // Add the updated header |
509 copy->AddHeader (current_epidemicHeader); | 595 copy->AddHeader (current_epidemicHeader); |
510 } | 596 } |
511 | 597 |
512 m_queue.Enqueue (newEntry); | 598 m_queue.Enqueue (newEntry); |
513 return true; | 599 return true; |
514 | 600 |
515 } | 601 } |
516 | 602 |
517 void | 603 void |
518 RoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4) | 604 RoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4) |
519 { | 605 { |
520 NS_LOG_FUNCTION (this); | 606 NS_LOG_FUNCTION (this << ipv4); |
521 m_ipv4 = ipv4; | 607 m_ipv4 = ipv4; |
522 Simulator::ScheduleNow (&RoutingProtocol::Start,this); | 608 Simulator::ScheduleNow (&RoutingProtocol::Start,this); |
523 } | 609 } |
524 | 610 |
525 void | 611 void |
526 RoutingProtocol::NotifyInterfaceUp (uint32_t i) | 612 RoutingProtocol::NotifyInterfaceUp (uint32_t i) |
527 { | 613 { |
528 NS_LOG_FUNCTION (this << i); | 614 NS_LOG_FUNCTION (this << i); |
529 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); | 615 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); |
530 Ipv4InterfaceAddress iface = l3->GetAddress (i,0); | 616 Ipv4InterfaceAddress iface = l3->GetAddress (i,0); |
531 if (iface.GetLocal () == Ipv4Address ("127.0.0.1")) | 617 if (iface.GetLocal () == Ipv4Address ("127.0.0.1")) |
532 { | 618 { |
533 return; | 619 return; |
534 } | 620 } |
535 if (m_mainAddress == Ipv4Address ()) | 621 if (m_mainAddress == Ipv4Address ()) |
536 { | 622 { |
537 m_mainAddress = iface.GetLocal (); | 623 m_mainAddress = iface.GetLocal (); |
538 } | 624 } |
539 | 625 |
540 /* | 626 /* |
541 Create a socket to be used for epidemic routing port | 627 Create a socket to be used for epidemic routing port |
542 */ | 628 */ |
543 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); | 629 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
544 Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (),tid); | 630 Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (),tid); |
545 socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvEpidemic,this)); | 631 socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvEpidemic,this)); |
632 socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), EPIDEMIC_PORT)); | |
546 socket->BindToNetDevice (l3->GetNetDevice (i)); | 633 socket->BindToNetDevice (l3->GetNetDevice (i)); |
547 socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), EPIDEMIC_PORT)); | |
548 socket->SetAllowBroadcast (true); | 634 socket->SetAllowBroadcast (true); |
549 m_socketAddresses.insert (std::make_pair (socket,iface)); | 635 m_socketAddresses.insert (std::make_pair (socket,iface)); |
550 } | 636 } |
551 | 637 |
552 void | 638 void |
553 RoutingProtocol::NotifyInterfaceDown (uint32_t i) | 639 RoutingProtocol::NotifyInterfaceDown (uint32_t i) |
554 { | 640 { |
555 NS_LOG_FUNCTION (this << m_ipv4->GetAddress (i, 0).GetLocal ()); | 641 NS_LOG_FUNCTION (this << m_ipv4->GetAddress (i, 0).GetLocal ()); |
556 // Disable layer 2 link state monitoring (if possible) | 642 // Disable layer 2 link state monitoring (if possible) |
557 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); | 643 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); |
558 Ptr<NetDevice> dev = l3->GetNetDevice (i); | 644 Ptr<NetDevice> dev = l3->GetNetDevice (i); |
559 // Close socket | 645 // Close socket |
560 Ptr<Socket> socket = FindSocketWithInterfaceAddress (m_ipv4->GetAddress (i, 0) ); | 646 Ptr<Socket> socket = FindSocketWithInterfaceAddress ( |
647 m_ipv4->GetAddress (i, 0)); | |
561 NS_ASSERT (socket); | 648 NS_ASSERT (socket); |
562 socket->Close (); | 649 socket->Close (); |
563 m_socketAddresses.erase (socket); | 650 m_socketAddresses.erase (socket); |
564 } | 651 } |
565 | 652 |
566 void | 653 void |
567 RoutingProtocol::NotifyAddAddress (uint32_t i,Ipv4InterfaceAddress address) | 654 RoutingProtocol::NotifyAddAddress (uint32_t i,Ipv4InterfaceAddress address) |
568 { | 655 { |
569 NS_LOG_FUNCTION (this << " interface " << i << " address " << address); | 656 NS_LOG_FUNCTION (this << i << address); |
570 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); | 657 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); |
571 if (!l3->IsUp (i)) | 658 if (!l3->IsUp (i)) |
572 { | 659 { |
573 return; | 660 return; |
574 } | 661 } |
575 if (l3->GetNAddresses (i) == 1) | 662 if (l3->GetNAddresses (i) == 1) |
576 { | 663 { |
577 Ipv4InterfaceAddress iface = l3->GetAddress (i, 0); | 664 Ipv4InterfaceAddress iface = l3->GetAddress (i, 0); |
578 Ptr<Socket> socket = FindSocketWithInterfaceAddress (iface); | 665 Ptr<Socket> socket = FindSocketWithInterfaceAddress (iface); |
579 if (!socket) | 666 if (!socket) |
580 { | 667 { |
581 if (iface.GetLocal () == Ipv4Address ("127.0.0.1")) | 668 if (iface.GetLocal () == Ipv4Address ("127.0.0.1")) |
582 { | 669 { |
583 return; | 670 return; |
584 } | 671 } |
585 // Create a socket to listen only on this interface | 672 // Create a socket to listen only on this interface |
586 Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (), | 673 Ptr<Socket> socket = |
587 UdpSocketFactory::GetTypeId ()); | 674 Socket::CreateSocket (GetObject<Node> (), |
675 UdpSocketFactory::GetTypeId ()); | |
588 NS_ASSERT (socket != 0); | 676 NS_ASSERT (socket != 0); |
589 socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvEpidemic, this)); | 677 socket->SetRecvCallback ( |
678 MakeCallback (&RoutingProtocol::RecvEpidemic,this)); | |
679 socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), | |
680 EPIDEMIC_PORT)); | |
590 socket->BindToNetDevice (l3->GetNetDevice (i)); | 681 socket->BindToNetDevice (l3->GetNetDevice (i)); |
591 socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), EPIDEMIC_PORT )); | |
592 socket->SetAllowBroadcast (true); | 682 socket->SetAllowBroadcast (true); |
593 m_socketAddresses.insert (std::make_pair (socket, iface)); | 683 m_socketAddresses.insert (std::make_pair (socket, iface)); |
594 } | 684 } |
595 } | 685 } |
596 else | 686 else |
597 { | 687 { |
598 NS_LOG_LOGIC ("Epidemic does not work with more then one address per each interface. Ignore added address"); | 688 NS_LOG_LOGIC ("Epidemic does not work with more then " |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
Where in the docs is this re
mjf.alenazi
2014/09/18 00:34:25
in LIMITATIONS in epidemic.rst
On 2014/09/12 20:
| |
689 "one address per each interface. Ignore added address"); | |
599 } | 690 } |
600 | 691 |
601 | 692 |
602 | 693 |
603 } | 694 } |
604 | 695 |
605 void | 696 void |
606 RoutingProtocol::NotifyRemoveAddress (uint32_t i,Ipv4InterfaceAddress address) | 697 RoutingProtocol::NotifyRemoveAddress (uint32_t i,Ipv4InterfaceAddress address) |
607 { | 698 { |
608 | 699 |
609 NS_LOG_FUNCTION (this); | 700 NS_LOG_FUNCTION (this << i << address); |
610 Ptr<Socket> socket = FindSocketWithInterfaceAddress (address); | 701 Ptr<Socket> socket = FindSocketWithInterfaceAddress (address); |
611 if (socket) | 702 if (socket) |
612 { | 703 { |
613 m_socketAddresses.erase (socket); | 704 m_socketAddresses.erase (socket); |
614 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); | 705 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); |
615 if (l3->GetNAddresses (i)) | 706 if (l3->GetNAddresses (i)) |
616 { | 707 { |
617 Ipv4InterfaceAddress iface = l3->GetAddress (i, 0); | 708 Ipv4InterfaceAddress iface = l3->GetAddress (i, 0); |
618 // Create a socket to listen only on this interface | 709 // Create a socket to listen only on this interface |
619 Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (), | 710 Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (), |
620 UdpSocketFactory::GetTypeId ()); | 711 UdpSocketFactory::GetTypeId ()); |
621 NS_ASSERT (socket != 0); | 712 NS_ASSERT (socket != 0); |
622 socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvEpidemic, this)); | 713 socket->SetRecvCallback ( |
714 MakeCallback (&RoutingProtocol::RecvEpidemic,this)); | |
623 // Bind to any IP address so that broadcasts can be received | 715 // Bind to any IP address so that broadcasts can be received |
624 socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), EPIDEMIC_PORT )); | 716 socket->Bind (InetSocketAddress ( |
717 Ipv4Address::GetAny (), EPIDEMIC_PORT)); | |
Peter Barnes
2015/04/28 23:42:33
Either do what Tommaso asked or explain why it's n
mjf.alenazi
2017/05/13 23:57:15
Done.
mjf.alenazi
2017/05/13 23:57:15
Done.
| |
625 socket->SetAllowBroadcast (true); | 718 socket->SetAllowBroadcast (true); |
626 m_socketAddresses.insert (std::make_pair (socket, iface)); | 719 m_socketAddresses.insert (std::make_pair (socket, iface)); |
627 | 720 |
628 } | 721 } |
629 | 722 |
630 } | 723 } |
631 else | 724 else |
632 { | 725 { |
633 NS_LOG_LOGIC ("Remove address not participating in Epidemic operation"); | 726 NS_LOG_LOGIC ("Remove address not participating in Epidemic operation"); |
634 } | 727 } |
635 } | 728 } |
636 | 729 |
637 | 730 |
638 | 731 |
639 void | 732 void |
640 RoutingProtocol::SendDisjointPackets (EpidemicSummaryVectorHeader packet_SMV,Ipv 4Address dest) | 733 RoutingProtocol::SendDisjointPackets (SummaryVectorHeader packet_SMV, |
641 { | 734 Ipv4Address dest) |
642 NS_LOG_FUNCTION (this << " send to " << dest); | 735 { |
736 NS_LOG_FUNCTION (this << dest); | |
643 /* | 737 /* |
644 This function is used to find send the packets listed in the vector list | 738 This function is used to find send the packets listed in the vector list |
645 */ | 739 */ |
646 EpidemicSummaryVectorHeader list = m_queue.FindDisjointPackets (packet_SMV); | 740 SummaryVectorHeader list = m_queue.FindDisjointPackets (packet_SMV); |
741 /* | |
742 To avoid collisions, packets are sent with incrementalTimeSlot | |
743 miliseconds apart | |
744 */ | |
745 uint16_t incrementalTimeSlot = 0; | |
746 | |
647 for (std::vector<uint32_t>::iterator | 747 for (std::vector<uint32_t>::iterator |
648 i = list.m_packets.begin (); | 748 i = list.m_packets.begin (); |
649 i != list.m_packets.end (); | 749 i != list.m_packets.end (); |
650 ++i) | 750 ++i) |
651 { | 751 { |
652 QueueEntry newEntry = m_queue.Find (*i); | 752 QueueEntry newEntry = m_queue.Find (*i); |
653 if (newEntry.GetPacket ()) | 753 if (newEntry.GetPacket ()) |
654 { | 754 { |
655 | 755 |
656 Simulator::Schedule (MilliSeconds (m_beaconRandomnessDistro->GetValue () * m_beaconRandomness), | 756 Simulator::Schedule (MilliSeconds (incrementalTimeSlot++), |
Peter Barnes
2014/09/12 20:33:49
(See comment line 139)
mjf.alenazi
2014/09/18 00:34:26
Done.
|
Peter Barnes
2015/04/28 23:42:33
Just change this line to
Simulator::Schedule (Tim
mjf.alenazi
2017/05/13 23:57:15
Done.
|
657 &RoutingProtocol::SendPacketFromQueue,this,dest, newEntry); | 757 &RoutingProtocol::SendPacketFromQueue, |
758 this,dest, newEntry); | |
658 } | 759 } |
659 } | 760 } |
660 } | 761 } |
661 | 762 |
662 | 763 |
663 | 764 |
664 Ptr<Socket> | 765 Ptr<Socket> |
665 RoutingProtocol::FindSocketWithInterfaceAddress (Ipv4InterfaceAddress addr ) con st | 766 RoutingProtocol::FindSocketWithInterfaceAddress |
767 (Ipv4InterfaceAddress addr ) const | |
666 { | 768 { |
667 NS_LOG_FUNCTION (this << addr); | 769 NS_LOG_FUNCTION (this << addr); |
668 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = | 770 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = |
669 m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) | 771 m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) |
670 { | 772 { |
671 Ptr<Socket> socket = j->first; | 773 Ptr<Socket> socket = j->first; |
672 Ipv4InterfaceAddress iface = j->second; | 774 Ipv4InterfaceAddress iface = j->second; |
673 if (iface == addr) | 775 if (iface == addr) |
674 { | 776 { |
675 return socket; | 777 return socket; |
676 } | 778 } |
677 } | 779 } |
678 Ptr<Socket> socket; | 780 Ptr<Socket> socket; |
679 return socket; | 781 return socket; |
680 } | 782 } |
681 | 783 |
682 | 784 |
683 void | 785 void |
684 RoutingProtocol::SendSummaryVector (Ipv4Address dest,bool firstNode) | 786 RoutingProtocol::SendSummaryVector (Ipv4Address dest,bool firstNode) |
685 { | 787 { |
686 NS_LOG_FUNCTION (this << " sent to " << dest); | 788 NS_LOG_FUNCTION (this << dest << firstNode); |
687 // Creating the packet | 789 // Creating the packet |
688 Ptr<Packet> packet_summary = Create<Packet> (); | 790 Ptr<Packet> packet_summary = Create<Packet> (); |
689 EpidemicSummaryVectorHeader header_summary = m_queue.GetSummaryVector (); | 791 SummaryVectorHeader header_summary = m_queue.GetSummaryVector (); |
792 packet_summary->AddHeader (header_summary); | |
690 TypeHeader tHeader; | 793 TypeHeader tHeader; |
691 if (firstNode) | 794 if (firstNode) |
692 { | 795 { |
693 tHeader.SetInstanceType (EPIDEMIC_TYPE_REPLY); | 796 tHeader.SetMessageType (TypeHeader::REPLY); |
694 } | 797 } |
695 else | 798 else |
696 { | 799 { |
697 tHeader.SetInstanceType (EPIDEMIC_TYPE_REPLY_BACK); | 800 tHeader.SetMessageType (TypeHeader::REPLY_BACK); |
698 } | 801 } |
699 packet_summary->AddHeader (header_summary); | 802 |
700 EpidemicTag tempTag (1); | |
701 packet_summary->AddHeader (tHeader); | 803 packet_summary->AddHeader (tHeader); |
804 ControlTag tempTag (ControlTag::CONTROL); | |
702 packet_summary->AddPacketTag (tempTag); | 805 packet_summary->AddPacketTag (tempTag); |
703 // Send the summary vector | 806 // Send the summary vector |
704 // NS_LOG_DEBUG ("Sending the summary vector 2 packet " << packet_summary->GetU id () << " to " << dest ); | 807 NS_LOG_INFO ("Sending the summary vector 2 packet " << header_summary ); |
705 InetSocketAddress addr = InetSocketAddress (dest, EPIDEMIC_PORT); | 808 InetSocketAddress addr = InetSocketAddress (dest, EPIDEMIC_PORT); |
706 Simulator::Schedule (MilliSeconds (m_beaconRandomnessDistro->GetValue () * m_b eaconRandomness), | 809 SendPacket (packet_summary, addr); |
Peter Barnes
2014/09/12 20:33:50
(See comment line 139)
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
707 &RoutingProtocol::SendPacket,this, packet_summary, addr); | |
708 | 810 |
709 } | 811 } |
710 | 812 |
711 | 813 |
712 | 814 |
713 void | 815 void |
714 RoutingProtocol::RecvEpidemic (Ptr<Socket> socket) | 816 RoutingProtocol::RecvEpidemic (Ptr<Socket> socket) |
715 { | 817 { |
716 NS_LOG_FUNCTION (this << socket); | 818 NS_LOG_FUNCTION (this << socket); |
717 m_queue.DropExpiredPackets (); | 819 m_queue.DropExpiredPackets (); |
718 Address address; | 820 Address address; |
719 Ptr<Packet> packet = socket->RecvFrom (address); | 821 Ptr<Packet> packet = socket->RecvFrom (address); |
720 TypeHeader tHeader (EPIDEMIC_TYPE_BEACON); | 822 TypeHeader tHeader (TypeHeader::BEACON); |
721 packet->RemoveHeader (tHeader); | 823 packet->RemoveHeader (tHeader); |
722 | 824 |
723 InetSocketAddress inetSourceAddr = InetSocketAddress::ConvertFrom (address); | 825 InetSocketAddress inetSourceAddr = InetSocketAddress::ConvertFrom (address); |
724 Ipv4Address sender = inetSourceAddr.GetIpv4 (); | 826 Ipv4Address sender = inetSourceAddr.GetIpv4 (); |
725 Ptr<Packet> packet_copy = packet->Copy (); | 827 if (tHeader.GetMessageType () == TypeHeader::BEACON) |
726 if (tHeader.GetMessageType () == EPIDEMIC_TYPE_BEACON) | 828 { |
727 { | 829 NS_LOG_LOGIC ("Got a beacon from " << sender << " " << packet->GetUid () |
728 NS_LOG_LOGIC ("Got a beacon from " << sender << " " << packet->GetUid () < < " " << m_mainAddress); | 830 << " " << m_mainAddress); |
729 // Anti-entropy session | 831 // Anti-entropy session |
730 // Check if you have the smaller address | 832 // Check if you have the smaller address and the host has not been |
731 if (m_mainAddress.Get () < sender.Get ()) | 833 // contacted recently |
834 if (m_mainAddress.Get () < sender.Get () | |
835 && !IsHostContactedRecently (sender)) | |
732 { | 836 { |
733 SendSummaryVector (sender,true); | 837 SendSummaryVector (sender,true); |
734 } | 838 } |
735 } | 839 } |
736 else if (tHeader.GetMessageType () == EPIDEMIC_TYPE_REPLY) | 840 else if (tHeader.GetMessageType () == TypeHeader::REPLY) |
737 { | 841 { |
738 NS_LOG_LOGIC ("Got a A reply from " << sender << " " << packet->GetUid () << " " << m_mainAddress); | 842 NS_LOG_LOGIC ("Got a A reply from " << sender << " " |
739 EpidemicSummaryVectorHeader packet_SMV; | 843 << packet->GetUid () << " " << m_mainA ddress); |
844 SummaryVectorHeader packet_SMV; | |
740 packet->RemoveHeader (packet_SMV); | 845 packet->RemoveHeader (packet_SMV); |
741 SendDisjointPackets (packet_SMV, sender); | 846 SendDisjointPackets (packet_SMV, sender); |
742 SendSummaryVector (sender,false); | 847 SendSummaryVector (sender,false); |
743 } | 848 } |
744 else if (tHeader.GetMessageType () == EPIDEMIC_TYPE_REPLY_BACK) | 849 else if (tHeader.GetMessageType () == TypeHeader::REPLY_BACK) |
745 { | 850 { |
746 NS_LOG_LOGIC ("Got a A reply back from " << sender << " " << packet->GetUi d () << " " << m_mainAddress); | 851 NS_LOG_LOGIC ("Got a A reply back from " << sender |
747 EpidemicSummaryVectorHeader packet_SMV; | 852 << " " << packet->GetUid () << " " << m_mainAddress); |
853 SummaryVectorHeader packet_SMV; | |
748 packet->RemoveHeader (packet_SMV); | 854 packet->RemoveHeader (packet_SMV); |
749 SendDisjointPackets (packet_SMV, sender); | 855 SendDisjointPackets (packet_SMV, sender); |
750 | 856 |
751 } | 857 } |
858 else | |
859 { | |
860 NS_LOG_LOGIC ("Unknown MessageType packet "); | |
861 } | |
752 } | 862 } |
753 } //end namespace epidemic | 863 } //end namespace epidemic |
754 } //end namespace ns3 | 864 } //end namespace ns3 |
LEFT | RIGHT |