From 939cee7ac3591fbb810da701d50edf3892ec54eb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 16 Mar 2008 18:42:23 +0100 Subject: [PATCH 01/35] do not use ObjectBase as a base class. --- src/common/packet.h | 3 ++- src/core/callback.h | 2 +- src/core/ptr.cc | 3 +-- src/devices/wifi/wifi-phy.cc | 2 +- src/simulator/event-impl.h | 2 +- src/simulator/simulator.cc | 3 ++- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/common/packet.h b/src/common/packet.h index afd16d5c5..2858f1153 100644 --- a/src/common/packet.h +++ b/src/common/packet.h @@ -74,7 +74,8 @@ class PacketPrinter; * The performance aspects of the Packet API are discussed in * \ref packetperf */ -class Packet : public ObjectBase { +class Packet +{ public: void Ref (void) const; void Unref (void) const; diff --git a/src/core/callback.h b/src/core/callback.h index 0e0f68798..7a70bb1ce 100644 --- a/src/core/callback.h +++ b/src/core/callback.h @@ -71,7 +71,7 @@ struct CallbackTraits } }; -class CallbackImplBase : public ObjectBase +class CallbackImplBase { public: CallbackImplBase () diff --git a/src/core/ptr.cc b/src/core/ptr.cc index 39b7f9554..3474d40ca 100644 --- a/src/core/ptr.cc +++ b/src/core/ptr.cc @@ -23,7 +23,6 @@ #ifdef RUN_SELF_TESTS #include "test.h" -#include "object-base.h" namespace ns3 { @@ -46,7 +45,7 @@ private: }; -class Base : public ObjectBase +class Base { public: Base (); diff --git a/src/devices/wifi/wifi-phy.cc b/src/devices/wifi/wifi-phy.cc index 43e598380..b62cfe230 100644 --- a/src/devices/wifi/wifi-phy.cc +++ b/src/devices/wifi/wifi-phy.cc @@ -78,7 +78,7 @@ WifiPhyListener::~WifiPhyListener () * Phy event class ****************************************************************/ -class RxEvent : public ObjectBase +class RxEvent { public: RxEvent (uint32_t size, WifiMode payloadMode, diff --git a/src/simulator/event-impl.h b/src/simulator/event-impl.h index dcb63e0ff..5526fb0c7 100644 --- a/src/simulator/event-impl.h +++ b/src/simulator/event-impl.h @@ -25,7 +25,7 @@ namespace ns3 { -class EventImpl : public ObjectBase +class EventImpl { public: EventImpl (); diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 86836684c..bf9a97655 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -595,7 +595,8 @@ static void cber5 (const int &, const int &, const int &, const int &, const int {} -class SimulatorTests : public Test, public ObjectBase { +class SimulatorTests : public Test +{ public: SimulatorTests (); // only here for testing of Ptr<> From 3aa97061505419bdb95c10feb2e90e72ed8d413b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 16 Mar 2008 19:24:50 +0100 Subject: [PATCH 02/35] replace TypeId::CreateObject with TypeId::GetConstructor --- src/core/object-factory.cc | 7 ++++- src/core/object.cc | 58 +++++++++++++++----------------------- src/core/object.h | 15 +++++----- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/core/object-factory.cc b/src/core/object-factory.cc index 264f03fc2..ebdf5e20c 100644 --- a/src/core/object-factory.cc +++ b/src/core/object-factory.cc @@ -59,7 +59,12 @@ ObjectFactory::GetTypeId (void) const Ptr ObjectFactory::Create (void) const { - Ptr object = m_tid.CreateObject (m_parameters); + Callback cb = m_tid.GetConstructor (); + ObjectBase *base = cb (); + Object *derived = dynamic_cast (base); + derived->SetTypeId (m_tid); + derived->Construct (m_parameters); + Ptr object = Ptr (derived, false); return object; } diff --git a/src/core/object.cc b/src/core/object.cc index 0ccd12870..50742da4f 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -44,17 +44,17 @@ public: void SetParent (uint16_t uid, uint16_t parent); void SetTypeName (uint16_t uid, std::string typeName); void SetGroupName (uint16_t uid, std::string groupName); - void AddConstructor (uint16_t uid, ns3::CallbackBase callback); + void AddConstructor (uint16_t uid, ns3::Callback callback); void HideFromDocumentation (uint16_t uid); uint16_t GetUid (std::string name) const; std::string GetName (uint16_t uid) const; uint16_t GetParent (uint16_t uid) const; std::string GetTypeName (uint16_t uid) const; std::string GetGroupName (uint16_t uid) const; - ns3::CallbackBase GetConstructor (uint16_t uid); - bool HasConstructor (uint16_t uid); - uint32_t GetRegisteredN (void); - uint16_t GetRegistered (uint32_t i); + ns3::Callback GetConstructor (uint16_t uid) const; + bool HasConstructor (uint16_t uid) const; + uint32_t GetRegisteredN (void) const; + uint16_t GetRegistered (uint32_t i) const; void AddAttribute (uint16_t uid, std::string name, std::string help, @@ -99,7 +99,7 @@ private: std::string typeName; std::string groupName; bool hasConstructor; - ns3::CallbackBase constructor; + ns3::Callback constructor; bool mustHideFromDocumentation; std::vector attributes; std::vector traceSources; @@ -174,7 +174,7 @@ IidManager::HideFromDocumentation (uint16_t uid) } void -IidManager::AddConstructor (uint16_t uid, ns3::CallbackBase callback) +IidManager::AddConstructor (uint16_t uid, ns3::Callback callback) { struct IidInformation *information = LookupInformation (uid); if (information->hasConstructor) @@ -225,8 +225,8 @@ IidManager::GetGroupName (uint16_t uid) const return information->groupName; } -ns3::CallbackBase -IidManager::GetConstructor (uint16_t uid) +ns3::Callback +IidManager::GetConstructor (uint16_t uid) const { struct IidInformation *information = LookupInformation (uid); if (!information->hasConstructor) @@ -237,19 +237,19 @@ IidManager::GetConstructor (uint16_t uid) } bool -IidManager::HasConstructor (uint16_t uid) +IidManager::HasConstructor (uint16_t uid) const { struct IidInformation *information = LookupInformation (uid); return information->hasConstructor; } uint32_t -IidManager::GetRegisteredN (void) +IidManager::GetRegisteredN (void) const { return m_information.size (); } uint16_t -IidManager::GetRegistered (uint32_t i) +IidManager::GetRegistered (uint32_t i) const { return i + 1; } @@ -529,7 +529,7 @@ TypeId::HasConstructor (void) const } void -TypeId::DoAddConstructor (CallbackBase cb) +TypeId::DoAddConstructor (Callback cb) { Singleton::Get ()->AddConstructor (m_tid, cb); } @@ -557,27 +557,11 @@ TypeId::AddAttribute (std::string name, return *this; } - -CallbackBase -TypeId::LookupConstructor (void) const +Callback +TypeId::GetConstructor (void) const { - CallbackBase constructor = Singleton::Get ()->GetConstructor (m_tid); - return constructor; -} - -Ptr -TypeId::CreateObject (void) const -{ - return CreateObject (AttributeList ()); -} -Ptr -TypeId::CreateObject (const AttributeList &attributes) const -{ - CallbackBase cb = LookupConstructor (); - Callback,const AttributeList &> realCb; - realCb.Assign (cb); - Ptr object = realCb (attributes); - return object; + Callback cb = Singleton::Get ()->GetConstructor (m_tid); + return cb; } bool @@ -1345,6 +1329,7 @@ Object::MaybeDelete (void) const #ifdef RUN_SELF_TESTS #include "test.h" +#include "object-factory.h" namespace { @@ -1481,11 +1466,14 @@ ObjectTest::RunTests (void) // Test the object creation code of TypeId - Ptr a = BaseA::GetTypeId ().CreateObject (); + ObjectFactory factory; + factory.SetTypeId (BaseA::GetTypeId ()); + Ptr a = factory.Create (); NS_TEST_ASSERT_EQUAL (a->GetObject (), a); NS_TEST_ASSERT_EQUAL (a->GetObject (DerivedA::GetTypeId ()), 0); NS_TEST_ASSERT_EQUAL (a->GetObject (), 0); - a = DerivedA::GetTypeId ().CreateObject (); + factory.SetTypeId (DerivedA::GetTypeId ()); + a = factory.Create (); NS_TEST_ASSERT_EQUAL (a->GetObject (), a); NS_TEST_ASSERT_EQUAL (a->GetObject (DerivedA::GetTypeId ()), a); NS_TEST_ASSERT_UNEQUAL (a->GetObject (), 0); diff --git a/src/core/object.h b/src/core/object.h index 53a0c4958..6d0e5d279 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -177,8 +177,7 @@ public: std::string GetTraceSourceHelp (uint32_t i) const; Ptr GetTraceSourceAccessor (uint32_t i) const; - Ptr CreateObject (const AttributeList &attributes) const; - Ptr CreateObject (void) const; + Callback GetConstructor (void) const; bool MustHideFromDocumentation (void) const; @@ -319,8 +318,7 @@ private: static bool LookupAttributeByFullName (std::string fullName, struct AttributeInfo *info); explicit TypeId (uint16_t tid); - void DoAddConstructor (CallbackBase callback); - CallbackBase LookupConstructor (void) const; + void DoAddConstructor (Callback callback); Ptr GetAttributeAccessor (uint32_t i) const; uint16_t m_tid; @@ -518,6 +516,8 @@ private: template friend Ptr CopyObject (Ptr object); + friend class ObjectFactory; + bool DoSet (Ptr spec, Attribute intialValue, Ptr checker, Attribute value); Ptr DoGetObject (TypeId tid) const; @@ -596,11 +596,12 @@ TypeId TypeId::AddConstructor (void) { struct Maker { - static Ptr Create (const AttributeList &attributes) { - return ns3::CreateObject (attributes); + static ObjectBase * Create () { + ObjectBase * base = new T (); + return base; } }; - CallbackBase cb = MakeCallback (&Maker::Create); + Callback cb = MakeCallback (&Maker::Create); DoAddConstructor (cb); return *this; } From 1806cbede90598122bf02089e5740aa8ee7a2b88 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 16 Mar 2008 20:47:46 +0100 Subject: [PATCH 03/35] get rid of unneeded object-base include --- src/common/packet.h | 1 - src/devices/wifi/wifi-phy.cc | 1 - src/simulator/event-impl.h | 1 - 3 files changed, 3 deletions(-) diff --git a/src/common/packet.h b/src/common/packet.h index 2858f1153..e13b946c1 100644 --- a/src/common/packet.h +++ b/src/common/packet.h @@ -31,7 +31,6 @@ #include "ns3/callback.h" #include "ns3/assert.h" #include "ns3/ptr.h" -#include "ns3/object-base.h" namespace ns3 { diff --git a/src/devices/wifi/wifi-phy.cc b/src/devices/wifi/wifi-phy.cc index b62cfe230..91af4a6c4 100644 --- a/src/devices/wifi/wifi-phy.cc +++ b/src/devices/wifi/wifi-phy.cc @@ -27,7 +27,6 @@ #include "ns3/random-variable.h" #include "ns3/assert.h" #include "ns3/log.h" -#include "ns3/object-base.h" #include "ns3/double.h" #include "ns3/uinteger.h" #include "ns3/enum.h" diff --git a/src/simulator/event-impl.h b/src/simulator/event-impl.h index 5526fb0c7..526beffd5 100644 --- a/src/simulator/event-impl.h +++ b/src/simulator/event-impl.h @@ -21,7 +21,6 @@ #define EVENT_IMPL_H #include -#include "ns3/object-base.h" namespace ns3 { From 660e414f365bd8b173c1cf3ad16fa2e95794efaf Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 16 Mar 2008 20:55:18 +0100 Subject: [PATCH 04/35] split code from object.h/object.cc --- src/core/attribute-helper.h | 4 - src/core/attribute-list.cc | 237 ++++++++++ src/core/attribute-list.h | 93 ++++ src/core/attribute.h | 6 +- src/core/callback.h | 1 - src/core/object-base.h | 15 + src/core/object.cc | 909 ------------------------------------ src/core/object.h | 371 +-------------- src/core/type-id.cc | 687 +++++++++++++++++++++++++++ src/core/type-id.h | 292 ++++++++++++ src/core/wscript | 4 + 11 files changed, 1332 insertions(+), 1287 deletions(-) create mode 100644 src/core/attribute-list.cc create mode 100644 src/core/attribute-list.h create mode 100644 src/core/type-id.cc create mode 100644 src/core/type-id.h diff --git a/src/core/attribute-helper.h b/src/core/attribute-helper.h index b2b444be1..400fc7353 100644 --- a/src/core/attribute-helper.h +++ b/src/core/attribute-helper.h @@ -21,7 +21,6 @@ #define ATTRIBUTE_HELPER_H #include "attribute.h" -#include "object-base.h" #include "attribute-accessor-helper.h" #include #include "fatal-error.h" @@ -57,8 +56,6 @@ MakeSimpleAttributeChecker (std::string name) } - - /** * \defgroup AttributeHelper * @@ -281,5 +278,4 @@ MakeSimpleAttributeChecker (std::string name) ATTRIBUTE_VALUE_IMPLEMENT (type); - #endif /* ATTRIBUTE_HELPER_H */ diff --git a/src/core/attribute-list.cc b/src/core/attribute-list.cc new file mode 100644 index 000000000..6ce983047 --- /dev/null +++ b/src/core/attribute-list.cc @@ -0,0 +1,237 @@ +#include "attribute-list.h" +#include "string.h" +#include "singleton.h" + +namespace ns3 { + +/********************************************************************* + * The AttributeList container implementation + *********************************************************************/ + +AttributeList::AttributeList () +{} + +AttributeList::AttributeList (const AttributeList &o) +{ + for (Attrs::const_iterator i = o.m_attributes.begin (); i != o.m_attributes.end (); i++) + { + struct Attr attr; + attr.checker = i->checker; + attr.value = i->value.Copy (); + m_attributes.push_back (attr); + } +} +AttributeList & +AttributeList::operator = (const AttributeList &o) +{ + Reset (); + for (Attrs::const_iterator i = o.m_attributes.begin (); i != o.m_attributes.end (); i++) + { + struct Attr attr; + attr.checker = i->checker; + attr.value = i->value.Copy (); + m_attributes.push_back (attr); + } + return *this; +} +AttributeList::~AttributeList () +{ + Reset (); +} + +void +AttributeList::Set (std::string name, Attribute value) +{ + struct TypeId::AttributeInfo info; + bool ok = TypeId::LookupAttributeByFullName (name, &info); + if (!ok) + { + NS_FATAL_ERROR ("Could not find attribute "< checker, Attribute value) +{ + // get rid of any previous value stored in this + // vector of values. + for (Attrs::iterator k = m_attributes.begin (); k != m_attributes.end (); k++) + { + if (k->checker == checker) + { + m_attributes.erase (k); + break; + } + } + // store the new value. + struct Attr attr; + attr.checker = checker; + attr.value = value.Copy (); + m_attributes.push_back (attr); +} +bool +AttributeList::DoSet (struct TypeId::AttributeInfo *info, Attribute value) +{ + if (info->checker == 0) + { + return false; + } + bool ok = info->checker->Check (value); + if (!ok) + { + // attempt to convert to string. + const StringValue *str = value.DynCast (); + if (str == 0) + { + return false; + } + // attempt to convert back to value. + Attribute v = info->checker->Create (); + ok = v.DeserializeFromString (str->Get ().Get (), info->checker); + if (!ok) + { + return false; + } + ok = info->checker->Check (v); + if (!ok) + { + return false; + } + value = v; + } + DoSetOne (info->checker, value); + return true; +} +void +AttributeList::Reset (void) +{ + m_attributes.clear (); +} +AttributeList * +AttributeList::GetGlobal (void) +{ + return Singleton::Get (); +} + +std::string +AttributeList::LookupAttributeFullNameByChecker (Ptr checker) const +{ + for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++) + { + TypeId tid = TypeId::GetRegistered (i); + for (uint32_t j = 0; j < tid.GetAttributeListN (); j++) + { + if (checker == tid.GetAttributeChecker (j)) + { + return tid.GetAttributeFullName (j); + } + } + } + NS_FATAL_ERROR ("Could not find requested Accessor."); + // quiet compiler. + return ""; +} + +std::string +AttributeList::SerializeToString (void) const +{ + std::ostringstream oss; + for (Attrs::const_iterator i = m_attributes.begin (); i != m_attributes.end (); i++) + { + std::string name = LookupAttributeFullNameByChecker (i->checker); + oss << name << "=" << i->value.SerializeToString (i->checker); + if (i != m_attributes.end ()) + { + oss << "|"; + } + } + return oss.str (); +} +bool +AttributeList::DeserializeFromString (std::string str) +{ + Reset (); + + std::string::size_type cur; + cur = 0; + do { + std::string::size_type equal = str.find ("=", cur); + if (equal == std::string::npos) + { + // XXX: invalid attribute. + break; + } + else + { + std::string name = str.substr (cur, equal-cur); + struct TypeId::AttributeInfo info; + if (!TypeId::LookupAttributeByFullName (name, &info)) + { + // XXX invalid name. + break; + } + else + { + std::string::size_type next = str.find ("|", cur); + std::string value; + if (next == std::string::npos) + { + value = str.substr (equal+1, str.size () - (equal+1)); + cur = str.size (); + } + else + { + value = str.substr (equal+1, next - (equal+1)); + cur++; + } + Attribute val = info.checker->Create (); + bool ok = val.DeserializeFromString (value, info.checker); + if (!ok) + { + // XXX invalid value + break; + } + else + { + DoSetOne (info.checker, val); + } + } + } + } while (cur != str.size ()); + + return true; +} + +} // namespace ns3 diff --git a/src/core/attribute-list.h b/src/core/attribute-list.h new file mode 100644 index 000000000..98adbecfc --- /dev/null +++ b/src/core/attribute-list.h @@ -0,0 +1,93 @@ +#ifndef ATTRIBUTE_LIST_H +#define ATTRIBUTE_LIST_H + +#include +#include +#include "attribute.h" +#include "type-id.h" + +namespace ns3 { + +/** + * \brief a container of attributes to be used during object's construction + * and in ns3::Object::Set. + * + */ +class AttributeList +{ +public: + AttributeList (); + AttributeList (const AttributeList &o); + AttributeList &operator = (const AttributeList &o); + ~AttributeList (); + /** + * \param name the full name of the attribute to set + * \param value the value to set + * + * This method checks that a attribute with the requested + * name exists and that the value specified is an acceptable + * value of that attribute. If any of these checks fails, + * the program terminates with a message. + */ + void Set (std::string name, Attribute value); + /** + * \param name the full name of the attribute to set + * \param value the value to set + * \returns true if the requested attribute exists and could be + * stored, false otherwise. + */ + bool SetFailSafe (std::string name, Attribute value); + /** + * \param tid the TypeId associated to this attribute + * \param name the name (not full!) of the attribute + * \param value the value to set + * + * This method checks that a attribute with the requested + * name exists and that the value specified is an acceptable + * value of that attribute. If any of these checks fails, + * the program terminates with a message. + */ + void SetWithTid (TypeId tid, std::string name, Attribute value); + + /** + * Clear the content of this instance. + */ + void Reset (void); + + /** + * \returns the global attribute container + * + * The global attribute container can be used to specify + * a set of attribute values without having to re-specify + * them for each object when it is created. This container + * is checked only during object construction and + * it is always checked last, after any per-object + * container is checked. + */ + static AttributeList *GetGlobal (void); + + // XXX: untested. + std::string SerializeToString (void) const; + bool DeserializeFromString (std::string value); +private: + friend class Object; + struct Attr { + Ptr checker; + Attribute value; + }; + typedef std::vector Attrs; + typedef Attrs::iterator Iterator; + typedef Attrs::const_iterator CIterator; + + + + bool DoSet (struct TypeId::AttributeInfo *info, Attribute param); + void DoSetOne (Ptr checker, Attribute param); + std::string LookupAttributeFullNameByChecker (Ptr checker) const; + + Attrs m_attributes; +}; + +} // namespace ns3 + +#endif /* ATTRIBUTE_LIST_H */ diff --git a/src/core/attribute.h b/src/core/attribute.h index 75e31f4be..687d772c0 100644 --- a/src/core/attribute.h +++ b/src/core/attribute.h @@ -23,13 +23,13 @@ #include #include #include "ptr.h" -#include "object-base.h" namespace ns3 { class AttributeAccessor; class AttributeChecker; class Attribute; +class ObjectBase; /** * \brief Hold a value for an Attribute. @@ -159,7 +159,7 @@ private: * of this base class are usually provided through the MakeAccessorHelper * template functions, hidden behind an ATTRIBUTE_HELPER_* macro. */ -class AttributeAccessor : public ObjectBase +class AttributeAccessor { public: AttributeAccessor (); @@ -202,7 +202,7 @@ private: * Most subclasses of this base class are implemented by the * ATTRIBUTE_HELPER_* macros. */ -class AttributeChecker : public ObjectBase +class AttributeChecker { public: AttributeChecker (); diff --git a/src/core/callback.h b/src/core/callback.h index 7a70bb1ce..8fd6d7ef6 100644 --- a/src/core/callback.h +++ b/src/core/callback.h @@ -26,7 +26,6 @@ #include "fatal-error.h" #include "empty.h" #include "type-traits.h" -#include "object-base.h" namespace ns3 { diff --git a/src/core/object-base.h b/src/core/object-base.h index 36f09ffe5..c5373e07d 100644 --- a/src/core/object-base.h +++ b/src/core/object-base.h @@ -1,6 +1,21 @@ #ifndef OBJECT_BASE_H #define OBJECT_BASE_H +#include "type-id.h" + +/** + * This macro should be invoked once for every class which + * defines a new GetTypeId method. + */ +#define NS_OBJECT_ENSURE_REGISTERED(type) \ + static struct X##type##RegistrationClass \ + { \ + X##type##RegistrationClass () { \ + ns3::TypeId tid = type::GetTypeId (); \ + tid.GetParent (); \ + } \ +} x_##type##RegistrationVariable + namespace ns3 { /** diff --git a/src/core/object.cc b/src/core/object.cc index 50742da4f..f96089c45 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -30,917 +30,8 @@ NS_LOG_COMPONENT_DEFINE ("Object"); -/********************************************************************* - * Helper code - *********************************************************************/ - -namespace { - -class IidManager -{ -public: - IidManager (); - uint16_t AllocateUid (std::string name); - void SetParent (uint16_t uid, uint16_t parent); - void SetTypeName (uint16_t uid, std::string typeName); - void SetGroupName (uint16_t uid, std::string groupName); - void AddConstructor (uint16_t uid, ns3::Callback callback); - void HideFromDocumentation (uint16_t uid); - uint16_t GetUid (std::string name) const; - std::string GetName (uint16_t uid) const; - uint16_t GetParent (uint16_t uid) const; - std::string GetTypeName (uint16_t uid) const; - std::string GetGroupName (uint16_t uid) const; - ns3::Callback GetConstructor (uint16_t uid) const; - bool HasConstructor (uint16_t uid) const; - uint32_t GetRegisteredN (void) const; - uint16_t GetRegistered (uint32_t i) const; - void AddAttribute (uint16_t uid, - std::string name, - std::string help, - uint32_t flags, - ns3::Attribute initialValue, - ns3::Ptr spec, - ns3::Ptr checker); - uint32_t GetAttributeListN (uint16_t uid) const; - std::string GetAttributeName (uint16_t uid, uint32_t i) const; - std::string GetAttributeHelp (uint16_t uid, uint32_t i) const; - uint32_t GetAttributeFlags (uint16_t uid, uint32_t i) const; - ns3::Attribute GetAttributeInitialValue (uint16_t uid, uint32_t i) const; - ns3::Ptr GetAttributeAccessor (uint16_t uid, uint32_t i) const; - ns3::Ptr GetAttributeChecker (uint16_t uid, uint32_t i) const; - void AddTraceSource (uint16_t uid, - std::string name, - std::string help, - ns3::Ptr accessor); - uint32_t GetTraceSourceN (uint16_t uid) const; - std::string GetTraceSourceName (uint16_t uid, uint32_t i) const; - std::string GetTraceSourceHelp (uint16_t uid, uint32_t i) const; - ns3::Ptr GetTraceSourceAccessor (uint16_t uid, uint32_t i) const; - bool MustHideFromDocumentation (uint16_t uid) const; - -private: - struct AttributeInformation { - std::string name; - std::string help; - uint32_t flags; - ns3::Attribute initialValue; - ns3::Ptr param; - ns3::Ptr checker; - }; - struct TraceSourceInformation { - std::string name; - std::string help; - ns3::Ptr accessor; - }; - struct IidInformation { - std::string name; - uint16_t parent; - std::string typeName; - std::string groupName; - bool hasConstructor; - ns3::Callback constructor; - bool mustHideFromDocumentation; - std::vector attributes; - std::vector traceSources; - }; - typedef std::vector::const_iterator Iterator; - - struct IidManager::IidInformation *LookupInformation (uint16_t uid) const; - - std::vector m_information; -}; - -IidManager::IidManager () -{} - -uint16_t -IidManager::AllocateUid (std::string name) -{ - uint16_t j = 1; - for (Iterator i = m_information.begin (); i != m_information.end (); i++) - { - if (i->name == name) - { - NS_FATAL_ERROR ("Trying to allocate twice the same uid: " << name); - return 0; - } - j++; - } - struct IidInformation information; - information.name = name; - information.parent = 0; - information.typeName = ""; - information.groupName = ""; - information.hasConstructor = false; - information.mustHideFromDocumentation = false; - m_information.push_back (information); - uint32_t uid = m_information.size (); - NS_ASSERT (uid <= 0xffff); - return uid; -} - -struct IidManager::IidInformation * -IidManager::LookupInformation (uint16_t uid) const -{ - NS_ASSERT (uid <= m_information.size ()); - return const_cast (&m_information[uid-1]); -} - -void -IidManager::SetParent (uint16_t uid, uint16_t parent) -{ - NS_ASSERT (parent <= m_information.size ()); - struct IidInformation *information = LookupInformation (uid); - information->parent = parent; -} -void -IidManager::SetTypeName (uint16_t uid, std::string typeName) -{ - struct IidInformation *information = LookupInformation (uid); - information->typeName = typeName; -} -void -IidManager::SetGroupName (uint16_t uid, std::string groupName) -{ - struct IidInformation *information = LookupInformation (uid); - information->groupName = groupName; -} -void -IidManager::HideFromDocumentation (uint16_t uid) -{ - struct IidInformation *information = LookupInformation (uid); - information->mustHideFromDocumentation = true; -} - -void -IidManager::AddConstructor (uint16_t uid, ns3::Callback callback) -{ - struct IidInformation *information = LookupInformation (uid); - if (information->hasConstructor) - { - NS_FATAL_ERROR (information->name<<" already has a constructor."); - } - information->hasConstructor = true; - information->constructor = callback; -} - -uint16_t -IidManager::GetUid (std::string name) const -{ - uint32_t j = 1; - for (Iterator i = m_information.begin (); i != m_information.end (); i++) - { - if (i->name == name) - { - NS_ASSERT (j <= 0xffff); - return j; - } - j++; - } - return 0; -} -std::string -IidManager::GetName (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - return information->name; -} -uint16_t -IidManager::GetParent (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - return information->parent; -} -std::string -IidManager::GetTypeName (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - return information->typeName; -} -std::string -IidManager::GetGroupName (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - return information->groupName; -} - -ns3::Callback -IidManager::GetConstructor (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - if (!information->hasConstructor) - { - NS_FATAL_ERROR ("Requested constructor for "<name<<" but it does not have one."); - } - return information->constructor; -} - -bool -IidManager::HasConstructor (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - return information->hasConstructor; -} - -uint32_t -IidManager::GetRegisteredN (void) const -{ - return m_information.size (); -} -uint16_t -IidManager::GetRegistered (uint32_t i) const -{ - return i + 1; -} - -void -IidManager::AddAttribute (uint16_t uid, - std::string name, - std::string help, - uint32_t flags, - ns3::Attribute initialValue, - ns3::Ptr spec, - ns3::Ptr checker) -{ - struct IidInformation *information = LookupInformation (uid); - for (std::vector::const_iterator j = information->attributes.begin (); - j != information->attributes.end (); j++) - { - if (j->name == name) - { - NS_FATAL_ERROR ("Registered the same attribute twice name=\""<name<<"\""); - return; - } - } - struct AttributeInformation param; - param.name = name; - param.help = help; - param.flags = flags; - param.initialValue = initialValue; - param.param = spec; - param.checker = checker; - information->attributes.push_back (param); -} - - -uint32_t -IidManager::GetAttributeListN (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - return information->attributes.size (); -} -std::string -IidManager::GetAttributeName (uint16_t uid, uint32_t i) const -{ - struct IidInformation *information = LookupInformation (uid); - NS_ASSERT (i < information->attributes.size ()); - return information->attributes[i].name; -} -std::string -IidManager::GetAttributeHelp (uint16_t uid, uint32_t i) const -{ - struct IidInformation *information = LookupInformation (uid); - NS_ASSERT (i < information->attributes.size ()); - return information->attributes[i].help; -} -uint32_t -IidManager::GetAttributeFlags (uint16_t uid, uint32_t i) const -{ - struct IidInformation *information = LookupInformation (uid); - NS_ASSERT (i < information->attributes.size ()); - return information->attributes[i].flags; -} -ns3::Attribute -IidManager::GetAttributeInitialValue (uint16_t uid, uint32_t i) const -{ - struct IidInformation *information = LookupInformation (uid); - NS_ASSERT (i < information->attributes.size ()); - return information->attributes[i].initialValue; -} -ns3::Ptr -IidManager::GetAttributeAccessor (uint16_t uid, uint32_t i) const -{ - struct IidInformation *information = LookupInformation (uid); - NS_ASSERT (i < information->attributes.size ()); - return information->attributes[i].param; -} -ns3::Ptr -IidManager::GetAttributeChecker (uint16_t uid, uint32_t i) const -{ - struct IidInformation *information = LookupInformation (uid); - NS_ASSERT (i < information->attributes.size ()); - return information->attributes[i].checker; -} - -void -IidManager::AddTraceSource (uint16_t uid, - std::string name, - std::string help, - ns3::Ptr accessor) -{ - struct IidInformation *information = LookupInformation (uid); - struct TraceSourceInformation source; - source.name = name; - source.help = help; - source.accessor = accessor; - information->traceSources.push_back (source); -} -uint32_t -IidManager::GetTraceSourceN (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - return information->traceSources.size (); -} -std::string -IidManager::GetTraceSourceName (uint16_t uid, uint32_t i) const -{ - struct IidInformation *information = LookupInformation (uid); - NS_ASSERT (i < information->traceSources.size ()); - return information->traceSources[i].name; -} -std::string -IidManager::GetTraceSourceHelp (uint16_t uid, uint32_t i) const -{ - struct IidInformation *information = LookupInformation (uid); - NS_ASSERT (i < information->traceSources.size ()); - return information->traceSources[i].help; -} -ns3::Ptr -IidManager::GetTraceSourceAccessor (uint16_t uid, uint32_t i) const -{ - struct IidInformation *information = LookupInformation (uid); - NS_ASSERT (i < information->traceSources.size ()); - return information->traceSources[i].accessor; -} -bool -IidManager::MustHideFromDocumentation (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - return information->mustHideFromDocumentation; -} - -} // anonymous namespace - namespace ns3 { -/********************************************************************* - * The TypeId class - *********************************************************************/ - -TypeId::TypeId () - : m_tid (0) -{} - -TypeId::TypeId (const char *name) -{ - uint16_t uid = Singleton::Get ()->AllocateUid (name); - NS_ASSERT (uid != 0); - m_tid = uid; -} - - -TypeId::TypeId (uint16_t tid) - : m_tid (tid) -{} -TypeId::~TypeId () -{} -TypeId -TypeId::LookupByName (std::string name) -{ - uint16_t uid = Singleton::Get ()->GetUid (name); - NS_ASSERT (uid != 0); - return TypeId (uid); -} -bool -TypeId::LookupByNameFailSafe (std::string name, TypeId *tid) -{ - uint16_t uid = Singleton::Get ()->GetUid (name); - if (uid == 0) - { - return false; - } - *tid = TypeId (uid); - return true; -} - -bool -TypeId::LookupAttributeByFullName (std::string fullName, struct TypeId::AttributeInfo *info) -{ - std::string::size_type pos = fullName.rfind ("::"); - if (pos == std::string::npos) - { - return 0; - } - std::string tidName = fullName.substr (0, pos); - std::string paramName = fullName.substr (pos+2, fullName.size () - (pos+2)); - TypeId tid; - bool ok = LookupByNameFailSafe (tidName, &tid); - if (!ok) - { - return false; - } - return tid.LookupAttributeByName (paramName, info); -} -uint32_t -TypeId::GetRegisteredN (void) -{ - return Singleton::Get ()->GetRegisteredN (); -} -TypeId -TypeId::GetRegistered (uint32_t i) -{ - return TypeId (Singleton::Get ()->GetRegistered (i)); -} - -bool -TypeId::LookupAttributeByName (std::string name, struct TypeId::AttributeInfo *info) const -{ - TypeId tid; - TypeId nextTid = *this; - do { - tid = nextTid; - for (uint32_t i = 0; i < tid.GetAttributeListN (); i++) - { - std::string paramName = tid.GetAttributeName (i); - if (paramName == name) - { - info->accessor = tid.GetAttributeAccessor (i); - info->flags = tid.GetAttributeFlags (i); - info->initialValue = tid.GetAttributeInitialValue (i); - info->checker = tid.GetAttributeChecker (i); - return true; - } - } - nextTid = tid.GetParent (); - } while (nextTid != tid); - return false; -} - -TypeId -TypeId::SetParent (TypeId tid) -{ - Singleton::Get ()->SetParent (m_tid, tid.m_tid); - return *this; -} -TypeId -TypeId::SetGroupName (std::string groupName) -{ - Singleton::Get ()->SetGroupName (m_tid, groupName); - return *this; -} -TypeId -TypeId::SetTypeName (std::string typeName) -{ - Singleton::Get ()->SetTypeName (m_tid, typeName); - return *this; -} -TypeId -TypeId::GetParent (void) const -{ - uint16_t parent = Singleton::Get ()->GetParent (m_tid); - return TypeId (parent); -} -std::string -TypeId::GetGroupName (void) const -{ - std::string groupName = Singleton::Get ()->GetGroupName (m_tid); - return groupName; -} -std::string -TypeId::GetTypeName (void) const -{ - std::string typeName = Singleton::Get ()->GetTypeName (m_tid); - return typeName; -} - -std::string -TypeId::GetName (void) const -{ - std::string name = Singleton::Get ()->GetName (m_tid); - return name; -} - -bool -TypeId::HasConstructor (void) const -{ - bool hasConstructor = Singleton::Get ()->HasConstructor (m_tid); - return hasConstructor; -} - -void -TypeId::DoAddConstructor (Callback cb) -{ - Singleton::Get ()->AddConstructor (m_tid, cb); -} - -TypeId -TypeId::AddAttribute (std::string name, - std::string help, - Attribute initialValue, - Ptr param, - Ptr checker) -{ - Singleton::Get ()->AddAttribute (m_tid, name, help, ATTR_SGC, initialValue, param, checker); - return *this; -} - -TypeId -TypeId::AddAttribute (std::string name, - std::string help, - uint32_t flags, - Attribute initialValue, - Ptr param, - Ptr checker) -{ - Singleton::Get ()->AddAttribute (m_tid, name, help, flags, initialValue, param, checker); - return *this; -} - -Callback -TypeId::GetConstructor (void) const -{ - Callback cb = Singleton::Get ()->GetConstructor (m_tid); - return cb; -} - -bool -TypeId::MustHideFromDocumentation (void) const -{ - bool mustHide = Singleton::Get ()->MustHideFromDocumentation (m_tid); - return mustHide; -} - -uint32_t -TypeId::GetAttributeListN (void) const -{ - uint32_t n = Singleton::Get ()->GetAttributeListN (m_tid); - return n; -} -std::string -TypeId::GetAttributeName (uint32_t i) const -{ - std::string name = Singleton::Get ()->GetAttributeName (m_tid, i); - return name; -} -std::string -TypeId::GetAttributeHelp (uint32_t i) const -{ - std::string help = Singleton::Get ()->GetAttributeHelp (m_tid, i); - return help; -} -std::string -TypeId::GetAttributeFullName (uint32_t i) const -{ - return GetName () + "::" + GetAttributeName (i); -} -Attribute -TypeId::GetAttributeInitialValue (uint32_t i) const -{ - Attribute value = Singleton::Get ()->GetAttributeInitialValue (m_tid, i); - return value; -} -Ptr -TypeId::GetAttributeAccessor (uint32_t i) const -{ - // Used exclusively by the Object class. - Ptr param = Singleton::Get ()->GetAttributeAccessor (m_tid, i); - return param; -} -uint32_t -TypeId::GetAttributeFlags (uint32_t i) const -{ - // Used exclusively by the Object class. - uint32_t flags = Singleton::Get ()->GetAttributeFlags (m_tid, i); - return flags; -} -Ptr -TypeId::GetAttributeChecker (uint32_t i) const -{ - // Used exclusively by the Object class. - Ptr checker = Singleton::Get ()->GetAttributeChecker (m_tid, i); - return checker; -} - -uint32_t -TypeId::GetTraceSourceN (void) const -{ - return Singleton::Get ()->GetTraceSourceN (m_tid); -} -std::string -TypeId::GetTraceSourceName (uint32_t i) const -{ - return Singleton::Get ()->GetTraceSourceName (m_tid, i); -} -std::string -TypeId::GetTraceSourceHelp (uint32_t i) const -{ - return Singleton::Get ()->GetTraceSourceHelp (m_tid, i); -} -Ptr -TypeId::GetTraceSourceAccessor (uint32_t i) const -{ - return Singleton::Get ()->GetTraceSourceAccessor (m_tid, i); -} - -TypeId -TypeId::AddTraceSource (std::string name, - std::string help, - Ptr accessor) -{ - Singleton::Get ()->AddTraceSource (m_tid, name, help, accessor); - return *this; -} - -TypeId -TypeId::HideFromDocumentation (void) -{ - Singleton::Get ()->HideFromDocumentation (m_tid); - return *this; -} - - -Ptr -TypeId::LookupTraceSourceByName (std::string name) const -{ - TypeId tid; - TypeId nextTid = *this; - do { - tid = nextTid; - for (uint32_t i = 0; i < tid.GetTraceSourceN (); i++) - { - std::string srcName = tid.GetTraceSourceName (i); - if (srcName == name) - { - return tid.GetTraceSourceAccessor (i); - } - } - nextTid = tid.GetParent (); - } while (nextTid != tid); - return 0; -} - -std::ostream & operator << (std::ostream &os, TypeId tid) -{ - os << tid.GetName (); - return os; -} -std::istream & operator >> (std::istream &is, TypeId &tid) -{ - std::string tidString; - is >> tidString; - bool ok = TypeId::LookupByNameFailSafe (tidString, &tid); - if (!ok) - { - is.setstate (std::ios_base::badbit); - } - return is; -} - - -ATTRIBUTE_HELPER_CPP (TypeId); - -bool operator == (TypeId a, TypeId b) -{ - return a.m_tid == b.m_tid; -} - -bool operator != (TypeId a, TypeId b) -{ - return a.m_tid != b.m_tid; -} - -/********************************************************************* - * The AttributeList container implementation - *********************************************************************/ - -AttributeList::AttributeList () -{} - -AttributeList::AttributeList (const AttributeList &o) -{ - for (Attrs::const_iterator i = o.m_attributes.begin (); i != o.m_attributes.end (); i++) - { - struct Attr attr; - attr.checker = i->checker; - attr.value = i->value.Copy (); - m_attributes.push_back (attr); - } -} -AttributeList & -AttributeList::operator = (const AttributeList &o) -{ - Reset (); - for (Attrs::const_iterator i = o.m_attributes.begin (); i != o.m_attributes.end (); i++) - { - struct Attr attr; - attr.checker = i->checker; - attr.value = i->value.Copy (); - m_attributes.push_back (attr); - } - return *this; -} -AttributeList::~AttributeList () -{ - Reset (); -} - -void -AttributeList::Set (std::string name, Attribute value) -{ - struct TypeId::AttributeInfo info; - bool ok = TypeId::LookupAttributeByFullName (name, &info); - if (!ok) - { - NS_FATAL_ERROR ("Could not find attribute "< checker, Attribute value) -{ - // get rid of any previous value stored in this - // vector of values. - for (Attrs::iterator k = m_attributes.begin (); k != m_attributes.end (); k++) - { - if (k->checker == checker) - { - m_attributes.erase (k); - break; - } - } - // store the new value. - struct Attr attr; - attr.checker = checker; - attr.value = value.Copy (); - m_attributes.push_back (attr); -} -bool -AttributeList::DoSet (struct TypeId::AttributeInfo *info, Attribute value) -{ - if (info->checker == 0) - { - return false; - } - bool ok = info->checker->Check (value); - if (!ok) - { - // attempt to convert to string. - const StringValue *str = value.DynCast (); - if (str == 0) - { - return false; - } - // attempt to convert back to value. - Attribute v = info->checker->Create (); - ok = v.DeserializeFromString (str->Get ().Get (), info->checker); - if (!ok) - { - return false; - } - ok = info->checker->Check (v); - if (!ok) - { - return false; - } - value = v; - } - DoSetOne (info->checker, value); - return true; -} -void -AttributeList::Reset (void) -{ - m_attributes.clear (); -} -AttributeList * -AttributeList::GetGlobal (void) -{ - return Singleton::Get (); -} - -std::string -AttributeList::LookupAttributeFullNameByChecker (Ptr checker) const -{ - for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++) - { - TypeId tid = TypeId::GetRegistered (i); - for (uint32_t j = 0; j < tid.GetAttributeListN (); j++) - { - if (checker == tid.GetAttributeChecker (j)) - { - return tid.GetAttributeFullName (j); - } - } - } - NS_FATAL_ERROR ("Could not find requested Accessor."); - // quiet compiler. - return ""; -} - -std::string -AttributeList::SerializeToString (void) const -{ - std::ostringstream oss; - for (Attrs::const_iterator i = m_attributes.begin (); i != m_attributes.end (); i++) - { - std::string name = LookupAttributeFullNameByChecker (i->checker); - oss << name << "=" << i->value.SerializeToString (i->checker); - if (i != m_attributes.end ()) - { - oss << "|"; - } - } - return oss.str (); -} -bool -AttributeList::DeserializeFromString (std::string str) -{ - Reset (); - - std::string::size_type cur; - cur = 0; - do { - std::string::size_type equal = str.find ("=", cur); - if (equal == std::string::npos) - { - // XXX: invalid attribute. - break; - } - else - { - std::string name = str.substr (cur, equal-cur); - struct TypeId::AttributeInfo info; - if (!TypeId::LookupAttributeByFullName (name, &info)) - { - // XXX invalid name. - break; - } - else - { - std::string::size_type next = str.find ("|", cur); - std::string value; - if (next == std::string::npos) - { - value = str.substr (equal+1, str.size () - (equal+1)); - cur = str.size (); - } - else - { - value = str.substr (equal+1, next - (equal+1)); - cur++; - } - Attribute val = info.checker->Create (); - bool ok = val.DeserializeFromString (value, info.checker); - if (!ok) - { - // XXX invalid value - break; - } - else - { - DoSetOne (info.checker, val); - } - } - } - } while (cur != str.size ()); - - return true; -} - - /********************************************************************* * The Object implementation *********************************************************************/ diff --git a/src/core/object.h b/src/core/object.h index 6d0e5d279..612dae5d3 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -29,19 +29,7 @@ #include "attribute.h" #include "object-base.h" #include "attribute-helper.h" - -/** - * This macro should be invoked once for every class which - * defines a new GetTypeId method. - */ -#define NS_OBJECT_ENSURE_REGISTERED(type) \ - static struct X##type##RegistrationClass \ - { \ - X##type##RegistrationClass () { \ - ns3::TypeId tid = type::GetTypeId (); \ - tid.GetParent (); \ - } \ -} x_##type##RegistrationVariable +#include "attribute-list.h" namespace ns3 { @@ -53,363 +41,6 @@ class AttributeValue; class AttributeList; class TraceSourceAccessor; -/** - * \brief a unique identifier for an interface. - * - * This class records a lot of meta-information about a - * subclass of the Object base class: - * - the base class of the subclass - * - the set of accessible constructors in the subclass - * - the set of 'attributes' accessible in the subclass - */ -class TypeId -{ -public: - enum { - /** - * The attribute can be read - */ - ATTR_GET = 1<<0, - /** - * The attribute can be written - */ - ATTR_SET = 1<<1, - /** - * The attribute can be written at construction-time. - */ - ATTR_CONSTRUCT = 1<<2, - /** - * The attribute can be read, and written at any time. - */ - ATTR_SGC = ATTR_GET | ATTR_SET | ATTR_CONSTRUCT, - }; - - /** - * \param name the name of the requested TypeId - * \returns the unique id associated with the requested - * name. - * - * This method cannot fail: it will crash if the input - * name is not a valid TypeId name. - */ - static TypeId LookupByName (std::string name); - /** - * \param name the name of the requested TypeId - * \param tid a pointer to the TypeId instance where the - * result of this function should be stored. - * \returns true if the requested name was found, false otherwise. - */ - static bool LookupByNameFailSafe (std::string name, TypeId *tid); - - /** - * \returns the number of TypeId instances registered. - */ - static uint32_t GetRegisteredN (void); - /** - * \param i index - * \returns the TypeId instance whose index is i. - */ - static TypeId GetRegistered (uint32_t i); - - /** - * \param name the name of the interface to construct. - * - * No two instances can share the same name. The name is expected to be - * the full c++ typename of associated c++ object. - */ - TypeId (const char * name); - - /** - * \returns the parent of this TypeId - * - * This method cannot fail. It will return itself - * if this TypeId has no parent. i.e., it is at the top - * of the TypeId hierarchy. Currently, this is the - * case for the TypeId associated to the Object class - * only. - */ - TypeId GetParent (void) const; - - /** - * \returns the name of the group associated to this TypeId. - */ - std::string GetGroupName (void) const; - /** - * \returns the fully-qualified C++ typename of this TypeId. - */ - std::string GetTypeName (void) const; - - /** - * \returns the name of this interface. - */ - std::string GetName (void) const; - - /** - * \returns true if this TypeId has a constructor - */ - bool HasConstructor (void) const; - - /** - * \returns the number of attributes associated to this TypeId - */ - uint32_t GetAttributeListN (void) const; - /** - * \param i index into attribute array - * \returns the name associated to the attribute whose - * index is i. - */ - std::string GetAttributeName (uint32_t i) const; - std::string GetAttributeHelp (uint32_t i) const; - /** - * \param i index into attribute array - * \returns the full name associated to the attribute whose - * index is i. - */ - std::string GetAttributeFullName (uint32_t i) const; - - Attribute GetAttributeInitialValue (uint32_t i) const; - uint32_t GetAttributeFlags (uint32_t i) const; - Ptr GetAttributeChecker (uint32_t i) const; - - - uint32_t GetTraceSourceN (void) const; - std::string GetTraceSourceName (uint32_t i) const; - std::string GetTraceSourceHelp (uint32_t i) const; - Ptr GetTraceSourceAccessor (uint32_t i) const; - - Callback GetConstructor (void) const; - - bool MustHideFromDocumentation (void) const; - - /** - * \param tid the TypeId of the base class. - * \return this TypeId instance. - * - * Record in this TypeId which TypeId is the TypeId - * of the base class of the subclass. - */ - TypeId SetParent (TypeId tid); - /** - * \return this TypeId instance. - * - * Record in this TypeId which TypeId is the TypeId - * of the base class of the subclass. - */ - template - TypeId SetParent (void); - - /** - * \param groupName the name of the group this TypeId belongs to. - * \returns this TypeId instance. - * - * The group name is purely an advisory information used to - * group together types according to a user-specific grouping - * scheme. - */ - TypeId SetGroupName (std::string groupName); - - /** - * \param typeName the fully-qualified C++ typename of this TypeId. - * \returns this TypeId instance. - */ - TypeId SetTypeName (std::string typeName); - - /** - * \returns this TypeId instance - * - * Record in this TypeId the fact that the default constructor - * is accessible. - */ - template - TypeId AddConstructor (void); - - /** - * \param name the name of the new attribute - * \param help some help text which describes the purpose of this - * attribute. - * \param initialValue the initial value for this attribute. - * \param accessor an instance of the associated AttributeAccessor subclass. - * \param checker an instance of the associated AttributeChecker subclass. - * \returns this TypeId instance - * - * Record in this TypeId the fact that a new attribute exists. - */ - TypeId AddAttribute (std::string name, - std::string help, - Attribute initialValue, - Ptr accessor, - Ptr checker); - - /** - * \param name the name of the new attribute - * \param help some help text which describes the purpose of this - * attribute - * \param flags flags which describe how this attribute can be read and/or written. - * \param initialValue the initial value for this attribute. - * \param accessor an instance of the associated AttributeAccessor subclass. - * \param checker an instance of the associated AttributeChecker subclass. - * \returns this TypeId instance - * - * Record in this TypeId the fact that a new attribute exists. - */ - TypeId AddAttribute (std::string name, - std::string help, - uint32_t flags, - Attribute initialValue, - Ptr accessor, - Ptr checker); - - /** - * \param name the name of the new trace source - * \param help some help text which describes the purpose of this - * trace source. - * \param accessor a pointer to a TraceSourceAccessor which can be - * used to connect/disconnect sinks to this trace source. - * \returns this TypeId instance. - */ - TypeId AddTraceSource (std::string name, - std::string help, - Ptr accessor); - - TypeId HideFromDocumentation (void); - - /** - * \brief store together a set of attribute properties. - */ - struct AttributeInfo { - // The accessor associated to the attribute. - Ptr accessor; - // The initial value associated to the attribute. - Attribute initialValue; - // The set of access control flags associated to the attribute. - uint32_t flags; - // The checker associated to the attribute. - Ptr checker; - }; - /** - * \param name the name of the requested attribute - * \param info a pointer to the TypeId::AttributeInfo data structure - * where the result value of this method will be stored. - * \returns true if the requested attribute could be found, false otherwise. - */ - bool LookupAttributeByName (std::string name, struct AttributeInfo *info) const; - - - // construct an invalid TypeId. - TypeId (); - ~TypeId (); - - ATTRIBUTE_HELPER_HEADER_1 (TypeId); -private: - friend class Object; - friend class AttributeList; - friend bool operator == (TypeId a, TypeId b); - friend bool operator != (TypeId a, TypeId b); - - - Ptr LookupTraceSourceByName (std::string name) const; - - /** - * \param fullName the full name of the requested attribute - * \param info a pointer to the TypeId::AttributeInfo data structure - * where the result value of this method will be stored. - * \returns the Accessor associated to the requested attribute - */ - static bool LookupAttributeByFullName (std::string fullName, struct AttributeInfo *info); - - explicit TypeId (uint16_t tid); - void DoAddConstructor (Callback callback); - Ptr GetAttributeAccessor (uint32_t i) const; - - uint16_t m_tid; -}; - -std::ostream & operator << (std::ostream &os, TypeId tid); -std::istream & operator >> (std::istream &is, TypeId &tid); - -ATTRIBUTE_HELPER_HEADER_2 (TypeId); - -/** - * \brief a container of attributes to be used during object's construction - * and in ns3::Object::Set. - * - */ -class AttributeList -{ -public: - AttributeList (); - AttributeList (const AttributeList &o); - AttributeList &operator = (const AttributeList &o); - ~AttributeList (); - /** - * \param name the full name of the attribute to set - * \param value the value to set - * - * This method checks that a attribute with the requested - * name exists and that the value specified is an acceptable - * value of that attribute. If any of these checks fails, - * the program terminates with a message. - */ - void Set (std::string name, Attribute value); - /** - * \param name the full name of the attribute to set - * \param value the value to set - * \returns true if the requested attribute exists and could be - * stored, false otherwise. - */ - bool SetFailSafe (std::string name, Attribute value); - /** - * \param tid the TypeId associated to this attribute - * \param name the name (not full!) of the attribute - * \param value the value to set - * - * This method checks that a attribute with the requested - * name exists and that the value specified is an acceptable - * value of that attribute. If any of these checks fails, - * the program terminates with a message. - */ - void SetWithTid (TypeId tid, std::string name, Attribute value); - - /** - * Clear the content of this instance. - */ - void Reset (void); - - /** - * \returns the global attribute container - * - * The global attribute container can be used to specify - * a set of attribute values without having to re-specify - * them for each object when it is created. This container - * is checked only during object construction and - * it is always checked last, after any per-object - * container is checked. - */ - static AttributeList *GetGlobal (void); - - // XXX: untested. - std::string SerializeToString (void) const; - bool DeserializeFromString (std::string value); -private: - friend class Object; - struct Attr { - Ptr checker; - Attribute value; - }; - typedef std::vector Attrs; - typedef Attrs::iterator Iterator; - typedef Attrs::const_iterator CIterator; - - - - bool DoSet (struct TypeId::AttributeInfo *info, Attribute param); - void DoSetOne (Ptr checker, Attribute param); - std::string LookupAttributeFullNameByChecker (Ptr checker) const; - - Attrs m_attributes; -}; - - /** * \brief a base class which provides memory management and object aggregation * diff --git a/src/core/type-id.cc b/src/core/type-id.cc new file mode 100644 index 000000000..61a443edb --- /dev/null +++ b/src/core/type-id.cc @@ -0,0 +1,687 @@ +#include "type-id.h" +#include "singleton.h" +#include "trace-source-accessor.h" +#include +#include + +/********************************************************************* + * Helper code + *********************************************************************/ + +namespace { + +class IidManager +{ +public: + IidManager (); + uint16_t AllocateUid (std::string name); + void SetParent (uint16_t uid, uint16_t parent); + void SetTypeName (uint16_t uid, std::string typeName); + void SetGroupName (uint16_t uid, std::string groupName); + void AddConstructor (uint16_t uid, ns3::Callback callback); + void HideFromDocumentation (uint16_t uid); + uint16_t GetUid (std::string name) const; + std::string GetName (uint16_t uid) const; + uint16_t GetParent (uint16_t uid) const; + std::string GetTypeName (uint16_t uid) const; + std::string GetGroupName (uint16_t uid) const; + ns3::Callback GetConstructor (uint16_t uid) const; + bool HasConstructor (uint16_t uid) const; + uint32_t GetRegisteredN (void) const; + uint16_t GetRegistered (uint32_t i) const; + void AddAttribute (uint16_t uid, + std::string name, + std::string help, + uint32_t flags, + ns3::Attribute initialValue, + ns3::Ptr spec, + ns3::Ptr checker); + uint32_t GetAttributeListN (uint16_t uid) const; + std::string GetAttributeName (uint16_t uid, uint32_t i) const; + std::string GetAttributeHelp (uint16_t uid, uint32_t i) const; + uint32_t GetAttributeFlags (uint16_t uid, uint32_t i) const; + ns3::Attribute GetAttributeInitialValue (uint16_t uid, uint32_t i) const; + ns3::Ptr GetAttributeAccessor (uint16_t uid, uint32_t i) const; + ns3::Ptr GetAttributeChecker (uint16_t uid, uint32_t i) const; + void AddTraceSource (uint16_t uid, + std::string name, + std::string help, + ns3::Ptr accessor); + uint32_t GetTraceSourceN (uint16_t uid) const; + std::string GetTraceSourceName (uint16_t uid, uint32_t i) const; + std::string GetTraceSourceHelp (uint16_t uid, uint32_t i) const; + ns3::Ptr GetTraceSourceAccessor (uint16_t uid, uint32_t i) const; + bool MustHideFromDocumentation (uint16_t uid) const; + +private: + struct AttributeInformation { + std::string name; + std::string help; + uint32_t flags; + ns3::Attribute initialValue; + ns3::Ptr param; + ns3::Ptr checker; + }; + struct TraceSourceInformation { + std::string name; + std::string help; + ns3::Ptr accessor; + }; + struct IidInformation { + std::string name; + uint16_t parent; + std::string typeName; + std::string groupName; + bool hasConstructor; + ns3::Callback constructor; + bool mustHideFromDocumentation; + std::vector attributes; + std::vector traceSources; + }; + typedef std::vector::const_iterator Iterator; + + struct IidManager::IidInformation *LookupInformation (uint16_t uid) const; + + std::vector m_information; +}; + +IidManager::IidManager () +{} + +uint16_t +IidManager::AllocateUid (std::string name) +{ + uint16_t j = 1; + for (Iterator i = m_information.begin (); i != m_information.end (); i++) + { + if (i->name == name) + { + NS_FATAL_ERROR ("Trying to allocate twice the same uid: " << name); + return 0; + } + j++; + } + struct IidInformation information; + information.name = name; + information.parent = 0; + information.typeName = ""; + information.groupName = ""; + information.hasConstructor = false; + information.mustHideFromDocumentation = false; + m_information.push_back (information); + uint32_t uid = m_information.size (); + NS_ASSERT (uid <= 0xffff); + return uid; +} + +struct IidManager::IidInformation * +IidManager::LookupInformation (uint16_t uid) const +{ + NS_ASSERT (uid <= m_information.size ()); + return const_cast (&m_information[uid-1]); +} + +void +IidManager::SetParent (uint16_t uid, uint16_t parent) +{ + NS_ASSERT (parent <= m_information.size ()); + struct IidInformation *information = LookupInformation (uid); + information->parent = parent; +} +void +IidManager::SetTypeName (uint16_t uid, std::string typeName) +{ + struct IidInformation *information = LookupInformation (uid); + information->typeName = typeName; +} +void +IidManager::SetGroupName (uint16_t uid, std::string groupName) +{ + struct IidInformation *information = LookupInformation (uid); + information->groupName = groupName; +} +void +IidManager::HideFromDocumentation (uint16_t uid) +{ + struct IidInformation *information = LookupInformation (uid); + information->mustHideFromDocumentation = true; +} + +void +IidManager::AddConstructor (uint16_t uid, ns3::Callback callback) +{ + struct IidInformation *information = LookupInformation (uid); + if (information->hasConstructor) + { + NS_FATAL_ERROR (information->name<<" already has a constructor."); + } + information->hasConstructor = true; + information->constructor = callback; +} + +uint16_t +IidManager::GetUid (std::string name) const +{ + uint32_t j = 1; + for (Iterator i = m_information.begin (); i != m_information.end (); i++) + { + if (i->name == name) + { + NS_ASSERT (j <= 0xffff); + return j; + } + j++; + } + return 0; +} +std::string +IidManager::GetName (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->name; +} +uint16_t +IidManager::GetParent (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->parent; +} +std::string +IidManager::GetTypeName (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->typeName; +} +std::string +IidManager::GetGroupName (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->groupName; +} + +ns3::Callback +IidManager::GetConstructor (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + if (!information->hasConstructor) + { + NS_FATAL_ERROR ("Requested constructor for "<name<<" but it does not have one."); + } + return information->constructor; +} + +bool +IidManager::HasConstructor (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->hasConstructor; +} + +uint32_t +IidManager::GetRegisteredN (void) const +{ + return m_information.size (); +} +uint16_t +IidManager::GetRegistered (uint32_t i) const +{ + return i + 1; +} + +void +IidManager::AddAttribute (uint16_t uid, + std::string name, + std::string help, + uint32_t flags, + ns3::Attribute initialValue, + ns3::Ptr spec, + ns3::Ptr checker) +{ + struct IidInformation *information = LookupInformation (uid); + for (std::vector::const_iterator j = information->attributes.begin (); + j != information->attributes.end (); j++) + { + if (j->name == name) + { + NS_FATAL_ERROR ("Registered the same attribute twice name=\""<name<<"\""); + return; + } + } + struct AttributeInformation param; + param.name = name; + param.help = help; + param.flags = flags; + param.initialValue = initialValue; + param.param = spec; + param.checker = checker; + information->attributes.push_back (param); +} + + +uint32_t +IidManager::GetAttributeListN (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->attributes.size (); +} +std::string +IidManager::GetAttributeName (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->attributes.size ()); + return information->attributes[i].name; +} +std::string +IidManager::GetAttributeHelp (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->attributes.size ()); + return information->attributes[i].help; +} +uint32_t +IidManager::GetAttributeFlags (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->attributes.size ()); + return information->attributes[i].flags; +} +ns3::Attribute +IidManager::GetAttributeInitialValue (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->attributes.size ()); + return information->attributes[i].initialValue; +} +ns3::Ptr +IidManager::GetAttributeAccessor (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->attributes.size ()); + return information->attributes[i].param; +} +ns3::Ptr +IidManager::GetAttributeChecker (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->attributes.size ()); + return information->attributes[i].checker; +} + +void +IidManager::AddTraceSource (uint16_t uid, + std::string name, + std::string help, + ns3::Ptr accessor) +{ + struct IidInformation *information = LookupInformation (uid); + struct TraceSourceInformation source; + source.name = name; + source.help = help; + source.accessor = accessor; + information->traceSources.push_back (source); +} +uint32_t +IidManager::GetTraceSourceN (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->traceSources.size (); +} +std::string +IidManager::GetTraceSourceName (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->traceSources.size ()); + return information->traceSources[i].name; +} +std::string +IidManager::GetTraceSourceHelp (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->traceSources.size ()); + return information->traceSources[i].help; +} +ns3::Ptr +IidManager::GetTraceSourceAccessor (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->traceSources.size ()); + return information->traceSources[i].accessor; +} +bool +IidManager::MustHideFromDocumentation (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->mustHideFromDocumentation; +} + +} // anonymous namespace + +namespace ns3 { + +/********************************************************************* + * The TypeId class + *********************************************************************/ + +TypeId::TypeId () + : m_tid (0) +{} + +TypeId::TypeId (const char *name) +{ + uint16_t uid = Singleton::Get ()->AllocateUid (name); + NS_ASSERT (uid != 0); + m_tid = uid; +} + + +TypeId::TypeId (uint16_t tid) + : m_tid (tid) +{} +TypeId::~TypeId () +{} +TypeId +TypeId::LookupByName (std::string name) +{ + uint16_t uid = Singleton::Get ()->GetUid (name); + NS_ASSERT (uid != 0); + return TypeId (uid); +} +bool +TypeId::LookupByNameFailSafe (std::string name, TypeId *tid) +{ + uint16_t uid = Singleton::Get ()->GetUid (name); + if (uid == 0) + { + return false; + } + *tid = TypeId (uid); + return true; +} + +bool +TypeId::LookupAttributeByFullName (std::string fullName, struct TypeId::AttributeInfo *info) +{ + std::string::size_type pos = fullName.rfind ("::"); + if (pos == std::string::npos) + { + return 0; + } + std::string tidName = fullName.substr (0, pos); + std::string paramName = fullName.substr (pos+2, fullName.size () - (pos+2)); + TypeId tid; + bool ok = LookupByNameFailSafe (tidName, &tid); + if (!ok) + { + return false; + } + return tid.LookupAttributeByName (paramName, info); +} +uint32_t +TypeId::GetRegisteredN (void) +{ + return Singleton::Get ()->GetRegisteredN (); +} +TypeId +TypeId::GetRegistered (uint32_t i) +{ + return TypeId (Singleton::Get ()->GetRegistered (i)); +} + +bool +TypeId::LookupAttributeByName (std::string name, struct TypeId::AttributeInfo *info) const +{ + TypeId tid; + TypeId nextTid = *this; + do { + tid = nextTid; + for (uint32_t i = 0; i < tid.GetAttributeListN (); i++) + { + std::string paramName = tid.GetAttributeName (i); + if (paramName == name) + { + info->accessor = tid.GetAttributeAccessor (i); + info->flags = tid.GetAttributeFlags (i); + info->initialValue = tid.GetAttributeInitialValue (i); + info->checker = tid.GetAttributeChecker (i); + return true; + } + } + nextTid = tid.GetParent (); + } while (nextTid != tid); + return false; +} + +TypeId +TypeId::SetParent (TypeId tid) +{ + Singleton::Get ()->SetParent (m_tid, tid.m_tid); + return *this; +} +TypeId +TypeId::SetGroupName (std::string groupName) +{ + Singleton::Get ()->SetGroupName (m_tid, groupName); + return *this; +} +TypeId +TypeId::SetTypeName (std::string typeName) +{ + Singleton::Get ()->SetTypeName (m_tid, typeName); + return *this; +} +TypeId +TypeId::GetParent (void) const +{ + uint16_t parent = Singleton::Get ()->GetParent (m_tid); + return TypeId (parent); +} +std::string +TypeId::GetGroupName (void) const +{ + std::string groupName = Singleton::Get ()->GetGroupName (m_tid); + return groupName; +} +std::string +TypeId::GetTypeName (void) const +{ + std::string typeName = Singleton::Get ()->GetTypeName (m_tid); + return typeName; +} + +std::string +TypeId::GetName (void) const +{ + std::string name = Singleton::Get ()->GetName (m_tid); + return name; +} + +bool +TypeId::HasConstructor (void) const +{ + bool hasConstructor = Singleton::Get ()->HasConstructor (m_tid); + return hasConstructor; +} + +void +TypeId::DoAddConstructor (Callback cb) +{ + Singleton::Get ()->AddConstructor (m_tid, cb); +} + +TypeId +TypeId::AddAttribute (std::string name, + std::string help, + Attribute initialValue, + Ptr param, + Ptr checker) +{ + Singleton::Get ()->AddAttribute (m_tid, name, help, ATTR_SGC, initialValue, param, checker); + return *this; +} + +TypeId +TypeId::AddAttribute (std::string name, + std::string help, + uint32_t flags, + Attribute initialValue, + Ptr param, + Ptr checker) +{ + Singleton::Get ()->AddAttribute (m_tid, name, help, flags, initialValue, param, checker); + return *this; +} + +Callback +TypeId::GetConstructor (void) const +{ + Callback cb = Singleton::Get ()->GetConstructor (m_tid); + return cb; +} + +bool +TypeId::MustHideFromDocumentation (void) const +{ + bool mustHide = Singleton::Get ()->MustHideFromDocumentation (m_tid); + return mustHide; +} + +uint32_t +TypeId::GetAttributeListN (void) const +{ + uint32_t n = Singleton::Get ()->GetAttributeListN (m_tid); + return n; +} +std::string +TypeId::GetAttributeName (uint32_t i) const +{ + std::string name = Singleton::Get ()->GetAttributeName (m_tid, i); + return name; +} +std::string +TypeId::GetAttributeHelp (uint32_t i) const +{ + std::string help = Singleton::Get ()->GetAttributeHelp (m_tid, i); + return help; +} +std::string +TypeId::GetAttributeFullName (uint32_t i) const +{ + return GetName () + "::" + GetAttributeName (i); +} +Attribute +TypeId::GetAttributeInitialValue (uint32_t i) const +{ + Attribute value = Singleton::Get ()->GetAttributeInitialValue (m_tid, i); + return value; +} +Ptr +TypeId::GetAttributeAccessor (uint32_t i) const +{ + // Used exclusively by the Object class. + Ptr param = Singleton::Get ()->GetAttributeAccessor (m_tid, i); + return param; +} +uint32_t +TypeId::GetAttributeFlags (uint32_t i) const +{ + // Used exclusively by the Object class. + uint32_t flags = Singleton::Get ()->GetAttributeFlags (m_tid, i); + return flags; +} +Ptr +TypeId::GetAttributeChecker (uint32_t i) const +{ + // Used exclusively by the Object class. + Ptr checker = Singleton::Get ()->GetAttributeChecker (m_tid, i); + return checker; +} + +uint32_t +TypeId::GetTraceSourceN (void) const +{ + return Singleton::Get ()->GetTraceSourceN (m_tid); +} +std::string +TypeId::GetTraceSourceName (uint32_t i) const +{ + return Singleton::Get ()->GetTraceSourceName (m_tid, i); +} +std::string +TypeId::GetTraceSourceHelp (uint32_t i) const +{ + return Singleton::Get ()->GetTraceSourceHelp (m_tid, i); +} +Ptr +TypeId::GetTraceSourceAccessor (uint32_t i) const +{ + return Singleton::Get ()->GetTraceSourceAccessor (m_tid, i); +} + +TypeId +TypeId::AddTraceSource (std::string name, + std::string help, + Ptr accessor) +{ + Singleton::Get ()->AddTraceSource (m_tid, name, help, accessor); + return *this; +} + +TypeId +TypeId::HideFromDocumentation (void) +{ + Singleton::Get ()->HideFromDocumentation (m_tid); + return *this; +} + + +Ptr +TypeId::LookupTraceSourceByName (std::string name) const +{ + TypeId tid; + TypeId nextTid = *this; + do { + tid = nextTid; + for (uint32_t i = 0; i < tid.GetTraceSourceN (); i++) + { + std::string srcName = tid.GetTraceSourceName (i); + if (srcName == name) + { + return tid.GetTraceSourceAccessor (i); + } + } + nextTid = tid.GetParent (); + } while (nextTid != tid); + return 0; +} + +std::ostream & operator << (std::ostream &os, TypeId tid) +{ + os << tid.GetName (); + return os; +} +std::istream & operator >> (std::istream &is, TypeId &tid) +{ + std::string tidString; + is >> tidString; + bool ok = TypeId::LookupByNameFailSafe (tidString, &tid); + if (!ok) + { + is.setstate (std::ios_base::badbit); + } + return is; +} + + +ATTRIBUTE_HELPER_CPP (TypeId); + +bool operator == (TypeId a, TypeId b) +{ + return a.m_tid == b.m_tid; +} + +bool operator != (TypeId a, TypeId b) +{ + return a.m_tid != b.m_tid; +} + +} // namespace ns3 diff --git a/src/core/type-id.h b/src/core/type-id.h new file mode 100644 index 000000000..f1550c892 --- /dev/null +++ b/src/core/type-id.h @@ -0,0 +1,292 @@ +#ifndef TYPE_ID_H +#define TYPE_ID_H + +#include "attribute.h" +#include "attribute-accessor-helper.h" +#include "attribute-helper.h" +#include "callback.h" +#include +#include + +namespace ns3 { + +class ObjectBase; +class TraceSourceAccessor; + +/** + * \brief a unique identifier for an interface. + * + * This class records a lot of meta-information about a + * subclass of the Object base class: + * - the base class of the subclass + * - the set of accessible constructors in the subclass + * - the set of 'attributes' accessible in the subclass + */ +class TypeId +{ +public: + enum { + /** + * The attribute can be read + */ + ATTR_GET = 1<<0, + /** + * The attribute can be written + */ + ATTR_SET = 1<<1, + /** + * The attribute can be written at construction-time. + */ + ATTR_CONSTRUCT = 1<<2, + /** + * The attribute can be read, and written at any time. + */ + ATTR_SGC = ATTR_GET | ATTR_SET | ATTR_CONSTRUCT, + }; + + /** + * \param name the name of the requested TypeId + * \returns the unique id associated with the requested + * name. + * + * This method cannot fail: it will crash if the input + * name is not a valid TypeId name. + */ + static TypeId LookupByName (std::string name); + /** + * \param name the name of the requested TypeId + * \param tid a pointer to the TypeId instance where the + * result of this function should be stored. + * \returns true if the requested name was found, false otherwise. + */ + static bool LookupByNameFailSafe (std::string name, TypeId *tid); + + /** + * \returns the number of TypeId instances registered. + */ + static uint32_t GetRegisteredN (void); + /** + * \param i index + * \returns the TypeId instance whose index is i. + */ + static TypeId GetRegistered (uint32_t i); + + /** + * \param name the name of the interface to construct. + * + * No two instances can share the same name. The name is expected to be + * the full c++ typename of associated c++ object. + */ + TypeId (const char * name); + + /** + * \returns the parent of this TypeId + * + * This method cannot fail. It will return itself + * if this TypeId has no parent. i.e., it is at the top + * of the TypeId hierarchy. Currently, this is the + * case for the TypeId associated to the Object class + * only. + */ + TypeId GetParent (void) const; + + /** + * \returns the name of the group associated to this TypeId. + */ + std::string GetGroupName (void) const; + /** + * \returns the fully-qualified C++ typename of this TypeId. + */ + std::string GetTypeName (void) const; + + /** + * \returns the name of this interface. + */ + std::string GetName (void) const; + + /** + * \returns true if this TypeId has a constructor + */ + bool HasConstructor (void) const; + + /** + * \returns the number of attributes associated to this TypeId + */ + uint32_t GetAttributeListN (void) const; + /** + * \param i index into attribute array + * \returns the name associated to the attribute whose + * index is i. + */ + std::string GetAttributeName (uint32_t i) const; + std::string GetAttributeHelp (uint32_t i) const; + /** + * \param i index into attribute array + * \returns the full name associated to the attribute whose + * index is i. + */ + std::string GetAttributeFullName (uint32_t i) const; + + Attribute GetAttributeInitialValue (uint32_t i) const; + uint32_t GetAttributeFlags (uint32_t i) const; + Ptr GetAttributeChecker (uint32_t i) const; + + + uint32_t GetTraceSourceN (void) const; + std::string GetTraceSourceName (uint32_t i) const; + std::string GetTraceSourceHelp (uint32_t i) const; + Ptr GetTraceSourceAccessor (uint32_t i) const; + Ptr GetAttributeAccessor (uint32_t i) const; + + Callback GetConstructor (void) const; + + bool MustHideFromDocumentation (void) const; + + /** + * \param tid the TypeId of the base class. + * \return this TypeId instance. + * + * Record in this TypeId which TypeId is the TypeId + * of the base class of the subclass. + */ + TypeId SetParent (TypeId tid); + /** + * \return this TypeId instance. + * + * Record in this TypeId which TypeId is the TypeId + * of the base class of the subclass. + */ + template + TypeId SetParent (void); + + /** + * \param groupName the name of the group this TypeId belongs to. + * \returns this TypeId instance. + * + * The group name is purely an advisory information used to + * group together types according to a user-specific grouping + * scheme. + */ + TypeId SetGroupName (std::string groupName); + + /** + * \param typeName the fully-qualified C++ typename of this TypeId. + * \returns this TypeId instance. + */ + TypeId SetTypeName (std::string typeName); + + /** + * \returns this TypeId instance + * + * Record in this TypeId the fact that the default constructor + * is accessible. + */ + template + TypeId AddConstructor (void); + + /** + * \param name the name of the new attribute + * \param help some help text which describes the purpose of this + * attribute. + * \param initialValue the initial value for this attribute. + * \param accessor an instance of the associated AttributeAccessor subclass. + * \param checker an instance of the associated AttributeChecker subclass. + * \returns this TypeId instance + * + * Record in this TypeId the fact that a new attribute exists. + */ + TypeId AddAttribute (std::string name, + std::string help, + Attribute initialValue, + Ptr accessor, + Ptr checker); + + /** + * \param name the name of the new attribute + * \param help some help text which describes the purpose of this + * attribute + * \param flags flags which describe how this attribute can be read and/or written. + * \param initialValue the initial value for this attribute. + * \param accessor an instance of the associated AttributeAccessor subclass. + * \param checker an instance of the associated AttributeChecker subclass. + * \returns this TypeId instance + * + * Record in this TypeId the fact that a new attribute exists. + */ + TypeId AddAttribute (std::string name, + std::string help, + uint32_t flags, + Attribute initialValue, + Ptr accessor, + Ptr checker); + + /** + * \param name the name of the new trace source + * \param help some help text which describes the purpose of this + * trace source. + * \param accessor a pointer to a TraceSourceAccessor which can be + * used to connect/disconnect sinks to this trace source. + * \returns this TypeId instance. + */ + TypeId AddTraceSource (std::string name, + std::string help, + Ptr accessor); + + TypeId HideFromDocumentation (void); + + /** + * \brief store together a set of attribute properties. + */ + struct AttributeInfo { + // The accessor associated to the attribute. + Ptr accessor; + // The initial value associated to the attribute. + Attribute initialValue; + // The set of access control flags associated to the attribute. + uint32_t flags; + // The checker associated to the attribute. + Ptr checker; + }; + /** + * \param name the name of the requested attribute + * \param info a pointer to the TypeId::AttributeInfo data structure + * where the result value of this method will be stored. + * \returns true if the requested attribute could be found, false otherwise. + */ + bool LookupAttributeByName (std::string name, struct AttributeInfo *info) const; + Ptr LookupTraceSourceByName (std::string name) const; + + + // construct an invalid TypeId. + TypeId (); + ~TypeId (); + + ATTRIBUTE_HELPER_HEADER_1 (TypeId); +private: + friend class AttributeList; + friend bool operator == (TypeId a, TypeId b); + friend bool operator != (TypeId a, TypeId b); + + + /** + * \param fullName the full name of the requested attribute + * \param info a pointer to the TypeId::AttributeInfo data structure + * where the result value of this method will be stored. + * \returns the Accessor associated to the requested attribute + */ + static bool LookupAttributeByFullName (std::string fullName, struct AttributeInfo *info); + + explicit TypeId (uint16_t tid); + void DoAddConstructor (Callback callback); + + uint16_t m_tid; +}; + +std::ostream & operator << (std::ostream &os, TypeId tid); +std::istream & operator >> (std::istream &is, TypeId &tid); + +ATTRIBUTE_HELPER_HEADER_2 (TypeId); + +} // namespace ns3 + +#endif /* TYPE_ID_H */ diff --git a/src/core/wscript b/src/core/wscript index 5d1dd97e9..c735f97a2 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -31,6 +31,8 @@ def build(bld): 'callback-test.cc', 'log.cc', 'breakpoint.cc', + 'type-id.cc', + 'attribute-list.cc', 'object-base.cc', 'ptr.cc', 'object.cc', @@ -73,6 +75,8 @@ def build(bld): 'empty.h', 'callback.h', 'object-base.h', + 'type-id.h', + 'attribute-list.h', 'ptr.h', 'object.h', 'log.h', From fd014845f72b4a6ce56721f4d917a24d93f942f0 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 16 Mar 2008 20:59:04 +0100 Subject: [PATCH 05/35] introduce ObjectBase::GetInstanceTypeId --- src/core/config.cc | 2 +- src/core/object-base.h | 1 + src/core/object.cc | 13 ++++++------- src/core/object.h | 4 ++-- src/core/trace-source-accessor.h | 5 +++-- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/core/config.cc b/src/core/config.cc index 94ac9017b..ca03fd25f 100644 --- a/src/core/config.cc +++ b/src/core/config.cc @@ -205,7 +205,7 @@ Resolver::DoResolve (std::string path, Ptr root) else { // this is a normal attribute. - TypeId tid = root->GetRealTypeId (); + TypeId tid = root->GetInstanceTypeId (); struct TypeId::AttributeInfo info; if (!tid.LookupAttributeByName (item, &info)) { diff --git a/src/core/object-base.h b/src/core/object-base.h index c5373e07d..0b5b3e284 100644 --- a/src/core/object-base.h +++ b/src/core/object-base.h @@ -28,6 +28,7 @@ class ObjectBase { public: virtual ~ObjectBase (); + virtual TypeId GetInstanceTypeId (void) const = 0; }; } // namespace ns3 diff --git a/src/core/object.cc b/src/core/object.cc index f96089c45..e704d46c1 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -46,6 +46,12 @@ GetObjectIid (void) return tid; } +TypeId +Object::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + TypeId Object::GetTypeId (void) { @@ -276,13 +282,6 @@ Object::TraceDisconnectWithoutContext (std::string name, std::string context, co return ok; } -TypeId -Object::GetRealTypeId (void) const -{ - return m_tid; -} - - Ptr Object::DoGetObject (TypeId tid) const { diff --git a/src/core/object.h b/src/core/object.h index 612dae5d3..181a4fcfa 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -53,6 +53,8 @@ public: Object (); virtual ~Object (); + virtual TypeId GetInstanceTypeId (void) const; + /** * \param name the name of the attribute to set * \param value the name of the attribute to set @@ -88,8 +90,6 @@ public: bool TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb); bool TraceDisconnectWithoutContext (std::string name, std::string context, const CallbackBase &cb); - TypeId GetRealTypeId (void) const; - /** * Increment the reference count. This method should not be called * by user code. Object instances are expected to be used in conjunction diff --git a/src/core/trace-source-accessor.h b/src/core/trace-source-accessor.h index 645149364..c244b2e40 100644 --- a/src/core/trace-source-accessor.h +++ b/src/core/trace-source-accessor.h @@ -21,18 +21,19 @@ #define TRACE_SOURCE_ACCESSOR_H #include -#include "object-base.h" #include "callback.h" #include "ptr.h" namespace ns3 { +class ObjectBase; + /** * \brief control access to objects' trace sources * * This class abstracts the kind of trace source to which we want to connect. */ -class TraceSourceAccessor : public ObjectBase +class TraceSourceAccessor { public: TraceSourceAccessor (); From 0bfc59440761e8a29b752962477dc12a9ec98b91 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 16 Mar 2008 21:00:31 +0100 Subject: [PATCH 06/35] remove Object::m_collecting --- src/core/object.cc | 1 - src/core/object.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/core/object.cc b/src/core/object.cc index e704d46c1..a725888ad 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -64,7 +64,6 @@ Object::Object () : m_count (1), m_tid (Object::GetTypeId ()), m_disposed (false), - m_collecting (false), m_next (this) {} Object::~Object () diff --git a/src/core/object.h b/src/core/object.h index 181a4fcfa..ec83a1c47 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -196,7 +196,6 @@ private: * has run, false otherwise. */ bool m_disposed; - mutable bool m_collecting; /** * A pointer to the next aggregate object. This is a circular * linked list of aggregated objects: the last one points From 0ef1e6b8660696c904cc8706710317a7159886fb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 16 Mar 2008 21:52:15 +0100 Subject: [PATCH 07/35] return the _current_ tid, not the Object tid. --- src/core/object.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/object.cc b/src/core/object.cc index a725888ad..071e09468 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -49,7 +49,7 @@ GetObjectIid (void) TypeId Object::GetInstanceTypeId (void) const { - return GetTypeId (); + return m_tid; } TypeId From 96e4874bdba3f694e94f2e0ad0eb7890b81b55fb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 05:22:29 +0100 Subject: [PATCH 08/35] move attribute code to ObjectBase. --- src/core/attribute-list.h | 2 +- src/core/object-base.cc | 253 +++++++++++++++++++++++++++++++++++++- src/core/object-base.h | 56 +++++++++ src/core/object.cc | 222 +-------------------------------- src/core/object.h | 42 +------ 5 files changed, 315 insertions(+), 260 deletions(-) diff --git a/src/core/attribute-list.h b/src/core/attribute-list.h index 98adbecfc..b7c211c0b 100644 --- a/src/core/attribute-list.h +++ b/src/core/attribute-list.h @@ -70,7 +70,7 @@ public: std::string SerializeToString (void) const; bool DeserializeFromString (std::string value); private: - friend class Object; + friend class ObjectBase; struct Attr { Ptr checker; Attribute value; diff --git a/src/core/object-base.cc b/src/core/object-base.cc index 98a080220..5eec563ea 100644 --- a/src/core/object-base.cc +++ b/src/core/object-base.cc @@ -1,3 +1,254 @@ #include "object-base.h" +#include "log.h" +#include "trace-source-accessor.h" +#include "string.h" -ns3::ObjectBase::~ObjectBase () {} +NS_LOG_COMPONENT_DEFINE ("ObjectBase"); + +namespace ns3 { + +static TypeId +GetObjectIid (void) +{ + TypeId tid = TypeId ("ns3::ObjectBase"); + tid.SetParent (tid); + return tid; +} + +TypeId +ObjectBase::GetTypeId (void) +{ + static TypeId tid = GetObjectIid (); + return tid; +} + +ObjectBase::~ObjectBase () +{} + +void +ObjectBase::NotifyConstructionCompleted (void) +{} + +void +ObjectBase::ConstructSelf (const AttributeList &attributes) +{ + // loop over the inheritance tree back to the Object base class. + TypeId tid = GetInstanceTypeId (); + do { + // loop over all attributes in object type + NS_LOG_DEBUG ("construct tid="<checker == checker) + { + // We have a matching attribute value. + DoSet (paramSpec, initial, checker, j->value); + NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< + tid.GetAttributeName (i)<<"\""); + found = true; + break; + } + } + if (!found) + { + // is this attribute stored in the global instance instance ? + for (AttributeList::Attrs::const_iterator j = AttributeList::GetGlobal ()->m_attributes.begin (); + j != AttributeList::GetGlobal ()->m_attributes.end (); j++) + { + if (j->checker == checker) + { + // We have a matching attribute value. + DoSet (paramSpec, initial, checker, j->value); + NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< + tid.GetAttributeName (i)<<"\" from global"); + found = true; + break; + } + } + } + if (!found) + { + // No matching attribute value so we set the default value. + paramSpec->Set (this, initial); + NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< + tid.GetAttributeName (i)<<"\" from initial value."); + } + } + tid = tid.GetParent (); + } while (tid != ObjectBase::GetTypeId ()); + NotifyConstructionCompleted (); +} + +bool +ObjectBase::DoSet (Ptr spec, Attribute initialValue, + Ptr checker, Attribute value) +{ + bool ok = checker->Check (value); + if (!ok) + { + // attempt to convert to string + const StringValue *str = value.DynCast (); + if (str == 0) + { + return false; + } + // attempt to convert back from string. + Attribute v = checker->Create (); + ok = v.DeserializeFromString (str->Get ().Get (), checker); + if (!ok) + { + return false; + } + ok = checker->Check (v); + if (!ok) + { + return false; + } + value = v; + } + ok = spec->Set (this, value); + return ok; +} +void +ObjectBase::SetAttribute (std::string name, Attribute value) +{ + struct TypeId::AttributeInfo info; + TypeId tid = GetInstanceTypeId (); + if (!tid.LookupAttributeByName (name, &info)) + { + NS_FATAL_ERROR ("Attribute name="< /** * This macro should be invoked once for every class which @@ -27,8 +30,61 @@ namespace ns3 { class ObjectBase { public: + static TypeId GetTypeId (void); + virtual ~ObjectBase (); virtual TypeId GetInstanceTypeId (void) const = 0; + + /** + * \param name the name of the attribute to set + * \param value the name of the attribute to set + * + * Set a single attribute. This cannot fail: if the input is invalid, + * it will crash immediately. + */ + void SetAttribute (std::string name, Attribute value); + /** + * \param name the name of the attribute to set + * \param value the name of the attribute to set + * \returns true if the requested attribute exists and could be set, + * false otherwise. + */ + bool SetAttributeFailSafe (std::string name, Attribute value); + /** + * \param name the name of the attribute to read + * \param value a reference to the string where the value of the + * attribute should be stored. + * \returns true if the requested attribute was found, false otherwise. + */ + bool GetAttribute (std::string name, std::string &value) const; + /** + * \param name the name of the attribute to read + * \returns the attribute read. + * + * If the input attribute name does not exist, this method crashes. + */ + Attribute GetAttribute (std::string name) const; + + bool TraceConnectWithoutContext (std::string name, const CallbackBase &cb); + bool TraceConnectWithoutContext (std::string name, std::string context, const CallbackBase &cb); + bool TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb); + bool TraceDisconnectWithoutContext (std::string name, std::string context, const CallbackBase &cb); + +protected: + virtual void NotifyConstructionCompleted (void); + /** + * \param attributes the attribute values used to initialize + * the member variables of this object's instance. + * + * Invoked from subclasses to initialize all of their + * attribute members. + */ + void ConstructSelf (const AttributeList &attributes); + +private: + bool DoSet (Ptr spec, Attribute intialValue, + Ptr checker, Attribute value); + }; } // namespace ns3 diff --git a/src/core/object.cc b/src/core/object.cc index 071e09468..7a0d5aca4 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -38,14 +38,6 @@ namespace ns3 { NS_OBJECT_ENSURE_REGISTERED (Object); -static TypeId -GetObjectIid (void) -{ - TypeId tid = TypeId ("ns3::Object"); - tid.SetParent (tid); - return tid; -} - TypeId Object::GetInstanceTypeId (void) const { @@ -55,7 +47,9 @@ Object::GetInstanceTypeId (void) const TypeId Object::GetTypeId (void) { - static TypeId tid = GetObjectIid (); + static TypeId tid = TypeId ("ns3::Object") + .SetParent () + ; return tid; } @@ -73,212 +67,7 @@ Object::~Object () void Object::Construct (const AttributeList &attributes) { - // loop over the inheritance tree back to the Object base class. - TypeId tid = m_tid; - do { - // loop over all attributes in object type - NS_LOG_DEBUG ("construct tid="<checker == checker) - { - // We have a matching attribute value. - DoSet (paramSpec, initial, checker, j->value); - NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< - tid.GetAttributeName (i)<<"\""); - found = true; - break; - } - } - if (!found) - { - // is this attribute stored in the global instance instance ? - for (AttributeList::Attrs::const_iterator j = AttributeList::GetGlobal ()->m_attributes.begin (); - j != AttributeList::GetGlobal ()->m_attributes.end (); j++) - { - if (j->checker == checker) - { - // We have a matching attribute value. - DoSet (paramSpec, initial, checker, j->value); - NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< - tid.GetAttributeName (i)<<"\" from global"); - found = true; - break; - } - } - } - if (!found) - { - // No matching attribute value so we set the default value. - paramSpec->Set (this, initial); - NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< - tid.GetAttributeName (i)<<"\" from initial value."); - } - } - tid = tid.GetParent (); - } while (tid != Object::GetTypeId ()); - NotifyConstructionCompleted (); -} -bool -Object::DoSet (Ptr spec, Attribute initialValue, - Ptr checker, Attribute value) -{ - bool ok = checker->Check (value); - if (!ok) - { - // attempt to convert to string - const StringValue *str = value.DynCast (); - if (str == 0) - { - return false; - } - // attempt to convert back from string. - Attribute v = checker->Create (); - ok = v.DeserializeFromString (str->Get ().Get (), checker); - if (!ok) - { - return false; - } - ok = checker->Check (v); - if (!ok) - { - return false; - } - value = v; - } - ok = spec->Set (this, value); - return ok; -} -void -Object::SetAttribute (std::string name, Attribute value) -{ - struct TypeId::AttributeInfo info; - if (!m_tid.LookupAttributeByName (name, &info)) - { - NS_FATAL_ERROR ("Attribute name="< -TypeId -TypeId::SetParent (void) -{ - return SetParent (T::GetTypeId ()); -} - -template -TypeId -TypeId::AddConstructor (void) -{ - struct Maker { - static ObjectBase * Create () { - ObjectBase * base = new T (); - return base; - } - }; - Callback cb = MakeCallback (&Maker::Create); - DoAddConstructor (cb); - return *this; -} - /************************************************************************* * The Object implementation which depends on templates *************************************************************************/ diff --git a/src/core/type-id.h b/src/core/type-id.h index f1550c892..4a4ffe8da 100644 --- a/src/core/type-id.h +++ b/src/core/type-id.h @@ -289,4 +289,34 @@ ATTRIBUTE_HELPER_HEADER_2 (TypeId); } // namespace ns3 +namespace ns3 { + +/************************************************************************* + * The TypeId implementation which depends on templates + *************************************************************************/ + +template +TypeId +TypeId::SetParent (void) +{ + return SetParent (T::GetTypeId ()); +} + +template +TypeId +TypeId::AddConstructor (void) +{ + struct Maker { + static ObjectBase * Create () { + ObjectBase * base = new T (); + return base; + } + }; + Callback cb = MakeCallback (&Maker::Create); + DoAddConstructor (cb); + return *this; +} + +} // namespace ns3 + #endif /* TYPE_ID_H */ From c7be59d0c132c46eeb1eedc645cf2164d43ca298 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 11:45:36 -0700 Subject: [PATCH 10/35] replace PacketPrinter with an iterator --- samples/wscript | 3 - src/common/packet-metadata-test.cc | 169 ++++------------------------- src/common/packet-metadata.cc | 155 ++++++++++++-------------- src/common/packet-metadata.h | 59 ++++++++-- src/common/packet.cc | 8 +- src/common/packet.h | 18 +-- src/common/wscript | 2 - 7 files changed, 146 insertions(+), 268 deletions(-) diff --git a/samples/wscript b/samples/wscript index b6500b517..e7868460b 100644 --- a/samples/wscript +++ b/samples/wscript @@ -13,9 +13,6 @@ def build(bld): obj = bld.create_ns3_program('main-packet-tag', ['common', 'simulator']) obj.source = 'main-packet-tag.cc' - obj = bld.create_ns3_program('main-packet-printer', ['common', 'simulator', 'internet-node']) - obj.source = 'main-packet-printer.cc' - obj = bld.create_ns3_program('main-test') obj.source = 'main-test.cc' diff --git a/src/common/packet-metadata-test.cc b/src/common/packet-metadata-test.cc index fb32b1934..45d7ab5a1 100644 --- a/src/common/packet-metadata-test.cc +++ b/src/common/packet-metadata-test.cc @@ -202,135 +202,40 @@ public: bool CheckHistory (Ptr p, const char *file, int line, uint32_t n, ...); virtual bool RunTests (void); private: - template - void PrintHeader (std::ostream &os, uint32_t packetUid, uint32_t size, const HistoryHeader *header); - template - void PrintTrailer (std::ostream &os, uint32_t packetUid, uint32_t size, const HistoryTrailer *trailer); - void PrintFragment (std::ostream &os,uint32_t packetUid, - uint32_t size,std::string & name, - struct PacketPrinter::FragmentInformation info); - void PrintPayload (std::ostream &os,uint32_t packetUid, - uint32_t size, - struct PacketPrinter::FragmentInformation info); - template - void RegisterHeader (void); - template - void RegisterTrailer (void); - void CleanupPrints (void); Ptr DoAddHeader (Ptr p); - bool Check (const char *file, int line, std::list expected); - - - bool m_headerError; - bool m_trailerError; - std::list m_prints; - PacketPrinter m_printer; }; PacketMetadataTest::PacketMetadataTest () : Test ("PacketMetadata") -{ - m_printer.SetPayloadPrinter (MakeCallback (&PacketMetadataTest::PrintPayload, this)); - m_printer.SetSeparator (""); -} +{} PacketMetadataTest::~PacketMetadataTest () {} -template -void -PacketMetadataTest::RegisterHeader (void) +bool +PacketMetadataTest::CheckHistory (Ptr p, const char *file, int line, uint32_t n, ...) { - static bool registered = false; - if (!registered) + std::list expected; + va_list ap; + va_start (ap, n); + for (uint32_t j = 0; j < n; j++) { - m_printer.SetHeaderPrinter (MakeCallback (&PacketMetadataTest::PrintHeader, this), - MakeCallback (&PacketMetadataTest::PrintFragment, this)); - registered = true; + int v = va_arg (ap, int); + expected.push_back (v); } -} + va_end (ap); -template -void -PacketMetadataTest::RegisterTrailer (void) -{ - static bool registered = false; - if (!registered) + PacketMetadata::ItemIterator k = p->BeginItem (); + std::list got; + while (k.HasNext ()) { - m_printer.SetTrailerPrinter (MakeCallback (&PacketMetadataTest::PrintTrailer, this), - MakeCallback (&PacketMetadataTest::PrintFragment, this)); - registered = true; + struct PacketMetadata::Item item = k.Next (); + got.push_back (item.currentSize); } -} - -template -void -PacketMetadataTest::PrintHeader (std::ostream &os, uint32_t packetUid, uint32_t size, - const HistoryHeader *header) -{ - if (!header->IsOk ()) - { - m_headerError = true; - } - m_prints.push_back (N); -} - -template -void -PacketMetadataTest::PrintTrailer (std::ostream &os, uint32_t packetUid, uint32_t size, - const HistoryTrailer *trailer) -{ - if (!trailer->IsOk ()) - { - m_trailerError = true; - } - m_prints.push_back (N); -} -void -PacketMetadataTest::PrintFragment (std::ostream &os,uint32_t packetUid, - uint32_t size,std::string & name, - struct PacketPrinter::FragmentInformation info) -{ - m_prints.push_back (size - (info.end + info.start)); -} -void -PacketMetadataTest::PrintPayload (std::ostream &os,uint32_t packetUid, - uint32_t size, - struct PacketPrinter::FragmentInformation info) -{ - m_prints.push_back (size - (info.end + info.start)); -} - - -void -PacketMetadataTest::CleanupPrints (void) -{ - m_prints.clear (); -} - -bool -PacketMetadataTest::Check (const char *file, int line, std::list expected) -{ - if (m_headerError) - { - Failure () << "PacketMetadata header error. file=" << file - << ", line=" << line << std::endl; - return false; - } - if (m_trailerError) - { - Failure () << "PacketMetadata trailer error. file=" << file - << ", line=" << line << std::endl; - return false; - } - if (expected.size () != m_prints.size ()) - { - goto error; - } - for (std::list::iterator i = m_prints.begin (), + for (std::list::iterator i = got.begin (), j = expected.begin (); - i != m_prints.end (); i++, j++) + i != got.end (); i++, j++) { NS_ASSERT (j != expected.end ()); if (*j != *i) @@ -342,8 +247,8 @@ PacketMetadataTest::Check (const char *file, int line, std::list expected) error: Failure () << "PacketMetadata error. file="<< file << ", line=" << line << ", got:\""; - for (std::list::iterator i = m_prints.begin (); - i != m_prints.end (); i++) + for (std::list::iterator i = got.begin (); + i != got.end (); i++) { Failure () << *i << ", "; } @@ -357,60 +262,24 @@ PacketMetadataTest::Check (const char *file, int line, std::list expected) return false; } -bool -PacketMetadataTest::CheckHistory (Ptr p, const char *file, int line, uint32_t n, ...) -{ - m_headerError = false; - m_trailerError = false; - std::list expected; - va_list ap; - va_start (ap, n); - for (uint32_t j = 0; j < n; j++) - { - int v = va_arg (ap, int); - expected.push_back (v); - } - va_end (ap); - - m_printer.PrintForward (); - p->Print (Failure (), m_printer); - bool ok = Check (file, line, expected); - CleanupPrints (); - if (!ok) - { - return false; - } - - m_printer.PrintBackward (); - p->Print (Failure (), m_printer); - expected.reverse (); - ok = Check (file, line, expected); - CleanupPrints (); - return ok; -} - #define ADD_HEADER(p, n) \ { \ HistoryHeader header; \ - RegisterHeader (); \ p->AddHeader (header); \ } #define ADD_TRAILER(p, n) \ { \ HistoryTrailer trailer; \ - RegisterTrailer (); \ p->AddTrailer (trailer); \ } #define REM_HEADER(p, n) \ { \ HistoryHeader header; \ - RegisterHeader (); \ p->RemoveHeader (header); \ } #define REM_TRAILER(p, n) \ { \ HistoryTrailer trailer; \ - RegisterTrailer (); \ p->RemoveTrailer (trailer); \ } #define CHECK_HISTORY(p, ...) \ diff --git a/src/common/packet-metadata.cc b/src/common/packet-metadata.cc index 0f6541f75..6b30b49d3 100644 --- a/src/common/packet-metadata.cc +++ b/src/common/packet-metadata.cc @@ -988,47 +988,6 @@ PacketMetadata::RemoveAtEnd (uint32_t end) } NS_ASSERT (leftToRemove == 0); } - -uint32_t -PacketMetadata::DoPrint (const struct PacketMetadata::SmallItem *item, - const struct PacketMetadata::ExtraItem *extraItem, - Buffer data, uint32_t offset, const PacketPrinter &printer, - std::ostream &os) const -{ - uint32_t uid = (item->typeUid & 0xfffffffe) >> 1; - if (uid == 0) - { - // payload. - printer.PrintPayload (os, extraItem->packetUid, item->size, - extraItem->fragmentStart, - item->size - extraItem->fragmentEnd); - } - else if (extraItem->fragmentStart != 0 || - extraItem->fragmentEnd != item->size) - { - printer.PrintChunkFragment (uid, os, extraItem->packetUid, item->size, - extraItem->fragmentStart, - item->size - extraItem->fragmentEnd); - } - else if (ChunkRegistry::IsHeader (uid)) - { - ns3::Buffer::Iterator j = data.Begin (); - j.Next (offset); - printer.PrintChunk (uid, j, os, extraItem->packetUid, item->size); - } - else if (ChunkRegistry::IsTrailer (uid)) - { - ns3::Buffer::Iterator j = data.End (); - j.Prev (data.GetSize () - (offset + item->size)); - printer.PrintChunk (uid, j, os, extraItem->packetUid, item->size); - } - else - { - NS_ASSERT (false); - } - return extraItem->fragmentEnd - extraItem->fragmentStart; -} - uint32_t PacketMetadata::GetTotalSize (void) const { @@ -1056,60 +1015,86 @@ PacketMetadata::GetUid (void) const { return m_packetUid; } - -void -PacketMetadata::Print (std::ostream &os, Buffer data, const PacketPrinter &printer) const +PacketMetadata::ItemIterator +PacketMetadata::BeginItem (Buffer buffer) const { - if (!m_enable) + return ItemIterator (this, buffer); +} +PacketMetadata::ItemIterator::ItemIterator (const PacketMetadata *metadata, Buffer buffer) + : m_metadata (metadata), + m_buffer (buffer), + m_current (metadata->m_head), + m_offset (0), + m_hasReadTail (false) +{} +bool +PacketMetadata::ItemIterator::HasNext (void) const +{ + if (m_current == 0xffff) { - return; + return false; } - NS_ASSERT (m_data != 0); - NS_ASSERT (GetTotalSize () == data.GetSize ()); - struct PacketMetadata::SmallItem item; - struct PacketMetadata::ExtraItem extraItem; - if (printer.m_forward) + if (m_hasReadTail) { - uint32_t current = m_head; - uint32_t offset = 0; - while (current != 0xffff) - { - ReadItems (current, &item, &extraItem); - uint32_t realSize = DoPrint (&item, &extraItem, data, offset, printer, os); - offset += realSize; - if (current == m_tail) - { - break; - } - if (item.next != 0xffff) - { - os << printer.m_separator; - } - NS_ASSERT (current != item.next); - current = item.next; - } + return false; + } + return true; +} +PacketMetadata::Item +PacketMetadata::ItemIterator::Next (void) +{ + struct PacketMetadata::Item item; + struct PacketMetadata::SmallItem smallItem; + struct PacketMetadata::ExtraItem extraItem; + m_metadata->ReadItems (m_current, &smallItem, &extraItem); + if (m_current == m_metadata->m_tail) + { + m_hasReadTail = true; + } + m_current = smallItem.next; + uint32_t uid = (smallItem.typeUid & 0xfffffffe) >> 1; + item.uid = uid; + item.currentTrimedFromStart = extraItem.fragmentStart; + item.currentTrimedFromEnd = extraItem.fragmentEnd - smallItem.size; + item.currentSize = extraItem.fragmentEnd - extraItem.fragmentStart; + if (extraItem.fragmentStart != 0 || extraItem.fragmentEnd != 0) + { + item.isFragment = true; } else { - uint32_t current = m_tail; - uint32_t offset = data.GetSize (); - while (current != 0xffff) + item.isFragment = false; + } + if (uid == 0) + { + item.type = PacketMetadata::Item::PAYLOAD; + } + else if (ChunkRegistry::IsHeader (uid)) + { + item.type = PacketMetadata::Item::HEADER; + if (!item.isFragment) { - ReadItems (current, &item, &extraItem); - uint32_t realSize = DoPrint (&item, &extraItem, data, offset - item.size, printer, os); - offset -= realSize; - if (current == m_head) - { - break; - } - if (item.prev != 0xffff) - { - os << printer.m_separator; - } - NS_ASSERT (current != item.prev); - current = item.prev; + ns3::Buffer::Iterator j = m_buffer.Begin (); + j.Next (m_offset); + item.current = j; } } + else if (ChunkRegistry::IsTrailer (uid)) + { + item.type = PacketMetadata::Item::TRAILER; + if (!item.isFragment) + { + ns3::Buffer::Iterator j = m_buffer.End (); + j.Prev (m_buffer.GetSize () - (m_offset + smallItem.size)); + item.current = j; + } + } + else + { + NS_ASSERT (false); + } + m_offset += extraItem.fragmentEnd - extraItem.fragmentStart; + return item; } uint32_t diff --git a/src/common/packet-metadata.h b/src/common/packet-metadata.h index 8b979ff35..2d71d712a 100644 --- a/src/common/packet-metadata.h +++ b/src/common/packet-metadata.h @@ -25,7 +25,7 @@ #include #include "ns3/callback.h" #include "ns3/assert.h" -#include "packet-printer.h" +#include "buffer.h" namespace ns3 { @@ -72,8 +72,54 @@ class Buffer; * The variable-size 32 bit integers are stored using the uleb128 * encoding. */ -class PacketMetadata { +class PacketMetadata +{ public: + struct Item + { + enum { + PAYLOAD, + HEADER, + TRAILER + } type; + /* true: this is a fragmented header, trailer, or, payload. + * false: this is a whole header, trailer, or, payload. + */ + bool isFragment; + /* uid of header or trailer. valid only if isPayload is false. + */ + uint32_t uid; + /* size of item. If fragment, size of fragment. Otherwise, + * size of original item. + */ + uint32_t currentSize; + /* how many bytes were trimed from the start of a fragment. + * if isFragment is true, this field is zero. + */ + uint32_t currentTrimedFromStart; + /* how many bytes were trimed from the end of a fragment. + * if isFragment is true, this field is zero. + */ + uint32_t currentTrimedFromEnd; + /* an iterator which can be fed to Deserialize. Valid only + * if isFragment and isPayload are false. + */ + Buffer::Iterator current; + }; + class ItemIterator + { + public: + ItemIterator (const PacketMetadata *metadata, Buffer buffer); + bool HasNext (void) const; + Item Next (void); + private: + const PacketMetadata *m_metadata; + Buffer m_buffer; + uint16_t m_current; + uint32_t m_offset; + bool m_hasReadTail; + }; + static void Enable (void); static void SetOptOne (bool optOne); @@ -100,14 +146,14 @@ public: uint32_t GetUid (void) const; - void Print (std::ostream &os, Buffer buffer, PacketPrinter const &printer) const; - uint32_t GetSerializedSize (void) const; void Serialize (Buffer::Iterator i, uint32_t size) const; uint32_t Deserialize (Buffer::Iterator i); static void PrintStats (void); + ItemIterator BeginItem (Buffer buffer) const; + private: struct Data { /* number of references to this struct Data instance. */ @@ -192,6 +238,7 @@ private: }; friend DataFreeList::~DataFreeList (); + friend class ItemIterator; PacketMetadata (); void DoAddHeader (uint32_t uid, uint32_t size); @@ -219,10 +266,6 @@ private: void AppendValueExtra (uint32_t value, uint8_t *buffer); inline void Reserve (uint32_t n); void ReserveCopy (uint32_t n); - uint32_t DoPrint (const struct PacketMetadata::SmallItem *item, - const struct PacketMetadata::ExtraItem *extraItem, - Buffer data, uint32_t offset, const PacketPrinter &printer, - std::ostream &os) const; uint32_t GetTotalSize (void) const; uint32_t ReadItems (uint16_t current, struct PacketMetadata::SmallItem *item, diff --git a/src/common/packet.cc b/src/common/packet.cc index 4636bdd9c..77539642b 100644 --- a/src/common/packet.cc +++ b/src/common/packet.cc @@ -187,13 +187,13 @@ Packet::PrintTags (std::ostream &os) const void Packet::Print (std::ostream &os) const { - m_metadata.Print (os, m_buffer, PacketPrinter ()); + //XXX } -void -Packet::Print (std::ostream &os, const PacketPrinter &printer) const +PacketMetadata::ItemIterator +Packet::BeginItem (void) const { - m_metadata.Print (os, m_buffer, printer); + return m_metadata.BeginItem (m_buffer); } void diff --git a/src/common/packet.h b/src/common/packet.h index e13b946c1..0bc5d5483 100644 --- a/src/common/packet.h +++ b/src/common/packet.h @@ -34,8 +34,6 @@ namespace ns3 { -class PacketPrinter; - /** * \brief network packets * @@ -287,20 +285,8 @@ public: * Trailer::DoPrint methods. */ void Print (std::ostream &os) const; - /** - * \param os output stream in which the data should be printed. - * \param printer the output formatter to use to print - * the content of this packet. - * - * Iterate over the headers and trailers present in this packet, - * either in the "forward" (first header to last trailer) or in - * the "backward" (last trailer to first header) direction, as - * specified by the PacketPrinter::PrintForward or the - * PacketPrinter::PrintBackward methods. For each header, trailer, - * or fragment of a header or a trailer, invoke the user-specified - * print callback stored in the specified PacketPrinter. - */ - void Print (std::ostream &os, const PacketPrinter &printer) const; + + PacketMetadata::ItemIterator BeginItem (void) const; /** * By default, packets do not keep around enough metadata to diff --git a/src/common/wscript b/src/common/wscript index b25aff575..16527fd99 100644 --- a/src/common/wscript +++ b/src/common/wscript @@ -5,7 +5,6 @@ def build(bld): common.source = [ 'buffer.cc', 'chunk-registry.cc', - 'packet-printer.cc', 'packet-metadata.cc', 'packet-metadata-test.cc', 'packet.cc', @@ -27,7 +26,6 @@ def build(bld): 'tag-registry.h', 'tag.h', 'packet.h', - 'packet-printer.h', 'packet-metadata.h', 'pcap-writer.h', 'data-rate.h', From bcf92901acb2d7ba759605f9bb2febefc45a28b6 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 11:47:50 -0700 Subject: [PATCH 11/35] really kill dead code. --- src/common/packet-metadata-test.cc | 1 - src/common/packet-printer.cc | 135 --------------------- src/common/packet-printer.h | 181 ----------------------------- src/common/packet.cc | 1 - 4 files changed, 318 deletions(-) delete mode 100644 src/common/packet-printer.cc delete mode 100644 src/common/packet-printer.h diff --git a/src/common/packet-metadata-test.cc b/src/common/packet-metadata-test.cc index 45d7ab5a1..b117cc317 100644 --- a/src/common/packet-metadata-test.cc +++ b/src/common/packet-metadata-test.cc @@ -27,7 +27,6 @@ #include "trailer.h" #include "packet.h" #include "packet-metadata.h" -#include "packet-printer.h" namespace ns3 { diff --git a/src/common/packet-printer.cc b/src/common/packet-printer.cc deleted file mode 100644 index bf879b813..000000000 --- a/src/common/packet-printer.cc +++ /dev/null @@ -1,135 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 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 "packet-printer.h" -#include "chunk-registry.h" - -namespace ns3 { - -PacketPrinter::PacketPrinter () - : m_forward (true), - m_separator (" ") -{} - -void -PacketPrinter::PrintForward (void) -{ - m_forward = true; -} -void -PacketPrinter::PrintBackward (void) -{ - m_forward = false; -} -void -PacketPrinter::SetSeparator (std::string separator) -{ - m_separator = separator; -} -void -PacketPrinter::SetPayloadPrinter (PayloadPrinter printer) -{ - m_payloadPrinter = printer; -} - -void -PacketPrinter::PrintChunk (uint32_t chunkUid, - Buffer::Iterator start, - std::ostream &os, - uint32_t packetUid, - uint32_t size) const -{ - uint8_t *instance = ChunkRegistry::GetStaticInstance (chunkUid); - ChunkRegistry::Deserialize (chunkUid, instance, start); - - for (PrinterList::const_iterator i = m_printerList.begin (); i != m_printerList.end (); i++) - { - if (i->m_chunkUid == chunkUid) - { - ChunkRegistry::InvokePrintCallback (chunkUid, instance, os, packetUid, size, i->m_printer); - return; - } - } - // if the over did not override this type of chunk, - // we print something by default. - std::string name = ChunkRegistry::GetName (chunkUid, instance); - os << name << " "; - ChunkRegistry::Print (chunkUid, instance, os); -} -void -PacketPrinter::PrintChunkFragment (uint32_t chunkUid, - std::ostream &os, - uint32_t packetUid, - uint32_t size, - uint32_t fragmentStart, - uint32_t fragmentEnd) const -{ - uint8_t *instance = ChunkRegistry::GetStaticInstance (chunkUid); - std::string name = ChunkRegistry::GetName (chunkUid, instance); - struct PacketPrinter::FragmentInformation info; - info.start = fragmentStart; - info.end = fragmentEnd; - for (PrinterList::const_iterator i = m_printerList.begin (); i != m_printerList.end (); i++) - { - if (i->m_chunkUid == chunkUid) - { - - i->m_fragmentPrinter (os, packetUid, size, name, info); - return; - } - } - // if the user did not override this type of chunk, - // we print something by default. - NS_ASSERT (info.start != 0 || info.end != 0); - os << name << " " - << "(" - << "length " << size - (info.end + info.start) << " " - << "trim_start " << info.start << " " - << "trim_end " << info.end - << ")"; -} -void -PacketPrinter::PrintPayload (std::ostream &os, uint32_t packetUid, uint32_t size, - uint32_t fragmentStart, uint32_t fragmentEnd) const -{ - struct PacketPrinter::FragmentInformation info; - info.start = fragmentStart; - info.end = fragmentEnd; - if (!m_payloadPrinter.IsNull ()) - { - m_payloadPrinter (os, packetUid, size, info); - return; - } - // if the user did not override the payload printer, - // we print something anyway. - os << "DATA (" - << "length " << size - (info.end + info.start); - if (info.start != 0 || info.end != 0) - { - os << " " - << "trim_start " << info.start << " " - << "trim_end " << info.end; - } - os << ")"; - -} - -} // namespace ns3 diff --git a/src/common/packet-printer.h b/src/common/packet-printer.h deleted file mode 100644 index ba789a9fe..000000000 --- a/src/common/packet-printer.h +++ /dev/null @@ -1,181 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 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 PACKET_PRINTER_H -#define PACKET_PRINTER_H - -#include "ns3/callback.h" -#include "ns3/ptr.h" -#include "buffer.h" -#include - -namespace ns3 { - -/** - * \brief hold a list of print callbacks for packet headers and trailers - * - * Users can register in instances of this class print callbacks - * which are used by Packet::Print to print the content of a packet. - */ -class PacketPrinter -{ -public: - /** - * \brief indicates how many bytes were trimmed from a header - * or a trailer. - */ - struct FragmentInformation - { - /** - * The number of bytes trimmed from the start of the header or the trailer. - */ - uint32_t start; - /** - * The number of bytes trimmed from the end of the header or the trailer. - */ - uint32_t end; - }; - /** - * \brief callback to print payload. - * - * Arguments: output stream, packet uid, size, fragment information - */ - typedef Callback - PayloadPrinter; - - /** - * \brief callback to print fragmented chunks. - * - * Arguments: output stream, packet uid, size, header/trailer name, fragment information - */ - typedef Callback - ChunkFragmentPrinter; - - /** - * \brief callback to print chunks for which no specific callback was specified. - * - * Arguments: output stream, packet uid, size, header/trailer name, fragment information - */ - typedef Callback - DefaultPrinter; - - PacketPrinter (); - - /** - * Print the content of the packet forward. - */ - void PrintForward (void); - /** - * Print the content of the packet backward. - */ - void PrintBackward (void); - /** - * \param separator the new separator - * - * The default separator is a single space character. - */ - void SetSeparator (std::string separator); - /** - * \param printer printer for payload - */ - void SetPayloadPrinter (PayloadPrinter printer); - /** - * \param printer printer for the specified chunk - * \param fragmentPrinter printer for a fragment of the specified chunk - * - * If the user does not force a user-specific printing function through - * a call to SetHeaderPrinter, the default print output is generated. - */ - template - void SetHeaderPrinter (Callback printer, - ChunkFragmentPrinter fragmentPrinter); - /** - * \param printer printer for the specified chunk - * \param fragmentPrinter printer for a fragment of the specified chunk - * - * If the user does not force a user-specific printing function through - * a call to SetTrailerPrinter, the default print output is generated. - */ - template - void SetTrailerPrinter (Callback printer, - ChunkFragmentPrinter fragmentPrinter); -private: - friend class PacketMetadata; - void PrintChunk (uint32_t uid, - Buffer::Iterator i, - std::ostream &os, - uint32_t packetUid, - uint32_t size) const; - void PrintChunkFragment (uint32_t uid, - std::ostream &os, - uint32_t packetUid, - uint32_t size, - uint32_t fragmentStart, - uint32_t fragmentEnd) const; - void PrintPayload (std::ostream &os, uint32_t packetUid, uint32_t size, - uint32_t fragmentStart, uint32_t fragmentEnd) const; - struct Printer - { - uint32_t m_chunkUid; - CallbackBase m_printer; - Callback m_fragmentPrinter; - }; - typedef std::vector PrinterList; - - PrinterList m_printerList; - PayloadPrinter m_payloadPrinter; - DefaultPrinter m_defaultPrinter; - bool m_forward; - std::string m_separator; -}; - -} // namespace ns3 - -namespace ns3 { - -template -void -PacketPrinter::SetHeaderPrinter (Callback printer, - ChunkFragmentPrinter fragmentPrinter) -{ - Printer p; - p.m_chunkUid = T::GetUid (); - p.m_printer = printer; - p.m_fragmentPrinter = fragmentPrinter; - m_printerList.push_back (p); -} - -template -void -PacketPrinter::SetTrailerPrinter (Callback printer, - ChunkFragmentPrinter fragmentPrinter) -{ - Printer p; - p.m_chunkUid = T::GetUid (); - p.m_printer = printer; - p.m_fragmentPrinter = fragmentPrinter; - m_printerList.push_back (p); -} - - -} // namespace ns3 - -#endif /* PACKET_PRINTER_H */ diff --git a/src/common/packet.cc b/src/common/packet.cc index 77539642b..22bca8349 100644 --- a/src/common/packet.cc +++ b/src/common/packet.cc @@ -19,7 +19,6 @@ * Author: Mathieu Lacage */ #include "packet.h" -#include "packet-printer.h" #include "ns3/assert.h" namespace ns3 { From b5bfc150e3dcd5a7b29f7e8f17c77f20e016820d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 11:55:46 -0700 Subject: [PATCH 12/35] add TypeId::IsChildOf --- src/core/type-id.cc | 10 ++++++++++ src/core/type-id.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/core/type-id.cc b/src/core/type-id.cc index 61a443edb..562cd7513 100644 --- a/src/core/type-id.cc +++ b/src/core/type-id.cc @@ -475,6 +475,16 @@ TypeId::GetParent (void) const uint16_t parent = Singleton::Get ()->GetParent (m_tid); return TypeId (parent); } +bool +TypeId::IsChildOf (TypeId other) const +{ + TypeId tmp = *this; + while (tmp != other && tmp != tmp) + { + tmp = tmp.GetParent (); + } + return tmp == other; +} std::string TypeId::GetGroupName (void) const { diff --git a/src/core/type-id.h b/src/core/type-id.h index 4a4ffe8da..ab649e36c 100644 --- a/src/core/type-id.h +++ b/src/core/type-id.h @@ -90,6 +90,8 @@ public: */ TypeId GetParent (void) const; + bool IsChildOf (TypeId other) const; + /** * \returns the name of the group associated to this TypeId. */ From de502593ef8db0306373c99a9cdb9ad094390936 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 12:12:17 -0700 Subject: [PATCH 13/35] make the base Header and Trailer classes virtual. --- src/common/header.cc | 8 ++++ src/common/header.h | 53 +++++++++++++++++--------- src/common/packet-metadata-test.cc | 12 +++--- src/common/trailer.cc | 8 ++++ src/common/trailer.h | 60 ++++++++++++++++++++---------- src/common/wscript | 2 + 6 files changed, 99 insertions(+), 44 deletions(-) create mode 100644 src/common/header.cc create mode 100644 src/common/trailer.cc diff --git a/src/common/header.cc b/src/common/header.cc new file mode 100644 index 000000000..9633d345b --- /dev/null +++ b/src/common/header.cc @@ -0,0 +1,8 @@ +#include "header.h" + +namespace ns3 { + +Header::~Header () +{} + +} // namespace ns3 diff --git a/src/common/header.h b/src/common/header.h index c983dd18d..9d849e8f8 100644 --- a/src/common/header.h +++ b/src/common/header.h @@ -59,23 +59,6 @@ namespace ns3 { * - a static method named GetUid: is used to uniquely identify * the type of each header. This method shall return a unique * integer allocated with Header::AllocateUid. - * - a method named Serialize: is used by Packet::AddHeader to - * store a header into the byte buffer of a packet. - * The input iterator points to the start of the byte buffer in - * which the header should write its data. The data written - * is expected to match bit-for-bit the representation of this - * header in a real network. - * - a method named GetSerializedSize: is used by Packet::AddHeader - * to store a header into the byte buffer of a packet. This method - * should return the number of bytes which are needed to store - * the full header data by Serialize. - * - a method named Deserialize: is used by Packet::RemoveHeader to - * re-create a header from the byte buffer of a packet. The input - * iterator points to the start of the byte buffer from which - * the header should read its data. The data read is expected to - * match bit-for-bit the representation of this header in real - * networks. This method shall return an integer which identifies - * the number of bytes read. * - a method named Print: is used by Packet::Print to print the * content of a header as ascii data to a c++ output stream. * Although the header is free to format its output as it @@ -92,8 +75,42 @@ namespace ns3 { * Sample code which shows how to create a new type of Header, and how to use it, * is shown in the sample file samples/main-packet-header.cc */ -class Header +class Header { +public: + virtual ~Header (); + /** + * \returns the expected size of the header. + * + * This method is used by Packet::AddHeader + * to store a header into the byte buffer of a packet. This method + * should return the number of bytes which are needed to store + * the full header data by Serialize. + */ + virtual uint32_t GetSerializedSize (void) const = 0; + /** + * \param start an iterator which points to where the header should + * be written. + * + * This method is used by Packet::AddHeader to + * store a header into the byte buffer of a packet. + * The data written + * is expected to match bit-for-bit the representation of this + * header in a real network. + */ + virtual void Serialize (Buffer::Iterator start) const = 0; + /** + * \param start an iterator which points to where the header should + * written. + * \returns the number of bytes read. + * + * This method is used by Packet::RemoveHeader to + * re-create a header from the byte buffer of a packet. + * The data read is expected to + * match bit-for-bit the representation of this header in real + * networks. + */ + virtual uint32_t Deserialize (Buffer::Iterator start) = 0; protected: template static uint32_t AllocateUid (std::string uuid); diff --git a/src/common/packet-metadata-test.cc b/src/common/packet-metadata-test.cc index b117cc317..fa69eb8d1 100644 --- a/src/common/packet-metadata-test.cc +++ b/src/common/packet-metadata-test.cc @@ -39,9 +39,9 @@ public: bool IsOk (void) const; std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: bool m_ok; }; @@ -119,9 +119,9 @@ public: bool IsOk (void) const; std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: bool m_ok; }; diff --git a/src/common/trailer.cc b/src/common/trailer.cc new file mode 100644 index 000000000..9422b2ec1 --- /dev/null +++ b/src/common/trailer.cc @@ -0,0 +1,8 @@ +#include "trailer.h" + +namespace ns3 { + +Trailer::~Trailer () +{} + +} // namespace ns3 diff --git a/src/common/trailer.h b/src/common/trailer.h index 5311d8910..92225eba6 100644 --- a/src/common/trailer.h +++ b/src/common/trailer.h @@ -23,6 +23,8 @@ #define TRAILER_H #include "chunk-registry.h" +#include "buffer.h" +#include /** * \relates ns3::Trailer @@ -58,26 +60,6 @@ namespace ns3 { * - a static method named GetUid: is used to uniquely identify * the type of each trailer. This method shall return a unique * integer allocated with Trailer::AllocateUid. - * - a method named Serialize: is used by Packet::AddTrailer to - * store a trailer into the byte buffer of a packet. - * The input iterator points to the end of the byte buffer in - * which the trailer should write its data: the user is thus - * required to call Buffer::Iterator::Prev prior to writing - * any data in the buffer. The data written is expected to - * match bit-for-bit the representation of this trailer in a - * real network. - * - a method named GetSerializedSize: is used by Packet::AddTrailer - * to store a trailer into the byte buffer of a packet. This method - * should return the number of bytes which are needed to store - * the full trailer data by Serialize. - * - a method named Deserialize: is used by Packet::RemoveTrailer to - * re-create a trailer from the byte buffer of a packet. The input - * iterator points to the end of the byte buffer from which - * the trailer should read its data: the user is thus required to - * call Buffer::Iterator::Prev prior to reading any data from the - * buffer. The data read is expected to match bit-for-bit the - * representation of this trailer in real networks. This method - * shall return an integer which identifies the number of bytes read. * - a method named Print: is used by Packet::Print to print the * content of a trailer as ascii data to a c++ output stream. * Although the trailer is free to format its output as it @@ -94,6 +76,44 @@ namespace ns3 { */ class Trailer { +public: + virtual ~Trailer (); + /** + * \returns the expected size of the trailer. + * + * This method is used by Packet::AddTrailer + * to store a trailer into the byte buffer of a packet. This method + * should return the number of bytes which are needed to store + * the full trailer data by Serialize. + */ + virtual uint32_t GetSerializedSize (void) const = 0; + /** + * \param start an iterator which points to where the trailer + * should be written. + * + * This method is used by Packet::AddTrailer to + * store a header into the byte buffer of a packet. + * The data written is expected to match bit-for-bit the + * representation of this trailer in real networks. + * The input iterator points to the end of the area where the + * data shall be written. This method is thus expected to call + * Buffer::Iterator::Prev prior to actually writing any data. + */ + virtual void Serialize (Buffer::Iterator start) const = 0; + /** + * \param start an iterator which points to where the trailer + * should be read. + * \returns the number of bytes read. + * + * This method is used by Packet::RemoveTrailer to + * re-create a trailer from the byte buffer of a packet. + * The data read is expected to match bit-for-bit the + * representation of this trailer in real networks. + * The input iterator points to the end of the area where the + * data shall be written. This method is thus expected to call + * Buffer::Iterator::Prev prio to actually reading any data. + */ + virtual uint32_t Deserialize (Buffer::Iterator end) = 0; protected: template static uint32_t AllocateUid (std::string uidString); diff --git a/src/common/wscript b/src/common/wscript index 16527fd99..1de57f8ac 100644 --- a/src/common/wscript +++ b/src/common/wscript @@ -10,6 +10,8 @@ def build(bld): 'packet.cc', 'tags.cc', 'tag-registry.cc', + 'header.cc', + 'trailer.cc', 'pcap-writer.cc', 'data-rate.cc', 'error-model.cc', From 5d4ecfc62cd13bcb2f622c7f4b3010af9096c884 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 13:12:17 -0700 Subject: [PATCH 14/35] define a TypeId for each Header/Trailer. --- samples/main-packet-header.cc | 22 +++++++-- src/common/header.cc | 11 +++++ src/common/header.h | 4 +- src/common/packet-metadata-test.cc | 43 ++++++++++++++++++ src/common/trailer.cc | 11 +++++ src/common/trailer.h | 4 +- src/devices/wifi/mgt-headers.cc | 68 ++++++++++++++++++++++++++++ src/devices/wifi/mgt-headers.h | 32 ++++++++----- src/devices/wifi/wifi-mac-header.cc | 15 ++++++ src/devices/wifi/wifi-mac-header.h | 10 ++-- src/devices/wifi/wifi-mac-trailer.cc | 16 +++++++ src/devices/wifi/wifi-mac-trailer.h | 8 ++-- src/internet-node/arp-header.cc | 15 ++++++ src/internet-node/arp-header.h | 2 + src/internet-node/ipv4-header.cc | 15 ++++++ src/internet-node/ipv4-header.h | 8 ++-- src/internet-node/tcp-header.cc | 15 ++++++ src/internet-node/tcp-header.h | 11 +++-- src/internet-node/udp-header.cc | 15 ++++++ src/internet-node/udp-header.h | 8 ++-- src/node/ethernet-header.cc | 15 ++++++ src/node/ethernet-header.h | 8 ++-- src/node/ethernet-trailer.cc | 15 ++++++ src/node/ethernet-trailer.h | 2 + src/node/llc-snap-header.cc | 15 ++++++ src/node/llc-snap-header.h | 8 ++-- src/routing/olsr/olsr-header.cc | 33 +++++++++++++- src/routing/olsr/olsr-header.h | 4 ++ utils/bench-packets.cc | 27 +++++++++-- 29 files changed, 416 insertions(+), 44 deletions(-) diff --git a/samples/main-packet-header.cc b/samples/main-packet-header.cc index 904b28800..a93c8cb87 100644 --- a/samples/main-packet-header.cc +++ b/samples/main-packet-header.cc @@ -19,11 +19,13 @@ public: void SetData (uint16_t data); uint16_t GetData (void) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; std::string GetName (void) const; void Print (std::ostream &os) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); - uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; private: uint16_t m_data; }; @@ -36,6 +38,20 @@ MyHeader::MyHeader () MyHeader::~MyHeader () {} +TypeId +MyHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::MyHeader") + .SetParent
() + ; + return tid; +} +TypeId +MyHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + uint32_t MyHeader::GetUid (void) { diff --git a/src/common/header.cc b/src/common/header.cc index 9633d345b..ef6fac914 100644 --- a/src/common/header.cc +++ b/src/common/header.cc @@ -2,7 +2,18 @@ namespace ns3 { +NS_OBJECT_ENSURE_REGISTERED (Header); + Header::~Header () {} +TypeId +Header::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::Header") + .SetParent () + ; + return tid; +} + } // namespace ns3 diff --git a/src/common/header.h b/src/common/header.h index 9d849e8f8..3d432646e 100644 --- a/src/common/header.h +++ b/src/common/header.h @@ -23,6 +23,7 @@ #define HEADER_H #include "chunk-registry.h" +#include "ns3/object-base.h" /** * \relates ns3::Header @@ -75,9 +76,10 @@ namespace ns3 { * Sample code which shows how to create a new type of Header, and how to use it, * is shown in the sample file samples/main-packet-header.cc */ -class Header +class Header : public ObjectBase { public: + static TypeId GetTypeId (void); virtual ~Header (); /** * \returns the expected size of the header. diff --git a/src/common/packet-metadata-test.cc b/src/common/packet-metadata-test.cc index fa69eb8d1..aabe3de39 100644 --- a/src/common/packet-metadata-test.cc +++ b/src/common/packet-metadata-test.cc @@ -34,6 +34,8 @@ template class HistoryHeader : public Header { public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); HistoryHeader (); bool IsOk (void) const; @@ -46,6 +48,25 @@ private: bool m_ok; }; +template +TypeId +HistoryHeader::GetTypeId (void) +{ + std::ostringstream oss; + oss << "ns3::HistoryHeader<"<"; + static TypeId tid = TypeId (oss.str ().c_str ()) + .SetParent
() + ; + return tid; +} + +template +TypeId +HistoryHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + template uint32_t HistoryHeader::GetUid (void) @@ -114,6 +135,8 @@ template class HistoryTrailer : public Trailer { public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); HistoryTrailer (); bool IsOk (void) const; @@ -126,6 +149,26 @@ private: bool m_ok; }; + +template +TypeId +HistoryTrailer::GetTypeId (void) +{ + std::ostringstream oss; + oss << "ns3::HistoryTrailer<"<"; + static TypeId tid = TypeId (oss.str ().c_str ()) + .SetParent () + ; + return tid; +} + +template +TypeId +HistoryTrailer::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + template uint32_t HistoryTrailer::GetUid (void) diff --git a/src/common/trailer.cc b/src/common/trailer.cc index 9422b2ec1..389e3d34b 100644 --- a/src/common/trailer.cc +++ b/src/common/trailer.cc @@ -2,7 +2,18 @@ namespace ns3 { +NS_OBJECT_ENSURE_REGISTERED (Trailer); + Trailer::~Trailer () {} +TypeId +Trailer::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::Trailer") + .SetParent () + ; + return tid; +} + } // namespace ns3 diff --git a/src/common/trailer.h b/src/common/trailer.h index 92225eba6..dd77e684b 100644 --- a/src/common/trailer.h +++ b/src/common/trailer.h @@ -22,6 +22,7 @@ #ifndef TRAILER_H #define TRAILER_H +#include "ns3/object-base.h" #include "chunk-registry.h" #include "buffer.h" #include @@ -74,9 +75,10 @@ namespace ns3 { * single word as all capitalized letters. * */ -class Trailer +class Trailer : public ObjectBase { public: + static TypeId GetTypeId (void); virtual ~Trailer (); /** * \returns the expected size of the trailer. diff --git a/src/devices/wifi/mgt-headers.cc b/src/devices/wifi/mgt-headers.cc index 864c35ff8..e3c7aeac0 100644 --- a/src/devices/wifi/mgt-headers.cc +++ b/src/devices/wifi/mgt-headers.cc @@ -27,6 +27,8 @@ namespace ns3 { * Probe Request ***********************************************************/ +NS_OBJECT_ENSURE_REGISTERED (MgtProbeRequestHeader); + MgtProbeRequestHeader::~MgtProbeRequestHeader () {} @@ -59,6 +61,19 @@ MgtProbeRequestHeader::GetSerializedSize (void) const size += m_rates.GetSerializedSize (); return size; } +TypeId +MgtProbeRequestHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::MgtProbeRequestHeader") + .SetParent
() + ; + return tid; +} +TypeId +MgtProbeRequestHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} uint32_t MgtProbeRequestHeader::GetUid (void) { @@ -97,6 +112,8 @@ MgtProbeRequestHeader::Deserialize (Buffer::Iterator start) * Probe Response ***********************************************************/ +NS_OBJECT_ENSURE_REGISTERED (MgtProbeResponseHeader); + MgtProbeResponseHeader::MgtProbeResponseHeader () {} MgtProbeResponseHeader::~MgtProbeResponseHeader () @@ -133,6 +150,19 @@ MgtProbeResponseHeader::SetSupportedRates (SupportedRates rates) { m_rates = rates; } +TypeId +MgtProbeResponseHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::MgtProbeResponseHeader") + .SetParent
() + ; + return tid; +} +TypeId +MgtProbeResponseHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} uint32_t MgtProbeResponseHeader::GetUid (void) { @@ -198,6 +228,11 @@ MgtProbeResponseHeader::Deserialize (Buffer::Iterator start) return i.GetDistanceFrom (start); } +/*********************************************************** + * Assoc Request + ***********************************************************/ + +NS_OBJECT_ENSURE_REGISTERED (MgtAssocRequestHeader); MgtAssocRequestHeader::MgtAssocRequestHeader () {} @@ -234,6 +269,20 @@ MgtAssocRequestHeader::GetListenInterval (void) const { return m_listenInterval; } + +TypeId +MgtAssocRequestHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::MgtAssocRequestHeader") + .SetParent
() + ; + return tid; +} +TypeId +MgtAssocRequestHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} uint32_t MgtAssocRequestHeader::GetUid (void) { @@ -281,6 +330,12 @@ MgtAssocRequestHeader::Deserialize (Buffer::Iterator start) return i.GetDistanceFrom (start); } +/*********************************************************** + * Assoc Response + ***********************************************************/ + +NS_OBJECT_ENSURE_REGISTERED (MgtAssocResponseHeader); + MgtAssocResponseHeader::MgtAssocResponseHeader () {} MgtAssocResponseHeader::~MgtAssocResponseHeader () @@ -307,6 +362,19 @@ MgtAssocResponseHeader::SetSupportedRates (SupportedRates rates) m_rates = rates; } +TypeId +MgtAssocResponseHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::MgtAssocResponseHeader") + .SetParent
() + ; + return tid; +} +TypeId +MgtAssocResponseHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} uint32_t MgtAssocResponseHeader::GetUid (void) { diff --git a/src/devices/wifi/mgt-headers.h b/src/devices/wifi/mgt-headers.h index 457ab1e03..67e8a315a 100644 --- a/src/devices/wifi/mgt-headers.h +++ b/src/devices/wifi/mgt-headers.h @@ -44,12 +44,14 @@ public: SupportedRates GetSupportedRates (void) const; uint16_t GetListenInterval (void) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: Ssid m_ssid; @@ -69,12 +71,14 @@ public: void SetSupportedRates (SupportedRates rates); void SetStatusCode (StatusCode code); + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: SupportedRates m_rates; @@ -92,12 +96,14 @@ public: Ssid GetSsid (void) const; SupportedRates GetSupportedRates (void) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: Ssid m_ssid; @@ -117,12 +123,14 @@ public: void SetBeaconIntervalUs (uint64_t us); void SetSupportedRates (SupportedRates rates); + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: Ssid m_ssid; diff --git a/src/devices/wifi/wifi-mac-header.cc b/src/devices/wifi/wifi-mac-header.cc index ed21375cf..e8e8324f8 100644 --- a/src/devices/wifi/wifi-mac-header.cc +++ b/src/devices/wifi/wifi-mac-header.cc @@ -34,6 +34,8 @@ std::Cout << "MAC80211HEADER " << x << std::Endl; namespace ns3 { +NS_OBJECT_ENSURE_REGISTERED (WifiMacHeader); + enum { TYPE_MGT = 0, TYPE_CTL = 1, @@ -766,6 +768,19 @@ case WIFI_MAC_ ## x: \ return "BIG_ERROR"; } +TypeId +WifiMacHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::WifiMacHeader") + .SetParent
() + ; + return tid; +} +TypeId +WifiMacHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} uint32_t WifiMacHeader::GetUid (void) { diff --git a/src/devices/wifi/wifi-mac-header.h b/src/devices/wifi/wifi-mac-header.h index c7123b01b..869a48628 100644 --- a/src/devices/wifi/wifi-mac-header.h +++ b/src/devices/wifi/wifi-mac-header.h @@ -65,14 +65,18 @@ enum WifiMacType_e { class WifiMacHeader : public Header { public: + WifiMacHeader (); ~WifiMacHeader (); + + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); void SetAssocReq (void); diff --git a/src/devices/wifi/wifi-mac-trailer.cc b/src/devices/wifi/wifi-mac-trailer.cc index 03853af2b..d083e0509 100644 --- a/src/devices/wifi/wifi-mac-trailer.cc +++ b/src/devices/wifi/wifi-mac-trailer.cc @@ -22,12 +22,28 @@ namespace ns3 { +NS_OBJECT_ENSURE_REGISTERED (WifiMacTrailer); + WifiMacTrailer::WifiMacTrailer () {} WifiMacTrailer::~WifiMacTrailer () {} +TypeId +WifiMacTrailer::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::WifiMacTrailer") + .SetParent () + ; + return tid; +} +TypeId +WifiMacTrailer::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + uint32_t WifiMacTrailer::GetUid (void) { diff --git a/src/devices/wifi/wifi-mac-trailer.h b/src/devices/wifi/wifi-mac-trailer.h index c2f366d5d..c295025c7 100644 --- a/src/devices/wifi/wifi-mac-trailer.h +++ b/src/devices/wifi/wifi-mac-trailer.h @@ -31,12 +31,14 @@ public: WifiMacTrailer (); ~WifiMacTrailer (); + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); }; } // namespace ns3 diff --git a/src/internet-node/arp-header.cc b/src/internet-node/arp-header.cc index 4fdfe19d7..3e114716c 100644 --- a/src/internet-node/arp-header.cc +++ b/src/internet-node/arp-header.cc @@ -26,6 +26,21 @@ namespace ns3 { NS_HEADER_ENSURE_REGISTERED (ArpHeader); +NS_OBJECT_ENSURE_REGISTERED (ArpHeader); + +TypeId +ArpHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::ArpHeader") + .SetParent
() + ; + return tid; +} +TypeId +ArpHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} uint32_t ArpHeader::GetUid (void) diff --git a/src/internet-node/arp-header.h b/src/internet-node/arp-header.h index 509530991..7fafefcef 100644 --- a/src/internet-node/arp-header.h +++ b/src/internet-node/arp-header.h @@ -34,6 +34,8 @@ namespace ns3 { class ArpHeader : public Header { public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); void SetRequest (Address sourceHardwareAddress, diff --git a/src/internet-node/ipv4-header.cc b/src/internet-node/ipv4-header.cc index 2fd8fc059..052be7c5a 100644 --- a/src/internet-node/ipv4-header.cc +++ b/src/internet-node/ipv4-header.cc @@ -29,9 +29,24 @@ NS_LOG_COMPONENT_DEFINE ("Ipv4Header"); namespace ns3 { NS_HEADER_ENSURE_REGISTERED (Ipv4Header); +NS_OBJECT_ENSURE_REGISTERED (Ipv4Header); bool Ipv4Header::m_calcChecksum = false; +TypeId +Ipv4Header::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::Ipv4Header") + .SetParent
() + ; + return tid; +} +TypeId +Ipv4Header::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + uint32_t Ipv4Header::GetUid (void) { diff --git a/src/internet-node/ipv4-header.h b/src/internet-node/ipv4-header.h index fe9317d7c..8375dd300 100644 --- a/src/internet-node/ipv4-header.h +++ b/src/internet-node/ipv4-header.h @@ -32,6 +32,8 @@ namespace ns3 { class Ipv4Header : public Header { public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); /** * \brief Construct a null IPv4 header @@ -142,9 +144,9 @@ public: std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: enum FlagsE { diff --git a/src/internet-node/tcp-header.cc b/src/internet-node/tcp-header.cc index f5920c355..dde46f36c 100644 --- a/src/internet-node/tcp-header.cc +++ b/src/internet-node/tcp-header.cc @@ -27,9 +27,24 @@ namespace ns3 { NS_HEADER_ENSURE_REGISTERED (TcpHeader); +NS_OBJECT_ENSURE_REGISTERED (TcpHeader); bool TcpHeader::m_calcChecksum = false; +TypeId +TcpHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::TcpHeader") + .SetParent
() + ; + return tid; +} +TypeId +TcpHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + uint32_t TcpHeader::GetUid (void) { diff --git a/src/internet-node/tcp-header.h b/src/internet-node/tcp-header.h index 1bdeb2fc5..2dbda5634 100644 --- a/src/internet-node/tcp-header.h +++ b/src/internet-node/tcp-header.h @@ -30,8 +30,11 @@ namespace ns3 { -class TcpHeader : public Header { +class TcpHeader : public Header +{ public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); TcpHeader (); @@ -138,9 +141,9 @@ public: std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: uint16_t m_sourcePort; diff --git a/src/internet-node/udp-header.cc b/src/internet-node/udp-header.cc index 96b57beba..d8b3594c7 100644 --- a/src/internet-node/udp-header.cc +++ b/src/internet-node/udp-header.cc @@ -25,9 +25,24 @@ namespace ns3 { NS_HEADER_ENSURE_REGISTERED (UdpHeader); +NS_OBJECT_ENSURE_REGISTERED (UdpHeader); bool UdpHeader::m_calcChecksum = false; +TypeId +UdpHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::UdpHeader") + .SetParent
() + ; + return tid; +} +TypeId +UdpHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + uint32_t UdpHeader::GetUid (void) { diff --git a/src/internet-node/udp-header.h b/src/internet-node/udp-header.h index 70503ef91..f55f42406 100644 --- a/src/internet-node/udp-header.h +++ b/src/internet-node/udp-header.h @@ -34,6 +34,8 @@ namespace ns3 { class UdpHeader : public Header { public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); /** @@ -86,9 +88,9 @@ public: std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: uint16_t m_sourcePort; diff --git a/src/node/ethernet-header.cc b/src/node/ethernet-header.cc index ec0d4a420..999dc9d08 100644 --- a/src/node/ethernet-header.cc +++ b/src/node/ethernet-header.cc @@ -32,6 +32,21 @@ NS_LOG_COMPONENT_DEFINE ("EthernetHeader"); namespace ns3 { NS_HEADER_ENSURE_REGISTERED (EthernetHeader); +NS_OBJECT_ENSURE_REGISTERED (EthernetHeader); + +TypeId +EthernetHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::EthernetHeader") + .SetParent
() + ; + return tid; +} +TypeId +EthernetHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} uint32_t EthernetHeader::GetUid (void) diff --git a/src/node/ethernet-header.h b/src/node/ethernet-header.h index f0c653722..189cb8e32 100644 --- a/src/node/ethernet-header.h +++ b/src/node/ethernet-header.h @@ -49,6 +49,8 @@ namespace ns3 { class EthernetHeader : public Header { public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); /** @@ -105,9 +107,9 @@ public: std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: static const int PREAMBLE_SIZE = 8; /// size of the preamble_sfd header field static const int LENGTH_SIZE = 2; /// size of the length_type header field diff --git a/src/node/ethernet-trailer.cc b/src/node/ethernet-trailer.cc index 055b21aee..6c94ed368 100644 --- a/src/node/ethernet-trailer.cc +++ b/src/node/ethernet-trailer.cc @@ -29,9 +29,24 @@ NS_LOG_COMPONENT_DEFINE ("EthernetTrailer"); namespace ns3 { NS_TRAILER_ENSURE_REGISTERED (EthernetTrailer); +NS_OBJECT_ENSURE_REGISTERED (EthernetTrailer); bool EthernetTrailer::m_calcFcs = false; +TypeId +EthernetTrailer::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::EthernetTrailer") + .SetParent () + ; + return tid; +} +TypeId +EthernetTrailer::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + uint32_t EthernetTrailer::GetUid (void) { diff --git a/src/node/ethernet-trailer.h b/src/node/ethernet-trailer.h index 5c574c6d8..694be0a70 100644 --- a/src/node/ethernet-trailer.h +++ b/src/node/ethernet-trailer.h @@ -39,6 +39,8 @@ class Packet; class EthernetTrailer : public Trailer { public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); /** diff --git a/src/node/llc-snap-header.cc b/src/node/llc-snap-header.cc index 5bcb9714b..9c801fbc0 100644 --- a/src/node/llc-snap-header.cc +++ b/src/node/llc-snap-header.cc @@ -26,6 +26,21 @@ namespace ns3 { NS_HEADER_ENSURE_REGISTERED (LlcSnapHeader); +NS_OBJECT_ENSURE_REGISTERED (LlcSnapHeader); + +TypeId +LlcSnapHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::LlcSnapHeader") + .SetParent
() + ; + return tid; +} +TypeId +LlcSnapHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} uint32_t LlcSnapHeader::GetUid (void) diff --git a/src/node/llc-snap-header.h b/src/node/llc-snap-header.h index cfc9a5d9c..32c20a484 100644 --- a/src/node/llc-snap-header.h +++ b/src/node/llc-snap-header.h @@ -31,6 +31,8 @@ namespace ns3 { class LlcSnapHeader : public Header { public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); LlcSnapHeader (); @@ -40,9 +42,9 @@ public: std::string GetName (void) const; void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: uint16_t m_etherType; }; diff --git a/src/routing/olsr/olsr-header.cc b/src/routing/olsr/olsr-header.cc index aacc3e440..eb488234a 100644 --- a/src/routing/olsr/olsr-header.cc +++ b/src/routing/olsr/olsr-header.cc @@ -33,7 +33,6 @@ namespace olsr { NS_LOG_COMPONENT_DEFINE("OlsrHeader"); - /// Scaling factor used in RFC 3626. #define OLSR_C 0.0625 @@ -95,12 +94,28 @@ EmfToSeconds (uint8_t olsrFormat) // ---------------- OLSR Packet ------------------------------- +NS_OBJECT_ENSURE_REGISTERED (PacketHeader); + PacketHeader::PacketHeader () {} PacketHeader::~PacketHeader () {} +TypeId +PacketHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::olsr::PacketHeader") + .SetParent
() + ; + return tid; +} +TypeId +PacketHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + uint32_t PacketHeader::GetUid (void) { @@ -141,6 +156,8 @@ PacketHeader::Deserialize (Buffer::Iterator start) // ---------------- OLSR Message ------------------------------- +NS_OBJECT_ENSURE_REGISTERED (MessageHeader); + MessageHeader::MessageHeader () : m_messageType (MessageHeader::MessageType (0)) {} @@ -148,6 +165,20 @@ MessageHeader::MessageHeader () MessageHeader::~MessageHeader () {} +TypeId +MessageHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::olsr::MessageHeader") + .SetParent
() + ; + return tid; +} +TypeId +MessageHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + uint32_t MessageHeader::GetUid (void) { diff --git a/src/routing/olsr/olsr-header.h b/src/routing/olsr/olsr-header.h index 36cb8e742..13583d16e 100644 --- a/src/routing/olsr/olsr-header.h +++ b/src/routing/olsr/olsr-header.h @@ -95,6 +95,8 @@ private: uint16_t m_packetSequenceNumber; public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; @@ -200,6 +202,8 @@ private: uint16_t m_messageSize; public: + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; diff --git a/utils/bench-packets.cc b/utils/bench-packets.cc index 5d4776ef9..920aa992b 100644 --- a/utils/bench-packets.cc +++ b/utils/bench-packets.cc @@ -33,13 +33,14 @@ public: BenchHeader (); bool IsOk (void) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; static uint32_t GetUid (void); - static std::string GetName (void); void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); private: bool m_ok; }; @@ -56,6 +57,24 @@ BenchHeader::IsOk (void) const return m_ok; } +template +TypeId +BenchHeader::GetTypeId (void) +{ + std::ostringstream oss; + oss << "ns3::BenchHeader<"<"; + static TypeId tid = TypeId (oss.str ().c_str ()) + .SetParent
() + ; + return tid; +} +template +TypeId +BenchHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + template uint32_t BenchHeader::GetUid (void) From f6438556ef4913205bed304445b3bd919fc5f2b3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 13:31:08 -0700 Subject: [PATCH 15/35] add TypeId::SetUid/GetUid --- src/core/type-id.cc | 11 +++++++++++ src/core/type-id.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/src/core/type-id.cc b/src/core/type-id.cc index 562cd7513..b2a59fe0b 100644 --- a/src/core/type-id.cc +++ b/src/core/type-id.cc @@ -664,6 +664,17 @@ TypeId::LookupTraceSourceByName (std::string name) const return 0; } +uint16_t +TypeId::GetUid (void) const +{ + return m_tid; +} +void +TypeId::SetUid (uint16_t tid) +{ + m_tid = tid; +} + std::ostream & operator << (std::ostream &os, TypeId tid) { os << tid.GetName (); diff --git a/src/core/type-id.h b/src/core/type-id.h index ab649e36c..3207ff1c0 100644 --- a/src/core/type-id.h +++ b/src/core/type-id.h @@ -258,6 +258,8 @@ public: bool LookupAttributeByName (std::string name, struct AttributeInfo *info) const; Ptr LookupTraceSourceByName (std::string name) const; + uint16_t GetUid (void) const; + void SetUid (uint16_t tid); // construct an invalid TypeId. TypeId (); From a0a7c6d88cd5fced0726d0582fbd7b8f0a9d7b21 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 14:01:55 -0700 Subject: [PATCH 16/35] stop using ChunkRegistry to get header and trailer uids --- src/common/packet-metadata.cc | 39 +++++++++++++++++++++-------- src/common/packet-metadata.h | 46 ++++++----------------------------- src/core/type-id.cc | 2 +- 3 files changed, 37 insertions(+), 50 deletions(-) diff --git a/src/common/packet-metadata.cc b/src/common/packet-metadata.cc index 6b30b49d3..5a0d4d7ff 100644 --- a/src/common/packet-metadata.cc +++ b/src/common/packet-metadata.cc @@ -24,7 +24,8 @@ #include "ns3/log.h" #include "packet-metadata.h" #include "buffer.h" -#include "chunk-registry.h" +#include "header.h" +#include "trailer.h" NS_LOG_COMPONENT_DEFINE ("PacketMetadata"); @@ -696,6 +697,12 @@ PacketMetadata::CreateFragment (uint32_t start, uint32_t end) const } void +PacketMetadata::AddHeader (const Header &header, uint32_t size) +{ + uint32_t uid = header.GetInstanceTypeId ().GetUid () << 1; + DoAddHeader (uid, size); +} +void PacketMetadata::DoAddHeader (uint32_t uid, uint32_t size) { if (!m_enable) @@ -703,7 +710,7 @@ PacketMetadata::DoAddHeader (uint32_t uid, uint32_t size) m_metadataSkipped = true; return; } - NS_LOG_PARAMS ("(uid=" << uid << ", size=" << size << ")"); + NS_LOG_PARAMS ("uid=" << uid << "size=" << size << ""); struct PacketMetadata::SmallItem item; item.next = m_head; @@ -716,8 +723,9 @@ PacketMetadata::DoAddHeader (uint32_t uid, uint32_t size) UpdateHead (written); } void -PacketMetadata::DoRemoveHeader (uint32_t uid, uint32_t size) +PacketMetadata::RemoveHeader (const Header &header, uint32_t size) { + uint32_t uid = header.GetInstanceTypeId ().GetUid () << 1; if (!m_enable) { m_metadataSkipped = true; @@ -753,13 +761,15 @@ PacketMetadata::DoRemoveHeader (uint32_t uid, uint32_t size) } } void -PacketMetadata::DoAddTrailer (uint32_t uid, uint32_t size) +PacketMetadata::AddTrailer (const Trailer &trailer, uint32_t size) { + uint32_t uid = trailer.GetInstanceTypeId ().GetUid () << 1; if (!m_enable) { m_metadataSkipped = true; return; } + NS_LOG_PARAMS ("(uid=" << uid << ", size=" << size << ")"); struct PacketMetadata::SmallItem item; item.next = 0xffff; item.prev = m_tail; @@ -771,13 +781,15 @@ PacketMetadata::DoAddTrailer (uint32_t uid, uint32_t size) UpdateTail (written); } void -PacketMetadata::DoRemoveTrailer (uint32_t uid, uint32_t size) +PacketMetadata::RemoveTrailer (const Trailer &trailer, uint32_t size) { + uint32_t uid = trailer.GetInstanceTypeId ().GetUid () << 1; if (!m_enable) { m_metadataSkipped = true; return; } + NS_LOG_PARAMS ("(uid=" << uid << ", size=" << size << ")"); struct PacketMetadata::SmallItem item; struct PacketMetadata::ExtraItem extraItem; uint32_t read = ReadItems (m_tail, &item, &extraItem); @@ -1065,11 +1077,13 @@ PacketMetadata::ItemIterator::Next (void) { item.isFragment = false; } + TypeId tid; + tid.SetUid (uid); if (uid == 0) { item.type = PacketMetadata::Item::PAYLOAD; } - else if (ChunkRegistry::IsHeader (uid)) + else if (tid.IsChildOf (Header::GetTypeId ())) { item.type = PacketMetadata::Item::HEADER; if (!item.isFragment) @@ -1079,7 +1093,7 @@ PacketMetadata::ItemIterator::Next (void) item.current = j; } } - else if (ChunkRegistry::IsTrailer (uid)) + else if (tid.IsChildOf (Trailer::GetTypeId ())) { item.type = PacketMetadata::Item::TRAILER; if (!item.isFragment) @@ -1119,7 +1133,9 @@ PacketMetadata::GetSerializedSize (void) const } else { - totalSize += 4 + ChunkRegistry::GetUidStringFromUid (uid).size (); + TypeId tid; + tid.SetUid (uid); + totalSize += 4 + tid.GetName ().size (); } totalSize += 1 + 4 + 2 + 4 + 4 + 4; if (current == m_tail) @@ -1150,7 +1166,9 @@ PacketMetadata::Serialize (Buffer::Iterator i, uint32_t size) const uint32_t uid = (item.typeUid & 0xfffffffe) >> 1; if (uid != 0) { - std::string uidString = ChunkRegistry::GetUidStringFromUid (uid); + TypeId tid; + tid.SetUid (uid); + std::string uidString = tid.GetName (); i.WriteU32 (uidString.size ()); bytesWritten += 4; i.Write ((uint8_t *)uidString.c_str (), uidString.size ()); @@ -1210,7 +1228,8 @@ PacketMetadata::Deserialize (Buffer::Iterator i) uidString.push_back (i.ReadU8 ()); size --; } - uid = ChunkRegistry::GetUidFromUidString (uidString); + TypeId tid = TypeId::LookupByName (uidString); + uid = tid.GetUid (); } uint8_t isBig = i.ReadU8 (); size --; diff --git a/src/common/packet-metadata.h b/src/common/packet-metadata.h index 2d71d712a..c1ca921ba 100644 --- a/src/common/packet-metadata.h +++ b/src/common/packet-metadata.h @@ -31,6 +31,8 @@ namespace ns3 { class Chunk; class Buffer; +class Header; +class Trailer; /** * \internal @@ -128,15 +130,11 @@ public: inline PacketMetadata &operator = (PacketMetadata const& o); inline ~PacketMetadata (); - template - void AddHeader (T const &header, uint32_t size); - template - void RemoveHeader (T const &header, uint32_t size); + void AddHeader (Header const &header, uint32_t size); + void RemoveHeader (Header const &header, uint32_t size); - template - void AddTrailer (T const &trailer, uint32_t size); - template - void RemoveTrailer (T const &trailer, uint32_t size); + void AddTrailer (Trailer const &trailer, uint32_t size); + void RemoveTrailer (Trailer const &trailer, uint32_t size); PacketMetadata CreateFragment (uint32_t start, uint32_t end) const; void AddAtEnd (PacketMetadata const&o); @@ -241,10 +239,6 @@ private: friend class ItemIterator; PacketMetadata (); - void DoAddHeader (uint32_t uid, uint32_t size); - void DoRemoveHeader (uint32_t uid, uint32_t size); - void DoAddTrailer (uint32_t uid, uint32_t size); - void DoRemoveTrailer (uint32_t uid, uint32_t size); inline uint16_t AddSmall (const PacketMetadata::SmallItem *item); uint16_t AddBig (uint32_t head, uint32_t tail, @@ -270,6 +264,7 @@ private: uint32_t ReadItems (uint16_t current, struct PacketMetadata::SmallItem *item, struct PacketMetadata::ExtraItem *extraItem) const; + void DoAddHeader (uint32_t uid, uint32_t size); static struct PacketMetadata::Data *Create (uint32_t size); @@ -304,33 +299,6 @@ private: namespace ns3 { -template -void -PacketMetadata::AddHeader (T const &header, uint32_t size) -{ - DoAddHeader (T::GetUid () << 1, size); -} - -template -void -PacketMetadata::RemoveHeader (T const &header, uint32_t size) -{ - DoRemoveHeader (T::GetUid () << 1, size); -} -template -void -PacketMetadata::AddTrailer (T const &trailer, uint32_t size) -{ - DoAddTrailer (T::GetUid () << 1, size); -} -template -void -PacketMetadata::RemoveTrailer (T const &trailer, uint32_t size) -{ - DoRemoveTrailer (T::GetUid () << 1, size); -} - - PacketMetadata::PacketMetadata (uint32_t uid, uint32_t size) : m_data (m_data = PacketMetadata::Create (10)), m_head (0xffff), diff --git a/src/core/type-id.cc b/src/core/type-id.cc index b2a59fe0b..1d0c26b69 100644 --- a/src/core/type-id.cc +++ b/src/core/type-id.cc @@ -479,7 +479,7 @@ bool TypeId::IsChildOf (TypeId other) const { TypeId tmp = *this; - while (tmp != other && tmp != tmp) + while (tmp != other && tmp != tmp.GetParent ()) { tmp = tmp.GetParent (); } From 6b9fc231e1d6743127e36f72a438b0d22ca4eafc Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 14:49:52 -0700 Subject: [PATCH 17/35] remove dead code. --- samples/main-packet-header.cc | 22 +--- src/common/chunk-registry.cc | 117 ----------------- src/common/chunk-registry.h | 180 --------------------------- src/common/header.cc | 6 + src/common/header.h | 66 +++------- src/common/packet-metadata-test.cc | 104 +++++----------- src/common/trailer.cc | 6 + src/common/trailer.h | 64 ++-------- src/common/wscript | 2 - src/devices/wifi/mgt-headers.cc | 44 ------- src/devices/wifi/mgt-headers.h | 16 +-- src/devices/wifi/wifi-mac-header.cc | 12 -- src/devices/wifi/wifi-mac-header.h | 4 +- src/devices/wifi/wifi-mac-trailer.cc | 13 -- src/devices/wifi/wifi-mac-trailer.h | 4 +- src/internet-node/arp-header.cc | 40 ++---- src/internet-node/arp-header.h | 15 +-- src/internet-node/ipv4-header.cc | 40 ++---- src/internet-node/ipv4-header.h | 8 +- src/internet-node/tcp-header.cc | 37 ++---- src/internet-node/tcp-header.h | 9 +- src/internet-node/udp-header.cc | 37 ++---- src/internet-node/udp-header.h | 8 +- src/node/ethernet-header.cc | 40 ++---- src/node/ethernet-header.h | 8 +- src/node/ethernet-trailer.cc | 44 +++---- src/node/ethernet-trailer.h | 15 +-- src/node/llc-snap-header.cc | 37 ++---- src/node/llc-snap-header.h | 9 +- src/routing/olsr/olsr-header.cc | 16 --- src/routing/olsr/olsr-header.h | 4 - utils/bench-packets.cc | 21 +--- 32 files changed, 193 insertions(+), 855 deletions(-) delete mode 100644 src/common/chunk-registry.cc delete mode 100644 src/common/chunk-registry.h diff --git a/samples/main-packet-header.cc b/samples/main-packet-header.cc index a93c8cb87..6ba8bb3da 100644 --- a/samples/main-packet-header.cc +++ b/samples/main-packet-header.cc @@ -11,7 +11,6 @@ using namespace ns3; class MyHeader : public Header { public: - static uint32_t GetUid (void); MyHeader (); virtual ~MyHeader (); @@ -21,8 +20,7 @@ public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - std::string GetName (void) const; - void Print (std::ostream &os) const; + virtual void Print (std::ostream &os) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); virtual uint32_t GetSerializedSize (void) const; @@ -52,24 +50,6 @@ MyHeader::GetInstanceTypeId (void) const return GetTypeId (); } -uint32_t -MyHeader::GetUid (void) -{ - // This string is used by the internals of the packet - // code to keep track of the packet metadata. - // You need to make sure that this string is absolutely - // unique. The code will detect any duplicate string. - static uint32_t uid = AllocateUid ("MyHeader.test.nsnam.org"); - return uid; -} - -std::string -MyHeader::GetName (void) const -{ - // This string is used to identify the type of - // my header by the packet printing routines. - return "MYHEADER"; -} void MyHeader::Print (std::ostream &os) const { diff --git a/src/common/chunk-registry.cc b/src/common/chunk-registry.cc deleted file mode 100644 index 57257a778..000000000 --- a/src/common/chunk-registry.cc +++ /dev/null @@ -1,117 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2005 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 "chunk-registry.h" -#include "ns3/assert.h" - -namespace ns3 { - -ChunkRegistry::InfoVector * -ChunkRegistry::GetInfoVector (void) -{ - static InfoVector vec; - return &vec; -} - -std::string -ChunkRegistry::GetUidStringFromUid (uint32_t uid) -{ - InfoVector *vec = GetInfoVector (); - NS_ASSERT (uid >= 1 && uid <= vec->size ()); - Info info = (*vec)[uid - 1]; - return info.uidString; -} -uint32_t -ChunkRegistry::GetUidFromUidString (std::string uidString) -{ - uint32_t uid = 1; - InfoVector *vec = GetInfoVector (); - for (InfoVector::iterator i = vec->begin (); i != vec->end (); i++) - { - if (i->uidString == uidString) - { - return uid; - } - uid++; - } - NS_FATAL_ERROR ("Trying to access a non-registered Header or Trailer: \"" << uidString << "\". "<< - "You could try calling NS_HEADER_ENSURE_REGISTER somewhere."); - return 0; -} - -uint8_t * -ChunkRegistry::GetStaticInstance (uint32_t uid) -{ - InfoVector *vec = GetInfoVector (); - NS_ASSERT (uid >= 1 && uid <= vec->size ()); - Info info = (*vec)[uid - 1]; - return info.getStaticInstance (); -} -bool -ChunkRegistry::IsHeader (uint32_t uid) -{ - InfoVector *vec = GetInfoVector (); - NS_ASSERT (uid >= 1 && uid <= vec->size ()); - Info info = (*vec)[uid - 1]; - return info.isHeader; -} -bool -ChunkRegistry::IsTrailer (uint32_t uid) -{ - return !IsHeader (uid); -} -uint32_t -ChunkRegistry::Deserialize (uint32_t uid, uint8_t *instance, Buffer::Iterator i) -{ - InfoVector *vec = GetInfoVector (); - NS_ASSERT (uid >= 1 && uid <= vec->size ()); - Info info = (*vec)[uid - 1]; - return info.deserialize (instance, i); -} -void -ChunkRegistry::Print (uint32_t uid, uint8_t *instance, std::ostream &os) -{ - InfoVector *vec = GetInfoVector (); - NS_ASSERT (uid >= 1 && uid <= vec->size ()); - Info info = (*vec)[uid - 1]; - return info.print (instance, os); -} -std::string -ChunkRegistry::GetName (uint32_t uid, uint8_t *instance) -{ - InfoVector *vec = GetInfoVector (); - NS_ASSERT (uid >= 1 && uid <= vec->size ()); - Info info = (*vec)[uid - 1]; - return info.getName (instance); -} -void -ChunkRegistry::InvokePrintCallback (uint32_t uid, uint8_t *instance, std::ostream &os, - uint32_t packetUid, uint32_t size, - CallbackBase callback) -{ - InfoVector *vec = GetInfoVector (); - NS_ASSERT (uid >= 1 && uid <= vec->size ()); - Info info = (*vec)[uid - 1]; - info.invokePrintCallback (instance, os, packetUid, size, callback); -} - - -} // namespace ns3 diff --git a/src/common/chunk-registry.h b/src/common/chunk-registry.h deleted file mode 100644 index 7d1cce212..000000000 --- a/src/common/chunk-registry.h +++ /dev/null @@ -1,180 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2005 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 CHUNK_REGISTRY_H -#define CHUNK_REGISTRY_H - -#include -#include -#include "buffer.h" -#include "ns3/ptr.h" -#include "ns3/callback.h" - -namespace ns3 { - -/** - * \brief this registry keeps track of all different - * types of headers and trailers and assigns to each of them - * a unique integer. - * \internal - */ -class ChunkRegistry -{ -public: - template - static uint32_t RegisterHeader (std::string uuid); - template - static uint32_t RegisterTrailer (std::string uuid); - - static std::string GetUidStringFromUid (uint32_t uid); - static uint32_t GetUidFromUidString (std::string uidString); - static uint8_t *GetStaticInstance (uint32_t uid); - static uint32_t Deserialize (uint32_t uid, uint8_t *instance, Buffer::Iterator i); - static void Print (uint32_t uid, uint8_t *instance, std::ostream &os); - static std::string GetName (uint32_t uid, uint8_t *instance); - static bool IsHeader (uint32_t uid); - static bool IsTrailer (uint32_t uid); - static void InvokePrintCallback (uint32_t uid, uint8_t *instance, std::ostream &os, - uint32_t packetUid, uint32_t size, - CallbackBase callback); -private: - typedef uint8_t *(*GetStaticInstanceCb) (void); - typedef uint32_t (*DeserializeCb) (uint8_t *, Buffer::Iterator); - typedef void (*PrintCb) (uint8_t *,std::ostream &); - typedef std::string (*GetNameCb) (uint8_t *); - typedef void (*InvokePrintCallbackCb) (uint8_t *instance, std::ostream &os, - uint32_t packetUid, uint32_t size, - CallbackBase callback); - struct Info { - std::string uidString; - bool isHeader; - GetStaticInstanceCb getStaticInstance; - DeserializeCb deserialize; - PrintCb print; - GetNameCb getName; - InvokePrintCallbackCb invokePrintCallback; - }; - typedef std::vector InfoVector; - static InfoVector *GetInfoVector (void); - template - static uint8_t *DoGetStaticInstance (void); - template - static uint32_t DoDeserialize (uint8_t *instance, Buffer::Iterator i); - template - static void DoPrint (uint8_t *instance, std::ostream &os); - template - static std::string DoGetName (uint8_t *instance); - template - static void DoInvokePrintCallback (uint8_t *instance, std::ostream &os, - uint32_t packetUid, uint32_t size, - CallbackBase callback); - template - static uint32_t GetUid (bool isHeader, std::string uidString); - -}; - - -} // namespace ns3 - -namespace ns3 { - -template -uint32_t -ChunkRegistry::RegisterHeader (std::string uuid) -{ - return GetUid (true, uuid); -} -template -uint32_t -ChunkRegistry::RegisterTrailer (std::string uuid) -{ - return GetUid (false, uuid); -} - -template -uint32_t -ChunkRegistry::GetUid (bool isHeader, std::string uidString) -{ - InfoVector *vec = GetInfoVector (); - uint32_t uid = 1; - for (InfoVector::iterator i = vec->begin (); i != vec->end (); i++) - { - if (i->uidString == uidString) - { - return uid; - } - uid++; - } - Info info; - info.getStaticInstance = &ChunkRegistry::DoGetStaticInstance; - info.print = &ChunkRegistry::DoPrint; - info.getName = &ChunkRegistry::DoGetName; - info.deserialize = &ChunkRegistry::DoDeserialize; - info.invokePrintCallback = &ChunkRegistry::DoInvokePrintCallback; - info.uidString = uidString; - info.isHeader = isHeader; - vec->push_back (info); - return vec->size (); -} - -template -uint8_t * -ChunkRegistry::DoGetStaticInstance () -{ - static T instance; - return reinterpret_cast (&instance); -} -template -uint32_t -ChunkRegistry::DoDeserialize (uint8_t *instance, Buffer::Iterator i) -{ - T *obj = reinterpret_cast (instance); - return obj->Deserialize (i); -} -template -void -ChunkRegistry::DoPrint (uint8_t *instance, std::ostream &os) -{ - T *obj = reinterpret_cast (instance); - obj->Print (os); -} -template -std::string -ChunkRegistry::DoGetName (uint8_t *instance) -{ - T *obj = reinterpret_cast (instance); - return obj->GetName (); -} -template -void -ChunkRegistry::DoInvokePrintCallback (uint8_t *instance, std::ostream &os, - uint32_t packetUid, uint32_t size, - CallbackBase callback) -{ - T *obj = reinterpret_cast (instance); - Callback cb; - cb.Assign (callback); - cb (os, packetUid, size, obj); -} - -} // namespace ns3 - -#endif /* CHUNK_H */ diff --git a/src/common/header.cc b/src/common/header.cc index ef6fac914..6a17e72c3 100644 --- a/src/common/header.cc +++ b/src/common/header.cc @@ -16,4 +16,10 @@ Header::GetTypeId (void) return tid; } +std::ostream & operator << (std::ostream &os, const Header &header) +{ + header.Print (os); + return os; +} + } // namespace ns3 diff --git a/src/common/header.h b/src/common/header.h index 3d432646e..71aba7752 100644 --- a/src/common/header.h +++ b/src/common/header.h @@ -22,30 +22,9 @@ #ifndef HEADER_H #define HEADER_H -#include "chunk-registry.h" #include "ns3/object-base.h" - -/** - * \relates ns3::Header - * \brief this macro should be instantiated exactly once for each - * new type of Header - * - * This macro will ensure that your new Header type is registered - * within the packet header registry. In most cases, this macro - * is not really needed but, for safety, please, use it all the - * time. - * - * Note: This macro is _absolutely_ needed if you try to run a - * distributed simulation. - */ -#define NS_HEADER_ENSURE_REGISTERED(x) \ -static class thisisaveryverylongclassname ##x \ -{ \ - public: \ - thisisaveryverylongclassname ##x () \ - { uint32_t uid; uid = x::GetUid ();} \ -} g_thisisanotherveryveryverylongname ## x; - +#include "buffer.h" +#include namespace ns3 { @@ -57,21 +36,6 @@ namespace ns3 { * implement the following public methods: * - a default constructor: is used by the internal implementation * if the Packet class. - * - a static method named GetUid: is used to uniquely identify - * the type of each header. This method shall return a unique - * integer allocated with Header::AllocateUid. - * - a method named Print: is used by Packet::Print to print the - * content of a header as ascii data to a c++ output stream. - * Although the header is free to format its output as it - * wishes, it is recommended to follow a few rules to integrate - * with the packet pretty printer: start with flags, small field - * values located between a pair of parens. Values should be separated - * by whitespace. Follow the parens with the important fields, - * separated by whitespace. - * i.e.: (field1 val1 field2 val2 field3 val3) field4 val4 field5 val5 - * - a method named GetName: is used by Packet::Print to print - * header fragments. This method should return a user-readable - * single word as all capitalized letters. * * Sample code which shows how to create a new type of Header, and how to use it, * is shown in the sample file samples/main-packet-header.cc @@ -113,21 +77,21 @@ public: * networks. */ virtual uint32_t Deserialize (Buffer::Iterator start) = 0; -protected: - template - static uint32_t AllocateUid (std::string uuid); + /** + * This method is used by Packet::Print to print the + * content of a trailer as ascii data to a c++ output stream. + * Although the trailer is free to format its output as it + * wishes, it is recommended to follow a few rules to integrate + * with the packet pretty printer: start with flags, small field + * values located between a pair of parens. Values should be separated + * by whitespace. Follow the parens with the important fields, + * separated by whitespace. + * i.e.: (field1 val1 field2 val2 field3 val3) field4 val4 field5 val5 + */ + virtual void Print (std::ostream &os) const = 0; }; -} // namespace ns3 - -namespace ns3 { - -template -uint32_t -Header::AllocateUid (std::string uuid) -{ - return ChunkRegistry::RegisterHeader (uuid); -} +std::ostream & operator << (std::ostream &os, const Header &header); } // namespace ns3 diff --git a/src/common/packet-metadata-test.cc b/src/common/packet-metadata-test.cc index aabe3de39..e274bbc4d 100644 --- a/src/common/packet-metadata-test.cc +++ b/src/common/packet-metadata-test.cc @@ -34,13 +34,11 @@ template class HistoryHeader : public Header { public: - static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); HistoryHeader (); bool IsOk (void) const; - std::string GetName (void) const; - void Print (std::ostream &os) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); @@ -48,6 +46,18 @@ private: bool m_ok; }; +template +HistoryHeader::HistoryHeader () + : m_ok (false) +{} + +template +bool +HistoryHeader::IsOk (void) const +{ + return m_ok; +} + template TypeId HistoryHeader::GetTypeId (void) @@ -66,38 +76,6 @@ HistoryHeader::GetInstanceTypeId (void) const { return GetTypeId (); } - -template -uint32_t -HistoryHeader::GetUid (void) -{ - std::ostringstream oss; - oss << N << "HistoryHeader.ns3"; - static uint32_t uid = AllocateUid > (oss.str()); - return uid; -} - -template -HistoryHeader::HistoryHeader () - : m_ok (false) -{} - -template -bool -HistoryHeader::IsOk (void) const -{ - return m_ok; -} - -template -std::string -HistoryHeader::GetName (void) const -{ - std::ostringstream oss; - oss << N; - return oss.str (); -} - template void HistoryHeader::Print (std::ostream &os) const @@ -135,13 +113,12 @@ template class HistoryTrailer : public Trailer { public: - static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); HistoryTrailer (); bool IsOk (void) const; - std::string GetName (void) const; - void Print (std::ostream &os) const; + + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); @@ -149,6 +126,17 @@ private: bool m_ok; }; +template +HistoryTrailer::HistoryTrailer () + : m_ok (false) +{} + +template +bool +HistoryTrailer::IsOk (void) const +{ + return m_ok; +} template TypeId @@ -168,38 +156,6 @@ HistoryTrailer::GetInstanceTypeId (void) const { return GetTypeId (); } - -template -uint32_t -HistoryTrailer::GetUid (void) -{ - std::ostringstream oss; - oss << N << "HistoryTrailer.ns3"; - static uint32_t uid = AllocateUid > (oss.str ()); - return uid; -} - - -template -HistoryTrailer::HistoryTrailer () - : m_ok (false) -{} - -template -bool -HistoryTrailer::IsOk (void) const -{ - return m_ok; -} - -template -std::string -HistoryTrailer::GetName (void) const -{ - std::ostringstream oss; - oss << N; - return oss.str (); -} template void HistoryTrailer::Print (std::ostream &os) const diff --git a/src/common/trailer.cc b/src/common/trailer.cc index 389e3d34b..98cdaa6b6 100644 --- a/src/common/trailer.cc +++ b/src/common/trailer.cc @@ -16,4 +16,10 @@ Trailer::GetTypeId (void) return tid; } +std::ostream & operator << (std::ostream &os, const Trailer &trailer) +{ + trailer.Print (os); + return os; +} + } // namespace ns3 diff --git a/src/common/trailer.h b/src/common/trailer.h index dd77e684b..f515211eb 100644 --- a/src/common/trailer.h +++ b/src/common/trailer.h @@ -23,30 +23,9 @@ #define TRAILER_H #include "ns3/object-base.h" -#include "chunk-registry.h" #include "buffer.h" #include -/** - * \relates ns3::Trailer - * \brief this macro should be instantiated exactly once for each - * new type of Trailer - * - * This macro will ensure that your new Trailer type is registered - * within the packet trailer registry. In most cases, this macro - * is not really needed but, for safety, please, use it all the - * time. - * - * Note: This macro is _absolutely_ needed if you try to run a - * distributed simulation. - */ -#define NS_TRAILER_ENSURE_REGISTERED(x) \ -static class thisisaveryverylongclassname ##x \ -{ \ - public: \ - thisisaveryverylongclassname ##x () \ - { uint32_t uid; uid = x::GetUid ();} \ -} g_thisisanotherveryveryverylongname ##x; namespace ns3 { @@ -58,22 +37,6 @@ namespace ns3 { * implement the following public methods: * - a default constructor: is used by the internal implementation * if the Packet class. - * - a static method named GetUid: is used to uniquely identify - * the type of each trailer. This method shall return a unique - * integer allocated with Trailer::AllocateUid. - * - a method named Print: is used by Packet::Print to print the - * content of a trailer as ascii data to a c++ output stream. - * Although the trailer is free to format its output as it - * wishes, it is recommended to follow a few rules to integrate - * with the packet pretty printer: start with flags, small field - * values located between a pair of parens. Values should be separated - * by whitespace. Follow the parens with the important fields, - * separated by whitespace. - * i.e.: (field1 val1 field2 val2 field3 val3) field4 val4 field5 val5 - * - a method named GetName: is used by Packet::Print to print - * trailer fragments. This method should return a user-readable - * single word as all capitalized letters. - * */ class Trailer : public ObjectBase { @@ -116,22 +79,21 @@ public: * Buffer::Iterator::Prev prio to actually reading any data. */ virtual uint32_t Deserialize (Buffer::Iterator end) = 0; -protected: - template - static uint32_t AllocateUid (std::string uidString); + /** + * This method is used by Packet::Print to print the + * content of a trailer as ascii data to a c++ output stream. + * Although the trailer is free to format its output as it + * wishes, it is recommended to follow a few rules to integrate + * with the packet pretty printer: start with flags, small field + * values located between a pair of parens. Values should be separated + * by whitespace. Follow the parens with the important fields, + * separated by whitespace. + * i.e.: (field1 val1 field2 val2 field3 val3) field4 val4 field5 val5 + */ + virtual void Print (std::ostream &os) const = 0; }; -} // namespace ns3 - -namespace ns3 { - -template -uint32_t -Trailer::AllocateUid (std::string uidString) -{ - return ChunkRegistry::RegisterTrailer (uidString); -} - +std::ostream & operator << (std::ostream &os, const Trailer &trailer); } // namespace ns3 diff --git a/src/common/wscript b/src/common/wscript index 1de57f8ac..13a27d50b 100644 --- a/src/common/wscript +++ b/src/common/wscript @@ -4,7 +4,6 @@ def build(bld): common = bld.create_ns3_module('common', ['core', 'simulator']) common.source = [ 'buffer.cc', - 'chunk-registry.cc', 'packet-metadata.cc', 'packet-metadata-test.cc', 'packet.cc', @@ -21,7 +20,6 @@ def build(bld): headers.module = 'common' headers.source = [ 'buffer.h', - 'chunk-registry.h', 'header.h', 'trailer.h', 'tags.h', diff --git a/src/devices/wifi/mgt-headers.cc b/src/devices/wifi/mgt-headers.cc index e3c7aeac0..87857bfd7 100644 --- a/src/devices/wifi/mgt-headers.cc +++ b/src/devices/wifi/mgt-headers.cc @@ -74,17 +74,6 @@ MgtProbeRequestHeader::GetInstanceTypeId (void) const { return GetTypeId (); } -uint32_t -MgtProbeRequestHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("MgtProbeRequestHeader.ns3.inria.fr"); - return uid; -} -std::string -MgtProbeRequestHeader::GetName (void) const -{ - return "PROBEREQ"; -} void MgtProbeRequestHeader::Print (std::ostream &os) const { @@ -164,17 +153,6 @@ MgtProbeResponseHeader::GetInstanceTypeId (void) const return GetTypeId (); } uint32_t -MgtProbeResponseHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("MgtProbeResponseHeader.ns3.inria.fr"); - return uid; -} -std::string -MgtProbeResponseHeader::GetName (void) const -{ - return "PROBERESP"; -} -uint32_t MgtProbeResponseHeader::GetSerializedSize (void) const { uint32_t size = 0; @@ -283,17 +261,6 @@ MgtAssocRequestHeader::GetInstanceTypeId (void) const { return GetTypeId (); } -uint32_t -MgtAssocRequestHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("MgtAssocRequestHeader.ns3.inria.fr"); - return uid; -} -std::string -MgtAssocRequestHeader::GetName (void) const -{ - return "ASSOCREQ"; -} uint32_t MgtAssocRequestHeader::GetSerializedSize (void) const { @@ -376,17 +343,6 @@ MgtAssocResponseHeader::GetInstanceTypeId (void) const return GetTypeId (); } uint32_t -MgtAssocResponseHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("MgtAssocResponseHeader.ns3.inria.fr"); - return uid; -} -std::string -MgtAssocResponseHeader::GetName (void) const -{ - return "ASSOCRESP"; -} -uint32_t MgtAssocResponseHeader::GetSerializedSize (void) const { uint32_t size = 0; diff --git a/src/devices/wifi/mgt-headers.h b/src/devices/wifi/mgt-headers.h index 67e8a315a..45996600e 100644 --- a/src/devices/wifi/mgt-headers.h +++ b/src/devices/wifi/mgt-headers.h @@ -46,9 +46,7 @@ public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - std::string GetName (void) const; - void Print (std::ostream &os) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); @@ -73,9 +71,7 @@ public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - std::string GetName (void) const; - void Print (std::ostream &os) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); @@ -98,9 +94,7 @@ public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - std::string GetName (void) const; - void Print (std::ostream &os) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); @@ -125,9 +119,7 @@ public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - std::string GetName (void) const; - void Print (std::ostream &os) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); diff --git a/src/devices/wifi/wifi-mac-header.cc b/src/devices/wifi/wifi-mac-header.cc index e8e8324f8..60e680f71 100644 --- a/src/devices/wifi/wifi-mac-header.cc +++ b/src/devices/wifi/wifi-mac-header.cc @@ -781,18 +781,6 @@ WifiMacHeader::GetInstanceTypeId (void) const { return GetTypeId (); } -uint32_t -WifiMacHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("WifiMacHeader.ns3.inria.fr"); - return uid; -} - -std::string -WifiMacHeader::GetName (void) const -{ - return "802.11"; -} void WifiMacHeader::PrintFrameControl (std::ostream &os) const diff --git a/src/devices/wifi/wifi-mac-header.h b/src/devices/wifi/wifi-mac-header.h index 869a48628..33eedd8dd 100644 --- a/src/devices/wifi/wifi-mac-header.h +++ b/src/devices/wifi/wifi-mac-header.h @@ -71,9 +71,7 @@ public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - std::string GetName (void) const; - void Print (std::ostream &os) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); diff --git a/src/devices/wifi/wifi-mac-trailer.cc b/src/devices/wifi/wifi-mac-trailer.cc index d083e0509..ab156dda5 100644 --- a/src/devices/wifi/wifi-mac-trailer.cc +++ b/src/devices/wifi/wifi-mac-trailer.cc @@ -44,19 +44,6 @@ WifiMacTrailer::GetInstanceTypeId (void) const return GetTypeId (); } -uint32_t -WifiMacTrailer::GetUid (void) -{ - static uint32_t uid = AllocateUid ("WifiMacTrailer.ns3.inria.fr"); - return uid; -} - -std::string -WifiMacTrailer::GetName (void) const -{ - return "802.11 FCS"; -} - void WifiMacTrailer::Print (std::ostream &os) const {} diff --git a/src/devices/wifi/wifi-mac-trailer.h b/src/devices/wifi/wifi-mac-trailer.h index c295025c7..717c12d70 100644 --- a/src/devices/wifi/wifi-mac-trailer.h +++ b/src/devices/wifi/wifi-mac-trailer.h @@ -33,9 +33,7 @@ public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - std::string GetName (void) const; - void Print (std::ostream &os) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); diff --git a/src/internet-node/arp-header.cc b/src/internet-node/arp-header.cc index 3e114716c..0ff15bd1d 100644 --- a/src/internet-node/arp-header.cc +++ b/src/internet-node/arp-header.cc @@ -25,30 +25,8 @@ namespace ns3 { -NS_HEADER_ENSURE_REGISTERED (ArpHeader); NS_OBJECT_ENSURE_REGISTERED (ArpHeader); -TypeId -ArpHeader::GetTypeId (void) -{ - static TypeId tid = TypeId ("ns3::ArpHeader") - .SetParent
() - ; - return tid; -} -TypeId -ArpHeader::GetInstanceTypeId (void) const -{ - return GetTypeId (); -} - -uint32_t -ArpHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("ArpHeader.ns3"); - return uid; -} - void ArpHeader::SetRequest (Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, @@ -104,12 +82,20 @@ ArpHeader::GetDestinationIpv4Address (void) return m_ipv4Dest; } -std::string -ArpHeader::GetName (void) const -{ - return "ARP"; -} +TypeId +ArpHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::ArpHeader") + .SetParent
() + ; + return tid; +} +TypeId +ArpHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} void ArpHeader::Print (std::ostream &os) const { diff --git a/src/internet-node/arp-header.h b/src/internet-node/arp-header.h index 7fafefcef..96b368b0b 100644 --- a/src/internet-node/arp-header.h +++ b/src/internet-node/arp-header.h @@ -34,10 +34,6 @@ namespace ns3 { class ArpHeader : public Header { public: - static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - void SetRequest (Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, @@ -53,11 +49,12 @@ public: Ipv4Address GetSourceIpv4Address (void); Ipv4Address GetDestinationIpv4Address (void); - std::string GetName (void) const; - void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator start) const; - uint32_t Deserialize (Buffer::Iterator start); + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); enum ArpType_e { ARP_TYPE_REQUEST = 1, diff --git a/src/internet-node/ipv4-header.cc b/src/internet-node/ipv4-header.cc index 052be7c5a..e8296018f 100644 --- a/src/internet-node/ipv4-header.cc +++ b/src/internet-node/ipv4-header.cc @@ -28,32 +28,10 @@ NS_LOG_COMPONENT_DEFINE ("Ipv4Header"); namespace ns3 { -NS_HEADER_ENSURE_REGISTERED (Ipv4Header); NS_OBJECT_ENSURE_REGISTERED (Ipv4Header); bool Ipv4Header::m_calcChecksum = false; -TypeId -Ipv4Header::GetTypeId (void) -{ - static TypeId tid = TypeId ("ns3::Ipv4Header") - .SetParent
() - ; - return tid; -} -TypeId -Ipv4Header::GetInstanceTypeId (void) const -{ - return GetTypeId (); -} - -uint32_t -Ipv4Header::GetUid (void) -{ - static uint32_t uid = AllocateUid ("Ipv4Header.ns3"); - return uid; -} - Ipv4Header::Ipv4Header () : m_payloadSize (0), m_identification (0), @@ -201,12 +179,20 @@ Ipv4Header::IsChecksumOk (void) const return m_goodChecksum; } -std::string -Ipv4Header::GetName (void) const -{ - return "IPV4"; -} +TypeId +Ipv4Header::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::Ipv4Header") + .SetParent
() + ; + return tid; +} +TypeId +Ipv4Header::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} void Ipv4Header::Print (std::ostream &os) const { diff --git a/src/internet-node/ipv4-header.h b/src/internet-node/ipv4-header.h index 8375dd300..6ea749046 100644 --- a/src/internet-node/ipv4-header.h +++ b/src/internet-node/ipv4-header.h @@ -32,9 +32,6 @@ namespace ns3 { class Ipv4Header : public Header { public: - static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); /** * \brief Construct a null IPv4 header */ @@ -142,8 +139,9 @@ public: */ bool IsChecksumOk (void) const; - std::string GetName (void) const; - void Print (std::ostream &os) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); diff --git a/src/internet-node/tcp-header.cc b/src/internet-node/tcp-header.cc index dde46f36c..989b9680e 100644 --- a/src/internet-node/tcp-header.cc +++ b/src/internet-node/tcp-header.cc @@ -26,32 +26,10 @@ namespace ns3 { -NS_HEADER_ENSURE_REGISTERED (TcpHeader); NS_OBJECT_ENSURE_REGISTERED (TcpHeader); bool TcpHeader::m_calcChecksum = false; -TypeId -TcpHeader::GetTypeId (void) -{ - static TypeId tid = TypeId ("ns3::TcpHeader") - .SetParent
() - ; - return tid; -} -TypeId -TcpHeader::GetInstanceTypeId (void) const -{ - return GetTypeId (); -} - -uint32_t -TcpHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("TcpHeader.ns3"); - return uid; -} - TcpHeader::TcpHeader () : m_sourcePort (0), m_destinationPort (0), @@ -156,12 +134,19 @@ TcpHeader::InitializeChecksum (Ipv4Address source, //XXX requires peeking into IP to get length of the TCP segment } -std::string -TcpHeader::GetName (void) const +TypeId +TcpHeader::GetTypeId (void) { - return "TCP"; + static TypeId tid = TypeId ("ns3::TcpHeader") + .SetParent
() + ; + return tid; +} +TypeId +TcpHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); } - void TcpHeader::Print (std::ostream &os) const { //XXX diff --git a/src/internet-node/tcp-header.h b/src/internet-node/tcp-header.h index 2dbda5634..3181b03be 100644 --- a/src/internet-node/tcp-header.h +++ b/src/internet-node/tcp-header.h @@ -33,10 +33,6 @@ namespace ns3 { class TcpHeader : public Header { public: - static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - TcpHeader (); virtual ~TcpHeader (); @@ -139,8 +135,9 @@ public: typedef enum { NONE = 0, FIN = 1, SYN = 2, RST = 4, PSH = 8, ACK = 16, URG = 32} Flags_t; - std::string GetName (void) const; - void Print (std::ostream &os) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); diff --git a/src/internet-node/udp-header.cc b/src/internet-node/udp-header.cc index d8b3594c7..7682ae880 100644 --- a/src/internet-node/udp-header.cc +++ b/src/internet-node/udp-header.cc @@ -24,32 +24,10 @@ namespace ns3 { -NS_HEADER_ENSURE_REGISTERED (UdpHeader); NS_OBJECT_ENSURE_REGISTERED (UdpHeader); bool UdpHeader::m_calcChecksum = false; -TypeId -UdpHeader::GetTypeId (void) -{ - static TypeId tid = TypeId ("ns3::UdpHeader") - .SetParent
() - ; - return tid; -} -TypeId -UdpHeader::GetInstanceTypeId (void) const -{ - return GetTypeId (); -} - -uint32_t -UdpHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("UdpHeader.ns3"); - return uid; -} - /* The magic values below are used only for debugging. * They can be used to easily detect memory corruption * problems so you can see the patterns in memory. @@ -115,12 +93,19 @@ UdpHeader::InitializeChecksum (Ipv4Address source, m_initialChecksum = Ipv4ChecksumCalculate (0, buf, 12); } -std::string -UdpHeader::GetName (void) const +TypeId +UdpHeader::GetTypeId (void) { - return "UDP"; + static TypeId tid = TypeId ("ns3::UdpHeader") + .SetParent
() + ; + return tid; +} +TypeId +UdpHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); } - void UdpHeader::Print (std::ostream &os) const { diff --git a/src/internet-node/udp-header.h b/src/internet-node/udp-header.h index f55f42406..c0e5a023e 100644 --- a/src/internet-node/udp-header.h +++ b/src/internet-node/udp-header.h @@ -34,9 +34,6 @@ namespace ns3 { class UdpHeader : public Header { public: - static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); /** * \brief Constructor @@ -86,8 +83,9 @@ public: Ipv4Address destination, uint8_t protocol); - std::string GetName (void) const; - void Print (std::ostream &os) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); diff --git a/src/node/ethernet-header.cc b/src/node/ethernet-header.cc index 999dc9d08..45b90f478 100644 --- a/src/node/ethernet-header.cc +++ b/src/node/ethernet-header.cc @@ -31,30 +31,8 @@ NS_LOG_COMPONENT_DEFINE ("EthernetHeader"); namespace ns3 { -NS_HEADER_ENSURE_REGISTERED (EthernetHeader); NS_OBJECT_ENSURE_REGISTERED (EthernetHeader); -TypeId -EthernetHeader::GetTypeId (void) -{ - static TypeId tid = TypeId ("ns3::EthernetHeader") - .SetParent
() - ; - return tid; -} -TypeId -EthernetHeader::GetInstanceTypeId (void) const -{ - return GetTypeId (); -} - -uint32_t -EthernetHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("EthernetHeader.ns3"); - return uid; -} - EthernetHeader::EthernetHeader (bool hasPreamble) : m_enPreambleSfd (hasPreamble), m_lengthType (0) @@ -121,12 +99,20 @@ EthernetHeader::GetHeaderSize (void) const return GetSerializedSize(); } -std::string -EthernetHeader::GetName (void) const -{ - return "ETHERNET"; -} +TypeId +EthernetHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::EthernetHeader") + .SetParent
() + ; + return tid; +} +TypeId +EthernetHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} void EthernetHeader::Print (std::ostream &os) const { diff --git a/src/node/ethernet-header.h b/src/node/ethernet-header.h index 189cb8e32..8434a3fca 100644 --- a/src/node/ethernet-header.h +++ b/src/node/ethernet-header.h @@ -49,9 +49,6 @@ namespace ns3 { class EthernetHeader : public Header { public: - static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); /** * \brief Construct a null ethernet header @@ -105,8 +102,9 @@ public: */ uint32_t GetHeaderSize() const; - std::string GetName (void) const; - void Print (std::ostream &os) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); diff --git a/src/node/ethernet-trailer.cc b/src/node/ethernet-trailer.cc index 6c94ed368..e1c0396f8 100644 --- a/src/node/ethernet-trailer.cc +++ b/src/node/ethernet-trailer.cc @@ -28,32 +28,10 @@ NS_LOG_COMPONENT_DEFINE ("EthernetTrailer"); namespace ns3 { -NS_TRAILER_ENSURE_REGISTERED (EthernetTrailer); NS_OBJECT_ENSURE_REGISTERED (EthernetTrailer); bool EthernetTrailer::m_calcFcs = false; -TypeId -EthernetTrailer::GetTypeId (void) -{ - static TypeId tid = TypeId ("ns3::EthernetTrailer") - .SetParent () - ; - return tid; -} -TypeId -EthernetTrailer::GetInstanceTypeId (void) const -{ - return GetTypeId (); -} - -uint32_t -EthernetTrailer::GetUid (void) -{ - static uint32_t uid = AllocateUid ("EthernetTrailer.ns3"); - return uid; -} - EthernetTrailer::EthernetTrailer () { Init(); @@ -76,7 +54,9 @@ EthernetTrailer::CheckFcs (Ptr p) const if (!m_calcFcs) { return true; - } else { + } + else + { NS_LOG_WARN ("FCS calculation is not yet enabled"); return false; } @@ -105,12 +85,20 @@ EthernetTrailer::GetTrailerSize (void) const { return GetSerializedSize(); } -std::string -EthernetTrailer::GetName (void) const -{ - return "ETHERNET"; -} +TypeId +EthernetTrailer::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::EthernetTrailer") + .SetParent () + ; + return tid; +} +TypeId +EthernetTrailer::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} void EthernetTrailer::Print (std::ostream &os) const { diff --git a/src/node/ethernet-trailer.h b/src/node/ethernet-trailer.h index 694be0a70..5a21523b0 100644 --- a/src/node/ethernet-trailer.h +++ b/src/node/ethernet-trailer.h @@ -39,10 +39,6 @@ class Packet; class EthernetTrailer : public Trailer { public: - static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - /** * \brief Construct a null ethernet trailer */ @@ -85,11 +81,12 @@ public: */ uint32_t GetTrailerSize() const; - std::string GetName (void) const; - void Print (std::ostream &os) const; - uint32_t GetSerializedSize (void) const; - void Serialize (Buffer::Iterator end) const; - uint32_t Deserialize (Buffer::Iterator end); + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; + virtual uint32_t GetSerializedSize (void) const; + virtual void Serialize (Buffer::Iterator end) const; + virtual uint32_t Deserialize (Buffer::Iterator end); private: /** diff --git a/src/node/llc-snap-header.cc b/src/node/llc-snap-header.cc index 9c801fbc0..51880a2bc 100644 --- a/src/node/llc-snap-header.cc +++ b/src/node/llc-snap-header.cc @@ -25,30 +25,8 @@ namespace ns3 { -NS_HEADER_ENSURE_REGISTERED (LlcSnapHeader); NS_OBJECT_ENSURE_REGISTERED (LlcSnapHeader); -TypeId -LlcSnapHeader::GetTypeId (void) -{ - static TypeId tid = TypeId ("ns3::LlcSnapHeader") - .SetParent
() - ; - return tid; -} -TypeId -LlcSnapHeader::GetInstanceTypeId (void) const -{ - return GetTypeId (); -} - -uint32_t -LlcSnapHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid ("LlcSnapHeader.ns3"); - return uid; -} - LlcSnapHeader::LlcSnapHeader () {} @@ -69,12 +47,19 @@ LlcSnapHeader::GetSerializedSize (void) const return 1 + 1 + 1 + 3 + 2; } -std::string -LlcSnapHeader::GetName (void) const +TypeId +LlcSnapHeader::GetTypeId (void) { - return "LLCSNAP"; + static TypeId tid = TypeId ("ns3::LlcSnapHeader") + .SetParent
() + ; + return tid; +} +TypeId +LlcSnapHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); } - void LlcSnapHeader::Print (std::ostream &os) const { diff --git a/src/node/llc-snap-header.h b/src/node/llc-snap-header.h index 32c20a484..1d70d4740 100644 --- a/src/node/llc-snap-header.h +++ b/src/node/llc-snap-header.h @@ -31,17 +31,14 @@ namespace ns3 { class LlcSnapHeader : public Header { public: - static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - LlcSnapHeader (); void SetType (uint16_t type); uint16_t GetType (void); - std::string GetName (void) const; - void Print (std::ostream &os) const; + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); diff --git a/src/routing/olsr/olsr-header.cc b/src/routing/olsr/olsr-header.cc index eb488234a..bc4c3a17b 100644 --- a/src/routing/olsr/olsr-header.cc +++ b/src/routing/olsr/olsr-header.cc @@ -116,14 +116,6 @@ PacketHeader::GetInstanceTypeId (void) const return GetTypeId (); } -uint32_t -PacketHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid - ("PacketHeader.nsnam.org"); - return uid; -} - uint32_t PacketHeader::GetSerializedSize (void) const { @@ -179,14 +171,6 @@ MessageHeader::GetInstanceTypeId (void) const return GetTypeId (); } -uint32_t -MessageHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid - ("MessageHeader.nsnam.org"); - return uid; -} - uint32_t MessageHeader::GetSerializedSize (void) const { diff --git a/src/routing/olsr/olsr-header.h b/src/routing/olsr/olsr-header.h index 13583d16e..e1579e898 100644 --- a/src/routing/olsr/olsr-header.h +++ b/src/routing/olsr/olsr-header.h @@ -97,12 +97,10 @@ private: public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); - virtual std::string GetName (void) const { return "OlsrPacket"; } }; @@ -204,12 +202,10 @@ private: public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); - virtual std::string GetName (void) const { return "OlsrMessage"; } // 5.1. MID Message Format // diff --git a/utils/bench-packets.cc b/utils/bench-packets.cc index 920aa992b..099882085 100644 --- a/utils/bench-packets.cc +++ b/utils/bench-packets.cc @@ -35,9 +35,7 @@ public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; - static uint32_t GetUid (void); - static std::string GetName (void); - void Print (std::ostream &os) const; + virtual void Print (std::ostream &os) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (Buffer::Iterator start) const; virtual uint32_t Deserialize (Buffer::Iterator start); @@ -75,23 +73,6 @@ BenchHeader::GetInstanceTypeId (void) const return GetTypeId (); } -template -uint32_t -BenchHeader::GetUid (void) -{ - static uint32_t uid = AllocateUid > (GetName ()); - return uid; -} - -template -std::string -BenchHeader::GetName (void) -{ - std::ostringstream oss; - oss << "BenchHeader" << N; - return oss.str (); -} - template void BenchHeader::Print (std::ostream &os) const From 8dc5510518178979cf128ba19707c153ddb46098 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 17:37:25 -0700 Subject: [PATCH 18/35] implement Packet::Print --- src/common/chunk.cc | 16 ++++++++++ src/common/chunk.h | 19 +++++++++++ src/common/header.cc | 2 +- src/common/header.h | 4 +-- src/common/packet-metadata.cc | 2 +- src/common/packet-metadata.h | 6 ++-- src/common/packet.cc | 59 +++++++++++++++++++++++++++++++++++ src/common/trailer.cc | 2 +- src/common/trailer.h | 4 +-- src/common/wscript | 2 ++ 10 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 src/common/chunk.cc create mode 100644 src/common/chunk.h diff --git a/src/common/chunk.cc b/src/common/chunk.cc new file mode 100644 index 000000000..247a5f6a9 --- /dev/null +++ b/src/common/chunk.cc @@ -0,0 +1,16 @@ +#include "chunk.h" + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (Chunk); + +TypeId +Chunk::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::Chunk") + .SetParent () + ; + return tid; +} + +} // namespace ns3 diff --git a/src/common/chunk.h b/src/common/chunk.h new file mode 100644 index 000000000..7e35fdba5 --- /dev/null +++ b/src/common/chunk.h @@ -0,0 +1,19 @@ +#ifndef CHUNK_H +#define CHUNK_H + +#include "ns3/object-base.h" +#include "buffer.h" + +namespace ns3 { + +class Chunk : public ObjectBase +{ + public: + static TypeId GetTypeId (void); + + virtual uint32_t Deserialize (Buffer::Iterator start) = 0; +}; + +} // namespace ns3 + +#endif /* CHUNK_H */ diff --git a/src/common/header.cc b/src/common/header.cc index 6a17e72c3..c31ed5ab0 100644 --- a/src/common/header.cc +++ b/src/common/header.cc @@ -11,7 +11,7 @@ TypeId Header::GetTypeId (void) { static TypeId tid = TypeId ("ns3::Header") - .SetParent () + .SetParent () ; return tid; } diff --git a/src/common/header.h b/src/common/header.h index 71aba7752..af4e14e04 100644 --- a/src/common/header.h +++ b/src/common/header.h @@ -22,7 +22,7 @@ #ifndef HEADER_H #define HEADER_H -#include "ns3/object-base.h" +#include "chunk.h" #include "buffer.h" #include @@ -40,7 +40,7 @@ namespace ns3 { * Sample code which shows how to create a new type of Header, and how to use it, * is shown in the sample file samples/main-packet-header.cc */ -class Header : public ObjectBase +class Header : public Chunk { public: static TypeId GetTypeId (void); diff --git a/src/common/packet-metadata.cc b/src/common/packet-metadata.cc index 5a0d4d7ff..fb0c78be4 100644 --- a/src/common/packet-metadata.cc +++ b/src/common/packet-metadata.cc @@ -1065,7 +1065,7 @@ PacketMetadata::ItemIterator::Next (void) } m_current = smallItem.next; uint32_t uid = (smallItem.typeUid & 0xfffffffe) >> 1; - item.uid = uid; + item.tid.SetUid (uid); item.currentTrimedFromStart = extraItem.fragmentStart; item.currentTrimedFromEnd = extraItem.fragmentEnd - smallItem.size; item.currentSize = extraItem.fragmentEnd - extraItem.fragmentStart; diff --git a/src/common/packet-metadata.h b/src/common/packet-metadata.h index c1ca921ba..b02320672 100644 --- a/src/common/packet-metadata.h +++ b/src/common/packet-metadata.h @@ -25,6 +25,7 @@ #include #include "ns3/callback.h" #include "ns3/assert.h" +#include "ns3/type-id.h" #include "buffer.h" namespace ns3 { @@ -88,9 +89,10 @@ public: * false: this is a whole header, trailer, or, payload. */ bool isFragment; - /* uid of header or trailer. valid only if isPayload is false. + /* TypeId of Header or Trailer. Valid only if type is + * header or trailer. */ - uint32_t uid; + TypeId tid; /* size of item. If fragment, size of fragment. Otherwise, * size of original item. */ diff --git a/src/common/packet.cc b/src/common/packet.cc index 22bca8349..cbebbe93b 100644 --- a/src/common/packet.cc +++ b/src/common/packet.cc @@ -187,6 +187,65 @@ void Packet::Print (std::ostream &os) const { //XXX + PacketMetadata::ItemIterator i = m_metadata.BeginItem (m_buffer); + while (i.HasNext ()) + { + PacketMetadata::Item item = i.Next (); + if (item.isFragment) + { + switch (item.type) { + case PacketMetadata::Item::PAYLOAD: + os << "Payload"; + break; + case PacketMetadata::Item::HEADER: + case PacketMetadata::Item::TRAILER: + os << item.tid.GetName (); + break; + } + os << " Fragment [" << item.currentTrimedFromStart<<":" + << (item.currentTrimedFromStart + item.currentSize) << "]"; + } + else + { + switch (item.type) { + case PacketMetadata::Item::PAYLOAD: + os << "Payload (size=" << item.currentSize << ")"; + break; + case PacketMetadata::Item::HEADER: + case PacketMetadata::Item::TRAILER: + os << item.tid.GetName () << "("; + { + NS_ASSERT (item.tid.HasConstructor ()); + Callback constructor = item.tid.GetConstructor (); + NS_ASSERT (constructor.IsNull ()); + ObjectBase *instance = constructor (); + NS_ASSERT (instance != 0); + Chunk *chunk = dynamic_cast (instance); + NS_ASSERT (chunk != 0); + chunk->Deserialize (item.current); + for (uint32_t j = 0; j < item.tid.GetAttributeListN (); j++) + { + std::string attrName = item.tid.GetAttributeName (j); + std::string value; + bool ok = chunk->GetAttribute (attrName, value); + NS_ASSERT (ok); + os << attrName << "=" << value; + if ((j + 1) < item.tid.GetAttributeListN ()) + { + os << ","; + } + } + } + os << ")"; + break; + } + } + if (i.HasNext ()) + { + os << " "; + } + } + } PacketMetadata::ItemIterator diff --git a/src/common/trailer.cc b/src/common/trailer.cc index 98cdaa6b6..fbe972945 100644 --- a/src/common/trailer.cc +++ b/src/common/trailer.cc @@ -11,7 +11,7 @@ TypeId Trailer::GetTypeId (void) { static TypeId tid = TypeId ("ns3::Trailer") - .SetParent () + .SetParent () ; return tid; } diff --git a/src/common/trailer.h b/src/common/trailer.h index f515211eb..d4aaea028 100644 --- a/src/common/trailer.h +++ b/src/common/trailer.h @@ -22,7 +22,7 @@ #ifndef TRAILER_H #define TRAILER_H -#include "ns3/object-base.h" +#include "chunk.h" #include "buffer.h" #include @@ -38,7 +38,7 @@ namespace ns3 { * - a default constructor: is used by the internal implementation * if the Packet class. */ -class Trailer : public ObjectBase +class Trailer : public Chunk { public: static TypeId GetTypeId (void); diff --git a/src/common/wscript b/src/common/wscript index 13a27d50b..89db5ace1 100644 --- a/src/common/wscript +++ b/src/common/wscript @@ -9,6 +9,7 @@ def build(bld): 'packet.cc', 'tags.cc', 'tag-registry.cc', + 'chunk.cc', 'header.cc', 'trailer.cc', 'pcap-writer.cc', @@ -20,6 +21,7 @@ def build(bld): headers.module = 'common' headers.source = [ 'buffer.h', + 'chunk.h', 'header.h', 'trailer.h', 'tags.h', From c8f4d234ea2272973be7ebf21f6683320a29e39e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 17:47:45 -0700 Subject: [PATCH 19/35] improve doxygen. --- src/core/trace-source-accessor.h | 3 +- src/core/type-id.cc | 28 ------------ src/core/type-id.h | 78 +++++++++++++++++++++++++------- 3 files changed, 64 insertions(+), 45 deletions(-) diff --git a/src/core/trace-source-accessor.h b/src/core/trace-source-accessor.h index c244b2e40..48ea7ac0a 100644 --- a/src/core/trace-source-accessor.h +++ b/src/core/trace-source-accessor.h @@ -31,7 +31,8 @@ class ObjectBase; /** * \brief control access to objects' trace sources * - * This class abstracts the kind of trace source to which we want to connect. + * This class abstracts the kind of trace source to which we want to connect + * and provides services to Connect and Disconnect a sink to a trace source. */ class TraceSourceAccessor { diff --git a/src/core/type-id.cc b/src/core/type-id.cc index 1d0c26b69..1f23c8241 100644 --- a/src/core/type-id.cc +++ b/src/core/type-id.cc @@ -16,14 +16,12 @@ public: IidManager (); uint16_t AllocateUid (std::string name); void SetParent (uint16_t uid, uint16_t parent); - void SetTypeName (uint16_t uid, std::string typeName); void SetGroupName (uint16_t uid, std::string groupName); void AddConstructor (uint16_t uid, ns3::Callback callback); void HideFromDocumentation (uint16_t uid); uint16_t GetUid (std::string name) const; std::string GetName (uint16_t uid) const; uint16_t GetParent (uint16_t uid) const; - std::string GetTypeName (uint16_t uid) const; std::string GetGroupName (uint16_t uid) const; ns3::Callback GetConstructor (uint16_t uid) const; bool HasConstructor (uint16_t uid) const; @@ -70,7 +68,6 @@ private: struct IidInformation { std::string name; uint16_t parent; - std::string typeName; std::string groupName; bool hasConstructor; ns3::Callback constructor; @@ -104,7 +101,6 @@ IidManager::AllocateUid (std::string name) struct IidInformation information; information.name = name; information.parent = 0; - information.typeName = ""; information.groupName = ""; information.hasConstructor = false; information.mustHideFromDocumentation = false; @@ -129,12 +125,6 @@ IidManager::SetParent (uint16_t uid, uint16_t parent) information->parent = parent; } void -IidManager::SetTypeName (uint16_t uid, std::string typeName) -{ - struct IidInformation *information = LookupInformation (uid); - information->typeName = typeName; -} -void IidManager::SetGroupName (uint16_t uid, std::string groupName) { struct IidInformation *information = LookupInformation (uid); @@ -187,12 +177,6 @@ IidManager::GetParent (uint16_t uid) const return information->parent; } std::string -IidManager::GetTypeName (uint16_t uid) const -{ - struct IidInformation *information = LookupInformation (uid); - return information->typeName; -} -std::string IidManager::GetGroupName (uint16_t uid) const { struct IidInformation *information = LookupInformation (uid); @@ -464,12 +448,6 @@ TypeId::SetGroupName (std::string groupName) return *this; } TypeId -TypeId::SetTypeName (std::string typeName) -{ - Singleton::Get ()->SetTypeName (m_tid, typeName); - return *this; -} -TypeId TypeId::GetParent (void) const { uint16_t parent = Singleton::Get ()->GetParent (m_tid); @@ -491,12 +469,6 @@ TypeId::GetGroupName (void) const std::string groupName = Singleton::Get ()->GetGroupName (m_tid); return groupName; } -std::string -TypeId::GetTypeName (void) const -{ - std::string typeName = Singleton::Get ()->GetTypeName (m_tid); - return typeName; -} std::string TypeId::GetName (void) const diff --git a/src/core/type-id.h b/src/core/type-id.h index 3207ff1c0..548134785 100644 --- a/src/core/type-id.h +++ b/src/core/type-id.h @@ -90,16 +90,21 @@ public: */ TypeId GetParent (void) const; + /** + * \param other a parent TypeId + * \returns true if the input TypeId is really a parent + * of this TypeId, false otherwise. + * + * Calling this method is roughly similar to calling dynamic_cast + * except that you do not need object instances: you can do the check + * with TypeId instances instead. + */ bool IsChildOf (TypeId other) const; /** * \returns the name of the group associated to this TypeId. */ std::string GetGroupName (void) const; - /** - * \returns the fully-qualified C++ typename of this TypeId. - */ - std::string GetTypeName (void) const; /** * \returns the name of this interface. @@ -121,6 +126,11 @@ public: * index is i. */ std::string GetAttributeName (uint32_t i) const; + /** + * \param i index into attribute array. + * \returns the help text associated to the attribute whose + * index is i. + */ std::string GetAttributeHelp (uint32_t i) const; /** * \param i index into attribute array @@ -129,21 +139,63 @@ public: */ std::string GetAttributeFullName (uint32_t i) const; + /** + * \param i index into attribute array. + * \returns the value with which the associated attribute + * is initialized. + */ Attribute GetAttributeInitialValue (uint32_t i) const; + /** + * \param i index into attribute array. + * \returns the flags associated to the requested attribute. + */ uint32_t GetAttributeFlags (uint32_t i) const; + /** + * \param i index into attribute array. + * \returns the checker associated to the requested attribute. + */ Ptr GetAttributeChecker (uint32_t i) const; - - - uint32_t GetTraceSourceN (void) const; - std::string GetTraceSourceName (uint32_t i) const; - std::string GetTraceSourceHelp (uint32_t i) const; - Ptr GetTraceSourceAccessor (uint32_t i) const; + /** + * \param i index into attribute array. + * \returns the accessor associated to the requested attribute. + */ Ptr GetAttributeAccessor (uint32_t i) const; + /** + * \returns a callback which can be used to instanciate an object + * of this type. + */ Callback GetConstructor (void) const; + /** + * \returns true if this TypeId should be hidden from the user, + * false otherwise. + */ bool MustHideFromDocumentation (void) const; + + /** + * \returns the number of trace sources defined in this TypeId. + */ + uint32_t GetTraceSourceN (void) const; + /** + * \param i index into trace source array. + * \returns the name of the requested trace source. + */ + std::string GetTraceSourceName (uint32_t i) const; + /** + * \param i index into trace source array. + * \returns the help text of the requested trace source. + */ + std::string GetTraceSourceHelp (uint32_t i) const; + /** + * \param i index into trace source array. + * \returns the accessor used to get access to the requested + * trace source. + */ + Ptr GetTraceSourceAccessor (uint32_t i) const; + + /** * \param tid the TypeId of the base class. * \return this TypeId instance. @@ -170,12 +222,6 @@ public: * scheme. */ TypeId SetGroupName (std::string groupName); - - /** - * \param typeName the fully-qualified C++ typename of this TypeId. - * \returns this TypeId instance. - */ - TypeId SetTypeName (std::string typeName); /** * \returns this TypeId instance From 30525bef9ab285c3148705dfda624a03e8bc4b8b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 10:30:59 -0700 Subject: [PATCH 20/35] make sure the Rx trace event also gets the llc header to be symetric with the tx event. --- src/devices/wifi/wifi-net-device.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/devices/wifi/wifi-net-device.cc b/src/devices/wifi/wifi-net-device.cc index 840d635bc..8475dcea0 100644 --- a/src/devices/wifi/wifi-net-device.cc +++ b/src/devices/wifi/wifi-net-device.cc @@ -302,10 +302,10 @@ WifiNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb) void WifiNetDevice::ForwardUp (Ptr packet, const Mac48Address &from) { + m_rxLogger (packet, from); LlcSnapHeader llc; packet->RemoveHeader (llc); m_forwardUp (this, packet, llc.GetType (), from); - m_rxLogger (packet, from); } void From 2f666d9622ca61d8ecde9e03187484f05be9fe61 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 10:31:50 -0700 Subject: [PATCH 21/35] make sure all headers and trailers gets a constructor registered in their TypeId. --- src/devices/wifi/mgt-headers.cc | 4 ++++ src/devices/wifi/wifi-mac-header.cc | 1 + src/devices/wifi/wifi-mac-trailer.cc | 1 + src/internet-node/arp-header.cc | 1 + src/internet-node/ipv4-header.cc | 1 + src/internet-node/tcp-header.cc | 1 + src/internet-node/udp-header.cc | 1 + src/node/ethernet-header.cc | 1 + src/node/ethernet-trailer.cc | 1 + src/node/llc-snap-header.cc | 1 + src/routing/olsr/olsr-header.cc | 2 ++ 11 files changed, 15 insertions(+) diff --git a/src/devices/wifi/mgt-headers.cc b/src/devices/wifi/mgt-headers.cc index 87857bfd7..d18baaecd 100644 --- a/src/devices/wifi/mgt-headers.cc +++ b/src/devices/wifi/mgt-headers.cc @@ -66,6 +66,7 @@ MgtProbeRequestHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::MgtProbeRequestHeader") .SetParent
() + .AddConstructor () ; return tid; } @@ -144,6 +145,7 @@ MgtProbeResponseHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::MgtProbeResponseHeader") .SetParent
() + .AddConstructor () ; return tid; } @@ -253,6 +255,7 @@ MgtAssocRequestHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::MgtAssocRequestHeader") .SetParent
() + .AddConstructor () ; return tid; } @@ -334,6 +337,7 @@ MgtAssocResponseHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::MgtAssocResponseHeader") .SetParent
() + .AddConstructor () ; return tid; } diff --git a/src/devices/wifi/wifi-mac-header.cc b/src/devices/wifi/wifi-mac-header.cc index 60e680f71..065ddb39b 100644 --- a/src/devices/wifi/wifi-mac-header.cc +++ b/src/devices/wifi/wifi-mac-header.cc @@ -773,6 +773,7 @@ WifiMacHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::WifiMacHeader") .SetParent
() + .AddConstructor () ; return tid; } diff --git a/src/devices/wifi/wifi-mac-trailer.cc b/src/devices/wifi/wifi-mac-trailer.cc index ab156dda5..f6d11915a 100644 --- a/src/devices/wifi/wifi-mac-trailer.cc +++ b/src/devices/wifi/wifi-mac-trailer.cc @@ -35,6 +35,7 @@ WifiMacTrailer::GetTypeId (void) { static TypeId tid = TypeId ("ns3::WifiMacTrailer") .SetParent () + .AddConstructor () ; return tid; } diff --git a/src/internet-node/arp-header.cc b/src/internet-node/arp-header.cc index 0ff15bd1d..a4a0b04fb 100644 --- a/src/internet-node/arp-header.cc +++ b/src/internet-node/arp-header.cc @@ -88,6 +88,7 @@ ArpHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::ArpHeader") .SetParent
() + .AddConstructor () ; return tid; } diff --git a/src/internet-node/ipv4-header.cc b/src/internet-node/ipv4-header.cc index e8296018f..6d9abcffd 100644 --- a/src/internet-node/ipv4-header.cc +++ b/src/internet-node/ipv4-header.cc @@ -185,6 +185,7 @@ Ipv4Header::GetTypeId (void) { static TypeId tid = TypeId ("ns3::Ipv4Header") .SetParent
() + .AddConstructor () ; return tid; } diff --git a/src/internet-node/tcp-header.cc b/src/internet-node/tcp-header.cc index 989b9680e..f721082ca 100644 --- a/src/internet-node/tcp-header.cc +++ b/src/internet-node/tcp-header.cc @@ -139,6 +139,7 @@ TcpHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::TcpHeader") .SetParent
() + .AddConstructor () ; return tid; } diff --git a/src/internet-node/udp-header.cc b/src/internet-node/udp-header.cc index 7682ae880..15aa6ba2d 100644 --- a/src/internet-node/udp-header.cc +++ b/src/internet-node/udp-header.cc @@ -98,6 +98,7 @@ UdpHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::UdpHeader") .SetParent
() + .AddConstructor () ; return tid; } diff --git a/src/node/ethernet-header.cc b/src/node/ethernet-header.cc index 45b90f478..9bafd3f30 100644 --- a/src/node/ethernet-header.cc +++ b/src/node/ethernet-header.cc @@ -105,6 +105,7 @@ EthernetHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::EthernetHeader") .SetParent
() + .AddConstructor () ; return tid; } diff --git a/src/node/ethernet-trailer.cc b/src/node/ethernet-trailer.cc index e1c0396f8..d032a1314 100644 --- a/src/node/ethernet-trailer.cc +++ b/src/node/ethernet-trailer.cc @@ -91,6 +91,7 @@ EthernetTrailer::GetTypeId (void) { static TypeId tid = TypeId ("ns3::EthernetTrailer") .SetParent () + .AddConstructor () ; return tid; } diff --git a/src/node/llc-snap-header.cc b/src/node/llc-snap-header.cc index 51880a2bc..8aacb47ff 100644 --- a/src/node/llc-snap-header.cc +++ b/src/node/llc-snap-header.cc @@ -52,6 +52,7 @@ LlcSnapHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::LlcSnapHeader") .SetParent
() + .AddConstructor () ; return tid; } diff --git a/src/routing/olsr/olsr-header.cc b/src/routing/olsr/olsr-header.cc index bc4c3a17b..51e297078 100644 --- a/src/routing/olsr/olsr-header.cc +++ b/src/routing/olsr/olsr-header.cc @@ -107,6 +107,7 @@ PacketHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::olsr::PacketHeader") .SetParent
() + .AddConstructor () ; return tid; } @@ -162,6 +163,7 @@ MessageHeader::GetTypeId (void) { static TypeId tid = TypeId ("ns3::olsr::MessageHeader") .SetParent
() + .AddConstructor () ; return tid; } From e5b7c283753729ea71bc05866e6af61eb3331061 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 10:32:06 -0700 Subject: [PATCH 22/35] add Print to the Chunk base class. --- src/common/chunk.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/chunk.h b/src/common/chunk.h index 7e35fdba5..f2a6a9404 100644 --- a/src/common/chunk.h +++ b/src/common/chunk.h @@ -12,6 +12,7 @@ class Chunk : public ObjectBase static TypeId GetTypeId (void); virtual uint32_t Deserialize (Buffer::Iterator start) = 0; + virtual void Print (std::ostream &os) const = 0; }; } // namespace ns3 From 1518a1dbd1715a1288fd36f8c01cfb20ecd0af42 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 11:10:02 -0700 Subject: [PATCH 23/35] add extra trace sources --- src/devices/wifi/wifi-phy.cc | 13 +++++++++++-- src/devices/wifi/wifi-phy.h | 5 +++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/devices/wifi/wifi-phy.cc b/src/devices/wifi/wifi-phy.cc index 91af4a6c4..9cb60e53c 100644 --- a/src/devices/wifi/wifi-phy.cc +++ b/src/devices/wifi/wifi-phy.cc @@ -235,6 +235,14 @@ WifiPhy::GetTypeId (void) .AddTraceSource ("State", "The WifiPhy state", MakeTraceSourceAccessor (&WifiPhy::m_stateLogger)) + .AddTraceSource ("RxOk", + "A packet has been received successfully.", + MakeTraceSourceAccessor (&WifiPhy::m_syncOkCallback)) + .AddTraceSource ("RxError", + "A packet has been received unsuccessfully.", + MakeTraceSourceAccessor (&WifiPhy::m_syncErrorCallback)) + .AddTraceSource ("Tx", "Packet transmission is starting.", + MakeTraceSourceAccessor (&WifiPhy::m_txCallback)) ; return tid; } @@ -363,12 +371,12 @@ WifiPhy::SetChannel (Ptr channel) void WifiPhy::SetReceiveOkCallback (SyncOkCallback callback) { - m_syncOkCallback = callback; + m_syncOkCallback.ConnectWithoutContext (callback); } void WifiPhy::SetReceiveErrorCallback (SyncErrorCallback callback) { - m_syncErrorCallback = callback; + m_syncErrorCallback.ConnectWithoutContext (callback); } void WifiPhy::StartReceivePacket (Ptr packet, @@ -472,6 +480,7 @@ WifiPhy::SendPacket (Ptr packet, WifiMode txMode, WifiPreamble pre */ NS_ASSERT (!IsStateTx ()); + m_txCallback (packet, txMode, preamble, txPower); Time txDuration = CalculateTxDuration (packet->GetSize (), txMode, preamble); NotifyTxStart (txDuration); SwitchToTx (txDuration); diff --git a/src/devices/wifi/wifi-phy.h b/src/devices/wifi/wifi-phy.h index 5a97b5699..dc4d3d0e1 100644 --- a/src/devices/wifi/wifi-phy.h +++ b/src/devices/wifi/wifi-phy.h @@ -343,8 +343,9 @@ private: Time m_previousStateChangeTime; Ptr m_channel; - SyncOkCallback m_syncOkCallback; - SyncErrorCallback m_syncErrorCallback; + TracedCallback, double, WifiMode, enum WifiPreamble> m_syncOkCallback; + TracedCallback, double> m_syncErrorCallback; + TracedCallback,WifiMode,WifiPreamble,uint8_t> m_txCallback; Modes m_modes; Listeners m_listeners; EventId m_endSyncEvent; From cabfd83031903064bb99a953a67d246410b22e89 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 11:10:37 -0700 Subject: [PATCH 24/35] improve pretty-printing output. --- src/devices/wifi/wifi-mac-header.cc | 5 ++--- src/internet-node/arp-header.cc | 8 ++------ src/internet-node/ipv4-header.cc | 5 ++--- src/internet-node/udp-header.cc | 5 ++--- src/node/ethernet-header.cc | 2 +- src/node/ethernet-trailer.cc | 2 +- src/node/llc-snap-header.cc | 3 +-- 7 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/devices/wifi/wifi-mac-header.cc b/src/devices/wifi/wifi-mac-header.cc index 065ddb39b..109906d98 100644 --- a/src/devices/wifi/wifi-mac-header.cc +++ b/src/devices/wifi/wifi-mac-header.cc @@ -786,11 +786,10 @@ WifiMacHeader::GetInstanceTypeId (void) const void WifiMacHeader::PrintFrameControl (std::ostream &os) const { - os << "(" - << "ToDS=" << m_ctrlToDs << ", FromDS=" << m_ctrlFromDs + os << "ToDS=" << m_ctrlToDs << ", FromDS=" << m_ctrlFromDs << ", MoreFrag=" << m_ctrlMoreFrag << ", Retry=" << m_ctrlRetry << ", MoreData=" << m_ctrlMoreData - << ")"; + ; } void diff --git a/src/internet-node/arp-header.cc b/src/internet-node/arp-header.cc index a4a0b04fb..5bf781c24 100644 --- a/src/internet-node/arp-header.cc +++ b/src/internet-node/arp-header.cc @@ -102,24 +102,20 @@ ArpHeader::Print (std::ostream &os) const { if (IsRequest ()) { - os << "(" - << "request " + os << "request " << "source mac: " << m_macSource << " " << "source ipv4: " << m_ipv4Source << " " << "dest ipv4: " << m_ipv4Dest - << ")" ; } else { NS_ASSERT (IsReply ()); - os << "(" - << "reply " + os << "reply " << "source mac: " << m_macSource << " " << "source ipv4: " << m_ipv4Source << " " << "dest mac: " << m_macDest << " " << "dest ipv4: " < " << m_destination ; } diff --git a/src/internet-node/udp-header.cc b/src/internet-node/udp-header.cc index 15aa6ba2d..08a2962a0 100644 --- a/src/internet-node/udp-header.cc +++ b/src/internet-node/udp-header.cc @@ -110,9 +110,8 @@ UdpHeader::GetInstanceTypeId (void) const void UdpHeader::Print (std::ostream &os) const { - os << "(" - << "length: " << m_payloadSize + GetSerializedSize () - << ") " + os << "length: " << m_payloadSize + GetSerializedSize () + << " " << m_sourcePort << " > " << m_destinationPort ; } diff --git a/src/node/ethernet-header.cc b/src/node/ethernet-header.cc index 9bafd3f30..ef9c40956 100644 --- a/src/node/ethernet-header.cc +++ b/src/node/ethernet-header.cc @@ -120,7 +120,7 @@ EthernetHeader::Print (std::ostream &os) const // ethernet, right ? if (m_enPreambleSfd) { - os << " preamble/sfd=" << m_preambleSfd << ","; + os << "preamble/sfd=" << m_preambleSfd << ","; } os << " length/type=0x" << std::hex << m_lengthType << std::dec diff --git a/src/node/ethernet-trailer.cc b/src/node/ethernet-trailer.cc index d032a1314..a2cb27583 100644 --- a/src/node/ethernet-trailer.cc +++ b/src/node/ethernet-trailer.cc @@ -103,7 +103,7 @@ EthernetTrailer::GetInstanceTypeId (void) const void EthernetTrailer::Print (std::ostream &os) const { - os << " fcs=" << m_fcs; + os << "fcs=" << m_fcs; } uint32_t EthernetTrailer::GetSerializedSize (void) const diff --git a/src/node/llc-snap-header.cc b/src/node/llc-snap-header.cc index 8aacb47ff..7b079d158 100644 --- a/src/node/llc-snap-header.cc +++ b/src/node/llc-snap-header.cc @@ -64,11 +64,10 @@ LlcSnapHeader::GetInstanceTypeId (void) const void LlcSnapHeader::Print (std::ostream &os) const { - os << "(type 0x"; + os << "type 0x"; os.setf (std::ios::hex, std::ios::basefield); os << m_etherType; os.setf (std::ios::dec, std::ios::basefield); - os << ")"; } void From 26f046edd5302b3b18189cf2cf8208bfcd9d8109 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 11:11:34 -0700 Subject: [PATCH 25/35] fragmentEnd is not zero when this is a fragment. --- src/common/packet-metadata.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/packet-metadata.cc b/src/common/packet-metadata.cc index fb0c78be4..0d6d3afd7 100644 --- a/src/common/packet-metadata.cc +++ b/src/common/packet-metadata.cc @@ -1069,7 +1069,7 @@ PacketMetadata::ItemIterator::Next (void) item.currentTrimedFromStart = extraItem.fragmentStart; item.currentTrimedFromEnd = extraItem.fragmentEnd - smallItem.size; item.currentSize = extraItem.fragmentEnd - extraItem.fragmentStart; - if (extraItem.fragmentStart != 0 || extraItem.fragmentEnd != 0) + if (extraItem.fragmentStart != 0 || extraItem.fragmentEnd != smallItem.size) { item.isFragment = true; } From 10bbdc4176dcc0bb1ee557b3b13c58321bc74bf3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 11:19:05 -0700 Subject: [PATCH 26/35] add SetAddress methods. --- src/devices/csma/csma-net-device.cc | 6 ++++++ src/devices/csma/csma-net-device.h | 2 ++ src/devices/point-to-point/point-to-point-net-device.cc | 6 ++++++ src/devices/point-to-point/point-to-point-net-device.h | 2 ++ 4 files changed, 16 insertions(+) diff --git a/src/devices/csma/csma-net-device.cc b/src/devices/csma/csma-net-device.cc index 87ed536d0..4534f0866 100644 --- a/src/devices/csma/csma-net-device.cc +++ b/src/devices/csma/csma-net-device.cc @@ -111,6 +111,12 @@ CsmaNetDevice::DoDispose () NetDevice::DoDispose (); } +void +CsmaNetDevice::SetAddress (Mac48Address self) +{ + m_address = self; +} + void CsmaNetDevice::SetSendEnable (bool sendEnable) { diff --git a/src/devices/csma/csma-net-device.h b/src/devices/csma/csma-net-device.h index b8b5f3163..df1787757 100644 --- a/src/devices/csma/csma-net-device.h +++ b/src/devices/csma/csma-net-device.h @@ -180,6 +180,8 @@ enum CsmaEncapsulationMode { void SetSendEnable (bool); void SetReceiveEnable (bool); + void SetAddress (Mac48Address self); + // inherited from NetDevice base class. virtual void SetName(const std::string name); diff --git a/src/devices/point-to-point/point-to-point-net-device.cc b/src/devices/point-to-point/point-to-point-net-device.cc index c8d68d2c2..2d57a8688 100644 --- a/src/devices/point-to-point/point-to-point-net-device.cc +++ b/src/devices/point-to-point/point-to-point-net-device.cc @@ -87,6 +87,12 @@ PointToPointNetDevice::PointToPointNetDevice () PointToPointNetDevice::~PointToPointNetDevice () {} +void +PointToPointNetDevice::SetAddress (Mac48Address self) +{ + m_address = self; +} + void PointToPointNetDevice::AddHeader(Ptr p, uint16_t protocolNumber) { diff --git a/src/devices/point-to-point/point-to-point-net-device.h b/src/devices/point-to-point/point-to-point-net-device.h index ba0160508..6d7737b3a 100644 --- a/src/devices/point-to-point/point-to-point-net-device.h +++ b/src/devices/point-to-point/point-to-point-net-device.h @@ -145,6 +145,8 @@ public: */ void Receive (Ptr p); + void SetAddress (Mac48Address self); + // inherited from NetDevice base class. virtual void SetName(const std::string name); virtual std::string GetName(void) const; From a3ed490b7f493cb19189d0cd70bd33a0a545b535 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 11:19:29 -0700 Subject: [PATCH 27/35] call SetAddress during construction to assign MAC-level addresses. --- src/helper/csma-helper.cc | 1 + src/helper/point-to-point-helper.cc | 2 ++ src/helper/wifi-helper.cc | 1 + 3 files changed, 4 insertions(+) diff --git a/src/helper/csma-helper.cc b/src/helper/csma-helper.cc index bd4723b26..f941306c1 100644 --- a/src/helper/csma-helper.cc +++ b/src/helper/csma-helper.cc @@ -55,6 +55,7 @@ CsmaHelper::Build (const NodeContainer &c, Ptr channel) { Ptr node = *i; Ptr device = m_deviceFactory.Create (); + device->SetAddress (Mac48Address::Allocate ()); node->AddDevice (device); Ptr queue = m_queueFactory.Create (); device->AddQueue (queue); diff --git a/src/helper/point-to-point-helper.cc b/src/helper/point-to-point-helper.cc index 33f34979d..5a7570d80 100644 --- a/src/helper/point-to-point-helper.cc +++ b/src/helper/point-to-point-helper.cc @@ -48,10 +48,12 @@ PointToPointHelper::Build (Ptr a, Ptr b) NetDeviceContainer container; Ptr devA = CreateObject (); + devA->SetAddress (Mac48Address::Allocate ()); a->AddDevice (devA); Ptr queueA = m_queueFactory.Create (); devA->AddQueue (queueA); Ptr devB = CreateObject (); + devB->SetAddress (Mac48Address::Allocate ()); b->AddDevice (devB); Ptr queueB = m_queueFactory.Create (); devB->AddQueue (queueB); diff --git a/src/helper/wifi-helper.cc b/src/helper/wifi-helper.cc index d9b990574..76926b718 100644 --- a/src/helper/wifi-helper.cc +++ b/src/helper/wifi-helper.cc @@ -110,6 +110,7 @@ WifiHelper::Build (NodeContainer c, Ptr channel) const Ptr manager = m_stationManager.Create (); Ptr mac = m_mac.Create (); Ptr phy = m_phy.Create (); + mac->SetAddress (Mac48Address::Allocate ()); device->SetMac (mac); device->SetPhy (phy); device->SetRemoteStationManager (manager); From 6534020b54bc0f669c16c2aa71df805a69b562d9 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 12:30:00 -0700 Subject: [PATCH 28/35] add failing tests --- src/common/packet-metadata-test.cc | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/common/packet-metadata-test.cc b/src/common/packet-metadata-test.cc index e274bbc4d..a44046a4e 100644 --- a/src/common/packet-metadata-test.cc +++ b/src/common/packet-metadata-test.cc @@ -547,6 +547,63 @@ PacketMetadataTest::RunTests (void) p->RemoveAtStart (8+10+8); CHECK_HISTORY (p, 1, 8); + p = Create (0); + ADD_HEADER (p, 8); + REM_HEADER (p, 8); + CHECK_HISTORY (p, 0); + + p = Create (0); + ADD_TRAILER (p, 8); + REM_TRAILER (p, 8); + CHECK_HISTORY (p, 0); + + p = Create (0); + ADD_HEADER (p, 8); + p->RemoveAtStart (8); + CHECK_HISTORY (p, 0); + + p = Create (0); + ADD_HEADER (p, 8); + ADD_TRAILER (p, 8); + REM_TRAILER (p, 8); + REM_HEADER (p, 8); + CHECK_HISTORY (p, 0); + + p = Create (0); + ADD_HEADER (p, 8); + ADD_TRAILER (p, 8); + REM_HEADER (p, 8); + REM_TRAILER (p, 8); + CHECK_HISTORY (p, 0); + + p = Create (0); + ADD_HEADER (p, 8); + ADD_TRAILER (p, 8); + REM_TRAILER (p, 8); + p->RemoveAtStart (8); + CHECK_HISTORY (p, 0); + + p = Create (0); + ADD_HEADER (p, 8); + ADD_TRAILER (p, 8); + REM_HEADER (p, 8); + p->RemoveAtEnd (8); + CHECK_HISTORY (p, 0); + + p = Create (0); + ADD_HEADER (p, 8); + ADD_TRAILER (p, 8); + REM_TRAILER (p, 8); + p->RemoveAtEnd (8); + CHECK_HISTORY (p, 0); + + p = Create (0); + ADD_HEADER (p, 8); + ADD_TRAILER (p, 8); + REM_HEADER (p, 8); + p->RemoveAtStart (8); + CHECK_HISTORY (p, 0); + return ok; } From c6aa4c8fb4f11483154c898a3fc2406181638807 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 12:30:19 -0700 Subject: [PATCH 29/35] fix failing tests --- src/common/packet-metadata.cc | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/common/packet-metadata.cc b/src/common/packet-metadata.cc index 0d6d3afd7..54b2a7997 100644 --- a/src/common/packet-metadata.cc +++ b/src/common/packet-metadata.cc @@ -750,7 +750,7 @@ PacketMetadata::RemoveHeader (const Header &header, uint32_t size) { m_used = m_head; } - if (item.next == 0xffff) + if (m_head == m_tail) { m_head = 0xffff; m_tail = 0xffff; @@ -808,7 +808,7 @@ PacketMetadata::RemoveTrailer (const Trailer &trailer, uint32_t size) { m_used = m_tail; } - if (item.prev == 0xffff) + if (m_head == m_tail) { m_head = 0xffff; m_tail = 0xffff; @@ -904,7 +904,15 @@ PacketMetadata::RemoveAtStart (uint32_t start) if (itemRealSize <= leftToRemove) { // remove from list. - m_head = item.next; + if (m_head == m_tail) + { + m_head = 0xffff; + m_tail = 0xffff; + } + else + { + m_head = item.next; + } leftToRemove -= itemRealSize; } else @@ -962,7 +970,15 @@ PacketMetadata::RemoveAtEnd (uint32_t end) if (itemRealSize <= leftToRemove) { // remove from list. - m_tail = item.prev; + if (m_head == m_tail) + { + m_head = 0xffff; + m_tail = 0xffff; + } + else + { + m_tail = item.prev; + } leftToRemove -= itemRealSize; } else From 5b42ef5b024f3ae4be8dee74ad8bd15165e0800c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 12:36:09 -0700 Subject: [PATCH 30/35] make sure the trace hooks get a full packet as input. --- src/devices/wifi/wifi-phy.cc | 14 ++++++++------ src/devices/wifi/wifi-phy.h | 8 +++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/devices/wifi/wifi-phy.cc b/src/devices/wifi/wifi-phy.cc index 9cb60e53c..aaac0abe5 100644 --- a/src/devices/wifi/wifi-phy.cc +++ b/src/devices/wifi/wifi-phy.cc @@ -237,12 +237,12 @@ WifiPhy::GetTypeId (void) MakeTraceSourceAccessor (&WifiPhy::m_stateLogger)) .AddTraceSource ("RxOk", "A packet has been received successfully.", - MakeTraceSourceAccessor (&WifiPhy::m_syncOkCallback)) + MakeTraceSourceAccessor (&WifiPhy::m_rxOkTrace)) .AddTraceSource ("RxError", "A packet has been received unsuccessfully.", - MakeTraceSourceAccessor (&WifiPhy::m_syncErrorCallback)) + MakeTraceSourceAccessor (&WifiPhy::m_rxErrorTrace)) .AddTraceSource ("Tx", "Packet transmission is starting.", - MakeTraceSourceAccessor (&WifiPhy::m_txCallback)) + MakeTraceSourceAccessor (&WifiPhy::m_txTrace)) ; return tid; } @@ -371,12 +371,12 @@ WifiPhy::SetChannel (Ptr channel) void WifiPhy::SetReceiveOkCallback (SyncOkCallback callback) { - m_syncOkCallback.ConnectWithoutContext (callback); + m_syncOkCallback = callback; } void WifiPhy::SetReceiveErrorCallback (SyncErrorCallback callback) { - m_syncErrorCallback.ConnectWithoutContext (callback); + m_syncErrorCallback = callback; } void WifiPhy::StartReceivePacket (Ptr packet, @@ -480,7 +480,7 @@ WifiPhy::SendPacket (Ptr packet, WifiMode txMode, WifiPreamble pre */ NS_ASSERT (!IsStateTx ()); - m_txCallback (packet, txMode, preamble, txPower); + m_txTrace (packet, txMode, preamble, txPower); Time txDuration = CalculateTxDuration (packet->GetSize (), txMode, preamble); NotifyTxStart (txDuration); SwitchToTx (txDuration); @@ -1350,6 +1350,7 @@ WifiPhy::EndSync (Ptr packet, Ptr event) { NotifySyncEndOk (); SwitchFromSync (); + m_rxOkTrace (packet, snr, event->GetPayloadMode (), event->GetPreambleType ()); m_syncOkCallback (packet, snr, event->GetPayloadMode (), event->GetPreambleType ()); } else @@ -1357,6 +1358,7 @@ WifiPhy::EndSync (Ptr packet, Ptr event) /* failure. */ NotifySyncEndError (); SwitchFromSync (); + m_rxErrorTrace (packet, snr); m_syncErrorCallback (packet, snr); } } diff --git a/src/devices/wifi/wifi-phy.h b/src/devices/wifi/wifi-phy.h index dc4d3d0e1..68dabdfd2 100644 --- a/src/devices/wifi/wifi-phy.h +++ b/src/devices/wifi/wifi-phy.h @@ -343,9 +343,11 @@ private: Time m_previousStateChangeTime; Ptr m_channel; - TracedCallback, double, WifiMode, enum WifiPreamble> m_syncOkCallback; - TracedCallback, double> m_syncErrorCallback; - TracedCallback,WifiMode,WifiPreamble,uint8_t> m_txCallback; + SyncOkCallback m_syncOkCallback; + SyncErrorCallback m_syncErrorCallback; + TracedCallback, double, WifiMode, enum WifiPreamble> m_rxOkTrace; + TracedCallback, double> m_rxErrorTrace; + TracedCallback,WifiMode,WifiPreamble,uint8_t> m_txTrace; Modes m_modes; Listeners m_listeners; EventId m_endSyncEvent; From 574ed6701116c891fc141d66a180aaccae08c72b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 12:36:27 -0700 Subject: [PATCH 31/35] convert Packet::Print to use Chunk::Print --- src/common/packet.cc | 55 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/common/packet.cc b/src/common/packet.cc index cbebbe93b..0a996aaaa 100644 --- a/src/common/packet.cc +++ b/src/common/packet.cc @@ -186,7 +186,58 @@ Packet::PrintTags (std::ostream &os) const void Packet::Print (std::ostream &os) const { - //XXX + PacketMetadata::ItemIterator i = m_metadata.BeginItem (m_buffer); + while (i.HasNext ()) + { + PacketMetadata::Item item = i.Next (); + if (item.isFragment) + { + switch (item.type) { + case PacketMetadata::Item::PAYLOAD: + os << "Payload"; + break; + case PacketMetadata::Item::HEADER: + case PacketMetadata::Item::TRAILER: + os << item.tid.GetName (); + break; + } + os << " Fragment [" << item.currentTrimedFromStart<<":" + << (item.currentTrimedFromStart + item.currentSize) << "]"; + } + else + { + switch (item.type) { + case PacketMetadata::Item::PAYLOAD: + os << "Payload (size=" << item.currentSize << ")"; + break; + case PacketMetadata::Item::HEADER: + case PacketMetadata::Item::TRAILER: + os << item.tid.GetName () << " ("; + { + NS_ASSERT (item.tid.HasConstructor ()); + Callback constructor = item.tid.GetConstructor (); + NS_ASSERT (!constructor.IsNull ()); + ObjectBase *instance = constructor (); + NS_ASSERT (instance != 0); + Chunk *chunk = dynamic_cast (instance); + NS_ASSERT (chunk != 0); + chunk->Deserialize (item.current); + chunk->Print (os); + } + os << ")"; + break; + } + } + if (i.HasNext ()) + { + os << " "; + } + } +#if 0 + // The code below will work only if headers and trailers + // define the right attributes which is not the case for + // now. So, as a temporary measure, we use the + // headers' and trailers' Print method as shown above. PacketMetadata::ItemIterator i = m_metadata.BeginItem (m_buffer); while (i.HasNext ()) { @@ -245,7 +296,7 @@ Packet::Print (std::ostream &os) const os << " "; } } - +#endif } PacketMetadata::ItemIterator From 931e5140e22346949456a7a5473c3613fcce5898 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 12:41:06 -0700 Subject: [PATCH 32/35] add a bunch of trace sinks for demonstration --- samples/main-ap-wifi.cc | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/samples/main-ap-wifi.cc b/samples/main-ap-wifi.cc index b4778dab8..9c3bcbade 100644 --- a/samples/main-ap-wifi.cc +++ b/samples/main-ap-wifi.cc @@ -47,12 +47,32 @@ using namespace ns3; void -WifiNetDeviceTrace (Ptr p, Mac48Address address) +DevTxTrace (std::string context, Ptr p, Mac48Address address) { - std::cout << " ad=" << address << " p: " << p << std::endl; + std::cout << " TX to=" << address << " p: " << *p << std::endl; } void -WifiPhyStateTrace (Time start, Time duration, enum WifiPhy::State state) +DevRxTrace (std::string context, Ptr p, Mac48Address address) +{ + std::cout << " RX from=" << address << " p: " << *p << std::endl; +} +void +PhyRxOkTrace (std::string context, Ptr packet, double snr, WifiMode mode, enum WifiPreamble preamble) +{ + std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl; +} +void +PhyRxErrorTrace (std::string context, Ptr packet, double snr) +{ + std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl; +} +void +PhyTxTrace (std::string context, Ptr packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower) +{ + std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl; +} +void +PhyStateTrace (std::string context, Time start, Time duration, enum WifiPhy::State state) { std::cout << " state="; switch (state) { @@ -161,8 +181,12 @@ int main (int argc, char *argv[]) Simulator::StopAt (Seconds (44.0)); - //NodeList::ConnectWithoutContext ("/nodes/*/devices/*/*", MakeCallback (&WifiNetDeviceTrace)); - //NodeList::ConnectWithoutContext ("/nodes/*/devices/*/phy/state", MakeCallback (&WifiPhyStateTrace)); + Config::Connect ("/NodeList/*/DeviceList/*/Tx", MakeCallback (&DevTxTrace)); + Config::Connect ("/NodeList/*/DeviceList/*/Rx", MakeCallback (&DevRxTrace)); + Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxOk", MakeCallback (&PhyRxOkTrace)); + Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxError", MakeCallback (&PhyRxErrorTrace)); + Config::Connect ("/NodeList/*/DeviceList/*/Phy/Tx", MakeCallback (&PhyTxTrace)); + Config::Connect ("/NodeList/*/DeviceList/*/Phy/State", MakeCallback (&PhyStateTrace)); Simulator::Run (); From 295c1cd3d520339e32b983c941d3df39723bb7d0 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 12:42:54 -0700 Subject: [PATCH 33/35] move wifi examples to example directory --- samples/main-adhoc-wifi.cc => examples/wifi-adhoc.cc | 0 samples/main-ap-wifi.cc => examples/wifi-ap.cc | 0 examples/wscript | 7 +++++++ samples/wscript | 8 -------- 4 files changed, 7 insertions(+), 8 deletions(-) rename samples/main-adhoc-wifi.cc => examples/wifi-adhoc.cc (100%) rename samples/main-ap-wifi.cc => examples/wifi-ap.cc (100%) diff --git a/samples/main-adhoc-wifi.cc b/examples/wifi-adhoc.cc similarity index 100% rename from samples/main-adhoc-wifi.cc rename to examples/wifi-adhoc.cc diff --git a/samples/main-ap-wifi.cc b/examples/wifi-ap.cc similarity index 100% rename from samples/main-ap-wifi.cc rename to examples/wifi-ap.cc diff --git a/examples/wscript b/examples/wscript index 2074067fc..2c43c92d4 100644 --- a/examples/wscript +++ b/examples/wscript @@ -66,4 +66,11 @@ def build(bld): ['point-to-point', 'internet-node']) obj.source = 'tcp-small-transfer-oneloss.cc' + obj = bld.create_ns3_program('wifi-adhoc', + ['core', 'simulator', 'mobility', 'wifi']) + obj.source = 'wifi-adhoc.cc' + + obj = bld.create_ns3_program('wifi-ap', + ['core', 'simulator', 'mobility', 'wifi']) + obj.source = 'wifi-ap.cc' diff --git a/samples/wscript b/samples/wscript index e7868460b..067743af3 100644 --- a/samples/wscript +++ b/samples/wscript @@ -27,14 +27,6 @@ def build(bld): obj = bld.create_ns3_program('main-random-topology', ['core', 'simulator', 'mobility']) obj.source = 'main-random-topology.cc' - - obj = bld.create_ns3_program('main-adhoc-wifi', - ['core', 'simulator', 'mobility', 'wifi']) - obj.source = 'main-adhoc-wifi.cc' - - obj = bld.create_ns3_program('main-ap-wifi', - ['core', 'simulator', 'mobility', 'wifi']) - obj.source = 'main-ap-wifi.cc' obj = bld.create_ns3_program('main-random-walk', ['core', 'simulator', 'mobility']) From 5cd592fe0664fd9372cc0bac77893ccaef46d848 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 13:10:01 -0700 Subject: [PATCH 34/35] remove template methods. --- src/common/packet.cc | 34 ++++++++++++++++++++++ src/common/packet.h | 67 +++----------------------------------------- 2 files changed, 38 insertions(+), 63 deletions(-) diff --git a/src/common/packet.cc b/src/common/packet.cc index 0a996aaaa..c3886b936 100644 --- a/src/common/packet.cc +++ b/src/common/packet.cc @@ -123,6 +123,40 @@ Packet::GetSize (void) const return m_buffer.GetSize (); } +void +Packet::AddHeader (const Header &header) +{ + uint32_t size = header.GetSerializedSize (); + m_buffer.AddAtStart (size); + header.Serialize (m_buffer.Begin ()); + m_metadata.AddHeader (header, size); +} +uint32_t +Packet::RemoveHeader (Header &header) +{ + uint32_t deserialized = header.Deserialize (m_buffer.Begin ()); + m_buffer.RemoveAtStart (deserialized); + m_metadata.RemoveHeader (header, deserialized); + return deserialized; +} +void +Packet::AddTrailer (const Trailer &trailer) +{ + uint32_t size = trailer.GetSerializedSize (); + m_buffer.AddAtEnd (size); + Buffer::Iterator end = m_buffer.End (); + trailer.Serialize (end); + m_metadata.AddTrailer (trailer, size); +} +uint32_t +Packet::RemoveTrailer (Trailer &trailer) +{ + uint32_t deserialized = trailer.Deserialize (m_buffer.End ()); + m_buffer.RemoveAtEnd (deserialized); + m_metadata.RemoveTrailer (trailer, deserialized); + return deserialized; +} + void Packet::AddAtEnd (Ptr packet) { diff --git a/src/common/packet.h b/src/common/packet.h index 0bc5d5483..629d7cf2c 100644 --- a/src/common/packet.h +++ b/src/common/packet.h @@ -126,8 +126,7 @@ public: * * \param header a reference to the header to add to this packet. */ - template - void AddHeader (T const &header); + void AddHeader (const Header & header); /** * Deserialize and remove the header from the internal buffer. * This method invokes Deserialize. @@ -135,8 +134,7 @@ public: * \param header a reference to the header to remove from the internal buffer. * \returns the number of bytes removed from the packet. */ - template - uint32_t RemoveHeader (T &header); + uint32_t RemoveHeader (Header &header); /** * Add trailer to this packet. This method invokes the * GetSerializedSize and Serialize @@ -145,8 +143,7 @@ public: * * \param trailer a reference to the trailer to add to this packet. */ - template - void AddTrailer (T const &trailer); + void AddTrailer (const Trailer &trailer); /** * Remove a deserialized trailer from the internal buffer. * This method invokes the Deserialize method. @@ -154,8 +151,7 @@ public: * \param trailer a reference to the trailer to remove from the internal buffer. * \returns the number of bytes removed from the end of the packet. */ - template - uint32_t RemoveTrailer (T &trailer); + uint32_t RemoveTrailer (Trailer &trailer); /** * \param tag a pointer to the tag to attach to this packet. * @@ -394,61 +390,6 @@ std::ostream& operator<< (std::ostream& os, const Packet &packet); namespace ns3 { -template -void -Packet::AddHeader (T const &header) -{ - const Header *testHeader; - // if the following assignment fails, it is because the - // input to this function is not a subclass of the Header class - testHeader = &header; - uint32_t size = header.GetSerializedSize (); - m_buffer.AddAtStart (size); - header.Serialize (m_buffer.Begin ()); - m_metadata.AddHeader (header, size); -} -template -uint32_t -Packet::RemoveHeader (T &header) -{ - Header *testHeader; - // if the following assignment fails, it is because the - // input to this function is not a subclass of the Header class - testHeader = &header; - uint32_t deserialized = header.Deserialize (m_buffer.Begin ()); - m_buffer.RemoveAtStart (deserialized); - m_metadata.RemoveHeader (header, deserialized); - return deserialized; -} -template -void -Packet::AddTrailer (T const &trailer) -{ - const Trailer *testTrailer; - // if the following assignment fails, it is because the - // input to this function is not a subclass of the Trailer class - testTrailer = &trailer; - uint32_t size = trailer.GetSerializedSize (); - m_buffer.AddAtEnd (size); - Buffer::Iterator end = m_buffer.End (); - trailer.Serialize (end); - m_metadata.AddTrailer (trailer, size); -} -template -uint32_t -Packet::RemoveTrailer (T &trailer) -{ - Trailer *testTrailer; - // if the following assignment fails, it is because the - // input to this function is not a subclass of the Trailer class - testTrailer = &trailer; - uint32_t deserialized = trailer.Deserialize (m_buffer.End ()); - m_buffer.RemoveAtEnd (deserialized); - m_metadata.RemoveTrailer (trailer, deserialized); - return deserialized; -} - - template void Packet::AddTag (T const& tag) const { From 627a2048f12368da41cad4137d4abeae192f4db4 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 19 Mar 2008 13:11:25 -0700 Subject: [PATCH 35/35] remove trailing ; --- src/common/packet-metadata.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/packet-metadata.h b/src/common/packet-metadata.h index b02320672..5ba229853 100644 --- a/src/common/packet-metadata.h +++ b/src/common/packet-metadata.h @@ -356,7 +356,7 @@ PacketMetadata::~PacketMetadata () } } -}; // namespace ns3 +} // namespace ns3 #endif /* PACKET_METADATA_H */