View | Details | Raw Unified | Return to bug 2882
Collapse All | Expand All

(-)a/src/uan/examples/uan-6lowpan-example.cc (+271 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License version 2 as
6
 * published by the Free Software Foundation;
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program; if not, write to the Free Software
15
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 *
17
 * Author: Hossam Khader <hossamkhader@gmail.com>
18
 */
19
20
#include "ns3/core-module.h"
21
#include "ns3/internet-module.h"
22
#include "ns3/node-container.h"
23
#include "ns3/mobility-helper.h"
24
#include "ns3/mobility-model.h"
25
#include "ns3/basic-energy-source-helper.h"
26
#include "ns3/energy-source-container.h"
27
#include "ns3/uan-helper.h"
28
#include "ns3/uan-channel.h"
29
#include "ns3/acoustic-modem-energy-model-helper.h"
30
#include "ns3/sixlowpan-helper.h"
31
#include "ns3/sixlowpan-net-device.h"
32
33
using namespace ns3;
34
35
/**
36
 *
37
 * This example shows the usage of UDP over 6LoWPAN to transfer data.
38
 * Two nodes are sending their remaining energy percentage (1 byte)
39
 * to a gateway node, that prints the received data.
40
 * The transmissions are scheduled at random times to avoid collisions
41
 *
42
 */
43
44
NS_LOG_COMPONENT_DEFINE ("Uan6lowpanExample");
45
46
47
class UanExperiment
48
{
49
public:
50
  UanExperiment ();
51
52
  /**
53
   * Set the UAN nodes position
54
   */
55
  void SetupPositions ();
56
57
  /**
58
   * Set the UAN nodes energy
59
   */
60
  void SetupEnergy ();
61
62
  /**
63
   * Set the UAN nodes communication channels
64
   */
65
  void SetupCommunications ();
66
67
  /**
68
   * Set the UAN nodes communication channels
69
   */
70
  void SetupApplications ();
71
72
  /**
73
   * Send a packet from all the nodes
74
   */
75
  void SendPackets ();
76
77
  /**
78
   * Send a packet from one of the nodes
79
   * \param sock The sending socket
80
   * \param pkt The packet
81
   * \param dst the destination
82
   */
83
  void SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv6Address dst);
84
85
  void PrintReceivedPacket (Ptr<Socket> socket);
86
87
  /**
88
   * Prepare the experiment
89
   */
90
  void Prepare ();
91
92
  /**
93
   * Teardown the experiment
94
   */
95
  void Teardown ();
96
97
private:
98
  NodeContainer m_nodes; //!< UAN nodes
99
  std::map<Ptr<Node>, Ptr<Socket> > m_sockets; //!< send and receive sockets
100
};
101
102
103
UanExperiment::UanExperiment ()
104
{
105
}
106
107
void
108
UanExperiment::SetupPositions ()
109
{
110
  MobilityHelper mobilityHelper;
111
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
112
  mobilityHelper.Install (m_nodes);
113
  m_nodes.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
114
  m_nodes.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
115
  m_nodes.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
116
}
117
118
void
119
UanExperiment::SetupEnergy ()
120
{
121
  BasicEnergySourceHelper energySourceHelper;
122
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
123
  energySourceHelper.Install (m_nodes);
124
}
125
126
void
127
UanExperiment::SetupCommunications ()
128
{
129
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
130
  UanHelper uanHelper;
131
  NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel);
132
  EnergySourceContainer energySourceContainer;
133
  NodeContainer::Iterator node = m_nodes.Begin ();
134
  while (node != m_nodes.End ())
135
    {
136
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
137
      node++;
138
    }
139
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
140
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
141
142
  SixLowPanHelper sixLowPanHelper;
143
  NetDeviceContainer sixlowpanNetDevices = sixLowPanHelper.Install (netDeviceContainer);
144
145
  InternetStackHelper internetStackHelper;
146
  internetStackHelper.Install (m_nodes);
147
148
  Ipv6AddressHelper ipv6AddressHelper;
149
  ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64));
150
  ipv6AddressHelper.Assign (sixlowpanNetDevices);
151
152
  node = m_nodes.Begin ();
153
  while (node != m_nodes.End ())
154
    {
155
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
156
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600)));
157
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000)));
158
      node++;
159
    }
