From 5e8a744bec99fd63e815ea2033528798faa1f0b1 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 3 May 2007 14:20:04 +0200 Subject: [PATCH] define and implement IUdp --- SConstruct | 18 +-- examples/simple-p2p.cc | 1 - samples/main-p2p-net-device-if.cc | 225 ------------------------------ samples/main-simple-p2p.cc | 116 --------------- samples/main-simple.cc | 6 +- src/node/i-udp-impl.cc | 54 +++++++ src/node/i-udp-impl.h | 46 ++++++ src/node/i-udp.cc | 32 +++++ src/node/i-udp.h | 42 ++++++ src/node/internet-node.cc | 21 ++- src/node/onoff-application.cc | 4 +- 11 files changed, 198 insertions(+), 367 deletions(-) delete mode 100644 samples/main-p2p-net-device-if.cc delete mode 100644 samples/main-simple-p2p.cc create mode 100644 src/node/i-udp-impl.cc create mode 100644 src/node/i-udp-impl.h create mode 100644 src/node/i-udp.cc create mode 100644 src/node/i-udp.h diff --git a/SConstruct b/SConstruct index a45469af6..6d83d47ad 100644 --- a/SConstruct +++ b/SConstruct @@ -221,6 +221,8 @@ node.add_sources ([ 'udp-socket.cc', 'pcap-trace.cc', 'ipv4-end-point-demux.cc', + 'i-udp.cc', + 'i-udp-impl.cc', ]) node.add_headers ([ 'ipv4-header.h', @@ -239,6 +241,8 @@ node.add_headers ([ 'queue.h', 'arp-ipv4-interface.h', 'udp-socket.h', + 'i-udp-impl.h', + 'udp.h', ]) node.add_inst_headers ([ 'node.h', @@ -263,12 +267,12 @@ node.add_inst_headers ([ 'onoff-application.h', 'ascii-trace.h', 'socket.h', - 'udp.h', 'ipv4-l4-protocol.h', 'ipv4-l4-demux.h', 'ipv4-end-point-demux.h', 'ipv4-end-point.h', 'pcap-trace.h', + 'i-udp.h', ]) p2p = build.Ns3Module ('p2p', 'src/devices/p2p') @@ -359,24 +363,12 @@ ns3.add(sample_test) sample_test.add_dep('core') sample_test.add_source('main-test.cc') -sample_p2p_net_device_if = build.Ns3Module ('sample-p2p-net-device-if', 'samples') -sample_p2p_net_device_if.set_executable () -ns3.add (sample_p2p_net_device_if) -sample_p2p_net_device_if.add_deps (['common', 'node', 'p2p']) -sample_p2p_net_device_if.add_source ('main-p2p-net-device-if.cc') - sample_simple = build.Ns3Module('sample-simple', 'samples') sample_simple.set_executable() ns3.add(sample_simple) sample_simple.add_deps(['core', 'simulator', 'node']) sample_simple.add_source('main-simple.cc') -sample_sp2p = build.Ns3Module('sample-simple-p2p', 'samples') -sample_sp2p.set_executable() -#n3.add(sample_sp2p) -sample_sp2p.add_deps(['core', 'simulator', 'node', 'p2p']) -sample_sp2p.add_source('main-simple-p2p.cc') - # examples example_simple_p2p = build.Ns3Module('simple-p2p', 'examples') example_simple_p2p.set_executable() diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index 759f502c2..6b47212da 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -56,7 +56,6 @@ #include "ns3/mac-address.h" #include "ns3/ipv4-address.h" #include "ns3/ipv4.h" -#include "ns3/udp.h" #include "ns3/socket.h" #include "ns3/ipv4-route.h" #include "ns3/drop-tail.h" diff --git a/samples/main-p2p-net-device-if.cc b/samples/main-p2p-net-device-if.cc deleted file mode 100644 index d61461672..000000000 --- a/samples/main-p2p-net-device-if.cc +++ /dev/null @@ -1,225 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 University of Washington - * All rights reserved. - * - * 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 - */ - -#include - -#include "ns3/debug.h" -#include "ns3/internet-node.h" -#include "ns3/packet.h" -#include "ns3/ipv4-address.h" -#include "ns3/p2p-channel.h" -#include "ns3/p2p-net-device.h" -#include "ns3/drop-tail.h" -#include "ns3/ipv4.h" -#include "ns3/udp.h" -#include "ns3/trace-context.h" -#include "ns3/socket.h" -#include "ns3/simulator.h" -#include "ns3/node-list.h" -#include "ns3/trace-root.h" - - -using namespace ns3; - -class Logger { -public: - - Logger (std::string const &filename) - { - m_filestr.open (filename.c_str ()); - NS_DEBUG_UNCOND("**** Logger(string const &)"); - } - - ~Logger () {} - - void Log (TraceContext const &context, const Packet &p) - { - NodeList::NodeIndex nodeIndex; - context.Get (nodeIndex); - m_filestr << "node=" << NodeList::PeekNode (nodeIndex)->GetId () << " "; - Ipv4::InterfaceIndex interfaceIndex; - context.Get (interfaceIndex); - m_filestr << "interface=" << interfaceIndex << " "; - enum Queue::TraceType type; - context.Get (type); - switch (type) - { - case Queue::ENQUEUE: - m_filestr << "enqueue"; - break; - case Queue::DEQUEUE: - m_filestr << "dequeue"; - break; - case Queue::DROP: - m_filestr << "drop"; - break; - } - m_filestr << " bytes=" << p.GetSize () << std::endl; - } - -protected: - std::ofstream m_filestr;; -}; - -static void -GenerateTraffic (Socket *socket, uint32_t size) -{ - std::cout << "Node: " << socket->PeekNode()->GetId () - << " at=" << Simulator::Now ().GetSeconds () << "s," - << " tx bytes=" << size << std::endl; - socket->Send (0, size); - if (size > 50) - { - Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50); - } -} - -static void -SocketPrinter (Socket *socket, uint32_t size, const Ipv4Address &from, uint16_t fromPort) -{ - std::cout << "Node: " << socket->PeekNode()->GetId () - << " at=" << Simulator::Now ().GetSeconds () << "s," - << " rx bytes=" << size << std::endl; -} - -static void -PrintTraffic (Socket *socket) -{ - socket->RecvDummy (MakeCallback (&SocketPrinter)); -} - - -int main (int argc, char *argv[]) -{ - NS_DEBUG_UNCOND("PointToPoint Net Device Test"); - - // create two nodes and a simple PointToPointChannel - InternetNode a; - InternetNode b; - PointToPointChannel ch = PointToPointChannel ("Test Channel", 1000, - Seconds (0.1)); - - NodeList::Add (&a); - NodeList::Add (&b); - - // create two NetDevices and assign one to each node - // Note: this would normally be done also in conjunction with - // creating a Channel - - PointToPointNetDevice neta(&a); - a.AddDevice (&neta); - - neta.AddQueue(new DropTailQueue () ); - neta.SetName("a.eth0"); - - PointToPointNetDevice netb(&b); - b.AddDevice (&netb); - - neta.AddQueue(new DropTailQueue () ); - netb.SetName("b.eth0"); - - // bind the two NetDevices together by using a simple Channel - // this method changed to do a bidirectional binding - neta.Attach (&ch); - netb.Attach (&ch); - - // Some simple prints to see whether it is working - NS_DEBUG_UNCOND("neta.GetMtu() <= " << neta.GetMtu()); - NS_DEBUG_UNCOND("netb.GetMtu() <= " << netb.GetMtu()); - NS_DEBUG_UNCOND("neta.GetAddress() <= " << neta.GetAddress()); - NS_DEBUG_UNCOND("netb.GetAddress() <= " << netb.GetAddress()); - - // Note: InternetNode constructor instantiates multiple Layer-3 - // protocols and registers them with the L3Demux object. - // This takes care of Layer-2 -> Layer-3 bindings. - // XXX TODO: will need to create a dummy IPv4 object for insertion - // into the Demux - - // We now need to bind the InternetNode to the various interfaces. - // to get the Layer-3 -> Layer-2 bindings. - - // We do this by calling an "AddArpIpv4Interface(neta)" function on node a. - // This should: - // i) create an Ipv4ArpInterface object (subclass of pure virtual - // Ipv4Interface object) - // ii) add the Ipv4ArpInterface object to the InternetNode's internal - // vector of Ipv4Interfaces (keyed off of ifIndex) - - NS_DEBUG_UNCOND("Adding ARP Interface to InternetNode a"); - Ipv4 *ipa = (&a)->GetIpv4 (); - uint32_t indexA = ipa->AddInterface (&neta); - NS_DEBUG_UNCOND("Adding Interface " << indexA); - - - // iii) give the interface an IP address - - NS_DEBUG_UNCOND("Giving IP address to ARP Interface"); - ipa->SetAddress(indexA, Ipv4Address("10.1.1.1")); - ipa->SetNetworkMask(indexA, Ipv4Mask("255.255.255.0")); - - // iv) set the interface's state to "UP" - - NS_DEBUG_UNCOND("Setting ARP interface to UP"); - ipa->SetUp(indexA); - - ipa->SetDefaultRoute (Ipv4Address ("10.1.1.2"), 1); - - - NS_DEBUG_UNCOND("Adding ARP Interface to InternetNode b"); - Ipv4 *ipb = (&b)->GetIpv4 (); - uint32_t indexB = ipb->AddInterface (&netb); - NS_DEBUG_UNCOND("Adding Interface " << indexB); - - - // iii) give the interface an IP address - - NS_DEBUG_UNCOND("Giving IP address to ARP Interface"); - ipb->SetAddress(indexB, Ipv4Address("10.1.1.2")); - ipb->SetNetworkMask(indexB, Ipv4Mask("255.255.255.0")); - - // iv) set the interface's state to "UP" - - NS_DEBUG_UNCOND("Setting ARP interface to UP"); - ipb->SetUp(indexB); - - ipb->SetDefaultRoute (Ipv4Address ("10.1.1.1"), 1); - - - Socket *source = a.GetUdp ()->CreateSocket (); - Socket *sink = b.GetUdp ()->CreateSocket (); - sink->Bind (80); - source->Connect (Ipv4Address ("10.1.1.2"), 80); - - Logger logger("p2p-net-test.log"); - - TraceRoot::Connect ("/nodes/*/ipv4/interfaces/*/netdevice/queue/*", - MakeCallback (&Logger::Log, &logger)); - - PrintTraffic (sink); - GenerateTraffic (source, 100); - - Simulator::Run (); - - Simulator::Destroy (); - - delete source; - delete sink; - - return 0; -} diff --git a/samples/main-simple-p2p.cc b/samples/main-simple-p2p.cc deleted file mode 100644 index 6b1d3a09d..000000000 --- a/samples/main-simple-p2p.cc +++ /dev/null @@ -1,116 +0,0 @@ -#include -#include - -#include "ns3/internet-node.h" -#include "ns3/simulator.h" -#include "ns3/datagram-socket.h" -#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" -#include "ns3/ipv4-route.h" - -using namespace ns3; - - -static void -GenerateTraffic (DatagramSocket *socket, uint32_t size) -{ - 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 -DatagramSocketPrinter (DatagramSocket *socket, uint32_t size, Ipv4Address from, uint16_t fromPort) -{ - std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << size << std::endl; -} - -static void -PrintTraffic (DatagramSocket *socket) -{ - socket->SetDummyRxCallback (MakeCallback (&DatagramSocketPrinter)); -} - -static void -DestroyP2PNetDevice (P2PNetDevice *dev) -{ - delete dev; -} - -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 -AddP2PLink (InternetNode *a, InternetNode *b) -{ - P2PChannel * channel = new P2PChannel (MilliSeconds (20), 10000); - 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); - 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); - Ipv4Address ipa = Ipv4Address ("192.168.0.2"); - Ipv4Address ipb = Ipv4Address ("192.168.0.3"); - Ipv4Mask netmask = Ipv4Mask ("255.255.255.0"); - a->GetIpv4 ()->AddHostRouteTo (ipb, indexA); - b->GetIpv4 ()->AddHostRouteTo (ipa, indexB); - - interfA->SetAddress (ipa); - interfA->SetNetworkMask (netmask); - interfA->SetUp (); - - interfB->SetAddress (ipb); - interfB->SetNetworkMask (netmask); - interfB->SetUp (); - - PrintRoutingTable (a, "a"); - PrintRoutingTable (b, "b"); -} - -int main (int argc, char *argv[]) -{ - - InternetNode *a = new InternetNode (); - InternetNode *b = new InternetNode (); - - AddP2PLink (a, b); - - DatagramSocket *sink = new DatagramSocket (a); - sink->Bind (80); - DatagramSocket *source = new DatagramSocket (b); - source->SetDefaultDestination (Ipv4Address ("192.168.0.2"), 80); - - GenerateTraffic (source, 500); - PrintTraffic (sink); - - - Simulator::Run (); - - Simulator::Destroy (); - - - delete a; - delete source; - delete sink; - - return 0; -} diff --git a/samples/main-simple.cc b/samples/main-simple.cc index 2bc4ae429..675a3d16e 100644 --- a/samples/main-simple.cc +++ b/samples/main-simple.cc @@ -2,7 +2,7 @@ #include "ns3/internet-node.h" #include "ns3/simulator.h" -#include "ns3/udp.h" +#include "ns3/i-udp.h" #include "ns3/socket.h" #include "ns3/nstime.h" @@ -39,8 +39,8 @@ int main (int argc, char *argv[]) { InternetNode *a = new InternetNode (); - Udp *udp; - udp = a->GetUdp (); + IUdp *udp; + udp = a->QueryInterface (IUdp::iid); Socket *sink = udp->CreateSocket (); sink->Bind (80); diff --git a/src/node/i-udp-impl.cc b/src/node/i-udp-impl.cc new file mode 100644 index 000000000..00b522f83 --- /dev/null +++ b/src/node/i-udp-impl.cc @@ -0,0 +1,54 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + * + * Author: Mathieu Lacage + */ +#include "i-udp-impl.h" +#include "udp.h" +#include "ns3/assert.h" + +namespace ns3 { + +IUdpImpl::IUdpImpl (Udp *udp) + : m_udp (udp) +{ + m_udp->Ref (); +} +IUdpImpl::~IUdpImpl () +{ + NS_ASSERT (m_udp == 0); +} + +Socket * +IUdpImpl::CreateSocket (void) +{ + return m_udp->CreateSocket (); +} + +void +IUdpImpl::DoDispose (void) +{ + if (m_udp != 0) + { + m_udp->Unref (); + m_udp = 0; + } + IUdp::DoDispose (); +} + +} // namespace ns3 diff --git a/src/node/i-udp-impl.h b/src/node/i-udp-impl.h new file mode 100644 index 000000000..625295f3f --- /dev/null +++ b/src/node/i-udp-impl.h @@ -0,0 +1,46 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + * + * Author: Mathieu Lacage + */ +#ifndef I_UDP_IMPL_H +#define I_UDP_IMPL_H + +#include "i-udp.h" + +namespace ns3 { + +class Udp; + +class IUdpImpl : public IUdp +{ +public: + IUdpImpl (Udp *udp); + virtual ~IUdpImpl (); + + virtual Socket *CreateSocket (void); + +protected: + virtual void DoDispose (void); +private: + Udp *m_udp; +}; + +} // namespace ns3 + +#endif /* I_UDP_IMPL_H */ diff --git a/src/node/i-udp.cc b/src/node/i-udp.cc new file mode 100644 index 000000000..963445d1f --- /dev/null +++ b/src/node/i-udp.cc @@ -0,0 +1,32 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + * + * Author: Mathieu Lacage + */ +#include "i-udp.h" +#include "ns3/iid-manager.h" + +namespace ns3 { + +const uint32_t IUdp::iid = IidManager::Allocate ("IUdp"); + +IUdp::IUdp () + : NsUnknown (IUdp::iid) +{} + +} // namespace ns3 diff --git a/src/node/i-udp.h b/src/node/i-udp.h new file mode 100644 index 000000000..4e7773b0c --- /dev/null +++ b/src/node/i-udp.h @@ -0,0 +1,42 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + * + * Author: Mathieu Lacage + */ +#ifndef I_UDP_H +#define I_UDP_H + +#include "ns3/ns-unknown.h" + +namespace ns3 { + +class Socket; + +class IUdp : public NsUnknown +{ +public: + static const uint32_t iid; + + IUdp (); + + virtual Socket *CreateSocket (void) = 0; +}; + +} // namespace ns3 + +#endif /* I_UDP_H */ diff --git a/src/node/internet-node.cc b/src/node/internet-node.cc index 1119f21f8..20bf814de 100644 --- a/src/node/internet-node.cc +++ b/src/node/internet-node.cc @@ -31,28 +31,34 @@ #include "ipv4.h" #include "arp.h" #include "net-device.h" +#include "i-udp-impl.h" namespace ns3 { InternetNode::InternetNode() { + Ipv4 *ipv4 = new Ipv4 (this); + Arp *arp = new Arp (this); + Udp *udp = new Udp (this); + + // Instantiate the capabilities ApplicationList *applicationList = new ApplicationList(this); L3Demux *l3Demux = new L3Demux(this); Ipv4L4Demux *ipv4L4Demux = new Ipv4L4Demux(this); + l3Demux->Insert (ipv4); + l3Demux->Insert (arp); + ipv4L4Demux->Insert (udp); + + IUdpImpl *udpImpl = new IUdpImpl (udp); + + NsUnknown::AddInterface (udpImpl); NsUnknown::AddInterface (applicationList); NsUnknown::AddInterface (l3Demux); NsUnknown::AddInterface (ipv4L4Demux); - Ipv4 *ipv4 = new Ipv4 (this); - Arp *arp = new Arp (this); - Udp *udp = new Udp (this); - - l3Demux->Insert (ipv4); - l3Demux->Insert (arp); - ipv4L4Demux->Insert (udp); applicationList->Unref (); l3Demux->Unref (); @@ -60,6 +66,7 @@ InternetNode::InternetNode() ipv4->Unref (); arp->Unref (); udp->Unref (); + udpImpl->Unref (); } InternetNode::~InternetNode () diff --git a/src/node/onoff-application.cc b/src/node/onoff-application.cc index 9c0808477..d3026271a 100644 --- a/src/node/onoff-application.cc +++ b/src/node/onoff-application.cc @@ -30,7 +30,7 @@ #include "ns3/random-variable.h" #include "socket.h" #include "ns3/simulator.h" -#include "udp.h" +#include "i-udp.h" using namespace std; @@ -170,7 +170,7 @@ void OnOffApplication::StartApplication() // Called at time specified by Star this)); #endif - Udp *udp = PeekNode ()->GetUdp (); + IUdp *udp = PeekNode ()->QueryInterface (IUdp::iid); m_socket = udp->CreateSocket (); udp->Unref (); m_socket->Connect (m_peerIP, m_peerPort);