diff --git a/SConstruct b/SConstruct index a487f95cf..4b4d4c4a3 100644 --- a/SConstruct +++ b/SConstruct @@ -19,10 +19,11 @@ core.add_sources([ 'reference-list-test.cc', 'callback-test.cc', 'ptr.cc', + 'debug.cc', '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', @@ -39,6 +40,7 @@ core.add_inst_headers([ 'reference-list.h', 'callback.h', 'ptr.h', + 'debug.h', 'test.h' ]) @@ -120,6 +122,7 @@ common.add_sources([ 'packet.cc', 'tags.cc', 'pcap-writer.cc', + 'trace-writer.cc', 'trace-container.cc', 'variable-tracer-test.cc', 'stream-tracer-test.cc', @@ -135,6 +138,7 @@ common.add_inst_headers([ 'f-variable-tracer.h', 'callback-tracer.h', 'stream-tracer.h', + 'trace-writer.h', 'trace-container.h', 'pcap-writer.h', ]) @@ -144,6 +148,8 @@ ns3.add (node) node.add_deps (['core', 'common', 'simulator']) node.add_sources ([ 'node.cc', + 'queue.cc', + 'drop-tail.cc', 'l3-demux.cc', 'l3-protocol.cc', 'ipv4-l3-protocol.cc', @@ -198,6 +204,8 @@ node.add_headers ([ ]) node.add_inst_headers ([ 'node.h', + 'queue.h', + 'drop-tail.h', 'internet-node.h', 'udp-socket.h', 'ipv4-address.h', @@ -287,6 +295,10 @@ ns3.add(sample_sp2p) sample_sp2p.add_deps(['core', 'simulator', 'node']) sample_sp2p.add_source('main-simple-p2p.cc') - +sample_tw = build.Ns3Module('sample-tw', 'samples') +sample_tw.set_executable() +ns3.add(sample_tw) +sample_tw.add_deps(['core', 'common']) +sample_tw.add_source('main-tw.cc') ns3.generate_dependencies() diff --git a/samples/main-tw.cc b/samples/main-tw.cc new file mode 100644 index 000000000..47d655782 --- /dev/null +++ b/samples/main-tw.cc @@ -0,0 +1,53 @@ +/* -*- 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/trace-writer.h" + +using namespace ns3; + +int main (int argc, char *argv[]) +{ + NS3_TRACEALL("TraceWriter Test") + + TraceWriter writer1; + writer1.Open("trace-writer-test.txt"); + writer1.Write("writer1.char*\n"); + writer1.Write(std::string("writer1.string\n")); + writer1.Close(); + + TraceWriter writer2; + writer2.Open(std::string("trace-writer-test.txt")); + writer2.Write("writer2.char*\n"); + writer2.Write(std::string("writer2.string\n")); + writer2.Close(); + + TraceWriter writer3("trace-writer-test.txt"); + writer3.Write("writer3.char*\n"); + writer3.Write(std::string("writer3.string\n")); + writer3.Close(); + + TraceWriter writer4(std::string("trace-writer-test.txt")); + writer4.Write("writer4.char*\n"); + writer4.Write(std::string("writer4.string\n")); + writer4.Close(); + + return 0; +} diff --git a/src/common/trace-writer.cc b/src/common/trace-writer.cc new file mode 100644 index 000000000..84d94ba6e --- /dev/null +++ b/src/common/trace-writer.cc @@ -0,0 +1,109 @@ +/* -*- 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 + * + * Thu Feb 8 10:42:52 PST 2007 craigdo: Created + */ + +#include "ns3/debug.h" +#include "trace-writer.h" + +namespace ns3 { + +namespace { + int twDebug = 0; +} + + void +TraceWriter::Init (const char *filename) +{ + NS3_TRACE(twDebug, "TraceWriter()::Init(" << filename << ")") + + std::streambuf *sb = m_filestr.rdbuf(); + rdbuf(sb); + + if (filename) + { + m_filestr.open (filename, std::ios::out | std::ios::app); + } +} + +TraceWriter::TraceWriter () : + m_filestr() +{ + NS3_TRACE(twDebug, "TraceWriter()::TraceWriter()") + + Init (0); +} + +TraceWriter::TraceWriter (std::string const &filename) : + m_filestr() +{ + NS3_TRACE(twDebug, "TraceWriter()::TraceWriter (\"" << filename << "\")") + + Init (filename.c_str()); +} + +TraceWriter::TraceWriter (char const *filename) : + m_filestr() +{ + NS3_TRACE(twDebug, "TraceWriter()::TraceWriter (\"" << filename << "\")") + + Init (filename); +} + + +TraceWriter::~TraceWriter () +{ + NS3_TRACE(twDebug, "TraceWriter()::~TraceWriter()") +} + + void +TraceWriter::Open (std::string const &filename) +{ + NS3_TRACE(twDebug, "TraceWriter()::Open (\"" << filename << "\")") + + Init(filename.c_str()); +} + + void +TraceWriter::Open (char const *filename) +{ + NS3_TRACE(twDebug, "TraceWriter()::Open (\"" << filename << "\")") + + Init(filename); +} + + void +TraceWriter::Close () +{ + NS3_TRACE(twDebug, "TraceWriter()::Close ()") + + m_filestr.close (); +} + + void +TraceWriter::Write (std::string const &str) +{ + NS3_TRACE(twDebug, "TraceWriter()::Write (\"" << str << "\")") + + m_filestr << str; +} + +}; // namespace ns3 diff --git a/src/common/trace-writer.h b/src/common/trace-writer.h new file mode 100644 index 000000000..0b53e2362 --- /dev/null +++ b/src/common/trace-writer.h @@ -0,0 +1,59 @@ +/* -*- 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 + * + * Thu Feb 8 10:41:40 PST 2007 craigdo: Created + */ + +#ifndef TRACE_WRITER_H +#define TRACE_WRITER_H + +#include +#include +#include +#include +#include "ns3/callback.h" + +namespace ns3 { + +class TraceWriter : public std::ostream { +public: + TraceWriter (); + TraceWriter (std::string const &filename); + TraceWriter (char const *filename); + + ~TraceWriter (); + + void Open (std::string const &filename); + void Open (char const *filename); + void Close (); + + /** + * \param String to write to output file + */ + void Write (std::string const &str); + +protected: + std::ofstream m_filestr; + void Init (const char *filename); +}; + +}; // namespace ns3 + +#endif /* TRACE_WRITER_H */ diff --git a/src/core/debug.cc b/src/core/debug.cc new file mode 100644 index 000000000..bc1da0cd6 --- /dev/null +++ b/src/core/debug.cc @@ -0,0 +1,156 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * 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: Mathieu Lacage + */ +#include +#include +#include +#include +#include "debug.h" + +// #include "ns3/core-config.h" + +#ifdef HAVE_STDLIB_H +#include +#endif + +namespace ns3 { + +typedef std::list > ComponentList; +typedef std::list >::iterator ComponentListI; + +static ComponentList g_components; +static bool g_firstDebug = true; + +void +DebugComponentEnableEnvVar (void) +{ +#ifdef HAVE_GETENV + char *envVar = getenv("NS3_DEBUG"); + if (envVar == 0) + { + return; + } + std::string env = envVar; + std::string::size_type cur = 0; + std::string::size_type next = 0; + while (true) + { + next = env.find_first_of (";", cur); + if (next == std::string::npos) + { + std::string tmp = env.substr (cur, next); + bool found = false; + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + if (i->first.compare (tmp) == 0) + { + found = true; + i->second->Enable (); + break; + } + } + if (!found) + { + std::cout << "No debug component named=\"" << tmp << "\"" << std::endl; + } + } + cur = next; + } +#endif +} + + +DebugComponent::DebugComponent (std::string name) + : m_isEnabled (false) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + assert (i->first.compare (name) != 0); + } + g_components.push_back (std::make_pair (name, this)); +} +bool +DebugComponent::IsEnabled (void) +{ + return m_isEnabled; +} +void +DebugComponent::Enable (void) +{ + if (g_firstDebug) { + DebugComponentEnableEnvVar (); + g_firstDebug = false; + } + m_isEnabled = true; +} +void +DebugComponent::Disable (void) +{ + m_isEnabled = false; +} + +void +DebugComponentEnable (char const *name) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + if (i->first.compare (name) == 0) + { + i->second->Enable (); + break; + } + } +} +void +DebugComponentDisable (char const *name) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + if (i->first.compare (name) == 0) + { + i->second->Disable (); + break; + } + } +} + + +void +DebugComponentPrintList (void) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + std::cout << i->first << "=" << (i->second->IsEnabled ()?"enabled":"disabled") << std::endl; + } +} + + + +}; // namespace ns3 diff --git a/src/core/debug.h b/src/core/debug.h new file mode 100644 index 000000000..239b89c17 --- /dev/null +++ b/src/core/debug.h @@ -0,0 +1,83 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * 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: Mathieu Lacage + */ +#ifndef DEBUG_H +#define DEBUG_H + +#include +#include + +#ifndef NDEBUG +#define NS3_DEBUG_ENABLE +#endif + +#ifdef NS3_DEBUG_ENABLE +#define NS3_DEBUG(x) x +#else +#define NS3_DEBUG(x) +#endif + +#define NS3_TRACEALL(traceout) NS3_DEBUG(std::cerr << traceout << std::endl;) + +#define NS3_TRACE(boolLevel, traceout) \ + NS3_DEBUG( \ + if (boolLevel) { \ + std::cerr << traceout << std::endl; \ + } \ + ) + + +namespace ns3 { + +void DebugComponentEnable (char const *name); +void DebugComponentDisable (char const *name); +void DebugComponentPrintList (void); + +class DebugComponent { +public: + DebugComponent (std::string name); + bool IsEnabled (void); + void Enable (void); + void Disable (void); +private: + bool m_isEnabled; +}; + +}; // namespace ns3 + + +#ifdef NS3_DEBUG_ENABLE + +#define DEBUG_COMPONENT_DEFINE(name) \ + static DebugComponent g_debug = DebugComponent (name); + +#define DEBUG(x) \ + if (g_debug.IsEnabled ()) \ + { \ + std::cout << x << std::endl; \ + } +#else /* NS3_DEBUG_ENABLE */ + +#define DEBUG_COMPONENT_DEFINE(name) +#define DEBUG(x) + +#endif /* NS3_DEBUG_ENABLE */ + +#endif /* DEBUG_H */ diff --git a/src/node/drop-tail.cc b/src/node/drop-tail.cc new file mode 100644 index 000000000..3b87ea5b6 --- /dev/null +++ b/src/node/drop-tail.cc @@ -0,0 +1,111 @@ +/* -*- 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 "ns3/debug.h" +#include "drop-tail.h" + +namespace ns3 { + +namespace { + int dtqDebug = 0; +} + +DropTailQueue::DropTailQueue () : + m_packets (), + m_maxPackets(DTQ_NPACKETS_MAX_DEFAULT) +{ + NS3_TRACE(dtqDebug, + "DropTailQueue::DropTailQueue ()") +} + +DropTailQueue::DropTailQueue (TraceContainer &traceContainer) : + m_packets(), + m_maxPackets(DTQ_NPACKETS_MAX_DEFAULT) +{ + NS3_TRACE(dtqDebug, + "DropTailQueue::DropTailQueue (" << &traceContainer << ")") + + RegisterTraces(traceContainer); +} + +DropTailQueue::~DropTailQueue () +{ + NS3_TRACE(dtqDebug, + "DropTailQueue::~DropTailQueue ()") +} + + void +DropTailQueue::SetMaxPackets (uint32_t npackets) +{ + NS3_TRACE(dtqDebug, + "DropTailQueue::SetMaxPackets (" << npackets << ")") + + m_maxPackets = npackets; +} + + uint32_t +DropTailQueue::GetMaxPackets (void) +{ + NS3_TRACE(dtqDebug, + "DropTailQueue::GetMaxPackets () <= " << m_maxPackets) + + return m_maxPackets; +} + + bool +DropTailQueue::DoEnque (const Packet& p) +{ + NS3_TRACE(dtqDebug, + "DropTailQueue::DoEnque (" << &p << ")") + + if (m_nPackets >= m_maxPackets) + { + NS3_TRACE(dtqDebug, + "DropTailQueue::DoEnque (): Queue full -- droppping pkt") + Drop (p); + return false; + } + + m_packets.push(p); + return true; +} + + bool +DropTailQueue::DoDeque (Packet& p) +{ + NS3_TRACE(dtqDebug, + "DropTailQueue::DoDeque (" << &p << ")") + + if (m_packets.empty()) + { + NS3_TRACE(dtqDebug, + "DropTailQueue::DoDeque (): Queue empty") + return false; + } + + p = m_packets.front (); + m_packets.pop (); + + NS3_TRACE(dtqDebug, + "DropTailQueue::DoDeque (): Popped " << &p << " <= true") + + return true; +} + +}; // namespace ns3 diff --git a/src/node/drop-tail.h b/src/node/drop-tail.h new file mode 100644 index 000000000..264693758 --- /dev/null +++ b/src/node/drop-tail.h @@ -0,0 +1,53 @@ +/* -*- 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 + */ + +#ifndef DROPTAIL_H +#define DROPTAIL_H + +#include +#include "ns3/packet.h" +#include "ns3/queue.h" + +namespace ns3 { + +const int DTQ_NPACKETS_MAX_DEFAULT = 100; + +class DropTailQueue : public Queue { +public: + DropTailQueue (); + + DropTailQueue (TraceContainer &traceContainer); + + virtual ~DropTailQueue(); + + void SetMaxPackets (uint32_t npackets); + uint32_t GetMaxPackets (void); + +protected: + bool DoEnque (const Packet& p); + bool DoDeque (Packet &p); + +private: + std::queue m_packets; + uint32_t m_maxPackets; +}; + +}; // namespace ns3 + +#endif /* DROPTAIL_H */ diff --git a/src/node/queue.cc b/src/node/queue.cc new file mode 100644 index 000000000..ec0631ddd --- /dev/null +++ b/src/node/queue.cc @@ -0,0 +1,185 @@ +/* -*- 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 "ns3/debug.h" +#include "queue.h" + +namespace ns3 { + +namespace { + int qDebug = 0; +} + +Queue::Queue() : + m_nBytes(0), + m_nTotalReceivedBytes(0), + m_nPackets(0), + m_nTotalReceivedPackets(0), + m_nTotalDroppedBytes(0), + m_nTotalDroppedPackets(0) +{ + NS3_TRACE(qDebug, "Queue::Queue ()") +} + +Queue::~Queue() +{ + NS3_TRACE(qDebug, "Queue::~Queue ()") +} + + bool +Queue::Enque (const Packet& p) +{ + NS3_TRACE(qDebug, "Queue::Enque (" << &p << ")") + + NS3_TRACE(qDebug, "Queue::Enque (): m_traceEnque (p)") + m_traceEnque ("+ ", p); + + bool retval = DoEnque (p); + if (retval) + { + m_nBytes += p.GetSize (); + m_nPackets++; + } + return retval; +} + + bool +Queue::Deque (Packet &p) +{ + NS3_TRACE(qDebug, "Queue::Deque (" << &p << ")") + + bool retval = DoDeque (p); + + if (retval) + { + m_nBytes -= p.GetSize (); + m_nPackets--; + + assert(m_nBytes >= 0); + assert(m_nPackets >= 0); + + NS3_TRACE(qDebug, "Queue::Deque (): m_traceDeque (p)") + m_traceDeque ("+ ", static_cast(p)); + } + + return retval; +} + + void +Queue::DequeAll (void) +{ + NS3_TRACE(qDebug, "Queue::DequeAll ()") + + assert (!"Don't know what to do with dequeued packets!"); +} + + uint32_t +Queue::GetNPackets (void) +{ + NS3_TRACE(qDebug, "Queue::GetNPackets () <= " << m_nPackets) + + return m_nPackets; +} + + uint32_t +Queue::GetNBytes (void) +{ + NS3_TRACE(qDebug, "Queue::GetNBytes () <= " << m_nBytes) + + return m_nBytes; +} + + + bool +Queue::IsEmpty (void) +{ + NS3_TRACE(qDebug, "Queue::IsEmpty () <= " << (m_nPackets == 0)) + return m_nPackets == 0; +} + + void +Queue::RegisterTraces (TraceContainer &container) +{ + NS3_TRACE(qDebug, "Queue::RegisterTraces (" << &container << ")") + + container.RegisterCallback ("Queue::Enque", &m_traceEnque); + container.RegisterCallback ("Queue::Deque", &m_traceDeque); + container.RegisterCallback ("Queue::Drop", &m_traceDrop); +} + + uint32_t +Queue::GetTotalReceivedBytes (void) +{ + NS3_TRACE(qDebug, + "Queue::GetTotalReceivedBytes () <= " << m_nTotalReceivedBytes) + + return m_nTotalReceivedBytes; +} + + uint32_t +Queue::GetTotalReceivedPackets (void) +{ + NS3_TRACE(qDebug, + "Queue::GetTotalReceivedPackets () <= " << m_nTotalReceivedPackets) + + return m_nTotalReceivedPackets; +} + + uint32_t +Queue:: GetTotalDroppedBytes (void) +{ + NS3_TRACE(qDebug, + "Queue::GetTotalDroppedBytes () <= " << m_nTotalDroppedBytes +) + return m_nTotalDroppedBytes; +} + + uint32_t +Queue::GetTotalDroppedPackets (void) +{ + NS3_TRACE(qDebug, + "Queue::GetTotalDroppedPackets () <= " << m_nTotalDroppedPackets) + + return m_nTotalDroppedPackets; +} + + void +Queue::ResetStatistics (void) +{ + NS3_TRACE(qDebug, "Queue::ResetStatistics ()") + + m_nTotalReceivedBytes = 0; + m_nTotalReceivedPackets = 0; + m_nTotalDroppedBytes = 0; + m_nTotalDroppedPackets = 0; +} + + void +Queue::Drop (const Packet& p) +{ + NS3_TRACE(qDebug, "Queue::Drop (" << &p << ")") + + m_nTotalDroppedPackets++; + m_nTotalDroppedBytes += p.GetSize (); + + NS3_TRACE(qDebug, "Queue::Drop (): m_traceDrop (p)") + m_traceEnque ("d ", p); +} + +}; // namespace ns3 diff --git a/src/node/queue.h b/src/node/queue.h new file mode 100644 index 000000000..cbe07ee35 --- /dev/null +++ b/src/node/queue.h @@ -0,0 +1,115 @@ +/* -*- 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 + */ + +// The queue base class does not have any limit based on the number +// of packets or number of bytes. It is, conceptually, infinite +// by default. Only subclasses define limitations. +// The base class implements tracing and basic statistics calculations. + +#ifndef QUEUE_H +#define QUEUE_H + +#include "ns3/packet.h" +#include "ns3/callback-tracer.h" +#include "ns3/trace-container.h" + +namespace ns3 { + +class Queue +{ +public: + Queue (); + virtual ~Queue (); + + bool Enque (const Packet& p); + bool Deque (Packet &p); + + void DequeAll (void); + uint32_t GetNPackets (void); + uint32_t GetNBytes (void); + + bool IsEmpty (void); + + void RegisterTraces (TraceContainer &container); + + uint32_t GetTotalReceivedBytes (void); + uint32_t GetTotalReceivedPackets (void); + uint32_t GetTotalDroppedBytes (void); + uint32_t GetTotalDroppedPackets (void); + + void ResetStatistics (void); + +#if 0 + // average calculation requires keeping around + // a buffer with the date of arrival of past received packets + // which are within the average window + // so, it is quite costly to do it all the time. + // Hence, it is disabled by default and must be explicitely + // enabled with this method which specifies the size + // of the average window in time units. + void EnableRunningAverage (Time averageWindow); + void DisableRunningAverage (void); + // average + double GetQueueSizeAverage (void); + double GetReceivedBytesPerSecondAverage (void); + double GetReceivedPacketsPerSecondAverage (void); + double GetDroppedBytesPerSecondAverage (void); + double GetDroppedPacketsPerSecondAverage (void); + // variance + double GetQueueSizeVariance (void); + double GetReceivedBytesPerSecondVariance (void); + double GetReceivedPacketsPerSecondVariance (void); + double GetDroppedBytesPerSecondVariance (void); + double GetDroppedPacketsPerSecondVariance (void); +#endif + +private: + virtual bool DoEnque (const Packet& p) = 0; + virtual bool DoDeque (Packet &p) = 0; + +protected: + // called by subclasses to notify parent of packet drops. + void Drop (const Packet& p); + + uint32_t m_nBytes; + uint32_t m_nTotalReceivedBytes; + uint32_t m_nPackets; + uint32_t m_nTotalReceivedPackets; + uint32_t m_nTotalDroppedBytes; + uint32_t m_nTotalDroppedPackets; + +private: + CallbackTracer m_traceEnque; + CallbackTracer m_traceDeque; + CallbackTracer m_traceDrop; + +#if 0 + // Static methods to manage queue default + // Set desired queue default + static void Default(const Queue& q); + // Return reference to the current default queue type + static Queue& Default(); + // Static variable pointing to current default queue + static Queue* defaultQueue; +#endif +}; + +}; // namespace ns3 + +#endif /* QUEUE_H */