examples/udp-echo.cc
changeset 1504 36ecc970ba96
parent 1502 4b4799567e2a
child 1514 addb79f47ba5
equal deleted inserted replaced
1503:53dd8f414ba6 1504:36ecc970ba96
    26 // - Tracing of queues and packet receptions to file "udp-echo.tr"
    26 // - Tracing of queues and packet receptions to file "udp-echo.tr"
    27 
    27 
    28 #include "ns3/command-line.h"
    28 #include "ns3/command-line.h"
    29 #include "ns3/default-value.h"
    29 #include "ns3/default-value.h"
    30 #include "ns3/ptr.h"
    30 #include "ns3/ptr.h"
    31 #include "ns3/debug.h"
    31 #include "ns3/log.h"
    32 #include "ns3/simulator.h"
    32 #include "ns3/simulator.h"
    33 #include "ns3/nstime.h"
    33 #include "ns3/nstime.h"
    34 #include "ns3/data-rate.h"
    34 #include "ns3/data-rate.h"
    35 #include "ns3/ascii-trace.h"
    35 #include "ns3/ascii-trace.h"
    36 #include "ns3/pcap-trace.h"
    36 #include "ns3/pcap-trace.h"
    48 #include "ns3/udp-echo-client.h"
    48 #include "ns3/udp-echo-client.h"
    49 #include "ns3/udp-echo-server.h"
    49 #include "ns3/udp-echo-server.h"
    50 
    50 
    51 using namespace ns3;
    51 using namespace ns3;
    52 
    52 
    53 NS_DEBUG_COMPONENT_DEFINE ("UdpEcho");
    53 NS_LOG_COMPONENT_DEFINE ("UdpEchoExample");
    54 
    54 
    55 int 
    55 int 
    56 main (int argc, char *argv[])
    56 main (int argc, char *argv[])
    57 {
    57 {
    58 //
    58 //
    59 // Users may find it convenient to turn on explicit debugging
    59 // Users may find it convenient to turn on explicit debugging
    60 // for selected modules; the below lines suggest how to do this
    60 // for selected modules; the below lines suggest how to do this
    61 //
    61 //
    62 #if 0
    62 #if 0
    63   DebugComponentEnable("UdpEcho");
    63   LogComponentEnable ("UdpEchoExample", LOG_LEVEL_INFO);
    64 
    64 
    65   DebugComponentEnable("Object");
    65   LogComponentEnable("Object", LOG_LEVEL_ALL);
    66   DebugComponentEnable("Queue");
    66   LogComponentEnable("Queue", LOG_LEVEL_ALL);
    67   DebugComponentEnable("DropTailQueue");
    67   LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL);
    68   DebugComponentEnable("Channel");
    68   LogComponentEnable("Channel", LOG_LEVEL_ALL);
    69   DebugComponentEnable("CsmaChannel");
    69   LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL);
    70   DebugComponentEnable("CsmaNetDevice");
    70   LogComponentEnable("NetDevice", LOG_LEVEL_ALL);
    71   DebugComponentEnable("Ipv4L3Protocol");
    71   LogComponentEnable("CsmaNetDevice", LOG_LEVEL_ALL);
    72   DebugComponentEnable("NetDevice");
    72   LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
    73   DebugComponentEnable("PacketSocket");
    73   LogComponentEnable("PacketSocket", LOG_LEVEL_ALL);
    74   DebugComponentEnable("OnOffApplication");
    74   LogComponentEnable("Socket", LOG_LEVEL_ALL);
    75   DebugComponentEnable("Socket");
    75   LogComponentEnable("UdpSocket", LOG_LEVEL_ALL);
    76   DebugComponentEnable("UdpSocket");
    76   LogComponentEnable("UdpL4Protocol", LOG_LEVEL_ALL);
    77   DebugComponentEnable("UdpL4Protocol");
    77   LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
    78   DebugComponentEnable("Ipv4L3Protocol");
    78   LogComponentEnable("Ipv4StaticRouting", LOG_LEVEL_ALL);
    79   DebugComponentEnable("Ipv4StaticRouting");
    79   LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL);
    80   DebugComponentEnable("Ipv4Interface");
    80   LogComponentEnable("ArpIpv4Interface", LOG_LEVEL_ALL);
    81   DebugComponentEnable("ArpIpv4Interface");
    81   LogComponentEnable("Ipv4LoopbackInterface", LOG_LEVEL_ALL);
    82   DebugComponentEnable("Ipv4LoopbackInterface");
    82   LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL);
    83   DebugComponentEnable("UdpEchoClient");
    83   LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL);
    84   DebugComponentEnable("UdpEchoServer");
    84   LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL);
       
    85   LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL);
    85 #endif
    86 #endif
    86 
       
    87   DebugComponentEnable("UdpEcho");
       
    88   DebugComponentEnable("UdpEchoClient");
       
    89   DebugComponentEnable("UdpEchoServer");
       
    90 //
    87 //
    91 // Set up default values for the simulation.  Use the DefaultValue::Bind()
    88 // Set up default values for the simulation.  Use the DefaultValue::Bind()
    92 // technique to tell the system what subclass of Queue to use.  The Bind
    89 // technique to tell the system what subclass of Queue to use.  The Bind
    93 // command command tells the queue factory which class to instantiate when the
    90 // command command tells the queue factory which class to instantiate when the
    94 // queue factory is invoked in the topology code
    91 // queue factory is invoked in the topology code
   100 //
    97 //
   101   CommandLine::Parse (argc, argv);
    98   CommandLine::Parse (argc, argv);
   102 //
    99 //
   103 // Explicitly create the nodes required by the topology (shown above).
   100 // Explicitly create the nodes required by the topology (shown above).
   104 //
   101 //
   105   NS_DEBUG("Create nodes.");
   102   NS_LOG_INFO ("Create nodes.");
   106   Ptr<Node> n0 = Create<InternetNode> ();
   103   Ptr<Node> n0 = Create<InternetNode> ();
   107   Ptr<Node> n1 = Create<InternetNode> (); 
   104   Ptr<Node> n1 = Create<InternetNode> (); 
   108   Ptr<Node> n2 = Create<InternetNode> (); 
   105   Ptr<Node> n2 = Create<InternetNode> (); 
   109   Ptr<Node> n3 = Create<InternetNode> ();
   106   Ptr<Node> n3 = Create<InternetNode> ();
   110 
   107 
   111   NS_DEBUG("Create channels.");
   108   NS_LOG_INFO ("Create channels.");
   112 //
   109 //
   113 // Explicitly create the channels required by the topology (shown above).
   110 // Explicitly create the channels required by the topology (shown above).
   114 //
   111 //
   115   Ptr<CsmaChannel> lan = CsmaTopology::CreateCsmaChannel(
   112   Ptr<CsmaChannel> lan = CsmaTopology::CreateCsmaChannel(
   116     DataRate(5000000), MilliSeconds(2));
   113     DataRate(5000000), MilliSeconds(2));
   117 
   114 
   118   NS_DEBUG("Build Topology.");
   115   NS_LOG_INFO ("Build Topology.");
   119 //
   116 //
   120 // Now fill out the topology by creating the net devices required to connect
   117 // Now fill out the topology by creating the net devices required to connect
   121 // the nodes to the channels and hooking them up.  AddIpv4CsmaNetDevice will
   118 // the nodes to the channels and hooking them up.  AddIpv4CsmaNetDevice will
   122 // create a net device, add a MAC address (in memory of the pink flamingo) and
   119 // create a net device, add a MAC address (in memory of the pink flamingo) and
   123 // connect the net device to a nodes and also to a channel. the 
   120 // connect the net device to a nodes and also to a channel. the 
   137   uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
   134   uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
   138     Mac48Address("08:00:2e:00:00:03"));
   135     Mac48Address("08:00:2e:00:00:03"));
   139 //
   136 //
   140 // We've got the "hardware" in place.  Now we need to add IP addresses.
   137 // We've got the "hardware" in place.  Now we need to add IP addresses.
   141 //
   138 //
   142   NS_DEBUG("Assign IP Addresses.");
   139   NS_LOG_INFO ("Assign IP Addresses.");
   143 //
   140 //
   144 // XXX BUGBUG
   141 // XXX BUGBUG
   145 // Need a better way to get the interface index.  The point-to-point topology
   142 // Need a better way to get the interface index.  The point-to-point topology
   146 // as implemented can't return the index since it creates interfaces on both
   143 // as implemented can't return the index since it creates interfaces on both
   147 // sides (i.e., it does AddIpv4Addresses, not AddIpv4Address).  We need a
   144 // sides (i.e., it does AddIpv4Addresses, not AddIpv4Address).  We need a
   162     Ipv4Mask("255.255.255.0"));
   159     Ipv4Mask("255.255.255.0"));
   163   
   160   
   164   CsmaIpv4Topology::AddIpv4Address (n3, nd3, Ipv4Address("10.1.1.4"), 
   161   CsmaIpv4Topology::AddIpv4Address (n3, nd3, Ipv4Address("10.1.1.4"), 
   165     Ipv4Mask("255.255.255.0"));
   162     Ipv4Mask("255.255.255.0"));
   166 
   163 
   167   NS_DEBUG("Create Applications.");
   164   NS_LOG_INFO ("Create Applications.");
   168 //
   165 //
   169 // Create a UdpEchoServer application on node one.
   166 // Create a UdpEchoServer application on node one.
   170 //
   167 //
   171   uint16_t port = 80;
   168   uint16_t port = 80;
   172 
   169 
   191   client->Stop (Seconds(10.));
   188   client->Stop (Seconds(10.));
   192 //
   189 //
   193 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
   190 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
   194 // Trace output will be sent to the file "udp-echo.tr"
   191 // Trace output will be sent to the file "udp-echo.tr"
   195 //
   192 //
   196   NS_DEBUG("Configure Tracing.");
   193   NS_LOG_INFO ("Configure Tracing.");
   197   AsciiTrace asciitrace ("udp-echo.tr");
   194   AsciiTrace asciitrace ("udp-echo.tr");
   198   asciitrace.TraceAllNetDeviceRx ();
   195   asciitrace.TraceAllNetDeviceRx ();
   199   asciitrace.TraceAllQueues ();
   196   asciitrace.TraceAllQueues ();
   200 //
   197 //
   201 // Also configure some tcpdump traces; each interface will be traced.
   198 // Also configure some tcpdump traces; each interface will be traced.
   207   PcapTrace pcaptrace ("udp-echo.pcap");
   204   PcapTrace pcaptrace ("udp-echo.pcap");
   208   pcaptrace.TraceAllIp ();
   205   pcaptrace.TraceAllIp ();
   209 //
   206 //
   210 // Now, do the actual simulation.
   207 // Now, do the actual simulation.
   211 //
   208 //
   212   NS_DEBUG("Run Simulation.");
   209   NS_LOG_INFO ("Run Simulation.");
   213   Simulator::Run ();
   210   Simulator::Run ();
   214   Simulator::Destroy ();
   211   Simulator::Destroy ();
   215   NS_DEBUG("Done.");
   212   NS_LOG_INFO ("Done.");
   216 }
   213 }