Advertisement
Guest User

Untitled

a guest
Jan 12th, 2012
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include "ns3/abort.h"
  2. #include "ns3/core-module.h"
  3. #include "ns3/internet-module.h"
  4. #include "ns3/network-module.h"
  5. #include "ns3/emu-module.h"
  6. #include "ns3/applications-module.h"
  7. #include "ns3/ipv4-static-routing-helper.h"
  8. #include "ns3/ipv4-list-routing-helper.h"
  9.  
  10. using namespace ns3;
  11.  
  12. NS_LOG_COMPONENT_DEFINE ("SocketTryout2");
  13.  
  14. void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
  15. {
  16.   NS_LOG_INFO ("SendStuff () called ...");
  17.   Ptr<Packet> p = Create<Packet> (reinterpret_cast<uint8_t const *> ("I am long 20 bytes!"), 20);
  18.   p->AddPaddingAtEnd (100);
  19.   sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
  20.   return;
  21. }
  22.  
  23. static void PingRtt (std::string context, Time rtt) {
  24.   NS_LOG_UNCOND ("Received Response with RTT = " << rtt);
  25. }
  26.  
  27. int main (int argc, char *argv[])
  28. {
  29.   NS_LOG_INFO ("SocketTryout2");
  30.  
  31.   std::string deviceName ("eth0");
  32.   std::string remote ("192.168.27.76");
  33.   //std::string remote ("88.149.128.3"); // 84.149.128.3 maya.ngi.it
  34.  
  35.   CommandLine cmd;
  36.   cmd.AddValue ("deviceName", "Device name", deviceName);
  37.   cmd.AddValue ("remote", "Remote IP address (dotted decimal only please)", remote);
  38.   cmd.Parse (argc, argv);
  39.  
  40.   Ipv4Address remoteIp (remote.c_str ());
  41.   Ipv4Address localIp ("192.168.27.90");
  42.   Ipv4Mask localMask ("255.255.255.0");
  43.  
  44.   GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
  45.   GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
  46.  
  47.   Ptr<Node> node = CreateObject<Node> ();
  48.   Ptr<EmuNetDevice> device = CreateObject<EmuNetDevice> ();
  49.   device->SetAttribute ("Address", Mac48AddressValue (Mac48Address::Allocate ()));
  50.   device->SetAttribute ("DeviceName", StringValue (deviceName));
  51.  
  52.   Ptr<Queue> queue = CreateObject<DropTailQueue> ();
  53.   device->SetQueue (queue);
  54.   node->AddDevice (device);
  55.  
  56.   InternetStackHelper internetStackHelper;
  57.   internetStackHelper.Install (node);
  58.  
  59.   Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
  60.   uint32_t interface = ipv4->AddInterface (device);
  61.   Ipv4InterfaceAddress address = Ipv4InterfaceAddress (localIp, localMask);
  62.   ipv4->AddAddress (interface, address);
  63.   ipv4->SetMetric (interface, 1);
  64.   ipv4->SetUp (interface);
  65.  
  66.   Ipv4Address gateway ("192.168.27.1");
  67.   NS_ABORT_MSG_IF (gateway == "1.2.3.4", "You must change the gateway IP address before running this example");
  68.  
  69.   Ipv4StaticRoutingHelper ipv4RoutingHelper;
  70.   Ptr<Ipv4StaticRouting> staticRouting = ipv4RoutingHelper.GetStaticRouting (ipv4);
  71.   staticRouting->SetDefaultRoute (gateway, interface);
  72.  
  73.   Ptr<V4Ping> app = CreateObject<V4Ping> ();
  74.   app->SetAttribute ("Remote", Ipv4AddressValue (remoteIp));
  75.   node->AddApplication (app);
  76.   app->SetStartTime (Seconds (1.0));
  77.   app->SetStopTime (Seconds (10.0));
  78.  
  79.   Names::Add ("app", app);
  80.  
  81.   Config::Connect ("/Names/app/Rtt", MakeCallback (&PingRtt));
  82.  
  83.   Ptr<Socket> srcSocket = Socket::CreateSocket (node, TypeId::LookupByName ("ns3::UdpSocketFactory"));
  84.   srcSocket->Bind ();
  85.   srcSocket->BindToNetDevice (device);
  86.   LogComponentEnableAll (LOG_PREFIX_TIME);
  87.   LogComponentEnable ("SocketTryout2", LOG_LEVEL_INFO);
  88.   Ipv4Address dstAddr = remoteIp;
  89.   uint16_t dstPort = 9999;
  90.   for (uint32_t i = 0; i < 20; i++) {
  91.       Simulator::Schedule (Seconds (1 + i * 0.5), &SendStuff, srcSocket, dstAddr, dstPort);
  92.   }
  93.  
  94.   NS_LOG_INFO ("Run Emulation.");
  95.   Simulator::Stop (Seconds (10.0));
  96.   Simulator::Run ();
  97.   Simulator::Destroy ();
  98.   NS_LOG_INFO ("Done.");
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement