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

(-)a/src/applications/helper/raw-application-helper.cc (+81 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 "raw-application-helper.h"
21
#include "ns3/names.h"
22
#include "ns3/packet-socket-factory.h"
23
#include "ns3/packet-socket-helper.h"
24
#include "ns3/net-device.h"
25
#include "ns3/address.h"
26
27
28
namespace ns3 {
29
30
RawApplicationHelper::RawApplicationHelper ()
31
{
32
  m_factory.SetTypeId ("ns3::RawApplication");
33
}
34
35
void
36
RawApplicationHelper::SetAttribute (std::string name, const AttributeValue &value)
37
{
38
  m_factory.Set (name, value);
39
}
40
41
ApplicationContainer
42
RawApplicationHelper::Install (Ptr<Node> node) const
43
{
44
  return ApplicationContainer (DoInstall (node));
45
}
46
47
ApplicationContainer
48
RawApplicationHelper::Install (std::string nodeName) const
49
{
50
  Ptr<Node> node = Names::Find<Node> (nodeName);
51
  return ApplicationContainer (DoInstall (node));
52
}
53
54
ApplicationContainer
55
RawApplicationHelper::Install (NodeContainer c) const
56
{
57
  ApplicationContainer apps;
58
  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
59
    {
60
      apps.Add (DoInstall (*i));
61
    }
62
63
  return apps;
64
}
65
66
Ptr<Application>
67
RawApplicationHelper::DoInstall (Ptr<Node> node) const
68
{
69
  if(node->GetObject<PacketSocketFactory> () == NULL)
70
    {
71
      PacketSocketHelper packetSocketHelper;
72
      packetSocketHelper.Install (node);
73
    }
74
75
  Ptr<Application> app = m_factory.Create<Application> ();
76
  node->AddApplication (app);
77
78
  return app;
79
}
80
81
} // namespace ns3
(-)a/src/applications/helper/raw-application-helper.h (+92 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
#ifndef RAW_APPLICATION_HELPER_H
21
#define RAW_APPLICATION_HELPER_H
22
23
24
#include "ns3/object-factory.h"
25
#include "ns3/attribute.h"
26
#include "ns3/node-container.h"
27
#include "ns3/application-container.h"
28
29
namespace ns3 {
30
31
/**
32
 * \ingroup applications
33
 * \brief A helper to make it easier to instantiate an ns3::RawApplication
34
 * on a set of nodes.
35
 */
36
class RawApplicationHelper
37
{
38
public:
39
  /**
40
   * Create a RawApplicationHelper to make it easier to work with RawApplication
41
   */
42
  RawApplicationHelper ();
43
44
  /**
45
   * Helper function used to set the underlying application attributes.
46
   */
47
  void SetAttribute (std::string name, const AttributeValue &value);
48
49
  /**
50
   * Install an ns3::RawApplication on each node of the input container
51
   * configured with all the attributes set with SetAttribute.
52
   *
53
   * \param c NodeContainer of the set of nodes on which a RawApplication
54
   * will be installed.
55
   * \returns Container of Ptr to the applications installed.
56
   */
57
  ApplicationContainer Install (NodeContainer c) const;
58
59
  /**
60
   * Install an ns3::RawApplication on the node configured with all the
61
   * attributes set with SetAttribute.
62
   *
63
   * \param node The node on which a RawApplication will be installed.
64
   * \returns Container of Ptr to the applications installed.
65
   */
66
  ApplicationContainer Install (Ptr<Node> node) const;
67
68
  /**
69
   * Install an ns3::RawApplication on the node configured with all the
70
   * attributes set with SetAttribute.
71
   *
72
   * \param nodeName The node on which a RawApplication will be installed.
73
   * \returns Container of Ptr to the applications installed.
74
   */
75
  ApplicationContainer Install (std::string nodeName) const;
76
77
private:
78
  /**
79
   * Install an ns3::RawApplication on the node configured with all the
80
   * attributes set with SetAttribute.
81
   *
82
   * \param node The node on which a RawApplication will be installed.
83
   * \returns Ptr to the application installed.
84
   */
85
  Ptr<Application> DoInstall (Ptr<Node> node) const;
86
87
  ObjectFactory m_factory; //!< Object factory.
88
};
89
90
} // namespace ns3
91
92
#endif /* RAW_APPLICATION_HELPER_H */
(-)a/src/applications/helper/udp-application-helper.cc (+91 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 "udp-application-helper.h"
21
#include "ns3/names.h"
22
#include "ns3/packet-socket-factory.h"
23
#include "ns3/packet-socket-helper.h"
24
#include "ns3/net-device.h"
25
#include "ns3/address.h"
26
#include "ns3/udp-application.h"
27
#include "ns3/internet-stack-helper.h"
28
29
30
namespace ns3 {
31
32
UdpApplicationHelper::UdpApplicationHelper ()
33
  : m_port (0)
34
{
35
  m_factory.SetTypeId ("ns3::UdpApplication");
36
}
37
38
void
39
UdpApplicationHelper::SetAttribute (std::string name, const AttributeValue &value)
40
{
41
  m_factory.Set (name, value);
42
}
43
44
ApplicationContainer
45
UdpApplicationHelper::Install (Ptr<Node> node) const
46
{
47
  return ApplicationContainer (DoInstall (node));
48
}
49
50
ApplicationContainer
51
UdpApplicationHelper::Install (std::string nodeName) const
52
{
53
  Ptr<Node> node = Names::Find<Node> (nodeName);
54
  return ApplicationContainer (DoInstall (node));
55
}
56
57
ApplicationContainer
58
UdpApplicationHelper::Install (NodeContainer c) const
59
{
60
  ApplicationContainer apps;
61
  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
62
    {
63
      apps.Add (DoInstall (*i));
64
    }
65
66
  return apps;
67
}
68
69
Ptr<Application>
70
UdpApplicationHelper::DoInstall (Ptr<Node> node) const
71
{
72
  if(node->GetObject<Ipv4> () == NULL)
73
    {
74
      InternetStackHelper internet;
75
      internet.Install (node);
76
    }
77
78
  Ptr<Application> app = m_factory.Create<Application> ();
79
  node->AddApplication (app);
80
  app->GetObject<UdpApplication> ()->SetPort (m_port);
81
82
  return app;
83
}
84
85
void
86
UdpApplicationHelper::SetPort (uint16_t port)
87
{
88
  m_port = port;
89
}
90
91
} // namespace ns3
(-)a/src/applications/helper/udp-application-helper.h (+100 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
#ifndef UDP_APPLICATION_HELPER_H
21
#define UDP_APPLICATION_HELPER_H
22
23
24
#include "ns3/object-factory.h"
25
#include "ns3/attribute.h"
26
#include "ns3/node-container.h"
27
#include "ns3/application-container.h"
28
29
namespace ns3 {
30
31
/**
32
 * \ingroup applications
33
 * \brief A helper to make it easier to instantiate an ns3::UdpApplication
34
 * on a set of nodes.
35
 */
36
class UdpApplicationHelper
37
{
38
public:
39
  /**
40
   * Create a UdpApplicationHelper to make it easier to work with UdpApplication
41
   */
42
  UdpApplicationHelper ();
43
44
  /**
45
   * Helper function used to set the underlying application attributes.
46
   */
47
  void SetAttribute (std::string name, const AttributeValue &value);
48
49
  /**
50
   * Install an ns3::UdpApplication on each node of the input container
51
   * configured with all the attributes set with SetAttribute.
52
   *
53
   * \param c NodeContainer of the set of nodes on which a UdpApplication
54
   * will be installed.
55
   * \returns Container of Ptr to the applications installed.
56
   */
57
  ApplicationContainer Install (NodeContainer c) const;
58
59
  /**
60
   * Install an ns3::UdpApplication on the node configured with all the
61
   * attributes set with SetAttribute.
62
   *
63
   * \param node The node on which a UdpApplication will be installed.
64
   * \returns Container of Ptr to the applications installed.
65
   */
66
  ApplicationContainer Install (Ptr<Node> node) const;
67
68
  /**
69
   * Install an ns3::UdpApplication on the node configured with all the
70
   * attributes set with SetAttribute.
71
   *
72
   * \param nodeName The node on which a UdpApplication will be installed.
73
   * \returns Container of Ptr to the applications installed.
74
   */
75
  ApplicationContainer Install (std::string nodeName) const;
76
77
  /**
78
   * Sets the UDP port number that the UdpApplication will use
79
   *
80
   * \param port The UDP port number
81
   */
82
  void SetPort (uint16_t port);
83
84
private:
85
  /**
86
   * Install an ns3::UdpApplication on the node configured with all the
87
   * attributes set with SetAttribute.
88
   *
89
   * \param node The node on which a UdpApplication will be installed.
90
   * \returns Ptr to the application installed.
91
   */
92
  Ptr<Application> DoInstall (Ptr<Node> node) const;
93
94
  ObjectFactory m_factory; //!< Object factory.
95
  uint16_t m_port;
96
};
97
98
} // namespace ns3
99
100
#endif /* UDP_APPLICATION_HELPER_H */
(-)a/src/applications/model/raw-application.cc (+173 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 "raw-application.h"
21
#include "ns3/log.h"
22
#include "ns3/socket-factory.h"
23
#include "ns3/packet-socket-address.h"
24
#include "ns3/packet-socket.h"
25
26
namespace ns3 {
27
28
NS_LOG_COMPONENT_DEFINE ("RawApplication");
29
30
NS_OBJECT_ENSURE_REGISTERED (RawApplication);
31
32
TypeId
33
RawApplication::GetTypeId (void)
34
{
35
  static TypeId tid = TypeId ("ns3::RawApplication")
36
    .SetParent<Application> ()
37
    .SetGroupName ("Applications")
38
    .AddConstructor<RawApplication> ()
39
  ;
40
  return tid;
41
}
42
43
RawApplication::RawApplication ()
44
{
45
  NS_LOG_FUNCTION (this);
46
  m_socket = NULL;
47
  m_packetReceivedCallback.Nullify ();
48
}
49
RawApplication::~RawApplication ()
50
{
51
  NS_LOG_FUNCTION (this);
52
}
53
54
void
55
RawApplication::DoDispose (void)
56
{
57
  NS_LOG_FUNCTION (this);
58
  m_socket = NULL;
59
  m_packetReceivedCallback.Nullify ();
60
  Application::DoDispose ();
61
}
62
63
uint32_t
64
RawApplication::GetApplicationId (void) const
65
{
66
  NS_LOG_FUNCTION (this);
67
  Ptr<Node> node = GetNode ();
68
  for (uint32_t i = 0; i < node->GetNApplications (); ++i)
69
    {
70
      if (node->GetApplication (i) == this)
71
        {
72
          return i;
73
        }
74
    }
75
  NS_ASSERT_MSG (false, "forgot to add application to node");
76
  return 0;
77
}
78
79
80
void
81
RawApplication::Send (Address dst, const Buffer dataBuffer)
82
{
83
  NS_LOG_FUNCTION (this);
84
  const uint8_t * data = dataBuffer.PeekData ();
85
  Address local = GetNode ()->GetDevice (0)->GetAddress ();
86
87
  if(dst != local)
88
    {
89
      PacketSocketAddress socketAddress;
90
      socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ());
91
      socketAddress.SetPhysicalAddress (dst);
92
      socketAddress.SetProtocol (0);
93
      Ptr<Packet> packet = Create<Packet> (data, dataBuffer.GetSize ());
94
      if(m_socket == NULL)
95
        {
96
          StartApplication ();
97
        }
98
      m_socket->SendTo (packet, 0, socketAddress);
99
    }
100
}
101
102
void
103
RawApplication::Receive (Ptr<Socket> socket)
104
{
105
  NS_LOG_FUNCTION (this << socket);
106
  NS_ASSERT (m_socket != NULL);
107
  while (m_socket->GetRxAvailable () > 0)
108
    {
109
      Address srcAddress;
110
      Ptr<Packet> packet = m_socket->RecvFrom (srcAddress);
111
      uint8_t *buffer = new uint8_t[packet->GetSize ()];
112
      packet->CopyData (buffer, packet->GetSize ());
113
114
      PacketSocketAddress packetSocketAddress = PacketSocketAddress::ConvertFrom (srcAddress);
115
      srcAddress = packetSocketAddress.GetPhysicalAddress ();
116
117
      if(!m_packetReceivedCallback.IsNull ())
118
        {
119
          Buffer dataBuffer;
120
          dataBuffer.AddAtEnd ( packet->GetSize ());
121
          Buffer::Iterator iterator = dataBuffer.Begin ();
122
          iterator.Write (buffer, packet->GetSize ());
123
          m_packetReceivedCallback (srcAddress, dataBuffer);
124
        }
125
      delete[] buffer;
126
    }
127
128
}
129
130
void
131
RawApplication::StartApplication (void)
132
{
133
  NS_LOG_FUNCTION (this);
134
  PacketSocketAddress socketAddress;
135
  socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ());
136
  socketAddress.SetProtocol (0);
137
  if (!m_socket)
138
    {
139
      m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::PacketSocketFactory"));
140
    }
141
  m_socket->Bind ();
142
  m_socket->Connect (socketAddress);
143
  m_socket->SetRecvCallback (MakeCallback (&RawApplication::Receive, this));
144
}
145
146
void
147
RawApplication::StopApplication (void)
148
{
149
  NS_LOG_FUNCTION (this);
150
  if(m_socket != NULL)
151
    {
152
      m_socket->Close ();
153
    }
154
  else
155
    {
156
      NS_LOG_WARN ("RawApplication found null socket to close in StopApplication");
157
    }
158
}
159
160
void
161
RawApplication::SetPacketReceivedCallback (PacketReceivedCallback callback)
162
{
163
  NS_LOG_FUNCTION (this);
164
  if (callback.IsNull ())
165
    {
166
      NS_LOG_DEBUG ("RawApplication:Setting NULL packet received callback!");
167
    }
168
  m_packetReceivedCallback = callback;
169
}
170
171
172
173
} // namespace ns3
(-)a/src/applications/model/raw-application.h (+104 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
#ifndef RAW_APPLICATION_H
21
#define RAW_APPLICATION_H
22
23
24
#include "ns3/application.h"
25
#include "ns3/packet.h"
26
#include "ns3/socket.h"
27
28
namespace ns3 {
29
30
/**
31
 * \ingroup applications
32
 * \brief a simple Raw application.
33
 *
34
 * The Application combines both the client-side and the server-side
35
 * functionality, and provides an easy to use bidirectional communications
36
 * between the nodes. It runs directly on top of the data link layer, so
37
 * raw data is sent in the payload of the frame.
38
 *
39
 * The user interacts with the application mainly through two functions:
40
 * Send () : Used to send the data in the buffer parameter to the
41
 * destination layer 2 address.
42
 * SetPacketReceivedCallback (): Used to set a receive callback where the raw
43
 * packet payload, and the source layer 2 address will be passed as
44
 * parameters.
45
 */
46
47
class RawApplication : public Application
48
{
49
public:
50
51
  typedef Callback<void, Address, Buffer> PacketReceivedCallback;
52
53
  /**
54
   * \brief Get the type ID.
55
   * \return the object TypeId
56
   */
57
  static TypeId GetTypeId (void);
58
59
  /**
60
   * create a raw application
61
   */
62
  RawApplication ();
63
  virtual ~RawApplication ();
64
65
  /**
66
   * \brief Send a packet to the destination
67
   * \param dst the destination address
68
   * \param dataBuffer the data to be sent
69
   */
70
  void Send (Address dst, const Buffer dataBuffer);
71
72
  /**
73
   * \brief Set a callback when a packet is received
74
   * \param callback the packet received callback
75
   */
76
  void SetPacketReceivedCallback (PacketReceivedCallback callback);
77
78
private:
79
80
  // inherited from Application base class.
81
  virtual void StartApplication (void);
82
  virtual void StopApplication (void);
83
  virtual void DoDispose (void);
84
  /**
85
   * \brief Return the application ID in the node.
86
   * \returns the application id
87
   */
88
  uint32_t GetApplicationId (void) const;
89
  /**
90
   * \brief Receive a packet
91
   * \param socket the receiving socket
92
   *
93
   * This function is called by lower layers through a callback.
94
   */
95
  void Receive (Ptr<Socket> socket);
96
97
  Ptr<Socket> m_socket; //!< Associated socket
98
  PacketReceivedCallback m_packetReceivedCallback;
99
100
};
101
102
} // namespace ns3
103
104
#endif /* RAW_APPLICATION_H */
(-)a/src/applications/model/udp-application.cc (+197 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 "udp-application.h"
21
#include "ns3/log.h"
22
#include "ns3/socket-factory.h"
23
#include "ns3/packet-socket-address.h"
24
#include "ns3/packet-socket.h"
25
#include "ns3/ipv4-l3-protocol.h"
26
#include "ns3/ipv6-l3-protocol.h"
27
28
namespace ns3 {
29
30
NS_LOG_COMPONENT_DEFINE ("UdpApplication");
31
32
NS_OBJECT_ENSURE_REGISTERED (UdpApplication);
33
34
TypeId
35
UdpApplication::GetTypeId (void)
36
{
37
  static TypeId tid = TypeId ("ns3::UdpApplication")
38
    .SetParent<Application> ()
39
    .SetGroupName ("Applications")
40
    .AddConstructor<UdpApplication> ()
41
  ;
42
  return tid;
43
}
44
45
UdpApplication::UdpApplication ()
46
{
47
  NS_LOG_FUNCTION (this);
48
  m_socket = NULL;
49
  m_packetReceivedCallback.Nullify ();
50
}
51
UdpApplication::~UdpApplication ()
52
{
53
  NS_LOG_FUNCTION (this);
54
}
55
56
void
57
UdpApplication::DoDispose (void)
58
{
59
  NS_LOG_FUNCTION (this);
60
  m_socket = NULL;
61
  m_packetReceivedCallback.Nullify ();
62
  Application::DoDispose ();
63
}
64
65
uint32_t
66
UdpApplication::GetApplicationId (void) const
67
{
68
  NS_LOG_FUNCTION (this);
69
  Ptr<Node> node = GetNode ();
70
  for (uint32_t i = 0; i < node->GetNApplications (); ++i)
71
    {
72
      if (node->GetApplication (i) == this)
73
        {
74
          return i;
75
        }
76
    }
77
  NS_ASSERT_MSG (false, "forgot to add application to node");
78
  return 0;
79
}
80
81
82
void
83
UdpApplication::Send (Address dst, const Buffer dataBuffer)
84
{
85
  NS_LOG_FUNCTION (this);
86
  const uint8_t * data = dataBuffer.PeekData ();
87
  Ptr<Packet> packet = Create<Packet> (data, dataBuffer.GetSize ());
88
  if(m_socket == NULL)
89
    {
90
      StartApplication ();
91
    }
92
93
  if(Ipv6Address::IsMatchingType (dst))
94
    {
95
      Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), m_port);
96
      m_socket->SendTo (packet, 0, ipv6_destination);
97
    }
98
  else if(Ipv4Address::IsMatchingType (dst))
99
    {
100
      InetSocketAddress ipv4_destination = InetSocketAddress (Ipv4Address::ConvertFrom (dst), m_port);
101
      m_socket->SendTo (packet, 0, ipv4_destination);
102
    }
103
}
104
105
void
106
UdpApplication::Receive (Ptr<Socket> socket)
107
{
108
  NS_LOG_FUNCTION (this << socket);
109
  NS_ASSERT (m_socket != NULL);
110
  Address srcAddress;
111
  while (m_socket->GetRxAvailable () > 0)
112
    {
113
      Ptr<Packet> packet = m_socket->RecvFrom (srcAddress);
114
      uint8_t *buffer = new uint8_t[packet->GetSize ()];
115
      packet->CopyData (buffer, packet->GetSize ());
116
      if(!m_packetReceivedCallback.IsNull ())
117
        {
118
          Buffer dataBuffer;
119
          dataBuffer.AddAtEnd ( packet->GetSize ());
120
          Buffer::Iterator iterator = dataBuffer.Begin ();
121
          iterator.Write (buffer, packet->GetSize ());
122
123
          if(Inet6SocketAddress::IsMatchingType (srcAddress))
124
            {
125
              m_packetReceivedCallback (Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 (), dataBuffer);
126
            }
127
          else if(InetSocketAddress::IsMatchingType (srcAddress))
128
            {
129
              m_packetReceivedCallback (InetSocketAddress::ConvertFrom (srcAddress).GetIpv4 (), dataBuffer);
130
            }
131
        }
132
      delete[] buffer;
133
    }
134
}
135
136
void
137
UdpApplication::StartApplication (void)
138
{
139
  NS_LOG_FUNCTION (this);
140
  if (!m_socket)
141
    {
142
      m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::UdpSocketFactory"));
143
    }
144
145
  if(GetNode ()->GetObject<Ipv6> () != NULL)
146
    {
147
      Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), m_port);
148
      m_socket->Bind (ipv6_local);
149
    }
150
151
  if(GetNode ()->GetObject<Ipv4> () != NULL)
152
    {
153
      InetSocketAddress ipv4_local = InetSocketAddress (Ipv4Address::GetAny (), m_port);
154
      m_socket->Bind (ipv4_local);
155
    }
156
  m_socket->SetRecvCallback (MakeCallback (&UdpApplication::Receive, this));
157
158
}
159
160
void
161
UdpApplication::StopApplication (void)
162
{
163
  NS_LOG_FUNCTION (this);
164
  if(m_socket != NULL)
165
    {
166
      m_socket->Close ();
167
    }
168
  else
169
    {
170
      NS_LOG_WARN ("UdpApplication found null socket to close in StopApplication");
171
    }
172
}
173
174
void
175
UdpApplication::SetPacketReceivedCallback (PacketReceivedCallback callback)
176
{
177
  NS_LOG_FUNCTION (this);
178
  if (callback.IsNull ())
179
    {
180
      NS_LOG_DEBUG ("UdpApplication:Setting NULL packet received callback!");
181
    }
182
  m_packetReceivedCallback = callback;
183
}
184
185
void
186
UdpApplication::SetPort (uint16_t port)
187
{
188
  m_port = port;
189
}
190
191
uint16_t
192
UdpApplication::GetPort () const
193
{
194
  return m_port;
195
}
196
197
} // namespace ns3
(-)a/src/applications/model/udp-application.h (+115 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
#ifndef UDP_APPLICATION_H
21
#define UDP_APPLICATION_H
22
23
24
#include "ns3/application.h"
25
#include "ns3/packet.h"
26
#include "ns3/mac8-address.h"
27
#include "ns3/socket.h"
28
29
namespace ns3 {
30
31
/**
32
 * \ingroup applications
33
 * \brief a simple UDP application.
34
 *
35
 * The Application uses the NS-3 IP stack, and UDP Sockets to send and received
36
 * data.
37
 * The user interacts with the application mainly through two functions:
38
 * Send () : Used to send the data in the buffer parameter to the
39
 * destination IPv4/IPv6 address.
40
 * SetPacketReceivedCallback (): Used to set a receive callback where the raw
41
 * packet payload, and the source IPv4/IPv6 address will be passed as
42
 * parameters.
43
 */
44
45
class UdpApplication : public Application
46
{
47
public:
48
49
  typedef Callback<void, Address, Buffer> PacketReceivedCallback;
50
51
  /**
52
   * \brief Get the type ID.
53
   * \return the object TypeId
54
   */
55
  static TypeId GetTypeId (void);
56
57
  /**
58
   * create a udp application
59
   */
60
  UdpApplication ();
61
  virtual ~UdpApplication ();
62
63
  /**
64
   * \brief Send a packet to the destination
65
   * \param dst the destination IPv4 address
66
   * \param dataBuffer the data to be sent
67
   */
68
  void Send (Address dst, const Buffer dataBuffer);
69
70
  /**
71
   * \brief Set a callback when a packet is received
72
   * \param callback the packet received callback
73
   */
74
  void SetPacketReceivedCallback (PacketReceivedCallback callback);
75
76
  /**
77
   * \brief Sets the UDP port number.
78
   * \param port The UDP port number
79
   */
80
  void SetPort (uint16_t port);
81
82
  /**
83
   * \brief Gets the UDP port number.
84
   * \return The UDP port number
85
   */
86
  uint16_t GetPort () const;
87
88
private:
89
90
  // inherited from Application base class.
91
  virtual void StartApplication (void);
92
  virtual void StopApplication (void);
93
  virtual void DoDispose (void);
94
  /**
95
   * \brief Return the application ID in the node.
96
   * \returns the application id
97
   */
98
  uint32_t GetApplicationId (void) const;
99
  /**
100
   * \brief Receive a packet
101
   * \param socket the receiving socket
102
   *
103
   * This function is called by lower layers through a callback.
104
   */
105
  void Receive (Ptr<Socket> socket);
106
107
  Ptr<Socket> m_socket; //!< Associated socket
108
  uint16_t m_port;      //!< Port number
109
  PacketReceivedCallback m_packetReceivedCallback;
110
111
};
112
113
} // namespace ns3
114
115
#endif /* UDP_APPLICATION_H */
(-)a/src/applications/wscript (+8 lines)
 Lines 14-24   def build(bld): Link Here 
14
        'model/udp-echo-client.cc',
14
        'model/udp-echo-client.cc',
15
        'model/udp-echo-server.cc',
15
        'model/udp-echo-server.cc',
16
        'model/application-packet-probe.cc',
16
        'model/application-packet-probe.cc',
17
        'model/udp-application.cc',
18
        'model/raw-application.cc',
17
        'helper/bulk-send-helper.cc',
19
        'helper/bulk-send-helper.cc',
18
        'helper/on-off-helper.cc',
20
        'helper/on-off-helper.cc',
19
        'helper/packet-sink-helper.cc',
21
        'helper/packet-sink-helper.cc',
20
        'helper/udp-client-server-helper.cc',
22
        'helper/udp-client-server-helper.cc',
21
        'helper/udp-echo-helper.cc',
23
        'helper/udp-echo-helper.cc',
24
        'helper/udp-application-helper.cc',
25
        'helper/raw-application-helper.cc',
22
        ]
26
        ]
