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

(-)a/src/uan/examples/uan-6lowpan-example.cc (+182 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/udp-application-helper.h"
31
#include "ns3/udp-application.h"
32
#include "ns3/sixlowpan-helper.h"
33
#include "ns3/sixlowpan-net-device.h"
34
35
using namespace ns3;
36
37
/**
38
 *
39
 * This example shows the usage of UdpApplication to transfer data.
40
 * Two nodes are sending their remaining energy percentage (1 byte)
41
 * to a gateway node, that prints the received data.
42
 * The transmissions are scheduled at random times to avoid collisions
43
 *
44
 */
45
46
NS_LOG_COMPONENT_DEFINE ("Uan6lowpanExample");
47
48
void SetupPositions ();
49
void SetupEnergy ();
50
void SetupCommunications ();
51
void PrintReceivedPacket (Address src, Buffer data);
52
void SetupApplications ();
53
void SendPacket ();
54
void Run ();
55
56
NodeContainer nodeContainer;
57
58
void
59
SetupPositions ()
60
{
61
  MobilityHelper mobilityHelper;
62
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
63
  mobilityHelper.Install (nodeContainer);
64
  nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
65
  nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
66
  nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
67
}
68
69
void
70
SetupEnergy ()
71
{
72
  BasicEnergySourceHelper energySourceHelper;
73
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
74
  energySourceHelper.Install (nodeContainer);
75
}
76
77
void
78
SetupCommunications ()
79
{
80
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
81
  UanHelper uanHelper;
82
  NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel);
83
  EnergySourceContainer energySourceContainer;
84
  NodeContainer::Iterator node = nodeContainer.Begin ();
85
  while (node != nodeContainer.End ())
86
    {
87
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
88
      node++;
89
    }
90
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
91
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
92
93
  SixLowPanHelper sixLowPanHelper;
94
  NetDeviceContainer sixlowpanNetDevices = sixLowPanHelper.Install (netDeviceContainer);
95
96
  InternetStackHelper internetStackHelper;
97
  internetStackHelper.Install (nodeContainer);
98
99
  Ipv6AddressHelper ipv6AddressHelper;
100
  ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64));
101
  ipv6AddressHelper.Assign (sixlowpanNetDevices);
102
103
  node = nodeContainer.Begin ();
104
  while (node != nodeContainer.End ())
105
    {
106
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
107
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600)));
108
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000)));
109
      node++;
110
    }
111
}
112
113
void
114
PrintReceivedPacket (Address src, Ptr<Packet> packet)
115
{
116
  uint8_t *buffer = new uint8_t[packet->GetSize()];
117
  packet->CopyData(buffer, sizeof(uint8_t));
118
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
119
            << "Node:" << Ipv6Address::ConvertFrom (src)
120
            << "|Energy:" << (int)buffer[0] << "%" << std::endl;
121
  delete [] buffer;
122
}
123
124
void
125
SetupApplications ()
126
{
127
  UdpApplicationHelper udpApplicationHelper;
128
  ApplicationContainer applicationContainer;
129
  udpApplicationHelper.SetPort (9);
130
  applicationContainer = udpApplicationHelper.Install (nodeContainer);
131
  applicationContainer.Start (Seconds (0));
132
  NodeContainer::Iterator node = nodeContainer.Begin ();
133
  while (node != nodeContainer.End ())
134
    {
135
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
136
      udpApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Ptr<Packet>>(&PrintReceivedPacket));
137
      node++;
138
    }
139
}
140
141
void
142
SendPacket ()
143
{
144
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
145
  NodeContainer::Iterator node = nodeContainer.Begin ();
146
  Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress ();
147
  node++;
148
  while (node != nodeContainer.End ())
149
    {
150
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
151
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
152
      Ptr<Packet> packet = Create<Packet> (&energy, sizeof (uint8_t) );
153
      double time = uniformRandomVariable->GetValue (0, 60);
154
      Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, packet);
155
      node++;
156
    }
157
  Simulator::Schedule (Hours (2), &SendPacket);
