diff --git a/src/node/channel-list.cc b/src/node/channel-list.cc new file mode 100644 index 000000000..06e3cd73b --- /dev/null +++ b/src/node/channel-list.cc @@ -0,0 +1,182 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 University of Washington + * + * 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 "ns3/simulator.h" +#include "ns3/object-vector.h" +#include "ns3/config.h" +#include "ns3/log.h" +#include "ns3/assert.h" +#include "channel-list.h" +#include "channel.h" + +namespace ns3 { + +NS_LOG_COMPONENT_DEFINE ("ChannelList"); + +/** + * \brief private implementation detail of the ChannelList API. + */ +class ChannelListPriv : public Object +{ +public: + static TypeId GetTypeId (void); + ChannelListPriv (); + ~ChannelListPriv (); + + uint32_t Add (Ptr channel); + + ChannelList::Iterator Begin (void) const; + ChannelList::Iterator End (void) const; + + Ptr GetChannel (uint32_t n); + uint32_t GetNChannels (void); + + static Ptr Get (void); + +private: + static Ptr *DoGet (void); + static void Delete (void); + std::vector > m_channels; +}; + +NS_OBJECT_ENSURE_REGISTERED (ChannelListPriv); + +TypeId +ChannelListPriv::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::ChannelListPriv") + .SetParent () + .AddAttribute ("ChannelList", "The list of all channels created during the simulation.", + ObjectVectorValue (), + MakeObjectVectorAccessor (&ChannelListPriv::m_channels), + MakeObjectVectorChecker ()) + ; + return tid; +} + +Ptr +ChannelListPriv::Get (void) +{ + return *DoGet (); +} + +Ptr * +ChannelListPriv::DoGet (void) +{ + static Ptr ptr = 0; + if (ptr == 0) + { + ptr = CreateObject (); + Config::RegisterRootNamespaceObject (ptr); + Simulator::ScheduleDestroy (&ChannelListPriv::Delete); + } + return &ptr; +} + +void +ChannelListPriv::Delete (void) +{ + NS_LOG_FUNCTION_NOARGS (); + Config::UnregisterRootNamespaceObject (Get ()); + (*DoGet ()) = 0; +} + +ChannelListPriv::ChannelListPriv () +{ + NS_LOG_FUNCTION_NOARGS (); +} + +ChannelListPriv::~ChannelListPriv () +{ + NS_LOG_FUNCTION_NOARGS (); + for (std::vector >::iterator i = m_channels.begin (); + i != m_channels.end (); i++) + { + Ptr channel = *i; + channel->Dispose (); + *i = 0; + } + m_channels.erase (m_channels.begin (), m_channels.end ()); +} + +uint32_t +ChannelListPriv::Add (Ptr channel) +{ + uint32_t index = m_channels.size (); + m_channels.push_back (channel); + return index; + +} + +ChannelList::Iterator +ChannelListPriv::Begin (void) const +{ + return m_channels.begin (); +} + +ChannelList::Iterator +ChannelListPriv::End (void) const +{ + return m_channels.end (); +} + +uint32_t +ChannelListPriv::GetNChannels (void) +{ + return m_channels.size (); +} + +Ptr +ChannelListPriv::GetChannel (uint32_t n) +{ + NS_ASSERT_MSG (n < m_channels.size (), "Channel index " << n << + " is out of range (only have " << m_channels.size () << " channels)."); + return m_channels[n]; +} + +uint32_t +ChannelList::Add (Ptr channel) +{ + return ChannelListPriv::Get ()->Add (channel); +} + +ChannelList::Iterator +ChannelList::Begin (void) +{ + return ChannelListPriv::Get ()->Begin (); +} + +ChannelList::Iterator +ChannelList::End (void) +{ + return ChannelListPriv::Get ()->End (); +} + +Ptr +ChannelList::GetChannel (uint32_t n) +{ + return ChannelListPriv::Get ()->GetChannel (n); +} + +uint32_t +ChannelList::GetNChannels (void) +{ + return ChannelListPriv::Get ()->GetNChannels (); +} + +} //namespace ns3 diff --git a/src/node/channel-list.h b/src/node/channel-list.h new file mode 100644 index 000000000..f3b9629ba --- /dev/null +++ b/src/node/channel-list.h @@ -0,0 +1,74 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 University of Washington + * + * 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 + */ + +#ifndef CHANNEL_LIST_H +#define CHANNEL_LIST_H + +#include +#include "ns3/ptr.h" + +namespace ns3 { + +class Channel; +class CallbackBase; + + +/** + * \ingroup node + * + * \brief the list of simulation channels. + * + * Every Channel created is automatically added to this list. + */ +class ChannelList +{ +public: + typedef std::vector< Ptr >::const_iterator Iterator; + + /** + * \param channel channel to add + * \returns index of channel in list. + * + * This method is called automatically from Channel::Channel so + * the user has little reason to call it himself. + */ + static uint32_t Add (Ptr channel); + /** + * \returns a C++ iterator located at the beginning of this + * list. + */ + static Iterator Begin (void); + /** + * \returns a C++ iterator located at the end of this + * list. + */ + static Iterator End (void); + /** + * \param n index of requested channel. + * \returns the Channel associated to index n. + */ + static Ptr GetChannel (uint32_t n); + /** + * \returns the number of channels currently in the list. + */ + static uint32_t GetNChannels (void); +}; + +}//namespace ns3 + +#endif /* CHANNEL_LIST_H */ diff --git a/src/node/channel.cc b/src/node/channel.cc index e85503c66..be32b65bd 100644 --- a/src/node/channel.cc +++ b/src/node/channel.cc @@ -16,10 +16,13 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "ns3/log.h" #include "channel.h" +#include "channel-list.h" #include "net-device.h" +#include "ns3/log.h" +#include "ns3/uinteger.h" + NS_LOG_COMPONENT_DEFINE ("Channel"); namespace ns3 { @@ -30,19 +33,31 @@ TypeId Channel::GetTypeId (void) { static TypeId tid = TypeId ("ns3::Channel") - .SetParent (); + .SetParent () + .AddAttribute ("Id", "The id (unique integer) of this Channel.", + TypeId::ATTR_GET, + UintegerValue (0), + MakeUintegerAccessor (&Channel::m_id), + MakeUintegerChecker ()); return tid; } Channel::Channel () + : m_id(0) { NS_LOG_FUNCTION_NOARGS (); + m_id = ChannelList::Add (this); } - Channel::~Channel () { NS_LOG_FUNCTION_NOARGS (); } +uint32_t +Channel::GetId (void) const +{ + return m_id; +} + } // namespace ns3 diff --git a/src/node/channel.h b/src/node/channel.h index 68f8d3175..67fd082a9 100644 --- a/src/node/channel.h +++ b/src/node/channel.h @@ -45,6 +45,14 @@ public: Channel (); virtual ~Channel (); + /** + * \returns the unique id of this channel + * + * This unique id happens to be also the index of the Channel into + * the ChannelList. + */ + uint32_t GetId (void) const; + /** * \returns the number of NetDevices connected to this Channel. * @@ -59,6 +67,8 @@ public: */ virtual Ptr GetDevice (uint32_t i) const = 0; +private: + uint32_t m_id; // Channel id for this channel }; } // namespace ns3 diff --git a/src/node/wscript b/src/node/wscript index 416db3879..e790fca53 100644 --- a/src/node/wscript +++ b/src/node/wscript @@ -24,6 +24,7 @@ def build(bld): 'drop-tail-queue.cc', 'channel.cc', 'node-list.cc', + 'channel-list.cc', 'socket.cc', 'socket-factory.cc', 'packet-socket-factory.cc', @@ -72,6 +73,7 @@ def build(bld): 'ethernet-trailer.h', 'channel.h', 'node-list.h', + 'channel-list.h', 'socket.h', 'socket-factory.h', 'packet-socket-factory.h',