23
27
24
    applications_test = bld.create_ns3_module_test_library('applications')
28
    applications_test = bld.create_ns3_module_test_library('applications')
 Lines 40-50   def build(bld): Link Here 
40
        'model/udp-echo-client.h',
44
        'model/udp-echo-client.h',
41
        'model/udp-echo-server.h',
45
        'model/udp-echo-server.h',
42
        'model/application-packet-probe.h',
46
        'model/application-packet-probe.h',
47
        'model/udp-application.h',
48
        'model/raw-application.h',
43
        'helper/bulk-send-helper.h',
49
        'helper/bulk-send-helper.h',
44
        'helper/on-off-helper.h',
50
        'helper/on-off-helper.h',
45
        'helper/packet-sink-helper.h',
51
        'helper/packet-sink-helper.h',
46
        'helper/udp-client-server-helper.h',
52
        'helper/udp-client-server-helper.h',
47
        'helper/udp-echo-helper.h',
53
        'helper/udp-echo-helper.h',
54
        'helper/udp-application-helper.h',
55
        'helper/raw-application-helper.h',
48
        ]
56
        ]
49
57
50
    bld.ns3_python_bindings()
58
    bld.ns3_python_bindings()
(-)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, Buffer data)
115
{
116
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
117
            << "Node:" << Ipv6Address::ConvertFrom (src)
118
            << "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl;
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, Buffer>(&PrintReceivedPacket));
134
      node++;