160
}
161
162
void
163
UanExperiment::PrintReceivedPacket (Ptr<Socket> socket)
164
{
165
  Address srcAddress;
166
  while (socket->GetRxAvailable () > 0)
167
    {
168
      Ptr<Packet> packet = socket->RecvFrom (srcAddress);
169
      uint8_t energyReading;
170
      packet->CopyData (&energyReading, 1);
171
172
      if(Inet6SocketAddress::IsMatchingType (srcAddress))
173
        {
174
          std::cout << "Time: " << Simulator::Now ().GetDays () << " | " << "Node: " <<
175
            Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " <<
176
            +energyReading << "%" << std::endl;
177
178
          NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetDays () << " | Node: " <<
179
                          Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " <<
180
                          +energyReading << "%");
181
        }
182
    }
183
}
184
185
void
186
UanExperiment::SetupApplications ()
187
{
188
  NodeContainer::Iterator node = m_nodes.Begin ();
189
  while (node != m_nodes.End ())
190
    {
191
      m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory"));
192
      if((*node)->GetObject<Ipv6> () != NULL)
193
        {
194
          Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), 9);
195
          m_sockets[*node]->Bind (ipv6_local);
196
          std::cout << "socket created" << std::endl;
197
        }
198
199
      m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this));
200
      node++;
201
    }
202
}
203
204
void
205
UanExperiment::SendPackets ()
206
{
207
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
208
209
  NodeContainer::Iterator node = m_nodes.Begin ();
210
  Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress ();
211
  node++;
212
  while (node != m_nodes.End ())
213
    {
214
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
215
216
      Ptr<Packet> pkt = Create<Packet> (&energy, 1);
217
218
      double time = uniformRandomVariable->GetValue (0, 60);
219
      Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
220
      node++;
221
    }
222
  Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this);
223
}
224
225
void
226
UanExperiment::SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv6Address dst)
227
{
228
  std::cout << Simulator::Now ().As (Time::MIN) << " packet sent to " << dst << std::endl;
229
  Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), 9);
230
  m_sockets[node]->SendTo (pkt, 0, ipv6_destination);
231
}
232
233
void
234
UanExperiment::Prepare ()
235
{
236
  m_nodes.Create (3);
237
  SetupPositions ();
238
  SetupEnergy ();
239
  SetupCommunications ();
240
  SetupApplications ();
241
  SendPackets ();
242
}
243
244
void
245
UanExperiment::Teardown ()
246
{
247
  std::map<Ptr<Node>, Ptr<Socket> >::iterator socket;
248
249
  for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++)
250
    {
251
      socket->second->Close ();
252
    }
