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

(-)a/src/applications/helper/raw-application-helper.cc (+80 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#include "raw-application-helper.h"
20
#include "ns3/names.h"
21
#include "ns3/packet-socket-factory.h"
22
#include "ns3/packet-socket-helper.h"
23
#include "ns3/net-device.h"
24
#include "ns3/address.h"
25
26
27
namespace ns3 {
28
29
RawApplicationHelper::RawApplicationHelper ()
30
{
31
  m_factory.SetTypeId ("ns3::UdpRawApplication");
32
}
33
34
void 
35
RawApplicationHelper::SetAttribute (std::string name, const AttributeValue &value)
36
{
37
  m_factory.Set (name, value);
38
}
39
40
ApplicationContainer
41
RawApplicationHelper::Install (Ptr<Node> node) const
42
{
43
  return ApplicationContainer (DoInstall (node));
44
}
45
46
ApplicationContainer
47
RawApplicationHelper::Install (std::string nodeName) const
48
{
49
  Ptr<Node> node = Names::Find<Node> (nodeName);
50
  return ApplicationContainer (DoInstall (node));
51
}
52
53
ApplicationContainer
54
RawApplicationHelper::Install (NodeContainer c) const
55
{
56
  ApplicationContainer apps;
57
  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
58
    {
59
      apps.Add (DoInstall (*i));
60
    }
61
62
  return apps;
63
}
64
65
Ptr<Application>
66
RawApplicationHelper::DoInstall (Ptr<Node> node) const
67
{
68
  if(node->GetObject<PacketSocketFactory> () == NULL)
69
    {
70
      PacketSocketHelper packetSocketHelper;
71
      packetSocketHelper.Install (node);
72
    }
73
74
  Ptr<Application> app = m_factory.Create<Application> ();
75
  node->AddApplication (app);
76
77
  return app;
78
}
79
80
} // 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
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#ifndef RAW_APPLICATION_HELPER_H
20
#define RAW_APPLICATION_HELPER_H
21
22
23
#include "ns3/object-factory.h"
24
#include "ns3/attribute.h"
25
#include "ns3/node-container.h"
26
#include "ns3/application-container.h"
27
28
namespace ns3 {
29
30
/**
31
 * \ingroup applications
32
 * \brief A helper to make it easier to instantiate an ns3::RawApplication 
33
 * on a set of nodes.
34
 */
35
class RawApplicationHelper
36
{
37
public:
38
  /**
39
   * Create a RawApplicationHelper to make it easier to work with RawApplication
40
   */
41
  RawApplicationHelper ();
42
43
  /**
44
   * Helper function used to set the underlying application attributes.
45
   */
46
  void SetAttribute (std::string name, const AttributeValue &value);
47
48
  /**
49
   * Install an ns3::RawApplication on each node of the input container
50
   * configured with all the attributes set with SetAttribute.
51
   *
52
   * \param c NodeContainer of the set of nodes on which a RawApplication 
53
   * will be installed.
54
   * \returns Container of Ptr to the applications installed.
55
   */
56
  ApplicationContainer Install (NodeContainer c) const;
57
58
  /**
59
   * Install an ns3::RawApplication on the node configured with all the 
60
   * attributes set with SetAttribute.
61
   *
62
   * \param node The node on which a RawApplication will be installed.
63
   * \returns Container of Ptr to the applications installed.
64
   */
65
  ApplicationContainer Install (Ptr<Node> node) const;
66
67
  /**
68
   * Install an ns3::RawApplication on the node configured with all the 
69
   * attributes set with SetAttribute.
70
   *
71
   * \param nodeName The node on which a RawApplication will be installed.
72
   * \returns Container of Ptr to the applications installed.
73
   */
74
  ApplicationContainer Install (std::string nodeName) const;
75
76
private:
77
  /**
78
   * Install an ns3::RawApplication on the node configured with all the 
79
   * attributes set with SetAttribute.
80
   *
81
   * \param node The node on which a RawApplication will be installed.
82
   * \returns Ptr to the application installed.
83
   */
84
  Ptr<Application> DoInstall (Ptr<Node> node) const;
85
86
  ObjectFactory m_factory; //!< Object factory.
87
};
88
89
} // namespace ns3
90
91
#endif /* RAW_APPLICATION_HELPER_H */
92
(-)a/src/applications/helper/udp-application-helper.cc (+90 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#include "udp-application-helper.h"
20
#include "ns3/names.h"
21
#include "ns3/packet-socket-factory.h"
22
#include "ns3/packet-socket-helper.h"
23
#include "ns3/net-device.h"
24
#include "ns3/address.h"
25
#include "ns3/udp-application.h"
26
#include "ns3/internet-stack-helper.h"
27
28
29
namespace ns3 {
30
31
UdpApplicationHelper::UdpApplicationHelper ()
32
  : m_port (0)
33
{
34
  m_factory.SetTypeId ("ns3::UdpApplication");
35
}
36
37
void 
38
UdpApplicationHelper::SetAttribute (std::string name, const AttributeValue &value)
39
{
40
  m_factory.Set (name, value);
41
}
42
43
ApplicationContainer
44
UdpApplicationHelper::Install (Ptr<Node> node) const
45
{
46
  return ApplicationContainer (DoInstall (node));
47
}
48
49
ApplicationContainer
50
UdpApplicationHelper::Install (std::string nodeName) const
51
{
52
  Ptr<Node> node = Names::Find<Node> (nodeName);
53
  return ApplicationContainer (DoInstall (node));
54
}
55
56
ApplicationContainer
57
UdpApplicationHelper::Install (NodeContainer c) const
58
{
59
  ApplicationContainer apps;
60
  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
61
    {
62
      apps.Add (DoInstall (*i));
63
    }
64
65
  return apps;
66
}
67
68
Ptr<Application>
69
UdpApplicationHelper::DoInstall (Ptr<Node> node) const
70
{
71
  if(node->GetObject<Ipv4> () == NULL)
72
    {
73
      InternetStackHelper internet;
74
      internet.Install (node);
75
    }
76
77
  Ptr<Application> app = m_factory.Create<Application> ();
78
  node->AddApplication (app);
79
  app->GetObject<UdpApplication> ()->SetPort (m_port);
80
81
  return app;
82
}
83
84
void
85
UdpApplicationHelper::SetPort (uint16_t port)
86
{
87
  m_port = port;
88
}
89
90
} // 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
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#ifndef UDP_APPLICATION_HELPER_H
20
#define UDP_APPLICATION_HELPER_H
21
22
23
#include "ns3/object-factory.h"
24
#include "ns3/attribute.h"
25
#include "ns3/node-container.h"
26
#include "ns3/application-container.h"
27
28
namespace ns3 {
29
30
/**
31
 * \ingroup applications
32
 * \brief A helper to make it easier to instantiate an ns3::UdpApplication 
33
 * on a set of nodes.
34
 */
35
class UdpApplicationHelper
36
{
37
public:
38
  /**
39
   * Create a UdpApplicationHelper to make it easier to work with UdpApplication
40
   */
41
  UdpApplicationHelper ();
42
43
  /**
44
   * Helper function used to set the underlying application attributes.
45
   */
46
  void SetAttribute (std::string name, const AttributeValue &value);
47
48
  /**
49
   * Install an ns3::UdpApplication on each node of the input container
50
   * configured with all the attributes set with SetAttribute.
51
   *
52
   * \param c NodeContainer of the set of nodes on which a UdpApplication 
53
   * will be installed.
54
   * \returns Container of Ptr to the applications installed.
55
   */
56
  ApplicationContainer Install (NodeContainer c) const;
57
58
  /**
59
   * Install an ns3::UdpApplication on the node configured with all the 
60
   * attributes set with SetAttribute.
61
   *
62
   * \param node The node on which a UdpApplication will be installed.
63
   * \returns Container of Ptr to the applications installed.
64
   */
65
  ApplicationContainer Install (Ptr<Node> node) const;
66
67
  /**
68
   * Install an ns3::UdpApplication on the node configured with all the 
69
   * attributes set with SetAttribute.
70
   *
71
   * \param nodeName The node on which a UdpApplication will be installed.
72
   * \returns Container of Ptr to the applications installed.
73
   */
74
  ApplicationContainer Install (std::string nodeName) const;
75
76
  /**
77
   * Sets the UDP port number that the UdpApplication will use
78
   *
79
   * \param port The UDP port number
80
   */
81
  void SetPort (uint16_t port);
82
83
private:
84
  /**
85
   * Install an ns3::UdpApplication on the node configured with all the 
86
   * attributes set with SetAttribute.
87
   *
88
   * \param node The node on which a UdpApplication will be installed.
89
   * \returns Ptr to the application installed.
90
   */
91
  Ptr<Application> DoInstall (Ptr<Node> node) const;
92
93
  ObjectFactory m_factory; //!< Object factory.
94
  uint16_t m_port;
95
};
96
97
} // namespace ns3
98
99
#endif /* UDP_APPLICATION_HELPER_H */
100
(-)a/src/applications/model/raw-application.cc (+171 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
#include "raw-application.h"
19
#include "ns3/log.h"
20
#include "ns3/socket-factory.h"
21
#include "ns3/packet-socket-address.h"
22
#include "ns3/packet-socket.h"
23
24
namespace ns3 {
25
26
NS_LOG_COMPONENT_DEFINE ("RawApplication");
27
28
NS_OBJECT_ENSURE_REGISTERED (RawApplication);
29
30
TypeId 
31
RawApplication::GetTypeId (void)
32
{
33
  static TypeId tid = TypeId ("ns3::RawApplication")
34
    .SetParent<Application> ()
35
    .SetGroupName ("Applications")
36
    .AddConstructor<RawApplication> ()
37
  ;
38
  return tid;
39
}
40
41
RawApplication::RawApplication ()
42
{
43
  NS_LOG_FUNCTION (this);
44
  m_socket = NULL;
45
  m_packetReceivedCallback.Nullify ();
46
}
47
RawApplication::~RawApplication ()
48
{
49
  NS_LOG_FUNCTION (this);
50
}
51
52
void
53
RawApplication::DoDispose (void)
54
{
55
  NS_LOG_FUNCTION (this);
56
  m_socket = NULL;
57
  m_packetReceivedCallback.Nullify ();
58
  Application::DoDispose ();
59
}
60
61
uint32_t
62
RawApplication::GetApplicationId (void) const
63
{
64
  NS_LOG_FUNCTION (this);
65
  Ptr<Node> node = GetNode ();
66
  for (uint32_t i = 0; i < node->GetNApplications (); ++i)
67
    {
68
      if (node->GetApplication (i) == this)
69
        {
70
          return i;
71
        }
72
    }
73
  NS_ASSERT_MSG (false, "forgot to add application to node");
74
  return 0;
75
}
76
77
78
void 
79
RawApplication::Send (Address dst, const Buffer dataBuffer)
80
{
81
  NS_LOG_FUNCTION (this);
82
  const uint8_t * data = dataBuffer.PeekData ();
83
  Address local = GetNode ()->GetDevice (0)->GetAddress ();
84
  
85
  if(dst != local)
86
    { 
87
      PacketSocketAddress socketAddress;
88
      socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ());
89
      socketAddress.SetPhysicalAddress (dst);
90
      socketAddress.SetProtocol (0);
91
      Ptr<Packet> packet = Create<Packet> (data, dataBuffer.GetSize ());
92
      if(m_socket == NULL)
93
        {
94
          StartApplication ();
95
        }
96
      m_socket->SendTo (packet, 0, socketAddress);
97
    }
98
}
99
100
void
101
RawApplication::Receive (Ptr<Socket> socket)
102
{
103
  NS_LOG_FUNCTION (this << socket);
104
  NS_ASSERT (m_socket != NULL);
105
  while (m_socket->GetRxAvailable () > 0)
106
    {
107
      Address srcAddress;
108
      Ptr<Packet> packet = m_socket->RecvFrom (srcAddress);
109
      uint8_t *buffer = new uint8_t[packet->GetSize ()];
110
      packet->CopyData (buffer, packet->GetSize ());
111
      
112
      PacketSocketAddress packetSocketAddress = PacketSocketAddress::ConvertFrom (srcAddress);
113
      srcAddress = packetSocketAddress.GetPhysicalAddress ();
114
      
115
      if(!m_packetReceivedCallback.IsNull ())
116
        {
117
          Buffer dataBuffer;
118
          dataBuffer.AddAtEnd ( packet->GetSize ());
119
          Buffer::Iterator iterator = dataBuffer.Begin ();
120
          iterator.Write (buffer, packet->GetSize ());
121
          m_packetReceivedCallback (srcAddress, dataBuffer);
122
        }
123
      delete[] buffer;
124
    }
125
126
} 
127
128
void 
129
RawApplication::StartApplication (void)
130
{
131
  NS_LOG_FUNCTION (this);
132
  PacketSocketAddress socketAddress;
133
  socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ());
134
  socketAddress.SetProtocol (0);
135
  if (!m_socket)
136
    {
137
      m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::PacketSocketFactory"));
138
    }
139
  m_socket->Bind ();
140
  m_socket->Connect (socketAddress);
141
  m_socket->SetRecvCallback (MakeCallback (&RawApplication::Receive, this));
142
}
143
 
144
void 
145
RawApplication::StopApplication (void)
146
{
147
  NS_LOG_FUNCTION (this);
148
  if(m_socket != NULL)
149
    {
150
      m_socket->Close ();
151
    }
152
  else
153
    {
154
      NS_LOG_WARN ("RawApplication found null socket to close in StopApplication");
155
    }
156
}
157
158
void
159
RawApplication::SetPacketReceivedCallback (PacketReceivedCallback callback)
160
{
161
  NS_LOG_FUNCTION (this);
162
  if (callback.IsNull ())
163
    {
164
      NS_LOG_DEBUG ("RawApplication:Setting NULL packet received callback!");
165
    }
166
  m_packetReceivedCallback = callback;
167
}
168
169
170
171
} // namespace ns3
(-)a/src/applications/model/raw-application.h (+103 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#ifndef RAW_APPLICATION_H
20
#define RAW_APPLICATION_H
21
22
23
#include "ns3/application.h"
24
#include "ns3/packet.h"
25
#include "ns3/socket.h"
26
27
namespace ns3 {
28
29
/**
30
 * \ingroup applications
31
 * \brief a simple Raw application.
32
 * 
33
 * The Application combines both the client-side and the server-side 
34
 * functionality, and provides an easy to use bidirectional communications 
35
 * between the nodes. It runs directly on top of the data link layer, so
36
 * raw data is sent in the payload of the frame.
37
 * 
38
 * The user interacts with the application mainly through two functions:
39
 * Send () : Used to send the data in the buffer parameter to the
40
 * destination layer 2 address.
41
 * SetPacketReceivedCallback (): Used to set a receive callback where the raw 
42
 * packet payload, and the source layer 2 address will be passed as
43
 * parameters.
44
 */
45
46
class RawApplication : public Application
47
{
48
public:
49
50
  typedef Callback<void, Address, Buffer> PacketReceivedCallback;
51
52
  /**
53
   * \brief Get the type ID.
54
   * \return the object TypeId
55
   */
56
  static TypeId GetTypeId (void);
57
58
  /**
59
   * create a raw application
60
   */
61
  RawApplication ();
62
  virtual ~RawApplication ();
63
64
  /**
65
   * \brief Send a packet to the destination
66
   * \param dst the destination address
67
   * \param dataBuffer the data to be sent
68
   */
69
  void Send (Address dst, const Buffer dataBuffer);
70
71
  /**
72
   * \brief Set a callback when a packet is received
73
   * \param callback the packet received callback
74
   */
75
  void SetPacketReceivedCallback (PacketReceivedCallback callback);
76
77
private:
78
79
  // inherited from Application base class.
80
  virtual void StartApplication (void);
81
  virtual void StopApplication (void);
82
  virtual void DoDispose (void);
83
  /**
84
   * \brief Return the application ID in the node.
85
   * \returns the application id
86
   */
87
  uint32_t GetApplicationId (void) const;
88
  /**
89
   * \brief Receive a packet
90
   * \param socket the receiving socket
91
   *
92
   * This function is called by lower layers through a callback.
93
   */
94
  void Receive (Ptr<Socket> socket);
95
96
  Ptr<Socket> m_socket; //!< Associated socket
97
  PacketReceivedCallback m_packetReceivedCallback;
98
99
};
100
101
} // namespace ns3
102
103
#endif /* RAW_APPLICATION_H */
(-)a/src/applications/model/udp-application.cc (+195 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
#include "udp-application.h"
19
#include "ns3/log.h"
20
#include "ns3/socket-factory.h"
21
#include "ns3/packet-socket-address.h"
22
#include "ns3/packet-socket.h"
23
#include "ns3/ipv4-l3-protocol.h"
24
#include "ns3/ipv6-l3-protocol.h"
25
26
namespace ns3 {
27
28
NS_LOG_COMPONENT_DEFINE ("UdpApplication");
29
30
NS_OBJECT_ENSURE_REGISTERED (UdpApplication);
31
32
TypeId 
33
UdpApplication::GetTypeId (void)
34
{
35
  static TypeId tid = TypeId ("ns3::UdpApplication")
36
    .SetParent<Application> ()
37
    .SetGroupName ("Applications")
38
    .AddConstructor<UdpApplication> ()
39
  ;
40
  return tid;
41
}
42
43
UdpApplication::UdpApplication ()
44
{
45
  NS_LOG_FUNCTION (this);
46
  m_socket = NULL;
47
  m_packetReceivedCallback.Nullify ();
48
}
49
UdpApplication::~UdpApplication ()
50
{
51
  NS_LOG_FUNCTION (this);
52
}
53
54
void
55
UdpApplication::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
UdpApplication::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
UdpApplication::Send (Address dst, const Buffer dataBuffer)
82
{
83
  NS_LOG_FUNCTION (this);
84
  const uint8_t * data = dataBuffer.PeekData ();
85
  Ptr<Packet> packet = Create<Packet> (data, dataBuffer.GetSize ());
86
  if(m_socket == NULL)
87
    {
88
      StartApplication ();
89
    }
90
  
91
    if(Ipv6Address::IsMatchingType (dst))
92
      {
93
        Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), m_port);
94
        m_socket->SendTo (packet, 0, ipv6_destination);
95
      }
96
    else if(Ipv4Address::IsMatchingType (dst))
97
      {
98
        InetSocketAddress ipv4_destination = InetSocketAddress (Ipv4Address::ConvertFrom (dst), m_port);
99
        m_socket->SendTo (packet, 0, ipv4_destination);
100
      }
101
}
102
103
void
104
UdpApplication::Receive (Ptr<Socket> socket)
105
{
106
  NS_LOG_FUNCTION (this << socket);
107
  NS_ASSERT (m_socket != NULL);
108
  Address srcAddress;
109
  while (m_socket->GetRxAvailable () > 0)
110
    {
111
      Ptr<Packet> packet = m_socket->RecvFrom (srcAddress);
112
      uint8_t *buffer = new uint8_t[packet->GetSize ()];
113
      packet->CopyData (buffer, packet->GetSize ());
114
      if(!m_packetReceivedCallback.IsNull ())
115
        {
116
          Buffer dataBuffer;
117
          dataBuffer.AddAtEnd ( packet->GetSize ());
118
          Buffer::Iterator iterator = dataBuffer.Begin ();
119
          iterator.Write (buffer, packet->GetSize ());
120
          
121
          if(Inet6SocketAddress::IsMatchingType (srcAddress))
122
            {
123
              m_packetReceivedCallback (Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 (), dataBuffer);
124
            }
125
          else if(InetSocketAddress::IsMatchingType (srcAddress))
126
            {
127
              m_packetReceivedCallback (InetSocketAddress::ConvertFrom (srcAddress).GetIpv4 (), dataBuffer);
128
            }
129
        }
130
      delete[] buffer;
131
    }
132
} 
133
134
void 
135
UdpApplication::StartApplication (void)
136
{
137
  NS_LOG_FUNCTION (this);
138
  if (!m_socket)
139
    {
140
      m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::UdpSocketFactory"));
141
    }
142
  
143
  if(GetNode ()->GetObject<Ipv6> () != NULL)
144
    {
145
      Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), m_port);
146
      m_socket->Bind (ipv6_local);
147
    }
148
149
  if(GetNode ()->GetObject<Ipv4> () != NULL)
150
    {
151
      InetSocketAddress ipv4_local = InetSocketAddress (Ipv4Address::GetAny (), m_port);
152
      m_socket->Bind (ipv4_local);
153
    }
154
  m_socket->SetRecvCallback (MakeCallback (&UdpApplication::Receive, this));
155
  
156
}
157
 
158
void 
159
UdpApplication::StopApplication (void)
160
{
161
  NS_LOG_FUNCTION (this);
162
  if(m_socket != NULL)
163
    {
164
      m_socket->Close ();
165
    }
166
  else
167
    {
168
      NS_LOG_WARN ("UdpApplication found null socket to close in StopApplication");
169
    }
170
}
171
172
void
173
UdpApplication::SetPacketReceivedCallback (PacketReceivedCallback callback)
174
{
175
  NS_LOG_FUNCTION (this);
176
  if (callback.IsNull ())
177
    {
178
      NS_LOG_DEBUG ("UdpApplication:Setting NULL packet received callback!");
179
    }
180
  m_packetReceivedCallback = callback;
181
}
182
183
void
184
UdpApplication::SetPort (uint16_t port)
185
{
186
  m_port = port;
187
}
188
189
uint16_t
190
UdpApplication::GetPort () const
191
{
192
  return m_port;
193
}
194
195
} // namespace ns3
(-)a/src/applications/model/udp-application.h (+114 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#ifndef UDP_APPLICATION_H
20
#define UDP_APPLICATION_H
21
22
23
#include "ns3/application.h"
24
#include "ns3/packet.h"
25
#include "ns3/mac8-address.h"
26
#include "ns3/socket.h"
27
28
namespace ns3 {
29
30
/**
31
 * \ingroup applications
32
 * \brief a simple UDP application.
33
 * 
34
 * The Application uses the NS-3 IP stack, and UDP Sockets to send and received
35
 * data.
36
 * The user interacts with the application mainly through two functions:
37
 * Send () : Used to send the data in the buffer parameter to the
38
 * destination IPv4/IPv6 address.
39
 * SetPacketReceivedCallback (): Used to set a receive callback where the raw 
40
 * packet payload, and the source IPv4/IPv6 address will be passed as
41
 * parameters.
42
 */
43
44
class UdpApplication : public Application
45
{
46
public:
47
48
  typedef Callback<void, Address, Buffer> PacketReceivedCallback;
49
50
  /**
51
   * \brief Get the type ID.
52
   * \return the object TypeId
53
   */
54
  static TypeId GetTypeId (void);
55
56
  /**
57
   * create a udp application
58
   */
59
  UdpApplication ();
60
  virtual ~UdpApplication ();
61
62
  /**
63
   * \brief Send a packet to the destination
64
   * \param dst the destination IPv4 address
65
   * \param dataBuffer the data to be sent
66
   */
67
  void Send (Address dst, const Buffer dataBuffer);
68
69
  /**
70
   * \brief Set a callback when a packet is received
71
   * \param callback the packet received callback
72
   */
73
  void SetPacketReceivedCallback (PacketReceivedCallback callback);
74
75
  /**
76
   * \brief Sets the UDP port number.
77
   * \param port The UDP port number
78
   */
79
  void SetPort (uint16_t port);
80
  
81
  /**
82
   * \brief Gets the UDP port number.
83
   * \return The UDP port number
84
   */
85
  uint16_t GetPort () const;
86
87
private:
88
89
  // inherited from Application base class.
90
  virtual void StartApplication (void);
91
  virtual void StopApplication (void);
92
  virtual void DoDispose (void);
93
  /**
94
   * \brief Return the application ID in the node.
95
   * \returns the application id
96
   */
97
  uint32_t GetApplicationId (void) const;
98
  /**
99
   * \brief Receive a packet
100
   * \param socket the receiving socket
101
   *
102
   * This function is called by lower layers through a callback.
103
   */
104
  void Receive (Ptr<Socket> socket);
105
106
  Ptr<Socket> m_socket; //!< Associated socket
107
  uint16_t m_port;      //!< Port number
108
  PacketReceivedCallback m_packetReceivedCallback;
109
110
};
111
112
} // namespace ns3
113
114
#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 (+181 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 *
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#include "ns3/core-module.h"
20
#include "ns3/internet-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/sixlowpan-helper.h"
32
#include "ns3/sixlowpan-net-device.h"
33
34
using namespace ns3;
35
36
/**
37
 *
38
 * This example shows the usage of UdpApplication to transfer data.
39
 * Two nodes are sending their remaining energy percentage (1 byte)
40
 * to a gateway node, that prints the received data.
41
 * The transmissions are scheduled at random times to avoid collisions
42
 *
43
 */
44
45
NS_LOG_COMPONENT_DEFINE ("Uan6lowpanExample");
46
47
void SetupPositions ();
48
void SetupEnergy ();
49
void SetupCommunications ();
50
void PrintReceivedPacket (Address src, Buffer data);
51
void SetupApplications ();
52
void SendPacket ();
53
void Run ();
54
55
NodeContainer nodeContainer;
56
57
void
58
SetupPositions ()
59
{
60
  MobilityHelper mobilityHelper;
61
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
62
  mobilityHelper.Install (nodeContainer);
63
  nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
64
  nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
65
  nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
66
}
67
68
void
69
SetupEnergy ()
70
{
71
  BasicEnergySourceHelper energySourceHelper;
72
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
73
  energySourceHelper.Install (nodeContainer);
74
}
75
76
void
77
SetupCommunications ()
78
{
79
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
80
  UanHelper uanHelper;
81
  NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel);
82
  EnergySourceContainer energySourceContainer;
83
  NodeContainer::Iterator node = nodeContainer.Begin ();
84
  while (node != nodeContainer.End ())
85
    {
86
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
87
      node++;
88
    }
89
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
90
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
91
92
  SixLowPanHelper sixLowPanHelper;
93
  NetDeviceContainer sixlowpanNetDevices = sixLowPanHelper.Install (netDeviceContainer);
94
  
95
  InternetStackHelper internetStackHelper;
96
  internetStackHelper.Install (nodeContainer);
97
  
98
  Ipv6AddressHelper ipv6AddressHelper;
99
  ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64));