158
}
159
160
void
161
Run ()
162
{
163
  nodeContainer.Create (3);
164
  SetupPositions ();
165
  SetupEnergy ();
166
  SetupCommunications ();
167
  SetupApplications ();
168
  SendPacket ();
169
}
170
171
int
172
main (int argc, char *argv[])
173
{
174
  CommandLine cmd;
175
  cmd.Parse (argc, argv);
176
  Run ();
177
  Simulator::Stop (Days (50));
178
  Simulator::Run ();
179
  Simulator::Destroy ();
180
181
  return 0;
182
}
(-)a/src/uan/examples/uan-ipv4-example.cc (+179 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/node-container.h"
22
#include "ns3/mobility-helper.h"
23
#include "ns3/mobility-model.h"
24
#include "ns3/basic-energy-source-helper.h"
25
#include "ns3/energy-source-container.h"
26
#include "ns3/uan-helper.h"
27
#include "ns3/uan-channel.h"
28
#include "ns3/acoustic-modem-energy-model-helper.h"
29
#include "ns3/udp-application-helper.h"
30
#include "ns3/udp-application.h"
31
#include "ns3/ipv4-address-helper.h"
32
#include "ns3/internet-stack-helper.h"
33
#include "ns3/ipv4-interface.h"
34
#include "ns3/arp-cache.h"
35
36
using namespace ns3;
37
38
/**
39
 *
40
 * This example shows the usage of UdpApplication to transfer data.
41
 * Two nodes are sending their remaining energy percentage (1 byte)
42
 * to a gateway node, that prints the received data.
43
 * The transmissions are scheduled at random times to avoid collisions
44
 * ARP parameters should be tuned, based on the network size
45
 *
46
 */
47
48
NS_LOG_COMPONENT_DEFINE ("UanIpv4Example");
49
50
void SetupPositions ();
51
void SetupEnergy ();
52
void SetupCommunications ();
53
void PrintReceivedPacket (Address src, Buffer data);
54
void SetupApplications ();
55
void SendPacket ();
56
void Run ();
57
58
NodeContainer nodeContainer;
59
60
void
61
SetupPositions ()
62
{
63
  MobilityHelper mobilityHelper;
64
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
65
  mobilityHelper.Install (nodeContainer);
66
  nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
67
  nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
68
  nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
69
}
70
71
void
72
SetupEnergy ()
73
{
74
  BasicEnergySourceHelper energySourceHelper;
75
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
76
  energySourceHelper.Install (nodeContainer);
77
}
78
79
void
80
SetupCommunications ()
81
{
82
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
83
  UanHelper uanHelper;
84
  NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel);
85
  EnergySourceContainer energySourceContainer;
86
  NodeContainer::Iterator node = nodeContainer.Begin ();
87
  while (node != nodeContainer.End ())
88
    {
89
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
90
      node++;
91
    }
92
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
93
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
94
95
  InternetStackHelper internetStackHelper;
96
  internetStackHelper.Install (nodeContainer);
97
  Ipv4AddressHelper ipv4AddressHelper;
98
  ipv4AddressHelper.SetBase ("10.0.0.0", "255.255.255.0");
99
  ipv4AddressHelper.Assign (netDeviceContainer);
100
101
  node = nodeContainer.Begin ();
102
  while (node != nodeContainer.End ())
103
    {
104
      (*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetArpCache ()->SetWaitReplyTimeout (Seconds (10));
105
      node++;
106
    }
107
}
108
109
void
110
PrintReceivedPacket (Address src, Ptr<Packet> packet)
111
{
112
  uint8_t *buffer = new uint8_t[packet->GetSize()];
113
  packet->CopyData(buffer, sizeof(uint8_t));
114
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
115
            << "Node:" << Ipv4Address::ConvertFrom (src)
116
            << "|Energy:" << (int)buffer[0] << "%" << std::endl;
117
  delete [] buffer;
118
}
119
120
121
void
122
SetupApplications ()
123
{
124
  UdpApplicationHelper udpApplicationHelper;
125
  ApplicationContainer applicationContainer;
126
  udpApplicationHelper.SetPort (9);
127
  applicationContainer = udpApplicationHelper.Install (nodeContainer);
128
  applicationContainer.Start (Seconds (0));
129
  NodeContainer::Iterator node = nodeContainer.Begin ();
130
  while (node != nodeContainer.End ())
131
    {
132
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
133
      udpApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Ptr<Packet>>(&PrintReceivedPacket));
134
      node++;
135
    }
136
}
137
138
void
139
SendPacket ()
140
{
141
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
142
  NodeContainer::Iterator node = nodeContainer.Begin ();
143
  Ipv4Address dst = (*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetAddress (0).GetLocal ();
144
  node++;
145
  while (node != nodeContainer.End ())
146
    {
147
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
148
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
149
      Ptr<Packet> packet = Create<Packet> (&energy, sizeof (uint8_t) );
150
      double time = uniformRandomVariable->GetValue (0, 15);
151
      Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, packet);
152
      node++;
153
    }
154
  Simulator::Schedule (Minutes (5), &SendPacket);
155
}
156
157
void
158
Run ()
159
{
160
  nodeContainer.Create (3);
161
  SetupPositions ();
162
  SetupEnergy ();
163
  SetupCommunications ();
164
  SetupApplications ();
165
  SendPacket ();
166
}
167
168
int
169
main (int argc, char *argv[])
170
{
171
  CommandLine cmd;
172
  cmd.Parse (argc, argv);
173
  Run ();
174
  Simulator::Stop (Days (10));
175
  Simulator::Run ();
176
  Simulator::Destroy ();
177
178
  return 0;
179
}
(-)a/src/uan/examples/uan-ipv6-example.cc (+178 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/udp-application-helper.h"
31
#include "ns3/udp-application.h"
32
33
using namespace ns3;
34
35
/**
36
 *
37
 * This example shows the usage of UdpApplication 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 ("UanIpv6Example");
45
46
void SetupPositions ();
47
void SetupEnergy ();
48
void SetupCommunications ();
49
void PrintReceivedPacket (Address src, Buffer data);
50
void SetupApplications ();
51
void SendPacket ();
52
void Run ();
53
54
NodeContainer nodeContainer;
55
56
void
57
SetupPositions ()
58
{
59
  MobilityHelper mobilityHelper;
60
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
61
  mobilityHelper.Install (nodeContainer);
62
  nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
63
  nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
64
  nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
65
}
66
67
void
68
SetupEnergy ()
69
{
70
  BasicEnergySourceHelper energySourceHelper;
71
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
72
  energySourceHelper.Install (nodeContainer);
73
}
74
75
void
76
SetupCommunications ()
77
{
78
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
79
  UanHelper uanHelper;
80
  NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel);
81
  EnergySourceContainer energySourceContainer;
82
  NodeContainer::Iterator node = nodeContainer.Begin ();
83
  while (node != nodeContainer.End ())
84
    {
85
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
86
      node++;
87
    }
88
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
89
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
90
91
  InternetStackHelper internetStackHelper;
92
  internetStackHelper.Install (nodeContainer);
93
94
  Ipv6AddressHelper ipv6AddressHelper;
95
  ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64));
96
  ipv6AddressHelper.Assign (netDeviceContainer);
97
98
  node = nodeContainer.Begin ();
99
  while (node != nodeContainer.End ())
100
    {
101
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
102
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600)));
103
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000)));
104
      node++;
105
    }
106
}
107
108
void
109
PrintReceivedPacket (Address src, Ptr<Packet> packet)
110
{
111
  uint8_t *buffer = new uint8_t[packet->GetSize()];
112
  packet->CopyData(buffer, sizeof(uint8_t));
113
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
114
            << "Node:" << Ipv6Address::ConvertFrom (src)
115
            << "|Energy:" << (int)buffer[0] << "%" << std::endl;
116
  delete [] buffer;
117
}
118
119
120
void
121
SetupApplications ()
122
{
123
  UdpApplicationHelper udpApplicationHelper;
124
  ApplicationContainer applicationContainer;
125
  udpApplicationHelper.SetPort (9);
126
  applicationContainer = udpApplicationHelper.Install (nodeContainer);
127
  applicationContainer.Start (Seconds (0));
128
  NodeContainer::Iterator node = nodeContainer.Begin ();
129
  while (node != nodeContainer.End ())
130
    {
131
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
132
      udpApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Ptr<Packet>>(&PrintReceivedPacket));
133
      node++;
134
    }
135
}
136
137
void
138
SendPacket ()
139
{
140
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
141
  NodeContainer::Iterator node = nodeContainer.Begin ();
142
  Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress ();
143
  node++;
144
  while (node != nodeContainer.End ())
145
    {
146
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
147
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
148
      Ptr<Packet> packet = Create<Packet> (&energy, sizeof (uint8_t) );
149
      double time = uniformRandomVariable->GetValue (0, 120);
150
      Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, packet);
151
      node++;
152
    }
153
  Simulator::Schedule (Hours (2), &SendPacket);
154
}
155
156
void
157
Run ()
158
{
159
  nodeContainer.Create (3);
160
  SetupPositions ();
161
  SetupEnergy ();
162
  SetupCommunications ();
163
  SetupApplications ();
164
  SendPacket ();
165
}
166
167
int
168
main (int argc, char *argv[])
169
{
170
  CommandLine cmd;
171
  cmd.Parse (argc, argv);
172
  Run ();
173
  Simulator::Stop (Days (30));
174
  Simulator::Run ();
175
  Simulator::Destroy ();
176
177
  return 0;
178
}
(-)a/src/uan/examples/uan-raw-example.cc (+159 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/node-container.h"
22
#include "ns3/mobility-helper.h"
23
#include "ns3/mobility-model.h"
24
#include "ns3/basic-energy-source-helper.h"
25
#include "ns3/energy-source-container.h"
26
#include "ns3/uan-helper.h"
27
#include "ns3/uan-channel.h"
28
#include "ns3/acoustic-modem-energy-model-helper.h"
29
#include "ns3/raw-application-helper.h"
30
#include "ns3/raw-application.h"
31
32
using namespace ns3;
33
34
/**
35
 *
36
 * This example shows the usage of RawApplication to transfer data.
37
 * Two nodes are sending their remaining energy percentage (1 byte)
38
 * to a gateway node, that prints the received data.
39
 * The transmissions are scheduled at random times to avoid collisions
40
 *
41
 */
42
43
NS_LOG_COMPONENT_DEFINE ("UanRawExample");
44
45
void SetupPositions ();
46
void SetupEnergy ();
47
void SetupCommunications ();
48
void PrintReceivedPacket (Address src, Buffer data);
49
void SetupApplications ();
50
void SendPacket ();
51
void Run ();
52
53
NodeContainer nodeContainer;
54
55
void
56
SetupPositions ()
57
{
58
  MobilityHelper mobilityHelper;
59
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
60
  mobilityHelper.Install (nodeContainer);
61
  nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
62
  nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
63
  nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
64
}
65
66
void
67
SetupEnergy ()
68
{
69
  BasicEnergySourceHelper energySourceHelper;
70
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
71
  energySourceHelper.Install (nodeContainer);
72
}
73
74
void
75
SetupCommunications ()
76
{
77
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
78
  UanHelper uanHelper;
79
  NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel);
80
  EnergySourceContainer energySourceContainer;
81
  NodeContainer::Iterator node = nodeContainer.Begin ();
82
  while (node != nodeContainer.End ())
83
    {
84
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
85
      node++;
86
    }
87
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
88
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
89
}
90
91
void
92
PrintReceivedPacket (Address src, Ptr<Packet> packet)
93
{
94
  uint8_t *buffer = new uint8_t[packet->GetSize()];
95
  packet->CopyData(buffer, sizeof(uint8_t));
96
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
97
            << "Node:" << Mac8Address::ConvertFrom (src)
98
            << "|Energy:" << (int)buffer[0] << "%" << std::endl;
99
  delete [] buffer;
100
}
101
102
void
103
SetupApplications ()
104
{
105
  RawApplicationHelper rawApplicationHelper;
106
  ApplicationContainer applicationContainer;
107
  applicationContainer = rawApplicationHelper.Install (nodeContainer);
108
  applicationContainer.Start (Seconds (0));
109
  NodeContainer::Iterator node = nodeContainer.Begin ();
110
  while (node != nodeContainer.End ())
111
    {
112
      Ptr<RawApplication> rawApplication = (*node)->GetApplication (0)->GetObject<RawApplication> ();
113
      rawApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Ptr<Packet>>(&PrintReceivedPacket));
114
      node++;
115
    }
