A Discrete-Event Network Simulator
API
wsn_node.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 
3 /*
4 * Copyright (c) 2020 DLTLT
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation;
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Corresponding author: Niki Hrovatin <niki.hrovatin@famnit.upr.si>
20 */
21 
22 #include "wsn_node.h"
23 #include "ns3/trace-source-accessor.h"
24 #include "ns3/traced-value.h"
25 #include "ns3/object.h"
26 #include "ns3/uinteger.h"
27 
28 NS_LOG_COMPONENT_DEFINE ("Wsn_node");
29 
30 namespace ns3 {
31 
32 //Zagotovi, da se registrira TypeId
33 NS_OBJECT_ENSURE_REGISTERED (Wsn_node);
34 
35 TypeId
37 {
38  static TypeId tid =
39  TypeId ("ns3::Wsn_node")
40  .SetParent<Application> ()
41  .AddConstructor<Wsn_node> ()
42  .AddAttribute ("ListenerPort", "Port on which we listen for incoming packets.",
43  TypeId::ATTR_CONSTRUCT | TypeId::ATTR_SET | TypeId::ATTR_GET,
44  UintegerValue (4242), MakeUintegerAccessor (&Wsn_node::m_port),
45  MakeUintegerChecker<uint16_t> ())
46  .AddAttribute ("OutputManager", "Manage the output of the simulation", PointerValue (0),
47  MakePointerAccessor (&Wsn_node::m_outputManager),
48  MakePointerChecker<OutputManager> ())
49  .AddAttribute ("OnionValidator", "Manage onions and when to abort them", PointerValue (0),
50  MakePointerAccessor (&Wsn_node::m_onionValidator),
51  MakePointerChecker<OnionValidator> ())
52  .AddAttribute ("Delay", "Starting delay of sensor nodes, delay is given in milliseconds",
53  TypeId::ATTR_CONSTRUCT | TypeId::ATTR_SET | TypeId::ATTR_GET,
54  UintegerValue (200), MakeUintegerAccessor (&Wsn_node::m_delay),
55  MakeUintegerChecker<uint16_t> ())
56  .AddAttribute ("MSS", "Maximum segment size",
57  TypeId::ATTR_CONSTRUCT | TypeId::ATTR_SET | TypeId::ATTR_GET,
58  UintegerValue (536), MakeUintegerAccessor (&Wsn_node::f_mss),
59  MakeUintegerChecker<uint16_t> ())
60  .AddAttribute ("OnionTimeout",
61  "A watchdog timer set to abort onion messagess, if the timer elepses "
62  "before the onion returns back to the sink node",
63  TypeId::ATTR_CONSTRUCT | TypeId::ATTR_SET | TypeId::ATTR_GET,
64  UintegerValue (100), MakeUintegerAccessor (&Wsn_node::m_onionTimeout),
65  MakeUintegerChecker<uint16_t> ())
66  .AddTraceSource ("AppTx", "Packet transmitted",
67  MakeTraceSourceAccessor (&Wsn_node::m_appTx),
68  "ns3::TracedValueCallback::Packet")
69  .AddTraceSource ("AppRx", "Packet received", MakeTraceSourceAccessor (&Wsn_node::m_appRx),
70  "ns3::TracedValueCallback::Packet");
71 
72  return tid;
73 }
74 
75 void
76 Wsn_node::NotifyTx (Ptr<const Packet> packet)
77 {
78  m_appTx (packet);
79 }
80 
81 void
82 Wsn_node::NotifyRx (Ptr<const Packet> packet)
83 {
84  m_appRx (packet);
85 }
86 
88 {
89 }
90 
91 void
93 {
94 }
95 
96 void
98 {
99 }
100 
101 void
103 {
104  //Get the address of the node on which the application is running
105  Ptr<Node> PtrNode = this->GetNode ();
106  Ptr<Ipv4> ipv4 = PtrNode->GetObject<Ipv4> ();
107  Ipv4InterfaceAddress iaddr = ipv4->GetAddress (1, 0);
108  Ipv4Address address = iaddr.GetLocal ();
109  m_address = address;
110 
111  m_socket = Socket::CreateSocket (GetNode (), TcpSocketFactory::GetTypeId ());
112  InetSocketAddress local (Ipv4Address::GetAny (), m_port);
113  m_socket->SetIpRecvTtl (true);
114  m_socket->Bind (local);
115  m_socket->Listen ();
116 
117  //Cordinates of the node
118  Ptr<MobilityModel> mob = PtrNode->GetObject<MobilityModel> ();
119  double coord_x = mob->GetPosition ().x;
120  double coord_y = mob->GetPosition ().y;
121 
122  //if the routing is OLSR we can print out the number of one-hop neighbours from routing info
123  if (m_outputManager->GetRouting () == Routing::OLSR)
124  {
125  //need to wait some seconds till routes are stable
126  Simulator::Schedule (Seconds (5), &Wsn_node::NodeDegree, this, coord_x, coord_y);
127  }
128  else
129  {
130  m_outputManager->AddNodeDetails (m_address, coord_x, coord_y);
131  }
132 }
133 
134 void
136 {
137 
138  Ptr<Node> PtrNode = this->GetNode ();
139  Ptr<NetDevice> device = PtrNode->GetDevice (0);
140  Ptr<WifiNetDevice> wifiDevice = DynamicCast<WifiNetDevice> (device);
141 
142  wifiDevice->GetPhy ()->SetOffMode ();
143 }
144 
145 void
147 {
148  Ptr<Node> PtrNode = this->GetNode ();
149  Ptr<NetDevice> device = PtrNode->GetDevice (0);
150  Ptr<WifiNetDevice> wifiDevice = DynamicCast<WifiNetDevice> (device);
151 
152  wifiDevice->GetPhy ()->ResumeFromOff ();
153 }
154 
155 /*
156 *
157 * print out the number of one hop-neighbours of a node
158 * only valid in OLSR, for other routing you need to define another method
159 *
160 */
161 
162 void
163 Wsn_node::NodeDegree (double coord_x, double coord_y)
164 {
165 
166  Ptr<Node> PtrNode = this->GetNode ();
168  Ptr<Ipv4> ipv4Ptr = PtrNode->GetObject<Ipv4> ();
169  Ptr<Ipv4RoutingProtocol> ipv4Routing = ipv4Ptr->GetRoutingProtocol ();
170  // Obtain the Ipv4ListRouting interface
171  Ptr<Ipv4ListRouting> ipv4ListRouting = DynamicCast<Ipv4ListRouting> (ipv4Routing);
172  // A list routing protocol has multiple Ipv4Routing protocols. Find OLSR in this list
173  Ptr<olsr::RoutingProtocol> olsrProtocol;
174  int16_t priority = 10;
175  for (uint32_t i = 0; i < ipv4ListRouting->GetNRoutingProtocols (); i++)
176  {
177  Ptr<Ipv4RoutingProtocol> proto = ipv4ListRouting->GetRoutingProtocol (0, priority);
178  olsrProtocol = DynamicCast<olsr::RoutingProtocol> (proto);
179  }
180  if (olsrProtocol != 0)
181  {
182  //break; // found the protocol we are looking for
183  }
184  else
185  {
186  NS_ASSERT_MSG (olsrProtocol, "Didn't find OLSR on this node");
187  }
188 
189  ns3::olsr::OlsrState state = olsrProtocol->GetOlsrState ();
190  ns3::olsr::NeighborSet n_set = state.GetNeighbors ();
191 
192  m_outputManager->AddNodeDetails (m_address, coord_x, coord_y, n_set.size ());
193 }
194 
195 //calculate when the node will start based on the given address
196 //allow nodes to start sequentially
197 uint32_t
198 Wsn_node::getNodeDelay (Ipv4Address node_address)
199 {
200  //Fixed delay
201  Ipv4Address net_address;
202  net_address.Set ("10.1.0.0");
203  uint32_t delay = node_address.Get () - net_address.Get ();
204  delay = delay * m_delay; //each node starts consequently after delay_ms
205 
206  return delay;
207 }
208 
209 /*
210 * Send a packet as a TCP segment to the remote node
211 * add a tag to the packet, which defines the size of the whole packet
212 * segment size is limited by MSS
213 * The packet if to large is automatically splitted in many segments
214 */
215 
216 void
217 Wsn_node::SendSegment (InetSocketAddress remote, Ptr<Packet> packet, bool b_onion)
218 {
219 
220  Ptr<Socket> socket = Socket::CreateSocket (GetNode (), TcpSocketFactory::GetTypeId ());
221  socket->Connect (remote);
222 
223  int pack_size = packet->GetSize ();
224  int seg_num = pack_size / f_mss;
225 
226  if (b_onion)
227  {
228  this->o_hopCount = m_onionValidator->OnionHopCount ();
229  Simulator::Schedule (Seconds (m_onionTimeout), &Wsn_node::CheckSentOnion, this,
230  this->o_hopCount);
231  }
232 
233  if (seg_num == 0)
234  { //packet fits in segment size, just send-it
235  socket->Send (packet);
236  }
237  else
238  {
239 
240  SegmentNum s_num (pack_size);
241  packet->AddByteTag (s_num);
242  socket->Send (packet);
243  }
244 }
245 
246 //receive a segment
247 
248 Ptr<Packet>
249 Wsn_node::RecvSegment (Ptr<Socket> socket)
250 {
251  Address from;
252  Ptr<Packet> p = Create<Packet> ();
253 
254  p = socket->RecvFrom (from);
255 
256  return RecvSeg (socket, p, from);
257 }
258 
259 //receive segment and store the sender's address
260 
261 Ptr<Packet>
262 Wsn_node::RecvSegment (Ptr<Socket> socket, Address &from)
263 {
264  Ptr<Packet> p = Create<Packet> ();
265  p = socket->RecvFrom (from);
266 
267  return RecvSeg (socket, p, from);
268 }
269 
270 /*
271 * Receive a segment
272 * The tag represents the size of the whole packet to be received
273 * merge parts into the whole packet
274 * pending_packet is the buffer where you aggregate segments
275 *
276 */
277 
278 Ptr<Packet>
279 Wsn_node::RecvSeg (Ptr<Socket> socket, Ptr<Packet> p, Address from)
280 {
281 
282  InetSocketAddress from_address = InetSocketAddress::ConvertFrom (from);
283 
284  SegmentNum s_num;
285  //if only one packet
286  if (!p->FindFirstMatchingByteTag (s_num))
287  {
288  //tag not found
289  socket->Close ();
290  return p;
291  }
292 
293  if (f_pendingPacket == NULL || from_address.GetIpv4 ().Get () != f_receivingAddress.Get ())
294  {
295  f_receivingAddress = from_address.GetIpv4 (); //Ipv4Address(0);
296  f_pendingPacket = Create<Packet> ();
297  f_segmentSize = s_num.GetSegNum ();
298  }
299  f_pendingPacket->AddAtEnd (p);
300 
301  f_segmentSize = f_segmentSize - p->GetSize ();
302 
303  if (f_segmentSize == 0)
304  {
305  p = f_pendingPacket->Copy ();
306  f_receivingAddress = Ipv4Address::GetAny ();
307  f_pendingPacket = NULL;
308  socket->Close ();
309  return p;
310  }
311  else
312  {
313  return NULL;
314  }
315 }
316 
318 
319 //Check onion sending
320 void
322 {
323  if (!m_onionValidator->CheckOnionReceived (count))
324  {
325  m_outputManager->AbortOnion (Simulator::Now ());
326  }
327 }
328 
329 //Signal, that the whole onion was received
330 void
332 {
333  m_onionValidator->OnionReceived ();
334 }
335 
336 } // namespace ns3
ns3::Wsn_node::f_mss
uint16_t f_mss
maximum segment size
Definition: wsn_node.h:233
ns3::Wsn_node::f_segmentSize
int f_segmentSize
the size of the whole packet
Definition: wsn_node.h:234
ns3::Wsn_node::m_socket
Ptr< Socket > m_socket
listening socket
Definition: wsn_node.h:228
ns3::SegmentNum::GetSegNum
uint32_t GetSegNum()
Definition: segmentnum.cc:60
ns3::OLSR
@ OLSR
Optimized Link State Routing Protocol ns3::Olsr.
Definition: enums.h:44
ns3::Wsn_node::RecvSegment
Ptr< Packet > RecvSegment(Ptr< Socket > socket)
method for receiving a segment calls ns3::Wsn_node::RecvSeg()
Definition: wsn_node.cc:249
ns3::Wsn_node::o_hopCount
int o_hopCount
track how the onion is is transiting in the network
Definition: wsn_node.h:240
ns3
Definition: sensornode-helper.cc:26
ns3::Wsn_node::SendSegment
void SendSegment(InetSocketAddress remote, Ptr< Packet > packet, bool b_onion)
Send a packet through a TCP connection to the remote address.
Definition: wsn_node.cc:217
ns3::SegmentNum
Class for adding a tag to packets used to track different segments of packets packets are fragmented ...
Definition: segmentnum.h:41
ns3::Wsn_node::getNodeDelay
uint32_t getNodeDelay(Ipv4Address node_address)
compute when the node should start the handshake process from the node ip address Used to not generat...
Definition: wsn_node.cc:198
ns3::Wsn_node::m_onionValidator
Ptr< OnionValidator > m_onionValidator
Pointer to the ns3::OnionValidator.
Definition: wsn_node.h:226
ns3::Wsn_node::m_appTx
TracedCallback< Ptr< const Packet > > m_appTx
traced callback for packet transmission
Definition: wsn_node.h:247
ns3::Wsn_node::m_address
Ipv4Address m_address
ns3::Ipv4Address of this node
Definition: wsn_node.h:227
ns3::Wsn_node::f_pendingPacket
Ptr< Packet > f_pendingPacket
pointer to the packet where received segment fragments are stored
Definition: wsn_node.h:236
wsn_node.h
ns3::Wsn_node::StopApplication
virtual void StopApplication(void)
Stop the application.
Definition: wsn_node.cc:97
ns3::Wsn_node::m_onionTimeout
uint16_t m_onionTimeout
timer in seconds, if elepsed and the onion was not recieved by the next receiver, then delete the oni...
Definition: wsn_node.h:244
ns3::Wsn_node::NotifyRx
void NotifyRx(Ptr< const Packet > packet)
Call to signal the receipt of a Packet at the application layer.
Definition: wsn_node.cc:82
ns3::Wsn_node::m_outputManager
Ptr< OutputManager > m_outputManager
Pointer to the ns3::OutputManager.
Definition: wsn_node.h:225
ns3::Wsn_node::DisableNode
void DisableNode()
The method disables the node.
Definition: wsn_node.cc:135
ns3::Wsn_node::CheckSentOnion
void CheckSentOnion(int count)
The method checks if the onion was received by the next receiving node If the onion was not received ...
Definition: wsn_node.cc:321
ns3::Wsn_node::m_appRx
TracedCallback< Ptr< const Packet > > m_appRx
traced callback for packet receipt
Definition: wsn_node.h:248
ns3::Wsn_node::m_port
uint16_t m_port
port of the application
Definition: wsn_node.h:224
ns3::Wsn_node::ActivateNode
void ActivateNode()
The method re-activates a node that was disabled using the DisableNode() method.
Definition: wsn_node.cc:146
ns3::Wsn_node::StartApplication
virtual void StartApplication(void)
Start the application.
Definition: wsn_node.cc:92
ns3::Wsn_node::GetTypeId
static TypeId GetTypeId(void)
Register this type.
Definition: wsn_node.cc:36
ns3::Wsn_node::Wsn_node
Wsn_node()
Default constructor.
Definition: wsn_node.cc:87
ns3::Wsn_node::NotifyTx
void NotifyTx(Ptr< const Packet > packet)
Call to signal the transmission of a Packet at the application layer.
Definition: wsn_node.cc:76
ns3::Wsn_node::Configure
void Configure(void)
1.
Definition: wsn_node.cc:102
ns3::Wsn_node::OnionReceived
void OnionReceived(void)
Signal to the ns3::OnionValidator that the onion was corrctly received.
Definition: wsn_node.cc:331
ns3::Wsn_node::NodeDegree
void NodeDegree(double coord_x, double coord_y)
Retrieve the degreee this node from the OLSR routing protocol.
Definition: wsn_node.cc:163
ns3::Wsn_node::RecvSeg
Ptr< Packet > RecvSeg(Ptr< Socket > socket, Ptr< Packet > p, Address from)
method for receiving packets able to merge segment fragments if a packet was split into multiple segm...
Definition: wsn_node.cc:279
ns3::Wsn_node::f_receivingAddress
Ipv4Address f_receivingAddress
the receiving address
Definition: wsn_node.h:237
ns3::Wsn_node::m_delay
uint16_t m_delay
delay after which the handshake process will start
Definition: wsn_node.h:229