100
  ipv6AddressHelper.Assign (sixlowpanNetDevices);
101
  
102
  node = nodeContainer.Begin ();
103
  while (node != nodeContainer.End ())
104
    {
105
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute("DAD", BooleanValue(false));
106
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute("ReachableTime", TimeValue (Seconds (3600)));
107
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute("RetransmissionTime", TimeValue (Seconds (1000)));
108
      node++;
109
    }
110
}
111
112
void
113
PrintReceivedPacket (Address src, Buffer data)
114
{
115
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
116
            << "Node:" << Ipv6Address::ConvertFrom (src)
117
            << "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl;
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, Buffer>(&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
      Buffer dataBuffer;
149
      dataBuffer.AddAtEnd (sizeof (uint8_t));
150
      Buffer::Iterator i = dataBuffer.Begin ();
151
      i.WriteU8 (energy);
152
      double time = uniformRandomVariable->GetValue (0, 60);
153
      Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, dataBuffer);
154
      node++;
155
    }
156
  Simulator::Schedule (Hours (2), &SendPacket);
157
}
158
159
void
160
Run ()
161
{
162
  nodeContainer.Create (3);
163
  SetupPositions ();
164
  SetupEnergy ();
165
  SetupCommunications ();
166
  SetupApplications ();
167
  SendPacket ();
168
}
169
170
int
171
main (int argc, char *argv[])
172
{
173
  CommandLine cmd;
174
  cmd.Parse (argc, argv);
175
  Run ();
176
  Simulator::Stop (Days (50));
177
  Simulator::Run ();
178
  Simulator::Destroy ();
179
180
  return 0;
181
}
(-)a/src/uan/examples/uan-ipv4-example.cc (+177 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 *
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#include "ns3/core-module.h"
20
#include "ns3/node-container.h"
21
#include "ns3/mobility-helper.h"
22
#include "ns3/mobility-model.h"
23
#include "ns3/basic-energy-source-helper.h"
24
#include "ns3/energy-source-container.h"
25
#include "ns3/uan-helper.h"
26
#include "ns3/uan-channel.h"
27
#include "ns3/acoustic-modem-energy-model-helper.h"
28
#include "ns3/udp-application-helper.h"
29
#include "ns3/udp-application.h"
30
#include "ns3/ipv4-address-helper.h"
31
#include "ns3/internet-stack-helper.h"
32
#include "ns3/ipv4-interface.h"
33
#include "ns3/arp-cache.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
 * ARP parameters should be tuned, based on the network size
44
 *
45
 */
46
47
NS_LOG_COMPONENT_DEFINE ("UanIpv4Example");
48
49
void SetupPositions ();
50
void SetupEnergy ();
51
void SetupCommunications ();
52
void PrintReceivedPacket (Address src, Buffer data);
53
void SetupApplications ();
54
void SendPacket ();
55
void Run ();
56
57
NodeContainer nodeContainer;
58
59
void
60
SetupPositions ()
61
{
62
  MobilityHelper mobilityHelper;
63
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
64
  mobilityHelper.Install (nodeContainer);
65
  nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
66
  nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
67
  nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
68
}
69
70
void
71
SetupEnergy ()
72
{
73
  BasicEnergySourceHelper energySourceHelper;
74
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
75
  energySourceHelper.Install (nodeContainer);
76
}
77
78
void
79
SetupCommunications ()
80
{
81
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
82
  UanHelper uanHelper;
83
  NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel);
84
  EnergySourceContainer energySourceContainer;
85
  NodeContainer::Iterator node = nodeContainer.Begin ();
86
  while (node != nodeContainer.End ())
87
    {
88
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
89
      node++;
90
    }
91
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
92
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
93
94
  InternetStackHelper internetStackHelper;
95
  internetStackHelper.Install (nodeContainer);
96
  Ipv4AddressHelper ipv4AddressHelper;
97
  ipv4AddressHelper.SetBase ("10.0.0.0", "255.255.255.0");  
98
  ipv4AddressHelper.Assign (netDeviceContainer);
99
100
  node = nodeContainer.Begin ();
101
  while (node != nodeContainer.End ())
102
    {
103
      (*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetArpCache ()->SetWaitReplyTimeout (Seconds (10));
104
      node++;
105
    }
106
}
107
108
void
109
PrintReceivedPacket (Address src, Buffer data)
110
{
111
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
112
            << "Node:" << Ipv4Address::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
  Ipv4Address dst = (*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetAddress (0).GetLocal ();
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, 15);
148
      i.WriteU8 (energy);
149
      Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, dataBuffer);
150
      node++;
151
    }