135
    }
136
}
137
138
void
139
SendPacket ()
140
{
141
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
142
  NodeContainer::Iterator node = nodeContainer.Begin ();
143
  Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress ();
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
      Buffer dataBuffer;
150
      dataBuffer.AddAtEnd (sizeof (uint8_t));
151
      Buffer::Iterator i = dataBuffer.Begin ();
152
      i.WriteU8 (energy);
153
      double time = uniformRandomVariable->GetValue (0, 60);
154
      Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, dataBuffer);
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 (+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/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, Buffer data)
111
{
112
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
113
            << "Node:" << Ipv4Address::ConvertFrom (src)
114
            << "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl;
115
}
116
117
void
118
SetupApplications ()
119
{
120
  UdpApplicationHelper udpApplicationHelper;
121
  ApplicationContainer applicationContainer;
122
  udpApplicationHelper.SetPort (9);
123
  applicationContainer = udpApplicationHelper.Install (nodeContainer);
124
  applicationContainer.Start (Seconds (0));
125
  NodeContainer::Iterator node = nodeContainer.Begin ();
126
  while (node != nodeContainer.End ())
127
    {
128
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
129
      udpApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Buffer>(&PrintReceivedPacket));
130
      node++;
131
    }
132
}
133
134
void
135
SendPacket ()
136
{
137
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
138
  NodeContainer::Iterator node = nodeContainer.Begin ();
139
  Ipv4Address dst = (*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetAddress (0).GetLocal ();
140
  node++;
141
  while (node != nodeContainer.End ())
142
    {
143
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
144
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
145
      Buffer dataBuffer;
146
      dataBuffer.AddAtEnd (sizeof (uint8_t));
147
      Buffer::Iterator i = dataBuffer.Begin ();
148
      double time = uniformRandomVariable->GetValue (0, 15);
149
      i.WriteU8 (energy);
150
      Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, dataBuffer);
151
      node++;
152
    }
153
  Simulator::Schedule (Minutes (5), &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 (10));
174
  Simulator::Run ();
175
  Simulator::Destroy ();
176
177
  return 0;
178
}
(-)a/src/uan/examples/uan-ipv6-example.cc (+177 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, Buffer data)
110
{
111
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
112
            << "Node:" << Ipv6Address::ConvertFrom (src)
113
            << "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl;
114
}
115
116
void
117
SetupApplications ()
118
{
119
  UdpApplicationHelper udpApplicationHelper;
120
  ApplicationContainer applicationContainer;
121
  udpApplicationHelper.SetPort (9);
122
  applicationContainer = udpApplicationHelper.Install (nodeContainer);
123
  applicationContainer.Start (Seconds (0));
124
  NodeContainer::Iterator node = nodeContainer.Begin ();
125
  while (node != nodeContainer.End ())
126
    {
127
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
128
      udpApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Buffer>(&PrintReceivedPacket));
129
      node++;
130
    }
