View | Details | Raw Unified | Return to bug 2889
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 (+163 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 Ptr<Packet> packet)
82
{
83
  NS_LOG_FUNCTION (this);
84
  Address local = GetNode ()->GetDevice (0)->GetAddress ();
85
86
  if(dst != local)
87
    {
88
      PacketSocketAddress socketAddress;
89
      socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ());
90
      socketAddress.SetPhysicalAddress (dst);
91
      socketAddress.SetProtocol (0);
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
      PacketSocketAddress packetSocketAddress = PacketSocketAddress::ConvertFrom (srcAddress);
110
      srcAddress = packetSocketAddress.GetPhysicalAddress ();
111
112
      if(!m_packetReceivedCallback.IsNull ())
113
        {
114
          m_packetReceivedCallback (srcAddress, packet);
115
        }
116
    }
117
118
}
119
120
void
121
RawApplication::StartApplication (void)
122
{
123
  NS_LOG_FUNCTION (this);
124
  PacketSocketAddress socketAddress;
125
  socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ());
126
  socketAddress.SetProtocol (0);
127
  if (!m_socket)
128
    {
129
      m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::PacketSocketFactory"));
130
    }
131
  m_socket->Bind ();
132
  m_socket->Connect (socketAddress);
133
  m_socket->SetRecvCallback (MakeCallback (&RawApplication::Receive, this));
134
}
135
136
void
137
RawApplication::StopApplication (void)
138
{
139
  NS_LOG_FUNCTION (this);
140
  if(m_socket != NULL)
141
    {
142
      m_socket->Close ();
143
    }
144
  else
145
    {
146
      NS_LOG_WARN ("RawApplication found null socket to close in StopApplication");
147
    }
148
}
149
150
void
151
RawApplication::SetPacketReceivedCallback (PacketReceivedCallback callback)
152
{
153
  NS_LOG_FUNCTION (this);
154
  if (callback.IsNull ())
155
    {
156
      NS_LOG_DEBUG ("RawApplication:Setting NULL packet received callback!");
157
    }
158
  m_packetReceivedCallback = callback;
159
}
160
161
162
163
} // 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, Ptr<Packet>> 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 packet the data packet to be sent
69
   */
70
  void Send (Address dst, const Ptr<Packet> packet);
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 (+187 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 Ptr<Packet> packet)
84
{
85
  NS_LOG_FUNCTION (this);
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
      if(!m_packetReceivedCallback.IsNull ())
113
        {
114
          if(Inet6SocketAddress::IsMatchingType (srcAddress))
115
            {
116
              m_packetReceivedCallback (Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 (), packet);
117
            }
118
          else if(InetSocketAddress::IsMatchingType (srcAddress))
119
            {
120
              m_packetReceivedCallback (InetSocketAddress::ConvertFrom (srcAddress).GetIpv4 (), packet);
121
            }
122
        }
123
    }
124
}
125
126
void
127
UdpApplication::StartApplication (void)
128
{
129
  NS_LOG_FUNCTION (this);
130
  if (!m_socket)
131
    {
132
      m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::UdpSocketFactory"));
133
    }
134
135
  if(GetNode ()->GetObject<Ipv6> () != NULL)
136
    {
137
      Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), m_port);
138
      m_socket->Bind (ipv6_local);
139
    }
140
141
  if(GetNode ()->GetObject<Ipv4> () != NULL)
142
    {
143
      InetSocketAddress ipv4_local = InetSocketAddress (Ipv4Address::GetAny (), m_port);
144
      m_socket->Bind (ipv4_local);
145
    }
146
  m_socket->SetRecvCallback (MakeCallback (&UdpApplication::Receive, this));
147
148
}
149
150
void
151
UdpApplication::StopApplication (void)
152
{
153
  NS_LOG_FUNCTION (this);
154
  if(m_socket != NULL)
155
    {
156
      m_socket->Close ();
157
    }
158
  else
159
    {
160
      NS_LOG_WARN ("UdpApplication found null socket to close in StopApplication");
161
    }
162
}
163
164
void
165
UdpApplication::SetPacketReceivedCallback (PacketReceivedCallback callback)
166
{
167
  NS_LOG_FUNCTION (this);
168
  if (callback.IsNull ())
169
    {
170
      NS_LOG_DEBUG ("UdpApplication:Setting NULL packet received callback!");
171
    }
172
  m_packetReceivedCallback = callback;
173
}
174
175
void
176
UdpApplication::SetPort (uint16_t port)
177
{
178
  m_port = port;
179
}
180
181
uint16_t
182
UdpApplication::GetPort () const
183
{
184
  return m_port;
185
}
186
187
} // 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, Ptr<Packet>> 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 packet the data packet to be sent
67
   */
68
  void Send (Address dst, const Ptr<Packet> packet);
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()

Return to bug 2889