SimpleChannel/SimpleNetDevice. Use them where needed.

This commit is contained in:
Mathieu Lacage
2008-03-20 15:27:48 -07:00
parent e22584cd79
commit 53b3d2bb1f
6 changed files with 324 additions and 13 deletions

View File

@@ -356,8 +356,8 @@ UdpSocket::ForwardUp (Ptr<Packet> 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 <string>
@@ -400,11 +400,11 @@ UdpSocketTest::RunTests (void)
// Receiver Node
Ptr<Node> rxNode = CreateObject<InternetNode> ();
Ptr<PointToPointNetDevice> rxDev1, rxDev2;
Ptr<SimpleNetDevice> rxDev1, rxDev2;
{ // first interface
rxDev1 = CreateObject<PointToPointNetDevice> ("Address", Mac48Address::Allocate ());
rxDev1 = CreateObject<SimpleNetDevice> ();
rxDev1->SetAddress (Mac48Address::Allocate ());
rxNode->AddDevice (rxDev1);
rxDev1->AddQueue(CreateObject<DropTailQueue> ());
Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
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<PointToPointNetDevice> ("Address", Mac48Address::Allocate ());
rxDev2 = CreateObject<SimpleNetDevice> ();
rxDev2->SetAddress (Mac48Address::Allocate ());
rxNode->AddDevice (rxDev2);
rxDev2->AddQueue(CreateObject<DropTailQueue> ());
Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
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<Node> txNode = CreateObject<InternetNode> ();
Ptr<PointToPointNetDevice> txDev;
Ptr<SimpleNetDevice> txDev;
{
txDev = CreateObject<PointToPointNetDevice> ("Address", Mac48Address::Allocate ());
txDev = CreateObject<SimpleNetDevice> ();
txDev->SetAddress (Mac48Address::Allocate ());
txNode->AddDevice (txDev);
txDev->AddQueue(CreateObject<DropTailQueue> ());
Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
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<PointToPointChannel> channel = CreateObject<PointToPointChannel> ();
rxDev1->Attach (channel);
txDev->Attach (channel);
Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
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

View File

@@ -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<Channel> ()
.AddConstructor<SimpleChannel> ()
;
return tid;
}
SimpleChannel::SimpleChannel ()
{}
void
SimpleChannel::Send (Ptr<Packet> p, uint16_t protocol,
Mac48Address to, Mac48Address from,
Ptr<SimpleNetDevice> sender)
{
for (std::vector<Ptr<SimpleNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
{
Ptr<SimpleNetDevice> tmp = *i;
if (tmp == sender)
{
continue;
}
tmp->Receive (p->Copy (), protocol, to, from);
}
}
void
SimpleChannel::Add (Ptr<SimpleNetDevice> device)
{
m_devices.push_back (device);
}
uint32_t
SimpleChannel::GetNDevices (void) const
{
return m_devices.size ();
}
Ptr<NetDevice>
SimpleChannel::DoGetDevice (uint32_t i) const
{
return m_devices[i];
}
} // namespace ns3

34
src/node/simple-channel.h Normal file
View File

@@ -0,0 +1,34 @@
#ifndef TEST_CHANNEL_H
#define TEST_CHANNEL_H
#include "channel.h"
#include "mac48-address.h"
#include <vector>
namespace ns3 {
class SimpleNetDevice;
class Packet;
class SimpleChannel : public Channel
{
public:
static TypeId GetTypeId (void);
SimpleChannel ();
void Send (Ptr<Packet> p, uint16_t protocol, Mac48Address to, Mac48Address from,
Ptr<SimpleNetDevice> sender);
void Add (Ptr<SimpleNetDevice> device);
virtual uint32_t GetNDevices (void) const;
private:
virtual Ptr<NetDevice> DoGetDevice (uint32_t i) const;
std::vector<Ptr<SimpleNetDevice> > m_devices;
};
} // namespace ns3
#endif /* TEST_CHANNEL_H */

View File

@@ -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<NetDevice> ()
.AddConstructor<SimpleNetDevice> ()
;
return tid;
}
SimpleNetDevice::SimpleNetDevice ()
: m_channel (0),
m_node (0),
m_mtu (0xffff),
m_name (""),
m_ifIndex (0)
{}
void
SimpleNetDevice::Receive (Ptr<Packet> 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<SimpleChannel> 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<Channel>
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<void> 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> packet, const Address& dest, uint16_t protocolNumber)
{
Mac48Address to = Mac48Address::ConvertFrom (dest);
m_channel->Send (packet, protocolNumber, to, m_address, this);
return true;
}
Ptr<Node>
SimpleNetDevice::GetNode (void) const
{
return m_node;
}
void
SimpleNetDevice::SetNode (Ptr<Node> node)
{
m_node = node;
}
bool
SimpleNetDevice::NeedsArp (void) const
{
return false;
}
void
SimpleNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
{
m_rxCallback = cb;
}
} // namespace ns3

View File

@@ -0,0 +1,59 @@
#ifndef TEST_NET_DEVICE_H
#define TEST_NET_DEVICE_H
#include "net-device.h"
#include "mac48-address.h"
#include <stdint.h>
#include <string>
namespace ns3 {
class SimpleChannel;
class Node;
class SimpleNetDevice : public NetDevice
{
public:
static TypeId GetTypeId (void);
SimpleNetDevice ();
void Receive (Ptr<Packet> packet, uint16_t protocol, Mac48Address to, Mac48Address from);
void SetChannel (Ptr<SimpleChannel> 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<Channel> 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<void> 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> packet, const Address& dest, uint16_t protocolNumber);
virtual Ptr<Node> GetNode (void) const;
virtual void SetNode (Ptr<Node> node);
virtual bool NeedsArp (void) const;
virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
private:
Ptr<SimpleChannel> m_channel;
NetDevice::ReceiveCallback m_rxCallback;
Ptr<Node> m_node;
uint16_t m_mtu;
std::string m_name;
uint32_t m_ifIndex;
Mac48Address m_address;
};
} // namespace ns3
#endif /* TEST_NET_DEVICE_H */

View File

@@ -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',
]