A Discrete-Event Network Simulator
API
main-packet-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 #include "ns3/ptr.h"
3 #include "ns3/packet.h"
4 #include "ns3/header.h"
5 #include <iostream>
6 
7 using namespace ns3;
8 
13 class MyHeader : public Header
14 {
15 public:
16 
17  MyHeader ();
18  virtual ~MyHeader ();
19 
24  void SetData (uint16_t data);
29  uint16_t GetData (void) const;
30 
35  static TypeId GetTypeId (void);
36  virtual TypeId GetInstanceTypeId (void) const;
37  virtual void Print (std::ostream &os) const;
38  virtual void Serialize (Buffer::Iterator start) const;
39  virtual uint32_t Deserialize (Buffer::Iterator start);
40  virtual uint32_t GetSerializedSize (void) const;
41 private:
42  uint16_t m_data;
43 };
44 
46 {
47  // we must provide a public default constructor,
48  // implicit or explicit, but never private.
49 }
51 {
52 }
53 
54 TypeId
56 {
57  static TypeId tid = TypeId ("ns3::MyHeader")
58  .SetParent<Header> ()
59  .AddConstructor<MyHeader> ()
60  ;
61  return tid;
62 }
63 TypeId
65 {
66  return GetTypeId ();
67 }
68 
69 void
70 MyHeader::Print (std::ostream &os) const
71 {
72  // This method is invoked by the packet printing
73  // routines to print the content of my header.
74  //os << "data=" << m_data << std::endl;
75  os << "data=" << m_data;
76 }
77 uint32_t
79 {
80  // we reserve 2 bytes for our header.
81  return 2;
82 }
83 void
85 {
86  // we can serialize two bytes at the start of the buffer.
87  // we write them in network byte order.
88  start.WriteHtonU16 (m_data);
89 }
90 uint32_t
92 {
93  // we can deserialize two bytes from the start of the buffer.
94  // we read them in network byte order and store them
95  // in host byte order.
96  m_data = start.ReadNtohU16 ();
97 
98  // we return the number of bytes effectively read.
99  return 2;
100 }
101 
102 void
104 {
105  m_data = data;
106 }
107 uint16_t
108 MyHeader::GetData (void) const
109 {
110  return m_data;
111 }
112 
113 
114 
115 int main (int argc, char *argv[])
116 {
117  // Enable the packet printing through Packet::Print command.
119 
120  // instantiate a header.
121  MyHeader sourceHeader;
122  sourceHeader.SetData (2);
123 
124  // instantiate a packet
125  Ptr<Packet> p = Create<Packet> ();
126 
127  // and store my header into the packet.
128  p->AddHeader (sourceHeader);
129 
130  // print the content of my packet on the standard output.
131  p->Print (std::cout);
132  std::cout << std::endl;
133 
134  // you can now remove the header from the packet:
135  MyHeader destinationHeader;
136  p->RemoveHeader (destinationHeader);
137 
138  // and check that the destination and source
139  // headers contain the same values.
140  NS_ASSERT (sourceHeader.GetData () == destinationHeader.GetData ());
141 
142  return 0;
143 }
Protocol header serialization and deserialization.
Definition: header.h:42
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:434
virtual ~MyHeader()
uint16_t GetData(void) const
Get the header data.
def start()
Definition: core.py:1844
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
virtual void Serialize(Buffer::Iterator start) const
void SetData(uint16_t data)
Set the header data.
iterator in a Buffer instance
Definition: buffer.h:98
A simple example of an Header implementation.
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
uint8_t data[writeSize]
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
uint16_t m_data
Header data.
static TypeId GetTypeId(void)
Get the type ID.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Print(std::ostream &os) const
virtual uint32_t Deserialize(Buffer::Iterator start)
void Print(ComponentCarrier cc)
virtual uint32_t GetSerializedSize(void) const
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256