Index: src/switched-ethernet/examples/switched-ethernet-packet-socket.cc |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/switched-ethernet/examples/switched-ethernet-packet-socket.cc |
@@ -0,0 +1,141 @@ |
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
+/* |
+ * This program is free software; you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 2 as |
+ * published by the Free Software Foundation; |
+ * |
+ * This program is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with this program; if not, write to the Free Software |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
+ */ |
+ |
+// |
+// Network topology |
+// |
+// n0 ====== n1 |
+// |
+ |
+// - Two packet socket flows: from n0 to n1 and from n1 to n0 |
+// - Default 512 byte packets generated by traffic generator |
+// - Output from the PacketSink trace source will be sent to the |
+// switched-eth-packet-socket-sink.tr file |
+// ASCII trace output will be sent to the switched-eth-packet-socket.tr file |
+ |
+#include <iostream> |
+#include <fstream> |
+#include <string> |
+#include <cassert> |
+ |
+#include "ns3/core-module.h" |
+#include "ns3/network-module.h" |
+#include "ns3/switched-ethernet-module.h" |
+#include "ns3/applications-module.h" |
+ |
+using namespace ns3; |
+ |
+NS_LOG_COMPONENT_DEFINE ("SwitchedEthernetPacketSocketExample"); |
+ |
+std::ofstream g_os; |
+ |
+static void |
+SinkRx (std::string path, Ptr<const Packet> p, const Address &address) |
+{ |
+ g_os << p->GetSize () << std::endl; |
+} |
+ |
+int |
+main (int argc, char *argv[]) |
+{ |
+#if 0 |
+ LogComponentEnable ("SwitchedEthernetPacketSocketExample", LOG_LEVEL_INFO); |
+#endif |
+ |
+ CommandLine cmd; |
+ cmd.Parse (argc, argv); |
+ |
+ g_os.open ("switched-ethernet-packet-socket-sink.tr",std::ios_base::binary | std::ios_base::out); |
+ |
+ NS_LOG_INFO ("Create nodes."); |
+ |
+ //Create two nodes - both can send and receive simultaneously |
+ NodeContainer nodes; |
+ nodes.Create (2); |
+ |
+ PacketSocketHelper packetSocket; |
+ packetSocket.Install (nodes); |
+ |
+ // create the two full-duplex channels that are used to send packets to node 0 from nodes 1 and 2 |
+ NS_LOG_INFO ("Create channels."); |
Peter Barnes
2012/07/16 20:08:48
channel
|
+ Ptr<SwitchedEthernetChannel> channel = CreateObjectWithAttributes<SwitchedEthernetChannel> ( |
+ "DataRate", DataRateValue (DataRate (5000000)), |
+ "Delay", TimeValue (MilliSeconds (2))); |
+ |
+ |
+ // use a helper function to connect our nodes to the full-duplex channel. |
+ NS_LOG_INFO ("Build Topology."); |
+ SwitchedEthernetHelper switchedEth; |
+ switchedEth.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc")); |
+ NetDeviceContainer devs = switchedEth.Install (nodes, channel); |
+ |
+ |
+ NS_LOG_INFO ("Create Applications."); |
+ // Create the OnOff application to send raw datagrams |
+ PacketSocketAddress socket; |
+ socket.SetSingleDevice (devs.Get (0)->GetIfIndex ()); |
+ socket.SetPhysicalAddress (devs.Get (1)->GetAddress ()); |
+ socket.SetProtocol (2); |
+ OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket)); |
+ onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1.0))); |
+ onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0))); |
+ ApplicationContainer apps = onoff.Install (nodes.Get (0)); |
+ apps.Start (Seconds (1.0)); |
+ apps.Stop (Seconds (10.0)); |
+ |
+ socket.SetSingleDevice (devs.Get (1)->GetIfIndex ()); |
+ socket.SetPhysicalAddress (devs.Get (0)->GetAddress ()); |
+ socket.SetProtocol (2); |
+ onoff.SetAttribute ("Remote", AddressValue (socket)); |
+ onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0))); |
+ apps = onoff.Install (nodes.Get (1)); |
+ apps.Start (Seconds (1.0)); |
+ apps.Stop (Seconds (10.0)); |
+ |
+ // Install packet sink on node 0 to receive packets from node 1 |
+ PacketSinkHelper sink = PacketSinkHelper ("ns3::PacketSocketFactory", |
+ socket); |
+ apps = sink.Install (nodes.Get (0)); |
+ apps.Start (Seconds (0.0)); |
+ apps.Stop (Seconds (20.0)); |
+ |
+ // While the below trace sink is hooked to all nodes (the wildcard "*") |
+ // only one packet sink (on node 0) is actually added above, so |
+ // only the receive events on node 0 will be traced |
+ Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx", |
+ MakeCallback (&SinkRx)); |
+ |
+ // Configure tracing of all enqueue, dequeue, and NetDevice receive events |
+ // Trace output will be sent to the csma-packet-socket.tr file |
+ NS_LOG_INFO ("Configure Tracing."); |
+ |
+ // For more detailed information on what is going on, enable these two log functions |
+ // in debug mode. Note that this logging will generate a large amount of output. |
+ //LogComponentEnable ("SwitchedEthernetNetDevice", LOG_LEVEL_ALL); |
+ //LogComponentEnable ("SwitchedEthernetChannel", LOG_LEVEL_ALL); |
+ |
+ AsciiTraceHelper ascii; |
+ switchedEth.EnableAsciiAll (ascii.CreateFileStream ("switched-ethernet-packet-socket.tr")); |
+ |
+ NS_LOG_INFO ("Run Simulation."); |
+ Simulator::Run (); |
+ Simulator::Destroy (); |
+ NS_LOG_INFO ("Done."); |
+ |
+ g_os.close (); |
+ |
+ return 0; |
+} |