152
  Simulator::Schedule (Minutes (5), &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 (10));
173
  Simulator::Run ();
174
  Simulator::Destroy ();
175
176
  return 0;
177
}
(-)a/src/uan/examples/uan-ipv6-example.cc (+176 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 *
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#include "ns3/core-module.h"
20
#include "ns3/internet-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
32
using namespace ns3;
33
34
/**
35
 *
36
 * This example shows the usage of UdpApplication 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 ("UanIpv6Example");
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
  InternetStackHelper internetStackHelper;
91
  internetStackHelper.Install (nodeContainer);
92
  
93
  Ipv6AddressHelper ipv6AddressHelper;
94
  ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64));
95
  ipv6AddressHelper.Assign (netDeviceContainer);
96
  
97
  node = nodeContainer.Begin ();
98
  while (node != nodeContainer.End ())
99
    {
100
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute("DAD", BooleanValue(false));
101
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute("ReachableTime", TimeValue (Seconds (3600)));
102
      (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute("RetransmissionTime", TimeValue (Seconds (1000)));
103
      node++;
104
    }
105
}
106
107
void
108
PrintReceivedPacket (Address src, Buffer data)
109
{
110
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
111
            << "Node:" << Ipv6Address::ConvertFrom (src)
112
            << "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl;
113
}
114
115
void
116
SetupApplications ()
117
{
118
  UdpApplicationHelper udpApplicationHelper;
119
  ApplicationContainer applicationContainer;
120
  udpApplicationHelper.SetPort (9);
121
  applicationContainer = udpApplicationHelper.Install (nodeContainer);
122
  applicationContainer.Start (Seconds (0));
123
  NodeContainer::Iterator node = nodeContainer.Begin ();
124
  while (node != nodeContainer.End ())
125
    {
126
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
127
      udpApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Buffer>(&PrintReceivedPacket));
128
      node++;
129
    }
130
}
131
132
void
133
SendPacket ()
134
{
135
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
136
  NodeContainer::Iterator node = nodeContainer.Begin ();
137
  Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress ();
138
  node++;
139
  while (node != nodeContainer.End ())
140
    {
141
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
142
      Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> ();
143
      Buffer dataBuffer;
144
      dataBuffer.AddAtEnd (sizeof (uint8_t));
145
      Buffer::Iterator i = dataBuffer.Begin ();
146
      double time = uniformRandomVariable->GetValue (0, 120);
147
      i.WriteU8 (energy);
148
      Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, dataBuffer);
149
      node++;
150
    }
151
  Simulator::Schedule (Hours (2), &SendPacket);
152
}
153
154
void
155
Run ()
156
{
157
  nodeContainer.Create (3);
158
  SetupPositions ();
159
  SetupEnergy ();
160
  SetupCommunications ();
161
  SetupApplications ();
162
  SendPacket ();
163
}
164
165
int
166
main (int argc, char *argv[])
167
{
168
  CommandLine cmd;
169
  cmd.Parse (argc, argv);
170
  Run ();
171
  Simulator::Stop (Days (30));
172
  Simulator::Run ();
173
  Simulator::Destroy ();
174
175
  return 0;
176
}
(-)a/src/uan/examples/uan-raw-example.cc (+158 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 *
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#include "ns3/core-module.h"
20
#include "ns3/node-container.h"
21
#include "ns3/mobility-helper.h"
22
#include "ns3/mobility-model.h"
23
#include "ns3/basic-energy-source-helper.h"
24
#include "ns3/energy-source-container.h"
25
#include "ns3/uan-helper.h"
26
#include "ns3/uan-channel.h"
27
#include "ns3/acoustic-modem-energy-model-helper.h"
28
#include "ns3/raw-application-helper.h"
29
#include "ns3/raw-application.h"
30
31
using namespace ns3;
32
33
/**
34
 *
35
 * This example shows the usage of RawApplication 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 ("UanRawExample");
43
44
void SetupPositions ();
45
void SetupEnergy ();
46
void SetupCommunications ();
47
void PrintReceivedPacket (Address src, Buffer data);
48
void SetupApplications ();
49
void SendPacket ();
50
void Run ();
51
52
NodeContainer nodeContainer;
53
54
void
55
SetupPositions ()
56
{
57
  MobilityHelper mobilityHelper;
58
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
59
  mobilityHelper.Install (nodeContainer);
60
  nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
61
  nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
62
  nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
63
}
64
65
void
66
SetupEnergy ()
67
{
68
  BasicEnergySourceHelper energySourceHelper;
69
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
70
  energySourceHelper.Install (nodeContainer);
71
}
72
73
void
74
SetupCommunications ()
75
{
76
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
77
  UanHelper uanHelper;
78
  NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel);
79
  EnergySourceContainer energySourceContainer;
80
  NodeContainer::Iterator node = nodeContainer.Begin ();
81
  while (node != nodeContainer.End ())
82
    {
83
      energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
84
      node++;
85
    }
86
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
87
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
88
}
89
90
void
91
PrintReceivedPacket (Address src, Buffer data)
92
{
93
  std::cout << "Time:" << Simulator::Now ().GetDays () << "|"
94
            << "Node:" << Mac8Address::ConvertFrom (src)
95
            << "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl;
96
}
97
98
void
99
SetupApplications ()
100
{
101
  RawApplicationHelper rawApplicationHelper;
102
  ApplicationContainer applicationContainer;
103
  applicationContainer = rawApplicationHelper.Install (nodeContainer);
104
  applicationContainer.Start (Seconds (0));
105
  NodeContainer::Iterator node = nodeContainer.Begin ();
106
  while (node != nodeContainer.End ())
107
    {
108
      Ptr<RawApplication> rawApplication = (*node)->GetApplication (0)->GetObject<RawApplication> ();
109
      rawApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Buffer>(&PrintReceivedPacket));
110
      node++;
111
    }
112
}
113
114
void
115
SendPacket ()
116
{
117
  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
118
  NodeContainer::Iterator node = nodeContainer.Begin ();
119
  node++;
120
  while (node != nodeContainer.End ())
121
    {
122
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
123
      Ptr<RawApplication> rawApplication = (*node)->GetApplication (0)->GetObject<RawApplication> ();
124
      Buffer dataBuffer;
125
      dataBuffer.AddAtEnd (sizeof (uint8_t));
126
      Buffer::Iterator i = dataBuffer.Begin ();
127
      double time = uniformRandomVariable->GetValue (0, 15);
128
      i.WriteU8 (energy);
129
      Simulator::Schedule (Seconds (time), &RawApplication::Send, rawApplication, Mac8Address (0), dataBuffer);
130
      node++;
131
    }
132
  Simulator::Schedule (Hours (2), &SendPacket);
133
}
134
135
void
136
Run ()
137
{
138
  Packet::EnablePrinting ();
139
  nodeContainer.Create (3);
140
  SetupPositions ();
141
  SetupEnergy ();
142
  SetupCommunications ();
143
  SetupApplications ();
144
  SendPacket ();
145
}
146
147
int
148
main (int argc, char *argv[])
149
{
150
  CommandLine cmd;
151
  cmd.Parse (argc, argv);
152
  Run ();
153
  Simulator::Stop (Days (60));
154
  Simulator::Run ();
155
  Simulator::Destroy ();
156
157
  return 0;
158
}
(-)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