about adding udpechoserver tracing sources

23 views
Skip to first unread message

Lester 宋梦凯

unread,
Jun 8, 2016, 11:04:12 PM6/8/16
to ns-3-users
hi,
sorry for my poor English,I'll try to make my question clear

I want to get how long a packet takes form source to destination(time delay)

there is a member variables named m_txTrace in class UpdEchoClient.when a packet sends,m_txTrace's value will change ,so I can get the starttime

but,there is no such a trace sources in UdpEchoServer.I plan to add one to UdpEchoServer.h and UdpEchoServer.cc

here is my code:

UdpEchoServer.h

namespace ns3 {

class Socket;
class Packet;

class UdpEchoServer : public Application 
{
public:
  static TypeId GetTypeId (void);
  UdpEchoServer ();
  virtual ~UdpEchoServer ();

protected:
  virtual void DoDispose (void);

private:

  virtual void StartApplication (void);
  virtual void StopApplication (void);

  void HandleRead (Ptr<Socket> socket);

  uint16_t m_port;
  Ptr<Socket> m_socket;
  Ptr<Socket> m_socket6;
  Address m_local;
  TracedCallback<Ptr<const Packet> > m_rxTrace;
};

} // namespace ns3

UdpEchoServer.cc

TypeId
UdpEchoServer::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::UdpEchoServer")
    .SetParent<Application> ()
    .AddConstructor<UdpEchoServer> ()
    .AddAttribute ("Port", "Port on which we listen for incoming packets.",
                   UintegerValue (9),
                   MakeUintegerAccessor (&UdpEchoServer::m_port),
                   MakeUintegerChecker<uint16_t> ())
    .AddTraceSource ("Rx", "A new packet is received",
                     MakeTraceSourceAccessor (&UdpEchoServer::m_rxTrace))
  ;
  return tid;
}

void 
UdpEchoServer::HandleRead (Ptr<Socket> socket)
{
  NS_LOG_FUNCTION (this << socket);

  Ptr<Packet> packet;
  Address from;
  while ((packet = socket->RecvFrom (from)))
    {
      m_rxTrace(packet);
      if (InetSocketAddress::IsMatchingType (from))
        {
          NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
                       InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
                       InetSocketAddress::ConvertFrom (from).GetPort ());
        }
      else if (Inet6SocketAddress::IsMatchingType (from))
        {
          NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
                       Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
                       InetSocketAddress::ConvertFrom (from).GetPort ());
        }

      packet->RemoveAllPacketTags ();
      packet->RemoveAllByteTags ();

      NS_LOG_LOGIC ("Echoing packet");
      socket->SendTo (packet, 0, from);

      if (InetSocketAddress::IsMatchingType (from))
        {
          NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server sent " << packet->GetSize () << " bytes to " <<
                       InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
                       InetSocketAddress::ConvertFrom (from).GetPort ());
        }
      else if (Inet6SocketAddress::IsMatchingType (from))
        {
          NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server sent " << packet->GetSize () << " bytes to " <<
                       Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
                       InetSocketAddress::ConvertFrom (from).GetPort ());
        }
    }
}

here is my test.cc code:

UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApp = echoServer.Install(nodes.Get(0));
serverApp.Start(Seconds(0));
serverApp.Stop(Seconds(totalTime));

UdpEchoClientHelper echoClient (interfaces.GetAddress(0),9);
echoClient.SetAttribute("MaxPackets",UintegerValue(10));
echoClient.SetAttribute("Interval",TimeValue(Seconds(1)));
echoClient.SetAttribute("PacketSize",UintegerValue(1024));
ApplicationContainer clientApp = echoClient.Install(nodes.Get(1));
clientApp.Start(Seconds(1.0));
clientApp.Stop(Seconds(totalTime)-Seconds(0.001));

ostringstream oss;
oss<<"/NodeList/"<<nodes.Get(1)->GetId()<<"/ApplicationList/"<<0<<"/$ns3::UdpEchoClient/Tx";
Config::Connect (oss.str(),MakeCallback (&Tx));

oss<<"/NodeList/"<<nodes.Get(0)->GetId()<<"/ApplicationList/"<<0<<"/$ns3::UdpEchoServer/Rx";
Config::Connect (oss.str(),MakeCallback (&Rx));

Rx and Tx are callback functions.

 
I don't konw whether it will work to modify the source code in src/ ,or i can't?

I kown I can add a new module to ns3 system to do this,but modifying the source code is easier

before I run the test.cc.I run this ../waf configure --enable-examples --enable-tests and ./waf build

The result is, when the packet is sending,Tx executes,but when server get the packet ,it won't execute Rx


by the way, my mythod to get the time-delay may be stupid (I'm new to ns3),so if there is any better way,please tell me

thanks a lot
Reply all
Reply to author
Forward
0 new messages