Files
unison/samples/main-simple-p2p.cc

117 lines
3.0 KiB
C++
Raw Normal View History

2007-02-12 20:15:19 +01:00
#include <iostream>
2007-02-12 22:44:41 +01:00
#include <string>
2007-02-12 20:15:19 +01:00
#include "ns3/internet-node.h"
#include "ns3/simulator.h"
2007-03-17 23:16:59 -07:00
#include "ns3/datagram-socket.h"
2007-02-12 20:15:19 +01:00
#include "ns3/nstime.h"
#include "ns3/p2p-channel.h"
#include "ns3/p2p-net-device.h"
#include "ns3/ipv4.h"
#include "ns3/arp-ipv4-interface.h"
2007-02-12 22:44:41 +01:00
#include "ns3/ipv4-route.h"
2007-02-12 20:15:19 +01:00
using namespace ns3;
static void
2007-03-17 23:16:59 -07:00
GenerateTraffic (DatagramSocket *socket, uint32_t size)
2007-02-12 20:15:19 +01:00
{
std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl;
socket->SendDummy (size);
if (size > 0)
{
Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
}
}
static void
2007-03-17 23:16:59 -07:00
DatagramSocketPrinter (DatagramSocket *socket, uint32_t size, Ipv4Address from, uint16_t fromPort)
2007-02-12 20:15:19 +01:00
{
std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << size << std::endl;
}
static void
2007-03-17 23:16:59 -07:00
PrintTraffic (DatagramSocket *socket)
2007-02-12 20:15:19 +01:00
{
2007-03-17 23:16:59 -07:00
socket->SetDummyRxCallback (MakeCallback (&DatagramSocketPrinter));
2007-02-12 20:15:19 +01:00
}
static void
DestroyP2PNetDevice (P2PNetDevice *dev)
{
delete dev;
}
2007-02-12 22:44:41 +01:00
static void
PrintRoutingTable (InternetNode *a, std::string name)
{
Ipv4 *ipv4 = a->GetIpv4 ();
std::cout << "routing table start node=" << name << std::endl;
for (uint32_t i = 0; i < ipv4->GetNRoutes (); i++)
{
Ipv4Route *route = ipv4->GetRoute (i);
std::cout << (*route) << std::endl;
}
std::cout << "routing table end" << std::endl;
}
static void
2007-02-12 20:15:19 +01:00
AddP2PLink (InternetNode *a, InternetNode *b)
{
P2PChannel * channel = new P2PChannel (MilliSeconds (20), 10000);
2007-02-12 20:15:19 +01:00
P2PNetDevice *devA = channel->CreateNetDevice (a, MacAddress ("00:00:00:00:00:01"));
P2PNetDevice *devB = channel->CreateNetDevice (b, MacAddress ("00:00:00:00:00:02"));
Simulator::ScheduleDestroy (&DestroyP2PNetDevice, devA);
Simulator::ScheduleDestroy (&DestroyP2PNetDevice, devB);
2007-02-12 23:24:20 +01:00
Ipv4Interface *interfA = new ArpIpv4Interface (a, devA);
Ipv4Interface *interfB = new ArpIpv4Interface (b, devB);
uint32_t indexA = a->GetIpv4 ()->AddInterface (interfA);
uint32_t indexB = b->GetIpv4 ()->AddInterface (interfB);
2007-02-12 20:15:19 +01:00
Ipv4Address ipa = Ipv4Address ("192.168.0.2");
Ipv4Address ipb = Ipv4Address ("192.168.0.3");
2007-02-12 23:24:20 +01:00
Ipv4Mask netmask = Ipv4Mask ("255.255.255.0");
2007-02-12 20:15:19 +01:00
a->GetIpv4 ()->AddHostRouteTo (ipb, indexA);
b->GetIpv4 ()->AddHostRouteTo (ipa, indexB);
2007-02-12 23:24:20 +01:00
interfA->SetAddress (ipa);
interfA->SetNetworkMask (netmask);
interfA->SetUp ();
interfB->SetAddress (ipb);
interfB->SetNetworkMask (netmask);
interfB->SetUp ();
2007-02-12 22:44:41 +01:00
PrintRoutingTable (a, "a");
PrintRoutingTable (b, "b");
2007-02-12 20:15:19 +01:00
}
int main (int argc, char *argv[])
{
InternetNode *a = new InternetNode ();
InternetNode *b = new InternetNode ();
AddP2PLink (a, b);
2007-03-17 23:16:59 -07:00
DatagramSocket *sink = new DatagramSocket (a);
2007-02-12 20:15:19 +01:00
sink->Bind (80);
2007-03-17 23:16:59 -07:00
DatagramSocket *source = new DatagramSocket (b);
2007-02-12 22:44:41 +01:00
source->SetDefaultDestination (Ipv4Address ("192.168.0.2"), 80);
2007-02-12 20:15:19 +01:00
GenerateTraffic (source, 500);
PrintTraffic (sink);
Simulator::Run ();
Simulator::Destroy ();
delete a;
delete source;
delete sink;
return 0;
}