253
}
254
255
int
256
main (int argc, char *argv[])
257
{
258
  CommandLine cmd;
259
  cmd.Parse (argc, argv);
260
261
  UanExperiment experiment;
262
  experiment.Prepare ();
263
264
  Simulator::Stop (Days (50));
265
  Simulator::Run ();
266
  Simulator::Destroy ();
267
268
  experiment.Teardown ();
269
270
  return 0;
271
}
(-)a/src/uan/examples/uan-ipv4-example.cc (+263 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License version 2 as
6
 * published by the Free Software Foundation;
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program; if not, write to the Free Software
15
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 *
17
 * Author: Hossam Khader <hossamkhader@gmail.com>
18
 */
19
20
#include "ns3/core-module.h"
21
#include "ns3/internet-module.h"
22
#include "ns3/node-container.h"
23
#include "ns3/mobility-helper.h"
24
#include "ns3/mobility-model.h"
25
#include "ns3/basic-energy-source-helper.h"
26
#include "ns3/energy-source-container.h"
27
#include "ns3/uan-helper.h"
28
#include "ns3/uan-channel.h"
29
#include "ns3/acoustic-modem-energy-model-helper.h"
30
31
using namespace ns3;
32
33
/**
34
 *
35
 * This example shows the usage of UDP over IPv4 to transfer data.
36
 * Two nodes are sending their remaining energy percentage (1 byte)
37
 * to a gateway node, that prints the received data.
38
 * The transmissions are scheduled at random times to avoid collisions
39
 *
40
 */
41
42
NS_LOG_COMPONENT_DEFINE ("UanIpv4Example");
43
44
45
class UanExperiment
46
{
47
public:
48
  UanExperiment ();
49
50
  /**
51
   * Set the UAN nodes position
52
   */
53
  void SetupPositions ();
54
55
  /**
56
   * Set the UAN nodes energy
57
   */
58
  void SetupEnergy ();
59
60
  /**
61
   * Set the UAN nodes communication channels
62
   */
63
  void SetupCommunications ();
64
65
  /**
66
   * Set the UAN nodes communication channels
67
   */
68
  void SetupApplications ();
69
70
  /**
71
   * Send a packet from all the nodes
72
   */
73
  void SendPackets ();
74
75
  /**
76
   * Send a packet from one of the nodes
77
   * \param sock The sending socket
78
   * \param pkt The packet
79
   * \param dst the destination
80
   */
81
  void SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv4Address dst);
82
83
  void PrintReceivedPacket (Ptr<Socket> socket);
84
85
  /**
86
   * Prepare the experiment
87
   */
88
  void Prepare ();
89
90
  /**
91
   * Teardown the experiment
92
   */
93
  void Teardown ();
94
95
private:
96
  NodeContainer m_nodes; //!< UAN nodes
97
  std::map<Ptr<Node>, Ptr<Socket> > m_sockets; //!< send and receive sockets
98
};
99
100
101
UanExperiment::UanExperiment ()
102
{
103
}
104
105
void
106
UanExperiment::SetupPositions ()
107
{
108
  MobilityHelper mobilityHelper;
109
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
110
  mobilityHelper.Install (m_nodes);
111
  m_nodes.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
112
  m_nodes.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
113
  m_nodes.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
114
}
115
116
void
117
UanExperiment::SetupEnergy ()
118
{
119
  BasicEnergySourceHelper energySourceHelper;
120
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
121
  energySourceHelper.Install (m_nodes);
122
}
123
124
void
125
UanExperiment::SetupCommunications ()
126
{
127
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
128
  UanHelper uanHelper;
129
  NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel);
130
  EnergySourceContainer energySourceContainer;
131
  NodeContainer::Iterator node = m_nodes.Begin ();
132
  while (node != m_nodes.End ())
133
    {
134
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
135
      node++;
136
    }
137
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
138
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
139
140
  InternetStackHelper internetStackHelper;
141
  internetStackHelper.Install (m_nodes);
142
143
  Ipv4AddressHelper ipv4AddressHelper;
144
  ipv4AddressHelper.SetBase ("10.0.0.0", "255.255.255.0");
145
  ipv4AddressHelper.Assign (netDeviceContainer);
146
  node = m_nodes.Begin ();
147
  while (node != m_nodes.End ())
148
    {
149
      (*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetArpCache ()->SetWaitReplyTimeout (Seconds (10));
150
      node++;
151
    }
152
}
153
154
void
155
UanExperiment::PrintReceivedPacket (Ptr<Socket> socket)
156
{
157
  Address srcAddress;
158
  while (socket->GetRxAvailable () > 0)
159
    {
160
      Ptr<Packet> packet = socket->RecvFrom (srcAddress);
161
      uint8_t energyReading;
162
      packet->CopyData (&energyReading, 1);
163
164
      if(InetSocketAddress::IsMatchingType (srcAddress))
165
        {
166
          std::cout << "Time: " << Simulator::Now ().GetDays () << " | " << "Node: " <<
167
            InetSocketAddress::ConvertFrom (srcAddress).GetIpv4 () << " | Energy: " <<
168
            +energyReading << "%" << std::endl;
169
170
          NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetDays () << " | Node: " <<
171
                          InetSocketAddress::ConvertFrom (srcAddress).GetIpv4 () << " | Energy: " <<
172
                          +energyReading << "%");
173
        }
174
    }
175
}
176
177
void
178
UanExperiment::SetupApplications ()
179
{
180
  NodeContainer::Iterator node = m_nodes.Begin ();
181
  while (node != m_nodes.End ())
182
    {
183
      m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory"));
184
      if((*node)->GetObject<Ipv4> () != NULL)
185
        {
186
          InetSocketAddress ipv4_local = InetSocketAddress (Ipv4Address::GetAny (), 9);
187
          m_sockets[*node]->Bind (ipv4_local);
188
          std::cout << "socket created" << std::endl;
189
        }
190
191
      m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this));
192
      node++;
193
    }
