diff --git a/SConstruct b/SConstruct index 4aff78b36..1982bb3de 100644 --- a/SConstruct +++ b/SConstruct @@ -169,6 +169,8 @@ node.add_sources ([ 'arp-cache.cc', 'arp-ipv4-interface.cc', 'arp.cc', + 'p2p-net-device.cc', + 'p2p-channel.cc' ]) node.add_headers ([ 'ipv4-address.h', @@ -185,6 +187,8 @@ node.add_headers ([ 'arp-cache-cache.h', 'arp-ipv4-interface.h', 'arp.h', + 'p2p-net-device.h', + 'p2p-channel.h' ]) node.add_inst_headers ([ 'node.h', diff --git a/src/node/p2p-channel.cc b/src/node/p2p-channel.cc new file mode 100644 index 000000000..04eec9c90 --- /dev/null +++ b/src/node/p2p-channel.cc @@ -0,0 +1,95 @@ +// -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- +// +// Copyright (c) 2006 Georgia Tech Research Corporation +// 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: George F. Riley +// + +// Implementation of simple point-to-point channel +// George F. Riley, Georgia Tech, Spring 2007 + +#include "ns3/simulator.h" +#include "ns3/packet.h" +#include "p2p-channel.h" +#include "p2p-net-device.h" +#include "node.h" + +namespace ns3 { + +P2PChannel::P2PChannel(const Time& delay, double maxRate) + : m_nd1(0), m_nd2(0), + m_delay (delay), + m_maxRate (maxRate) +{ +} + +// Channels create compatible net devices +P2PNetDevice* P2PChannel::CreateNetDevice(Node &node, MacAddress address) +{ + // Create a new point-to-point network device + P2PNetDevice* nd = new P2PNetDevice(node, address); + nd->Connect (this); + // Add to list of peers + if (!m_nd1) m_nd1 = nd; + else m_nd2 = nd; + return nd; +} + +void P2PChannel::RemoveNetDevice(NetDevice* nd) +{ + if (nd == m_nd1) m_nd1 = 0; + if (nd == m_nd2) m_nd2 = 0; + // Now if all removed, remove the channel as well + if (!m_nd1 && !m_nd2) + { + delete this; + } +} + +void P2PChannel::Send(P2PNetDevice *device, Packet& p, double rate) +{ // Schedule a receive event at receiver + // First calculate time in future + double maxRate; + if (rate < m_maxRate) + { + maxRate = rate; + } + else + { + maxRate = m_maxRate; + } + Time txTime = Seconds (p.GetSize() * 8 / maxRate); + Time rxTime = m_delay + txTime; + P2PNetDevice *to, *from; + if (device == m_nd1) + { + from = m_nd1; + to = m_nd2; + } + else + { + from = m_nd2; + to = m_nd1; + } + // Schedule the receive event at receiver + Simulator::Schedule(rxTime, &P2PNetDevice::Receive, to, p); + // Schedule the link free event + Simulator::Schedule(txTime, &P2PNetDevice::TxComplete, from); + +} + +}//namespace ns3 diff --git a/src/node/p2p-channel.h b/src/node/p2p-channel.h new file mode 100644 index 000000000..3e04d4617 --- /dev/null +++ b/src/node/p2p-channel.h @@ -0,0 +1,56 @@ +// -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- +// +// Copyright (c) 2006 Georgia Tech Research Corporation +// 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: George F. Riley +// + +// Definition of a simple point-to-point channel +// George F. Riley, Georgia Tech, Spring 2007 + +#ifndef P2P_CHANNEL_H +#define P2P_CHANNEL_H + +#include "ns3/nstime.h" +#include "mac-address.h" + +namespace ns3 { + +class P2PNetDevice; +class NetDevice; +class Node; +class Packet; + +class P2PChannel { +public: + P2PChannel(const Time& delay, double maxRate /* bits/s */); + ~P2PChannel(); + + P2PNetDevice* CreateNetDevice(Node &node, MacAddress address); + void RemoveNetDevice (NetDevice *device); + void Send (P2PNetDevice *device, Packet&p, double rate /* bits/s */); +private: + // The two endpoints of this channel + P2PNetDevice* m_nd1; + P2PNetDevice* m_nd2; + Time m_delay; + double m_maxRate; +}; + +}//namespace ns3 + +#endif diff --git a/src/node/p2p-net-device.cc b/src/node/p2p-net-device.cc new file mode 100644 index 000000000..f9733cbea --- /dev/null +++ b/src/node/p2p-net-device.cc @@ -0,0 +1,68 @@ +// -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- +// +// Copyright (c) 2006 Georgia Tech Research Corporation +// 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: George F. Riley +// + +// Implementation of a point-to-point network device +// George F. Riley, Georgia Tech, Spring 2007 + +#include "p2p-net-device.h" +#include "p2p-channel.h" + +namespace ns3 { + +P2PNetDevice::P2PNetDevice (Node &node, MacAddress const &addr) + : NetDevice (node, addr) +{} + +P2PNetDevice::~P2PNetDevice() +{ // Inform channel that we are destroyed + m_channel->RemoveNetDevice(this); +} + +void +P2PNetDevice::SetRate (double rate) +{ + m_rate = rate; +} + +void +P2PNetDevice::Connect (P2PChannel *channel) +{ + m_channel = channel; +} + +bool +P2PNetDevice::SendTo (Packet& p, const MacAddress&) +{ + m_channel->Send (this, p, m_rate); + return true; +} + +void +P2PNetDevice::Receive(Packet p) +{ + ForwardUp (p); +} + +void +P2PNetDevice::TxComplete (void) +{} + +}//namespace ns3 diff --git a/src/node/p2p-net-device.h b/src/node/p2p-net-device.h new file mode 100644 index 000000000..21b4d0557 --- /dev/null +++ b/src/node/p2p-net-device.h @@ -0,0 +1,52 @@ +// -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- +// +// Copyright (c) 2006 Georgia Tech Research Corporation +// 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: George F. Riley +// + +// Definition for a Point-to-Point network device +// George F. Riley, Georgia Tech, Spring 2007 + +#ifndef P2P_NET_DEVICE_H +#define P2P_NET_DEVICE_H + +#include "net-device.h" + +namespace ns3 { + +class P2PChannel; + +class P2PNetDevice : public NetDevice { +public: + P2PNetDevice(Node &node, MacAddress const &addr); + virtual ~P2PNetDevice(); + + void SetRate (double Rate); + void Connect (P2PChannel *channel); + void Receive(Packet p); + void TxComplete (void); + private: + virtual bool SendTo (Packet& p, const MacAddress& dest); + double m_rate; + P2PChannel *m_channel; +}; + +}//namespace ns3 + +#endif /* P2P_NET_DEVICE_H */ +