A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
main-packet-header.cc
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 
9 /* A sample Header implementation
10  */
11 class MyHeader : public Header
12 {
13 public:
14 
15  MyHeader ();
16  virtual ~MyHeader ();
17 
18  void SetData (uint16_t data);
19  uint16_t GetData (void) const;
20 
21  static TypeId GetTypeId (void);
22  virtual TypeId GetInstanceTypeId (void) const;
23  virtual void Print (std::ostream &os) const;
24  virtual void Serialize (Buffer::Iterator start) const;
25  virtual uint32_t Deserialize (Buffer::Iterator start);
26  virtual uint32_t GetSerializedSize (void) const;
27 private:
28  uint16_t m_data;
29 };
30 
31 MyHeader::MyHeader ()
32 {
33  // we must provide a public default constructor,
34  // implicit or explicit, but never private.
35 }
36 MyHeader::~MyHeader ()
37 {
38 }
39 
40 TypeId
41 MyHeader::GetTypeId (void)
42 {
43  static TypeId tid = TypeId ("ns3::MyHeader")
44  .SetParent<Header> ()
45  .AddConstructor<MyHeader> ()
46  ;
47  return tid;
48 }
49 TypeId
51 {
52  return GetTypeId ();
53 }
54 
55 void
56 MyHeader::Print (std::ostream &os) const
57 {
58  // This method is invoked by the packet printing
59  // routines to print the content of my header.
60  //os << "data=" << m_data << std::endl;
61  os << "data=" << m_data;
62 }
63 uint32_t
65 {
66  // we reserve 2 bytes for our header.
67  return 2;
68 }
69 void
71 {
72  // we can serialize two bytes at the start of the buffer.
73  // we write them in network byte order.
74  start.WriteHtonU16 (m_data);
75 }
76 uint32_t
78 {
79  // we can deserialize two bytes from the start of the buffer.
80  // we read them in network byte order and store them
81  // in host byte order.
82  m_data = start.ReadNtohU16 ();
83 
84  // we return the number of bytes effectively read.
85  return 2;
86 }
87 
88 void
89 MyHeader::SetData (uint16_t data)
90 {
91  m_data = data;
92 }
93 uint16_t
94 MyHeader::GetData (void) const
95 {
96  return m_data;
97 }
98 
99 
100 
101 int main (int argc, char *argv[])
102 {
103  // Enable the packet printing through Packet::Print command.
105 
106  // instantiate a header.
107  MyHeader sourceHeader;
108  sourceHeader.SetData (2);
109 
110  // instantiate a packet
111  Ptr<Packet> p = Create<Packet> ();
112 
113  // and store my header into the packet.
114  p->AddHeader (sourceHeader);
115 
116  // print the content of my packet on the standard output.
117  p->Print (std::cout);
118  std::cout << std::endl;
119 
120  // you can now remove the header from the packet:
121  MyHeader destinationHeader;
122  p->RemoveHeader (destinationHeader);
123 
124  // and check that the destination and source
125  // headers contain the same values.
126  NS_ASSERT (sourceHeader.GetData () == destinationHeader.GetData ());
127 
128  return 0;
129 }
Protocol header serialization and deserialization.
Definition: header.h:42
uint32_t RemoveHeader(Header &header)
Definition: packet.cc:285
virtual TypeId GetInstanceTypeId(void) const
#define NS_ASSERT(condition)
Definition: assert.h:64
void Print(std::ostream &os) const
Definition: packet.cc:450
iterator in a Buffer instance
Definition: buffer.h:98
static void EnablePrinting(void)
Definition: packet.cc:575
void WriteHtonU16(uint16_t data)
Definition: buffer.h:726
virtual uint32_t GetSerializedSize(void) const
virtual uint32_t Deserialize(Buffer::Iterator start)
virtual void Print(std::ostream &os) const
uint16_t ReadNtohU16(void)
Definition: buffer.h:767
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471
virtual void Serialize(Buffer::Iterator start) const
void AddHeader(const Header &header)
Definition: packet.cc:270