194
}
195
196
void
197
UanExperiment::SendPackets ()
198
{
199
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
200
201
  NodeContainer::Iterator node = m_nodes.Begin ();
202
  Ipv4Address dst = (*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetAddress (0).GetLocal ();
203
  node++;
204
  while (node != m_nodes.End ())
205
    {
206
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
207
208
      Ptr<Packet> pkt = Create<Packet> (&energy, 1);
209
210
      double time = uniformRandomVariable->GetValue (0, 60);
211
      Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
212
      node++;
213
    }
214
  Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this);
215
}
216
217
void
218
UanExperiment::SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv4Address dst)
219
{
220
  std::cout << Simulator::Now ().As (Time::MIN) << " packet sent to " << dst << std::endl;
221
  InetSocketAddress ipv4_destination = InetSocketAddress (dst, 9);
222
  m_sockets[node]->SendTo (pkt, 0, ipv4_destination);
223
}
224
225
void
226
UanExperiment::Prepare ()
227
{
228
  m_nodes.Create (3);
229
  SetupPositions ();
230
  SetupEnergy ();
231
  SetupCommunications ();
232
  SetupApplications ();
233
  SendPackets ();
234
}
235
236
void
237
UanExperiment::Teardown ()
238
{
239
  std::map<Ptr<Node>, Ptr<Socket> >::iterator socket;
240
241
  for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++)
242
    {
243
      socket->second->Close ();
244
    }
245
}
246
247
int
248
main (int argc, char *argv[])
249
{
250
  CommandLine cmd;
251
  cmd.Parse (argc, argv);
252
253
  UanExperiment experiment;
254
  experiment.Prepare ();
255
256
  Simulator::Stop (Days (50));
257
  Simulator::Run ();
258
  Simulator::Destroy ();
259
260
  experiment.Teardown ();
261
262
  return 0;
263
}
(-)a/src/uan/examples/uan-ipv6-example.cc (+268 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License version 2 as
6
 * published by the Free Software Foundation;
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program; if not, write to the Free Software
15
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 *
17
 * Author: Hossam Khader <hossamkhader@gmail.com>
18
 */
19
20
#include "ns3/core-module.h"
21
#include "ns3/internet-module.h"
22
#include "ns3/node-container.h"
23
#include "ns3/mobility-helper.h"
24
#include "ns3/mobility-model.h"
25
#include "ns3/basic-energy-source-helper.h"
26
#include "ns3/energy-source-container.h"
27
#include "ns3/uan-helper.h"
28
#include "ns3/uan-channel.h"
29
#include "ns3/acoustic-modem-energy-model-helper.h"
30
31
using namespace ns3;
32
33
/**
34
 *
35
 * This example shows the usage of UDP over IPv6 to transfer data.
36
 * Two nodes are sending their remaining energy percentage (1 byte)
37
 * to a gateway node, that prints the received data.
38
 * The transmissions are scheduled at random times to avoid collisions
39
 *
40
 */
41
42
NS_LOG_COMPONENT_DEFINE ("UanIpv6Example");
43
44
45
class UanExperiment
46
{
47
public:
48
  UanExperiment ();
49
50
  /**
51
   * Set the UAN nodes position
52
   */
53
  void SetupPositions ();
54
55
  /**
56
   * Set the UAN nodes energy
57
   */
58
  void SetupEnergy ();
59
60
  /**
61
   * Set the UAN nodes communication channels
62
   */
63
  void SetupCommunications ();
64
65
  /**
66
   * Set the UAN nodes communication channels
67
   */
68
  void SetupApplications ();
69
70
  /**
71
   * Send a packet from all the nodes
72
   */
73
  void SendPackets ();
74
75
  /**
76
   * Send a packet from one of the nodes
77
   * \param sock The sending socket
78
   * \param pkt The packet
79
   * \param dst the destination
80
   */
81
  void SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv6Address dst);
82
83
  void PrintReceivedPacket (Ptr<Socket> socket);
84
85
  /**
86
   * Prepare the experiment
87
   */
88
  void Prepare ();
89
90
  /**
91
   * Teardown the experiment
92
   */
93
  void Teardown ();
94
95
private:
96
  NodeContainer m_nodes; //!< UAN nodes
97
  std::map<Ptr<Node>, Ptr<Socket> > m_sockets; //!< send and receive sockets
98
};
99
100
101
UanExperiment::UanExperiment ()
102
{
103
}
104
105
void
106
UanExperiment::SetupPositions ()
107
{
108
  MobilityHelper mobilityHelper;
109
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
110
  mobilityHelper.Install (m_nodes);
111
  m_nodes.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
112
  m_nodes.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
113
  m_nodes.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
114
}
115
116
void
117
UanExperiment::SetupEnergy ()
118
{
119
  BasicEnergySourceHelper energySourceHelper;
120
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
121
  energySourceHelper.Install (m_nodes);
122
}
123
124
void
125
UanExperiment::SetupCommunications ()
126
{
127
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
128
  UanHelper uanHelper;
129
  NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel);
