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

(-)a/src/network/utils/simple-recv-app.cc (+125 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2013 Universita' di Firenze
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
7
 * published by the Free Software Foundation;
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19
 */
20
21
#include "ns3/log.h"
22
#include "ns3/nstime.h"
23
#include "ns3/packet-socket-address.h"
24
#include "ns3/packet-socket.h"
25
#include "ns3/packet-socket-factory.h"
26
#include "ns3/socket.h"
27
#include "ns3/simulator.h"
28
#include "ns3/socket-factory.h"
29
#include "ns3/packet.h"
30
#include "ns3/uinteger.h"
31
#include "ns3/abort.h"
32
#include "simple-recv-app.h"
33
#include <cstdlib>
34
#include <cstdio>
35
36
namespace ns3 {
37
38
NS_LOG_COMPONENT_DEFINE ("SimpleRecvApp")
39
  ;
40
NS_OBJECT_ENSURE_REGISTERED (SimpleRecvApp)
41
  ;
42
43
TypeId
44
SimpleRecvApp::GetTypeId (void)
45
{
46
  static TypeId tid = TypeId ("ns3::SimpleRecvApp")
47
    .SetParent<Application> ()
48
    .AddConstructor<SimpleRecvApp> ()
49
    .AddTraceSource ("Rx", "A packet has been received",
50
                     MakeTraceSourceAccessor (&SimpleRecvApp::m_rxTrace))
51
  ;
52
  return tid;
53
}
54
55
SimpleRecvApp::SimpleRecvApp ()
56
{
57
  NS_LOG_FUNCTION (this);
58
  m_pktRx = 0;
59
  m_bytesRx = 0;
60
  m_socket = 0;
61
}
62
63
SimpleRecvApp::~SimpleRecvApp ()
64
{
65
  NS_LOG_FUNCTION (this);
66
}
67
68
void
69
SimpleRecvApp::DoDispose (void)
70
{
71
  NS_LOG_FUNCTION (this);
72
  Application::DoDispose ();
73
}
74
75
void
76
SimpleRecvApp::StartApplication (void)
77
{
78
  NS_LOG_FUNCTION (this);
79
80
  if (m_socket == 0)
81
    {
82
      TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
83
      m_socket = Socket::CreateSocket (GetNode (), tid);
84
      m_socket->Bind ();
85
    }
86
87
  m_socket->SetRecvCallback (MakeCallback (&SimpleRecvApp::HandleRead, this));
88
}
89
90
void
91
SimpleRecvApp::StopApplication (void)
92
{
93
  NS_LOG_FUNCTION (this);
94
  m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
95
  m_socket->Close ();
96
}
97
98
void
99
SimpleRecvApp::HandleRead (Ptr<Socket> socket)
100
{
101
  NS_LOG_FUNCTION (this << socket);
102
  Ptr<Packet> packet;
103
  Address from;
104
  while ((packet = socket->RecvFrom (from)))
105
    {
106
      if (packet->GetSize () == 0)
107
        { //EOF
108
          break;
109
        }
110
      m_pktRx ++;
111
      m_bytesRx += packet->GetSize ();
112
      if (PacketSocketAddress::IsMatchingType (from))
113
        {
114
          NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
115
                       << "s packet sink received "
116
                       << packet->GetSize () << " bytes from "
117
                       << PacketSocketAddress::ConvertFrom(from)
118
                       << " total Rx " << m_pktRx << " packets"
119
                       << " and " << m_bytesRx << " bytes");
120
        }
121
      m_rxTrace (packet, from);
122
    }
123
}
124
125
} // Namespace ns3
(-)a/src/network/utils/simple-recv-app.h (+80 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2013 Universita' di Firenze
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
7
 * published by the Free Software Foundation;
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19
 */
20
21
#ifndef SIMPLE_RECV_APP_H
22
#define SIMPLE_RECV_APP_H
23
24
#include "ns3/application.h"
25
#include "ns3/event-id.h"
26
#include "ns3/ptr.h"
27
#include "ns3/packet-socket-address.h"
28
29
namespace ns3 {
30
31
class Socket;
32
class Packet;
33
34
/**
35
 * \ingroup socket
36
 *
37
 * \brief A simple client.
38
 *
39
 * Sends packets using PacketSocket. It does not require (or use) IP.
40
 *
41
 */
42
class SimpleRecvApp : public Application
43
{
44
public:
45
  /**
46
   * \brief Get the type ID.
47
   * \return the object TypeId
48
   */
49
  static TypeId GetTypeId (void);
50
51
  SimpleRecvApp ();
52
53
  virtual ~SimpleRecvApp ();
54
55
protected:
56
  virtual void DoDispose (void);
57
58
private:
59
60
  virtual void StartApplication (void);
61
  virtual void StopApplication (void);
62
63
  /**
64
   * \brief Handle a packet received by the application
65
   * \param socket the receiving socket
66
   */
67
  void HandleRead (Ptr<Socket> socket);
68
69
  uint32_t m_pktRx;    //!< The number of received packets
70
  uint32_t m_bytesRx;  //!< Total bytes received
71
72
  Ptr<Socket> m_socket; //!< Socket
73
74
  /// Traced Callback: received packets, source address.
75
  TracedCallback<Ptr<const Packet>, const Address &> m_rxTrace;
76
};
77
78
} // namespace ns3
79
80
#endif /* SIMPLE_RECV_APP_H */
(-)a/src/network/utils/simple-send-app.cc (+169 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2013 Universita' di Firenze
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
7
 * published by the Free Software Foundation;
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19
 */
20
21
#include "ns3/log.h"
22
#include "ns3/nstime.h"
23
#include "ns3/packet-socket-address.h"
24
#include "ns3/packet-socket.h"
25
#include "ns3/packet-socket-factory.h"
26
#include "ns3/socket.h"
27
#include "ns3/simulator.h"
28
#include "ns3/socket-factory.h"
29
#include "ns3/packet.h"
30
#include "ns3/uinteger.h"
31
#include "ns3/abort.h"
32
#include "simple-send-app.h"
33
#include <cstdlib>
34
#include <cstdio>
35
36
namespace ns3 {
37
38
NS_LOG_COMPONENT_DEFINE ("SimpleSendApp")
39
  ;
40
NS_OBJECT_ENSURE_REGISTERED (SimpleSendApp)
41
  ;
42
43
TypeId
44
SimpleSendApp::GetTypeId (void)
45
{
46
  static TypeId tid = TypeId ("ns3::SimpleSendApp")
47
    .SetParent<Application> ()
48
    .AddConstructor<SimpleSendApp> ()
49
    .AddAttribute ("MaxPackets",
50
                   "The maximum number of packets the application will send",
51
                   UintegerValue (100),
52
                   MakeUintegerAccessor (&SimpleSendApp::m_count),
53
                   MakeUintegerChecker<uint32_t> ())
54
    .AddAttribute ("Interval",
55
                   "The time to wait between packets", TimeValue (Seconds (1.0)),
56
                   MakeTimeAccessor (&SimpleSendApp::m_interval),
57
                   MakeTimeChecker ())
58
    .AddAttribute ("RemoteAddress",
59
                   "The destination Address of the outbound packets",
60
                   AddressValue (),
61
                   MakeAddressAccessor (&SimpleSendApp::m_peerAddress),
62
                   MakeAddressChecker ())
63
    .AddAttribute ("PacketSize",
64
                   "Size of packets generated. The minimum packet size is 12 bytes which is the size of the header carrying the sequence number and the time stamp.",
65
                   UintegerValue (1024),
66
                   MakeUintegerAccessor (&SimpleSendApp::m_size),
67
                   MakeUintegerChecker<uint32_t> (12,1500))
68
  ;
69
  return tid;
70
}
71
72
SimpleSendApp::SimpleSendApp ()
73
{
74
  NS_LOG_FUNCTION (this);
75
  m_sent = 0;
76
  m_socket = 0;
77
  m_sendEvent = EventId ();
78
}
79
80
SimpleSendApp::~SimpleSendApp ()
81
{
82
  NS_LOG_FUNCTION (this);
83
}
84
85
void
86
SimpleSendApp::SetRemote (PacketSocketAddress addr)
87
{
88
  NS_LOG_FUNCTION (this << addr);
89
  m_peerAddress = Address(addr);
90
}
91
92
void
93
SimpleSendApp::SetRemote (Address addr)
94
{
95
  NS_LOG_FUNCTION (this << addr);
96
  m_peerAddress = addr;
97
}
98
99
void
100
SimpleSendApp::DoDispose (void)
101
{
102
  NS_LOG_FUNCTION (this);
103
  Application::DoDispose ();
104
}
105
106
void
107
SimpleSendApp::StartApplication (void)
108
{
109
  NS_LOG_FUNCTION (this);
110
111
  if (m_socket == 0)
112
    {
113
      TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
114
      m_socket = Socket::CreateSocket (GetNode (), tid);
115
      if (PacketSocketAddress::IsMatchingType(m_peerAddress) == true)
116
        {
117
          m_socket->Bind ();
118
          m_socket->Connect (m_peerAddress);
119
        }
120
      else
121
        {
122
          NS_ABORT_MSG ("Peer address type not matching PacketSocketAddress");
123
        }
124
    }
125
126
  m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
127
  m_sendEvent = Simulator::Schedule (Seconds (0.0), &SimpleSendApp::Send, this);
128
}
129
130
void
131
SimpleSendApp::StopApplication (void)
132
{
133
  NS_LOG_FUNCTION (this);
134
  Simulator::Cancel (m_sendEvent);
135
  m_socket->Close ();
136
}
137
138
void
139
SimpleSendApp::Send (void)
140
{
141
  NS_LOG_FUNCTION (this);
142
  NS_ASSERT (m_sendEvent.IsExpired ());
143
144
  Ptr<Packet> p = Create<Packet> (m_size);
145
146
  std::stringstream peerAddressStringStream;
147
  peerAddressStringStream << PacketSocketAddress::ConvertFrom (m_peerAddress);
148
149
  if ((m_socket->Send (p)) >= 0)
150
    {
151
      ++m_sent;
152
      NS_LOG_INFO ("TraceDelay TX " << m_size << " bytes to "
153
                                    << peerAddressStringStream.str () << " Uid: "
154
                                    << p->GetUid () << " Time: "
155
                                    << (Simulator::Now ()).GetSeconds ());
156
    }
157
  else
158
    {
159
      NS_LOG_INFO ("Error while sending " << m_size << " bytes to "
160
                                          << peerAddressStringStream.str ());
161
    }
162
163
  if (m_sent < m_count)
164
    {
165
      m_sendEvent = Simulator::Schedule (m_interval, &SimpleSendApp::Send, this);
166
    }
167
}
168
169
} // Namespace ns3
(-)a/src/network/utils/simple-send-app.h (+92 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2013 Universita' di Firenze
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
7
 * published by the Free Software Foundation;
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19
 */
20
21
#ifndef SIMPLE_SEND_APP_H
22
#define SIMPLE_SEND_APP_H
23
24
#include "ns3/application.h"
25
#include "ns3/event-id.h"
26
#include "ns3/ptr.h"
27
#include "ns3/packet-socket-address.h"
28
29
namespace ns3 {
30
31
class Socket;
32
class Packet;
33
34
/**
35
 * \ingroup socket
36
 *
37
 * \brief A simple client.
38
 *
39
 * Sends packets using PacketSocket. It does not require (or use) IP.
40
 *
41
 */
42
class SimpleSendApp : public Application
43
{
44
public:
45
  /**
46
   * \brief Get the type ID.
47
   * \return the object TypeId
48
   */
49
  static TypeId GetTypeId (void);
50
51
  SimpleSendApp ();
52
53
  virtual ~SimpleSendApp ();
54
55
  /**
56
   * \brief set the remote address and port
57
   * \param addr remote address
58
   */
59
  void SetRemote (PacketSocketAddress addr);
60
  /**
61
   * \brief set the remote address and port
62
   * \param addr remote address
63
   */
64
  void SetRemote (Address addr);
65
66
protected:
67
  virtual void DoDispose (void);
68
69
private:
70
71
  virtual void StartApplication (void);
72
  virtual void StopApplication (void);
73
74
  /**
75
   * \brief Send a packet
76
   */
77
  void Send (void);
78
79
  uint32_t m_count; //!< Maximum number of packets the application will send
80
  Time m_interval; //!< Packet inter-send time
81
  uint32_t m_size; //!< Size of the sent packet
82
83
  uint32_t m_sent; //!< Counter for sent packets
84
  Ptr<Socket> m_socket; //!< Socket
85
  Address m_peerAddress; //!< Remote peer address
86
  EventId m_sendEvent; //!< Event to send the next packet
87
88
};
89
90
} // namespace ns3
91
92
#endif /* SIMPLE_SEND_APP_H */
(-)a/src/network/wscript (+4 lines)
 Lines 53-58    Link Here 
53
        'utils/red-queue.cc',
53
        'utils/red-queue.cc',
54
        'utils/simple-channel.cc',
54
        'utils/simple-channel.cc',
55
        'utils/simple-net-device.cc',
55
        'utils/simple-net-device.cc',
56
        'utils/simple-send-app.cc',
57
        'utils/simple-recv-app.cc',
56
        'utils/packet-data-calculators.cc',
58
        'utils/packet-data-calculators.cc',
57
        'utils/packet-probe.cc',
59
        'utils/packet-probe.cc',
58
        'helper/application-container.cc',
60
        'helper/application-container.cc',
 Lines 134-139    Link Here 
134
        'utils/sgi-hashmap.h',
136
        'utils/sgi-hashmap.h',
135
        'utils/simple-channel.h',
137
        'utils/simple-channel.h',
136
        'utils/simple-net-device.h',
138
        'utils/simple-net-device.h',
139
        'utils/simple-send-app.h',
140
        'utils/simple-recv-app.h',
137
        'utils/pcap-test.h',
141
        'utils/pcap-test.h',
138
        'utils/packet-data-calculators.h',
142
        'utils/packet-data-calculators.h',
139
        'utils/packet-probe.h',
143
        'utils/packet-probe.h',

Return to bug 1811