131
}
132
133
void
134
SendPacket ()
135
{
136
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
137
  NodeContainer::Iterator node = nodeContainer.Begin ();
138
  Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress ();
139
  node++;
140
  while (node != nodeContainer.End ())
141
    {
142
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
143
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
144
      Buffer dataBuffer;
145
      dataBuffer.AddAtEnd (sizeof (uint8_t));
146
      Buffer::Iterator i = dataBuffer.Begin ();
147
      double time = uniformRandomVariable->GetValue (0, 120);
148
      i.WriteU8 (energy);
149
      Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, dataBuffer);
150
      node++;
151
    }
152
  Simulator::Schedule (Hours (2), &SendPacket);
153
}
154
155
void
156
Run ()
157
{
158
  nodeContainer.Create (3);
159
  SetupPositions ();
160
  SetupEnergy ();
161
  SetupCommunications ();
162
  SetupApplications ();
163
  SendPacket ();
164
}
165
166
int
167
main (int argc, char *argv[])
168
{
169
  CommandLine cmd;
170
  cmd.Parse (argc, argv);
171
  Run ();
172
  Simulator::Stop (Days (30));
173
  Simulator::Run ();
174
  Simulator::Destroy ();
175
176
  return 0;
177
}
(-)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, Buffer data)
93
{
94
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
95
            << "Node:" << Mac8Address::ConvertFrom (src)
96
            << "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl;
97
}
98
99
void
100
SetupApplications ()
101
{
102
  RawApplicationHelper rawApplicationHelper;
103
  ApplicationContainer applicationContainer;
104
  applicationContainer = rawApplicationHelper.Install (nodeContainer);
105
  applicationContainer.Start (Seconds (0));
106
  NodeContainer::Iterator node = nodeContainer.Begin ();
107
  while (node != nodeContainer.End ())
108
    {
109
      Ptr<RawApplication> rawApplication = (*node)->GetApplication (0)->GetObject<RawApplication> ();
110
      rawApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Buffer>(&PrintReceivedPacket));
111
      node++;
112
    }
113
}
114
115
void
116
SendPacket ()
117
{
118
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
119
  NodeContainer::Iterator node = nodeContainer.Begin ();
120
  node++;
121
  while (node != nodeContainer.End ())
122
    {
123
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
124
      Ptr<RawApplication> rawApplication = (*node)->GetApplication (0)->GetObject<RawApplication> ();
125
      Buffer dataBuffer;
126
      dataBuffer.AddAtEnd (sizeof (uint8_t));
127
      Buffer::Iterator i = dataBuffer.Begin ();
128
      double time = uniformRandomVariable->GetValue (0, 15);
129
      i.WriteU8 (energy);
130
      Simulator::Schedule (Seconds (time), &RawApplication::Send, rawApplication, Mac8Address (0), dataBuffer);
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