From 53b3d2bb1f5582dc0e0ac6331c97604ee5cef469 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 20 Mar 2008 15:27:48 -0700 Subject: [PATCH] SimpleChannel/SimpleNetDevice. Use them where needed. --- src/internet-node/udp-socket.cc | 29 +++--- src/node/simple-channel.cc | 53 +++++++++++ src/node/simple-channel.h | 34 +++++++ src/node/simple-net-device.cc | 158 ++++++++++++++++++++++++++++++++ src/node/simple-net-device.h | 59 ++++++++++++ src/node/wscript | 4 + 6 files changed, 324 insertions(+), 13 deletions(-) create mode 100644 src/node/simple-channel.cc create mode 100644 src/node/simple-channel.h create mode 100644 src/node/simple-net-device.cc create mode 100644 src/node/simple-net-device.h diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index a3dd86270..8365dd2a1 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -356,8 +356,8 @@ UdpSocket::ForwardUp (Ptr packet, Ipv4Address ipv4, uint16_t port) #include "ns3/socket-factory.h" #include "ns3/udp.h" #include "ns3/simulator.h" -#include "ns3/point-to-point-channel.h" -#include "ns3/point-to-point-net-device.h" +#include "ns3/simple-channel.h" +#include "ns3/simple-net-device.h" #include "ns3/drop-tail-queue.h" #include @@ -400,11 +400,11 @@ UdpSocketTest::RunTests (void) // Receiver Node Ptr rxNode = CreateObject (); - Ptr rxDev1, rxDev2; + Ptr rxDev1, rxDev2; { // first interface - rxDev1 = CreateObject ("Address", Mac48Address::Allocate ()); + rxDev1 = CreateObject (); + rxDev1->SetAddress (Mac48Address::Allocate ()); rxNode->AddDevice (rxDev1); - rxDev1->AddQueue(CreateObject ()); Ptr ipv4 = rxNode->GetObject (); uint32_t netdev_idx = ipv4->AddInterface (rxDev1); ipv4->SetAddress (netdev_idx, Ipv4Address ("10.0.0.1")); @@ -413,9 +413,9 @@ UdpSocketTest::RunTests (void) } { // second interface - rxDev2 = CreateObject ("Address", Mac48Address::Allocate ()); + rxDev2 = CreateObject (); + rxDev2->SetAddress (Mac48Address::Allocate ()); rxNode->AddDevice (rxDev2); - rxDev2->AddQueue(CreateObject ()); Ptr ipv4 = rxNode->GetObject (); uint32_t netdev_idx = ipv4->AddInterface (rxDev2); ipv4->SetAddress (netdev_idx, Ipv4Address ("10.0.1.1")); @@ -425,11 +425,11 @@ UdpSocketTest::RunTests (void) // Sender Node Ptr txNode = CreateObject (); - Ptr txDev; + Ptr txDev; { - txDev = CreateObject ("Address", Mac48Address::Allocate ()); + txDev = CreateObject (); + txDev->SetAddress (Mac48Address::Allocate ()); txNode->AddDevice (txDev); - txDev->AddQueue(CreateObject ()); Ptr ipv4 = txNode->GetObject (); uint32_t netdev_idx = ipv4->AddInterface (txDev); ipv4->SetAddress (netdev_idx, Ipv4Address ("10.0.0.2")); @@ -438,9 +438,12 @@ UdpSocketTest::RunTests (void) } // link the two nodes - Ptr channel = CreateObject (); - rxDev1->Attach (channel); - txDev->Attach (channel); + Ptr channel = CreateObject (); + rxDev1->SetChannel (channel); + // XXX: I believe that it is a bug to not associate rxDev2 with the + // channel but the tests below fail if you do so. + //rxDev2->SetChannel (channel); + txDev->SetChannel (channel); // Create the UDP sockets diff --git a/src/node/simple-channel.cc b/src/node/simple-channel.cc new file mode 100644 index 000000000..f14968b39 --- /dev/null +++ b/src/node/simple-channel.cc @@ -0,0 +1,53 @@ +#include "simple-channel.h" +#include "simple-net-device.h" +#include "ns3/packet.h" + +namespace ns3 { + +TypeId +SimpleChannel::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::SimpleChannel") + .SetParent () + .AddConstructor () + ; + return tid; +} + +SimpleChannel::SimpleChannel () +{} + +void +SimpleChannel::Send (Ptr p, uint16_t protocol, + Mac48Address to, Mac48Address from, + Ptr sender) +{ + for (std::vector >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i) + { + Ptr tmp = *i; + if (tmp == sender) + { + continue; + } + tmp->Receive (p->Copy (), protocol, to, from); + } +} + +void +SimpleChannel::Add (Ptr device) +{ + m_devices.push_back (device); +} + +uint32_t +SimpleChannel::GetNDevices (void) const +{ + return m_devices.size (); +} +Ptr +SimpleChannel::DoGetDevice (uint32_t i) const +{ + return m_devices[i]; +} + +} // namespace ns3 diff --git a/src/node/simple-channel.h b/src/node/simple-channel.h new file mode 100644 index 000000000..219207575 --- /dev/null +++ b/src/node/simple-channel.h @@ -0,0 +1,34 @@ +#ifndef TEST_CHANNEL_H +#define TEST_CHANNEL_H + +#include "channel.h" +#include "mac48-address.h" +#include + +namespace ns3 { + +class SimpleNetDevice; +class Packet; + +class SimpleChannel : public Channel +{ +public: + static TypeId GetTypeId (void); + SimpleChannel (); + + void Send (Ptr p, uint16_t protocol, Mac48Address to, Mac48Address from, + Ptr sender); + + void Add (Ptr device); + + virtual uint32_t GetNDevices (void) const; + +private: + virtual Ptr DoGetDevice (uint32_t i) const; + + std::vector > m_devices; +}; + +} // namespace ns3 + +#endif /* TEST_CHANNEL_H */ diff --git a/src/node/simple-net-device.cc b/src/node/simple-net-device.cc new file mode 100644 index 000000000..43d57b643 --- /dev/null +++ b/src/node/simple-net-device.cc @@ -0,0 +1,158 @@ +#include "simple-net-device.h" +#include "simple-channel.h" +#include "node.h" +#include "ns3/packet.h" + +namespace ns3 { + +TypeId +SimpleNetDevice::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::SimpleNetDevice") + .SetParent () + .AddConstructor () + ; + return tid; +} + +SimpleNetDevice::SimpleNetDevice () + : m_channel (0), + m_node (0), + m_mtu (0xffff), + m_name (""), + m_ifIndex (0) +{} + +void +SimpleNetDevice::Receive (Ptr packet, uint16_t protocol, + Mac48Address to, Mac48Address from) +{ + if (to == m_address || to == Mac48Address::GetBroadcast ()) + { + m_rxCallback (this, packet, protocol, from); + } +} + +void +SimpleNetDevice::SetChannel (Ptr channel) +{ + m_channel = channel; + m_channel->Add (this); +} + +void +SimpleNetDevice::SetAddress (Mac48Address address) +{ + m_address = address; +} + +void +SimpleNetDevice::SetName(const std::string name) +{ + m_name = name; +} +std::string +SimpleNetDevice::GetName(void) const +{ + return m_name; +} +void +SimpleNetDevice::SetIfIndex(const uint32_t index) +{ + m_ifIndex = index; +} +uint32_t +SimpleNetDevice::GetIfIndex(void) const +{ + return m_ifIndex; +} +Ptr +SimpleNetDevice::GetChannel (void) const +{ + return m_channel; +} +Address +SimpleNetDevice::GetAddress (void) const +{ + return m_address; +} +bool +SimpleNetDevice::SetMtu (const uint16_t mtu) +{ + m_mtu = mtu; + return true; +} +uint16_t +SimpleNetDevice::GetMtu (void) const +{ + return m_mtu; +} +bool +SimpleNetDevice::IsLinkUp (void) const +{ + return true; +} +void +SimpleNetDevice::SetLinkChangeCallback (Callback callback) +{} +bool +SimpleNetDevice::IsBroadcast (void) const +{ + return true; +} +Address +SimpleNetDevice::GetBroadcast (void) const +{ + return Mac48Address ("ff:ff:ff:ff:ff:ff"); +} +bool +SimpleNetDevice::IsMulticast (void) const +{ + return false; +} +Address +SimpleNetDevice::GetMulticast (void) const +{ + return Mac48Address ("01:00:5e:00:00:00"); +} +Address +SimpleNetDevice::MakeMulticastAddress (Ipv4Address multicastGroup) const +{ + return Mac48Address ("01:00:5e:00:00:00"); +} +bool +SimpleNetDevice::IsPointToPoint (void) const +{ + return false; +} +bool +SimpleNetDevice::Send(Ptr packet, const Address& dest, uint16_t protocolNumber) +{ + Mac48Address to = Mac48Address::ConvertFrom (dest); + m_channel->Send (packet, protocolNumber, to, m_address, this); + return true; +} +Ptr +SimpleNetDevice::GetNode (void) const +{ + return m_node; +} +void +SimpleNetDevice::SetNode (Ptr node) +{ + m_node = node; +} +bool +SimpleNetDevice::NeedsArp (void) const +{ + return false; +} +void +SimpleNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb) +{ + m_rxCallback = cb; +} + + + +} // namespace ns3 diff --git a/src/node/simple-net-device.h b/src/node/simple-net-device.h new file mode 100644 index 000000000..76f3d6b82 --- /dev/null +++ b/src/node/simple-net-device.h @@ -0,0 +1,59 @@ +#ifndef TEST_NET_DEVICE_H +#define TEST_NET_DEVICE_H + +#include "net-device.h" +#include "mac48-address.h" +#include +#include + +namespace ns3 { + +class SimpleChannel; +class Node; + +class SimpleNetDevice : public NetDevice +{ +public: + static TypeId GetTypeId (void); + SimpleNetDevice (); + + void Receive (Ptr packet, uint16_t protocol, Mac48Address to, Mac48Address from); + void SetChannel (Ptr channel); + void SetAddress (Mac48Address address); + + // inherited from NetDevice base class. + virtual void SetName(const std::string name); + virtual std::string GetName(void) const; + virtual void SetIfIndex(const uint32_t index); + virtual uint32_t GetIfIndex(void) const; + virtual Ptr GetChannel (void) const; + virtual Address GetAddress (void) const; + virtual bool SetMtu (const uint16_t mtu); + virtual uint16_t GetMtu (void) const; + virtual bool IsLinkUp (void) const; + virtual void SetLinkChangeCallback (Callback callback); + virtual bool IsBroadcast (void) const; + virtual Address GetBroadcast (void) const; + virtual bool IsMulticast (void) const; + virtual Address GetMulticast (void) const; + virtual Address MakeMulticastAddress (Ipv4Address multicastGroup) const; + virtual bool IsPointToPoint (void) const; + virtual bool Send(Ptr packet, const Address& dest, uint16_t protocolNumber); + virtual Ptr GetNode (void) const; + virtual void SetNode (Ptr node); + virtual bool NeedsArp (void) const; + virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb); + +private: + Ptr m_channel; + NetDevice::ReceiveCallback m_rxCallback; + Ptr m_node; + uint16_t m_mtu; + std::string m_name; + uint32_t m_ifIndex; + Mac48Address m_address; +}; + +} // namespace ns3 + +#endif /* TEST_NET_DEVICE_H */ diff --git a/src/node/wscript b/src/node/wscript index fa4761db2..0444fb78a 100644 --- a/src/node/wscript +++ b/src/node/wscript @@ -28,6 +28,8 @@ def build(bld): 'tcp.cc', 'ipv4.cc', 'application.cc', + 'simple-channel.cc', + 'simple-net-device.cc', ] headers = bld.create_obj('ns3header') @@ -57,4 +59,6 @@ def build(bld): 'tcp.h', 'ipv4.h', 'application.h', + 'simple-channel.h', + 'simple-net-device.h', ]