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