implement Packet::Print
This commit is contained in:
16
src/common/chunk.cc
Normal file
16
src/common/chunk.cc
Normal file
@@ -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<ObjectBase> ()
|
||||
;
|
||||
return tid;
|
||||
}
|
||||
|
||||
} // namespace ns3
|
||||
19
src/common/chunk.h
Normal file
19
src/common/chunk.h
Normal file
@@ -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 */
|
||||
@@ -11,7 +11,7 @@ TypeId
|
||||
Header::GetTypeId (void)
|
||||
{
|
||||
static TypeId tid = TypeId ("ns3::Header")
|
||||
.SetParent<ObjectBase> ()
|
||||
.SetParent<Chunk> ()
|
||||
;
|
||||
return tid;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#ifndef HEADER_H
|
||||
#define HEADER_H
|
||||
|
||||
#include "ns3/object-base.h"
|
||||
#include "chunk.h"
|
||||
#include "buffer.h"
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <vector>
|
||||
#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.
|
||||
*/
|
||||
|
||||
@@ -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<ObjectBase *> constructor = item.tid.GetConstructor ();
|
||||
NS_ASSERT (constructor.IsNull ());
|
||||
ObjectBase *instance = constructor ();
|
||||
NS_ASSERT (instance != 0);
|
||||
Chunk *chunk = dynamic_cast<Chunk *> (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
|
||||
|
||||
@@ -11,7 +11,7 @@ TypeId
|
||||
Trailer::GetTypeId (void)
|
||||
{
|
||||
static TypeId tid = TypeId ("ns3::Trailer")
|
||||
.SetParent<ObjectBase> ()
|
||||
.SetParent<Chunk> ()
|
||||
;
|
||||
return tid;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#ifndef TRAILER_H
|
||||
#define TRAILER_H
|
||||
|
||||
#include "ns3/object-base.h"
|
||||
#include "chunk.h"
|
||||
#include "buffer.h"
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user