130
  EnergySourceContainer energySourceContainer;
131
  NodeContainer::Iterator node = m_nodes.Begin ();
132
  while (node != m_nodes.End ())
133
    {
134
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
135
      node++;
136
    }
137
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
138
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
139
140
  InternetStackHelper internetStackHelper;
141
  internetStackHelper.Install (m_nodes);
142
143
  Ipv6AddressHelper ipv6AddressHelper;
144
  ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64));
145
  ipv6AddressHelper.Assign (netDeviceContainer);
146
147
  node = m_nodes.Begin ();
148
  while (node != m_nodes.End ())
149
    {
150
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
151
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600)));
152
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000)));
153
      node++;
154
    }
155
}
156
157
void
158
UanExperiment::PrintReceivedPacket (Ptr<Socket> socket)
159
{
160
  std::cout << "packet received" << std::endl;
161
162
  Address srcAddress;
163
  while (socket->GetRxAvailable () > 0)
164
    {
165
      Ptr<Packet> packet = socket->RecvFrom (srcAddress);
166
      uint8_t energyReading;
167
      packet->CopyData (&energyReading, 1);
168
169
      if(Inet6SocketAddress::IsMatchingType (srcAddress))
170
        {
171
          std::cout << "Time: " << Simulator::Now ().GetDays () << " | " << "Node: " <<
172
            Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " <<
173
            +energyReading << "%" << std::endl;
174
175
          NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetDays () << " | Node: " <<
176
                          Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " <<
177
                          +energyReading << "%");
178
        }
179
    }
180
}
181
182
void
183
UanExperiment::SetupApplications ()
184
{
185
  NodeContainer::Iterator node = m_nodes.Begin ();
186
  while (node != m_nodes.End ())
187
    {
188
      m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory"));
189
      if((*node)->GetObject<Ipv6> () != NULL)
190
        {
191
          Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), 9);
192
          m_sockets[*node]->Bind (ipv6_local);
193
          std::cout << "socket created" << std::endl;
194
        }
195
196
      m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this));
197
      node++;
198
    }
199
}
200
201
void
202
UanExperiment::SendPackets ()
203
{
204
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
205
206
  NodeContainer::Iterator node = m_nodes.Begin ();
207
  Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress ();
208
  node++;
209
  while (node != m_nodes.End ())
210
    {
211
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
212
213
      Ptr<Packet> pkt = Create<Packet> (&energy, 1);
214
215
      double time = uniformRandomVariable->GetValue (0, 60);
216
      Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
217
      node++;
218
    }
219
  Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this);
220
}
221
222
void
223
UanExperiment::SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv6Address dst)
224
{
225
  std::cout << Simulator::Now ().As (Time::MIN) << " packet sent to " << dst << std::endl;
226
  Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), 9);
227
  m_sockets[node]->SendTo (pkt, 0, ipv6_destination);
228
}
229
230
void
231
UanExperiment::Prepare ()
232
{
233
  m_nodes.Create (3);
234
  SetupPositions ();
235
  SetupEnergy ();
236
  SetupCommunications ();
237
  SetupApplications ();
238
  SendPackets ();
239
}
240
241
void
242
UanExperiment::Teardown ()
243
{
244
  std::map<Ptr<Node>, Ptr<Socket> >::iterator socket;
245
246
  for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++)
247
    {
248
      socket->second->Close ();
249
    }
