diff --git a/SConstruct b/SConstruct index e9938aa35..7320c051a 100644 --- a/SConstruct +++ b/SConstruct @@ -24,7 +24,7 @@ core.add_sources([ 'test.cc', ]) env = Environment() -if env['PLATFORM'] == 'posix' or env['PLATFORM'] == 'darwin': +if env['PLATFORM'] == 'posix' or env['PLATFORM'] == 'darwin' or env['PLATFORM'] == 'cygwin': core.add_external_dep('pthread') core.add_sources([ 'unix-system-wall-clock-ms.cc', @@ -197,6 +197,8 @@ node.add_sources ([ 'serial-channel.cc', 'queue.cc', 'drop-tail.cc', + 'layer-connector.cc', + 'channel.cc', ]) node.add_headers ([ 'ipv4-header.h', @@ -236,6 +238,8 @@ node.add_inst_headers ([ 'serial-channel.h', 'queue.h', 'drop-tail.h', + 'layer-connector.h', + 'channel.h', 'serial-net-device.h' ]) @@ -335,5 +339,12 @@ ns3.add(sample_simple_tcl) sample_simple_tcl.add_deps(['core', 'simulator', 'node']) sample_simple_tcl.add_source('ns-2/simple.tcl.cc') +sample_channel = build.Ns3Module('sample-channel', 'samples') +sample_channel.set_executable() +ns3.add(sample_channel) +sample_channel.add_dep ('common') +sample_channel.add_dep ('node') +sample_channel.add_dep ('core') +sample_channel.add_source('main-channel.cc') ns3.generate_dependencies() diff --git a/samples/main-channel.cc b/samples/main-channel.cc new file mode 100644 index 000000000..86ee4b024 --- /dev/null +++ b/samples/main-channel.cc @@ -0,0 +1,216 @@ +/* -*- 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/packet.h" +#include "ns3/drop-tail.h" +#include "ns3/layer-connector.h" +#include "ns3/channel.h" + +using namespace ns3; + +// =========================================================================== +// Cook up a simplistic Internet Node +// =========================================================================== +class FakeInternetNode : public LayerConnectorUpper +{ +public: + FakeInternetNode (); + ~FakeInternetNode (); + + void Doit (void); + +protected: + bool UpperDoSendUp (Packet &p); + bool UpperDoPull (Packet &p); + + DropTailQueue m_dtqOutbound; + DropTailQueue m_dtqInbound; +}; + +FakeInternetNode::FakeInternetNode () +{ + NS_DEBUG_UNCOND("FakeInternetNode::FakeInternetNode ()") +} + +FakeInternetNode::~FakeInternetNode () +{ + NS_DEBUG_UNCOND("FakeInternetNode::~FakeInternetNode ()") +} + + void +FakeInternetNode::Doit (void) +{ + NS_DEBUG_UNCOND("FakeInternetNode::Doit ()") + NS_DEBUG_UNCOND("FakeInternetNode::Doit (): **** Send outbound packet") + Packet p; + + m_dtqOutbound.Enque(p); + UpperNotify(); +} + + bool +FakeInternetNode::UpperDoSendUp (Packet &p) +{ + NS_DEBUG_UNCOND("FakeInternetNode::UpperDoSendUp (" << &p << ")") + + NS_DEBUG_UNCOND("FakeInternetNode::UpperDoSendUp (): **** Receive inbound packet") + m_dtqInbound.Enque(p); + return m_dtqInbound.Deque(p); +} + + bool +FakeInternetNode::UpperDoPull (Packet &p) +{ + NS_DEBUG_UNCOND("FakeInternetNode::DoPull (" << &p << ")") + + return m_dtqOutbound.Deque(p); +} + +// =========================================================================== +// Cook up a simplistic Physical Layer +// =========================================================================== +class FakePhysicalLayer : + public LayerConnectorLower, + public LayerConnectorUpper +{ +public: + FakePhysicalLayer (); + ~FakePhysicalLayer (); + +protected: + bool LowerDoNotify (LayerConnectorUpper *upper); + bool UpperDoSendUp (Packet &p); + bool UpperDoPull (Packet &p); + + DropTailQueue m_dtqInbound; + DropTailQueue m_dtqOutbound; +}; + +FakePhysicalLayer::FakePhysicalLayer () +{ + NS_DEBUG_UNCOND("FakePhysicalLayer::FakePhysicalLayer ()") +} + +FakePhysicalLayer::~FakePhysicalLayer () +{ + NS_DEBUG_UNCOND("FakePhysicalLayer::~FakePhysicalLayer ()") +} + + bool +FakePhysicalLayer::LowerDoNotify (LayerConnectorUpper *upper) +{ + NS_DEBUG_UNCOND("FakePhysicalLayer::LowerDoNotify ()") + + Packet p; + + NS_DEBUG_UNCOND("FakePhysicalLayer::LowerDoNotify (): Starting pull") + + assert(m_upperPartner); + m_upperPartner->UpperPull(p); + + m_dtqOutbound.Enque(p); + + NS_DEBUG_UNCOND("FakePhysicalLayer::LowerDoNotify (): Got bits, Notify lower") + + assert(m_lowerPartner); + return m_lowerPartner->LowerNotify(this); +} + + bool +FakePhysicalLayer::UpperDoSendUp (Packet &p) +{ + NS_DEBUG_UNCOND("FakePhysicalLayer::UpperDoSendUp (" << &p << ")") + + assert(m_upperPartner); + return m_upperPartner->UpperSendUp(p); +} + + bool +FakePhysicalLayer::UpperDoPull (Packet &p) +{ + NS_DEBUG_UNCOND("FakePhysicalLayer::DoPull (" << &p << ")") + + return m_dtqOutbound.Deque(p); +} + +// =========================================================================== +// Cook up a simplistic Channel, just to add any moby hack we feel like +// =========================================================================== +class FakeChannel : public Channel +{ +public: + FakeChannel (); + ~FakeChannel (); +}; + +FakeChannel::FakeChannel () +{ + NS_DEBUG_UNCOND("FakeChannel::FakeChannel ()") +} + +FakeChannel::~FakeChannel () +{ + NS_DEBUG_UNCOND("FakeChannel::~FakeChannel ()") +} + +int main (int argc, char *argv[]) +{ + NS_DEBUG_UNCOND("Channel Hackorama") + + DebugComponentEnable("Queue"); + DebugComponentEnable("DropTailQueue"); + DebugComponentEnable("LayerConnector"); + DebugComponentEnable("Channel"); + + FakeInternetNode node1, node2, node3, node4; + FakePhysicalLayer phys1, phys2, phys3, phys4; + FakeChannel channel; + Packet pkt; + + // all the hassle above lets us do something very simple here + + node1.ConnectToLower(phys1); + phys1.ConnectToUpper(node1); + phys1.ConnectToLower(channel); + channel.ConnectToUpper(phys1); + + node2.ConnectToLower(phys2); + phys2.ConnectToUpper(node2); + phys2.ConnectToLower(channel); + channel.ConnectToUpper(phys2); + + node3.ConnectToLower(phys3); + phys3.ConnectToUpper(node3); + phys3.ConnectToLower(channel); + channel.ConnectToUpper(phys3); + + node4.ConnectToLower(phys4); + phys4.ConnectToUpper(node4); + phys4.ConnectToLower(channel); + channel.ConnectToUpper(phys4); + + node1.Doit(); + node2.Doit(); + node3.Doit(); + node4.Doit(); + + return 0; +} diff --git a/src/node/channel.cc b/src/node/channel.cc index f28d4379e..eaade96d2 100644 --- a/src/node/channel.cc +++ b/src/node/channel.cc @@ -30,32 +30,49 @@ namespace ns3 { Channel::Channel () { - NS_DEBUG ("Channel::Channel ()") + NS_DEBUG("Channel::Channel ()") } Channel::~Channel () { - NS_DEBUG ("Channel::~Channel ()") + NS_DEBUG("Channel::~Channel ()") } bool -Channel::Connect (PhysicalLayer &phys) +Channel::DoConnectToUpper (LayerConnectorUpper &upper) { - NS_DEBUG ("Channel::Connect (" << &phys << ")") - m_physList.push_back(static_cast(&phys)); + NS_DEBUG("Channel::DoConnectToUpper (" << &upper << ")") + m_connectorList.push_back(&upper); + return true; } bool -Channel::Propagate (Propagator &propagator) +Channel::LowerDoNotify (LayerConnectorUpper *upper) { - NS_DEBUG ("Channel::Propagate (" << &propagator << ")") + NS_DEBUG("Channel::LowerDoNotify ()") - for (PhysicalLayerList::const_iterator i = m_physList.begin (); - i != m_physList.end (); + Packet p; + + NS_DEBUG("Channel::LowerDoNotify (): Starting pull") + + upper->UpperPull(p); + + NS_DEBUG("Channel::LowerDoNotify (): Got bits, Propagate()") + + return Propagate(p); +} + + bool +Channel::Propagate (Packet &p) +{ + NS_DEBUG("Channel::Propagate (" << &p << ")") + + for (ConnectorList::const_iterator i = m_connectorList.begin (); + i != m_connectorList.end (); i++) { - (*i)->Receive (propagator); + (*i)->UpperSendUp (p); } return true; diff --git a/src/node/channel.h b/src/node/channel.h index 25fc32d3e..67b74178e 100644 --- a/src/node/channel.h +++ b/src/node/channel.h @@ -22,36 +22,33 @@ */ #include -#include "physical-layer.h" +#include "ns3/packet.h" +#include "layer-connector.h" #ifndef CHANNEL_H #define CHANNEL_H namespace ns3 { -class Propagator; - -class Channel +class Channel : public LayerConnectorLower { public: Channel (); virtual ~Channel (); - // called by the builderto introduce the channel and the physical layer - // of the node. - bool Connect (PhysicalLayer &phys); - // Called by the physical layer to cause bits to propagate along the channel // The channel will call Receive on each of the phys. - bool Propagate (Propagator &p); + bool Propagate (Packet &p); + bool DoConnectToUpper (LayerConnectorUpper &upper); + bool LowerDoNotify (LayerConnectorUpper *upper); protected: - typedef std::list PhysicalLayerList; - PhysicalLayerList m_physList; + typedef std::list ConnectorList; + ConnectorList m_connectorList; private: }; }; // namespace ns3 -#endif /* SERIAL_PHY_H */ +#endif /* CHANNEL_H */ diff --git a/src/node/layer-connector.cc b/src/node/layer-connector.cc new file mode 100644 index 000000000..6b6232815 --- /dev/null +++ b/src/node/layer-connector.cc @@ -0,0 +1,137 @@ +/* -*- 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 + * + * Author: Craig Dowell + * + * Fri Feb 16 12:18:11 PST 2007 craigdo: created + */ + +#include "ns3/debug.h" +#include "layer-connector.h" + +NS_DEBUG_COMPONENT_DEFINE ("LayerConnector"); + +namespace ns3 { + +LayerConnectorUpper::LayerConnectorUpper () +{ + NS_DEBUG("LayerConnectorUpper::LayerConnectorUpper ()") +} + +LayerConnectorUpper::~LayerConnectorUpper () +{ + NS_DEBUG("LayerConnectorUpper::~LayerConnectorUpper ()") +} + + bool +LayerConnectorUpper::ConnectToLower (LayerConnectorLower &partner) +{ + NS_DEBUG("LayerConnectorUpper::ConnectToLower (" << &partner << ")") + + return DoConnectToLower(partner); +} + + bool +LayerConnectorUpper::DoConnectToLower (LayerConnectorLower &partner) +{ + NS_DEBUG("LayerConnectorUpper::DoConnectToLower (" << &partner << ")") + + m_lowerPartner = &partner; + assert(m_lowerPartner); + return true; +} + + bool +LayerConnectorUpper::UpperSendUp (Packet &p) +{ + NS_DEBUG("LayerConnectorUpper::UpperSendUp (" << &p << ")") + + return UpperDoSendUp(p); +} + + bool +LayerConnectorUpper::UpperPull (Packet &p) +{ + NS_DEBUG("LayerConnectorUpper::UpperPull (" << &p << ")") + + return UpperDoPull(p); +} + + bool +LayerConnectorUpper::UpperNotify () +{ + NS_DEBUG("LayerConnectorUpper::UpperNotify ()") + + assert(m_lowerPartner); + return m_lowerPartner->LowerNotify(this); +} + +LayerConnectorLower::LayerConnectorLower () +{ + NS_DEBUG("LayerConnectorLower::LayerConnectorLower ()") +} + +LayerConnectorLower::~LayerConnectorLower () +{ + NS_DEBUG("LayerConnectorLower::~LayerConnectorLower ()") +} + + bool +LayerConnectorLower::ConnectToUpper (LayerConnectorUpper &partner) +{ + NS_DEBUG("LayerConnectorLower::ConnectToUpper (" << &partner << ")") + + return DoConnectToUpper(partner); +} + + bool +LayerConnectorLower::DoConnectToUpper (LayerConnectorUpper &partner) +{ + NS_DEBUG("LayerConnectorLower::DoConnectToUpper (" << &partner << ")") + + m_upperPartner = &partner; + assert(m_upperPartner); + return true; +} + + bool +LayerConnectorLower::LowerSendUp (Packet &p) +{ + NS_DEBUG("LayerConnectorLower::LowerSendUp (" << &p << ")") + + assert(m_upperPartner); + return m_upperPartner->UpperSendUp(p); +} + + bool +LayerConnectorLower::LowerPull (Packet &p) +{ + NS_DEBUG("LayerConnectorLower::LowerPull (" << &p << ")") + + assert(m_upperPartner); + return m_upperPartner->UpperPull(p); +} + + bool +LayerConnectorLower::LowerNotify (LayerConnectorUpper *upper) +{ + NS_DEBUG("LayerConnectorLower::LowerNotify ()") + return LowerDoNotify(upper); +} + +} // namespace ns3 diff --git a/src/node/layer-connector.h b/src/node/layer-connector.h new file mode 100644 index 000000000..0c3f37626 --- /dev/null +++ b/src/node/layer-connector.h @@ -0,0 +1,97 @@ +/* -*- 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 + * + * Author: Craig Dowell + * + * Wed Feb 14 16:05:46 PST 2007 craigdo: created + */ + +#include "ns3/packet.h" + +#ifndef LAYER_CONNECTOR_H +#define LAYER_CONNECTOR_H + +namespace ns3 { + +class LayerConnectorLower; + +class LayerConnectorUpper { +public: + LayerConnectorUpper (); + virtual ~LayerConnectorUpper (); + + // called by the layer connector to introduce the lower interface to us here + bool ConnectToLower (LayerConnectorLower &lower); + + // called by the lower layer to send a received packet on up the protocol + // stack to us here. + bool UpperSendUp (Packet &p); + + // Call this function to tell a lower layer that there is data available + // here. It is expected that the lower layer will call pull as soon as + // possible to get the data. + bool UpperNotify (); + + // called by the lower layer to get available packet data that was implied + // by a previous Notify() call. + bool UpperPull(Packet &p); + +protected: + virtual bool DoConnectToLower (LayerConnectorLower &lower); + virtual bool UpperDoSendUp (Packet &p) = 0; + virtual bool UpperDoPull (Packet &p) = 0; + + LayerConnectorLower *m_lowerPartner; + +private: +}; + +class LayerConnectorLower +{ +public: + LayerConnectorLower (); + virtual ~LayerConnectorLower (); + + // This function is called by the layer connector to introduce the upper + // layer interface to us here + bool ConnectToUpper (LayerConnectorUpper &upper); + + // Notify is called by the upper layer connector to tell us that there + // is data available. It is expected that we will call pull (below) as + // soon as possible to get the data. + bool LowerNotify (LayerConnectorUpper *upper); + + // The lower connector calls this method to send a received packet on up + // the protocol stack + bool LowerSendUp (Packet &p); + + // Call this function to get available packet data from the upper connector + // that was implied by a previous Notify() call. + bool LowerPull(Packet &p); + +protected: + virtual bool DoConnectToUpper (LayerConnectorUpper &upper); + virtual bool LowerDoNotify (LayerConnectorUpper *upper) = 0; + LayerConnectorUpper *m_upperPartner; + +private: +}; + +}; // namespace ns3 + +#endif /* LAYER_CONNECTOR_H */