116
}
117
118
void
119
SendPacket ()
120
{
121
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
122
  NodeContainer::Iterator node = nodeContainer.Begin ();
123
  node++;
124
  while (node != nodeContainer.End ())
125
    {
126
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
127
      Ptr<RawApplication> rawApplication = (*node)->GetApplication (0)->GetObject<RawApplication> ();
128
      Ptr<Packet> packet = Create<Packet> (&energy, sizeof (uint8_t));
129
      double time = uniformRandomVariable->GetValue (0, 15);
130
      Simulator::Schedule (Seconds (time), &RawApplication::Send, rawApplication, Mac8Address (0), packet);
131
      node++;
132
    }
133
  Simulator::Schedule (Hours (2), &SendPacket);
134
}
135
136
void
137
Run ()
138
{
139
  Packet::EnablePrinting ();
140
  nodeContainer.Create (3);
141
  SetupPositions ();
142
  SetupEnergy ();
143
  SetupCommunications ();
144
  SetupApplications ();
145
  SendPacket ();
146
}
147
148
int
149
main (int argc, char *argv[])
150
{
151
  CommandLine cmd;
152
  cmd.Parse (argc, argv);
153
  Run ();
154
  Simulator::Stop (Days (60));
155
  Simulator::Run ();
156
  Simulator::Destroy ();
157
158
  return 0;
159
}
(-)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', 'applications', 'uan'])
11
    obj.source = 'uan-raw-example.cc'
12
13
    obj = bld.create_ns3_program('uan-ipv4-example', ['internet', 'mobility', 'stats', 'applications', 'uan'])
14
    obj.source = 'uan-ipv4-example.cc'
15
16
    obj = bld.create_ns3_program('uan-ipv6-example', ['internet', 'mobility', 'stats', 'applications', 'uan'])
17
    obj.source = 'uan-ipv6-example.cc'
18
19
    obj = bld.create_ns3_program('uan-6lowpan-example', ['internet', 'mobility', 'stats', 'applications', 'uan', 'sixlowpan'])
20
    obj.source = 'uan-6lowpan-example.cc'

Return to bug 2882