250
}
251
252
int
253
main (int argc, char *argv[])
254
{
255
  CommandLine cmd;
256
  cmd.Parse (argc, argv);
257
258
  UanExperiment experiment;
259
  experiment.Prepare ();
260
261
  Simulator::Stop (Days (50));
262
  Simulator::Run ();
263
  Simulator::Destroy ();
264
265
  experiment.Teardown ();
266
267
  return 0;
268
}
(-)a/src/uan/examples/uan-raw-example.cc (+257 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License version 2 as
6
 * published by the Free Software Foundation;
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program; if not, write to the Free Software
15
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 *
17
 * Author: Hossam Khader <hossamkhader@gmail.com>
18
 */
19
20
#include "ns3/core-module.h"
21
#include "ns3/internet-module.h"
22
#include "ns3/node-container.h"
23
#include "ns3/mobility-helper.h"
24
#include "ns3/mobility-model.h"
25
#include "ns3/basic-energy-source-helper.h"
26
#include "ns3/energy-source-container.h"
27
#include "ns3/uan-helper.h"
28
#include "ns3/uan-channel.h"
29
#include "ns3/acoustic-modem-energy-model-helper.h"
30
#include "ns3/packet-socket-helper.h"
31
#include "ns3/packet-socket-address.h"
32
33
using namespace ns3;
34
35
/**
36
 *
37
 * This example shows the usage of raw packets transfer data.
38
 * Two nodes are sending their remaining energy percentage (1 byte)
39
 * to a gateway node, that prints the received data.
40
 * The transmissions are scheduled at random times to avoid collisions
41
 *
42
 */
43
44
NS_LOG_COMPONENT_DEFINE ("UanRawExample");
45
46
47
class UanExperiment
48
{
49
public:
50
  UanExperiment ();
51
52
  /**
53
   * Set the UAN nodes position
54
   */
55
  void SetupPositions ();
56
57
  /**
58
   * Set the UAN nodes energy
59
   */
60
  void SetupEnergy ();
61
62
  /**
63
   * Set the UAN nodes communication channels
64
   */
65
  void SetupCommunications ();
66
67
  /**
68
   * Set the UAN nodes communication channels
69
   */
70
  void SetupApplications ();
71
72
  /**
73
   * Send a packet from all the nodes
74
   */
75
  void SendPackets ();
76
77
  /**
78
   * Send a packet from one of the nodes
79
   * \param sock The sending socket
80
   * \param pkt The packet
81
   * \param dst the destination
82
   */
83
  void SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Mac8Address dst);
84
85
  void PrintReceivedPacket (Ptr<Socket> socket);
86
87
  /**
88
   * Prepare the experiment
89
   */
90
  void Prepare ();
91
92
  /**
93
   * Teardown the experiment
94
   */
95
  void Teardown ();
96
97
private:
98
  NodeContainer m_nodes; //!< UAN nodes
99
  std::map<Ptr<Node>, Ptr<Socket> > m_sockets; //!< send and receive sockets
100
};
101
102
103
UanExperiment::UanExperiment ()
104
{
105
}
106
107
void
108
UanExperiment::SetupPositions ()
109
{
110
  MobilityHelper mobilityHelper;
111
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
112
  mobilityHelper.Install (m_nodes);
113
  m_nodes.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
114
  m_nodes.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
115
  m_nodes.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
116
}
117
118
void
119
UanExperiment::SetupEnergy ()
120
{
121
  BasicEnergySourceHelper energySourceHelper;
122
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
123
  energySourceHelper.Install (m_nodes);
124
}
125
126
void
127
UanExperiment::SetupCommunications ()
128
{
129
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
130
  UanHelper uanHelper;
131
  NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel);
132
  EnergySourceContainer energySourceContainer;
133
  NodeContainer::Iterator node = m_nodes.Begin ();
134
  while (node != m_nodes.End ())
135
    {
136
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
137
      node++;
138
    }
139
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
140
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
141
}
142
143
void
144
UanExperiment::PrintReceivedPacket (Ptr<Socket> socket)
145
{
146
  Address srcAddress;
147
  while (socket->GetRxAvailable () > 0)
148
    {
149
      Ptr<Packet> packet = socket->RecvFrom (srcAddress);
150
      PacketSocketAddress packetSocketAddress = PacketSocketAddress::ConvertFrom (srcAddress);
151
      srcAddress = packetSocketAddress.GetPhysicalAddress ();
152
      uint8_t energyReading;
153
      packet->CopyData (&energyReading, 1);
154
155
      if(Mac8Address::IsMatchingType (srcAddress))
156
        {
157
          std::cout << "Time: " << Simulator::Now ().GetDays () << " | " << "Node: " <<
158
            Mac8Address::ConvertFrom (srcAddress) << " | Energy: " <<
159
            +energyReading << "%" << std::endl;
160
161
          NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetDays () << " | Node: " <<
162
                          Mac8Address::ConvertFrom (srcAddress) << " | Energy: " <<
163
                          +energyReading << "%");
164
        }
165
    }
166
}
167
168
void
169
UanExperiment::SetupApplications ()
170
{
171
  NodeContainer::Iterator node = m_nodes.Begin ();
172
  PacketSocketHelper packetSocketHelper;
173
  while (node != m_nodes.End ())
174
    {
175
      packetSocketHelper.Install (*node);
176
      PacketSocketAddress socketAddress;
177
      socketAddress.SetSingleDevice ((*node)->GetDevice (0)->GetIfIndex ());
178
      socketAddress.SetProtocol (0);
179
      m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::PacketSocketFactory"));
180
      m_sockets[*node]->Bind ();
181
      m_sockets[*node]->Connect (socketAddress);
182
      m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this));
183
      node++;
184
    }
185
}
186
187
void
188
UanExperiment::SendPackets ()
189
{
190
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
191
192
  NodeContainer::Iterator node = m_nodes.Begin ();
193
  Mac8Address dst = Mac8Address::ConvertFrom ((*node)->GetDevice (0)->GetAddress ());
194
  node++;
195
  while (node != m_nodes.End ())
196
    {
197
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
198
199
      Ptr<Packet> pkt = Create<Packet> (&energy, 1);
200
201
      double time = uniformRandomVariable->GetValue (0, 60);
202
      Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
203
      node++;
204
    }
205
  Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this);
206
}
207
208
void
209
UanExperiment::SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Mac8Address dst)
210
{
211
  std::cout << Simulator::Now ().As (Time::MIN) << " packet sent to " << dst << std::endl;
212
  PacketSocketAddress socketAddress;
213
  socketAddress.SetSingleDevice (node->GetDevice (0)->GetIfIndex ());
214
  socketAddress.SetPhysicalAddress (dst);
215
  socketAddress.SetProtocol (0);
216
  m_sockets[node]->SendTo (pkt, 0, socketAddress);
217
}
218
219
void
220
UanExperiment::Prepare ()
221
{
222
  m_nodes.Create (3);
223
  SetupPositions ();
224
  SetupEnergy ();
225
  SetupCommunications ();
226
  SetupApplications ();
227
  SendPackets ();
228
}
229
230
void
231
UanExperiment::Teardown ()
232
{
233
  std::map<Ptr<Node>, Ptr<Socket> >::iterator socket;
234
235
  for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++)
236
    {
237
      socket->second->Close ();
238
    }
239
}
240
241
int
242
main (int argc, char *argv[])
243
{
244
  CommandLine cmd;
245
  cmd.Parse (argc, argv);
246
247
  UanExperiment experiment;
248
  experiment.Prepare ();
249
250
  Simulator::Stop (Days (50));
251
  Simulator::Run ();
252
  Simulator::Destroy ();
253
254
  experiment.Teardown ();
255
256
  return 0;
257
}
(-)a/src/uan/examples/wscript (+12 lines)
 Lines 6-8   def build(bld): Link Here 
6
6
7
    obj = bld.create_ns3_program('uan-rc-example', ['internet', 'mobility', 'stats', 'applications', 'uan'])
7
    obj = bld.create_ns3_program('uan-rc-example', ['internet', 'mobility', 'stats', 'applications', 'uan'])
8
    obj.source = 'uan-rc-example.cc'
8
    obj.source = 'uan-rc-example.cc'
9
10
    obj = bld.create_ns3_program ('uan-raw-example', ['internet', 'mobility', 'stats', 'uan'])
11
    obj.source = 'uan-raw-example.cc'
12
13
    obj = bld.create_ns3_program ('uan-ipv4-example', ['internet', 'mobility', 'stats', 'uan'])
14
    obj.source = 'uan-ipv4-example.cc'
15
16
    obj = bld.create_ns3_program ('uan-ipv6-example', ['internet', 'mobility', 'stats', 'uan'])
17
    obj.source = 'uan-ipv6-example.cc'
18
19
    obj = bld.create_ns3_program ('uan-6lowpan-example', ['internet', 'mobility', 'stats', 'uan', 'sixlowpan'])
20
    obj.source = 'uan-6lowpan-example.cc'

Return to bug 2882