A Discrete-Event Network Simulator
API
serializationwrapper.cc
Go to the documentation of this file.
1 
2 /*
3 * Copyright (c) 2020 DLTLT
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Corresponding author: Niki Hrovatin <niki.hrovatin@famnit.upr.si>
19 */
20 
21 #include "serializationwrapper.h"
22 #include "ns3/header.h"
23 #include "ns3/network-module.h"
24 
25 namespace ns3 {
26 
27 //Zagotovi, da se registrira TypeId
28 NS_OBJECT_ENSURE_REGISTERED (SerializationWrapper);
29 
30 TypeId
32 {
33  static TypeId tid = TypeId ("ns3::SerializationWrapper")
34  .SetParent<Header> ()
35  .AddConstructor<SerializationWrapper> ()
36  .SetGroupName ("Network");
37  return tid;
38 }
39 
40 //nic vaznega neznam kaj rabi - obvezno, ker je bilo virtual
41 TypeId
43 {
44  return GetTypeId ();
45 }
46 
48 {
49 }
50 
52 {
53  //delete[] m_data;
54 }
55 
56 //nastavi protobuf object
57 void
59 {
60 
61  m_dataSize = message.ByteSizeLong ();
62 
63  m_data = new uint8_t[m_dataSize + 4];
64 
65  m_data[0] = m_dataSize;
66  m_data[1] = m_dataSize >> 8;
67  m_data[2] = m_dataSize >> 16;
68  m_data[3] = m_dataSize >> 24;
69 
70  int size = 0;
71  memcpy (&size, &m_data[0], 4);
72 
73  message.SerializeToArray (&m_data[4], m_dataSize);
74 }
75 
76 //vrne protobuf object
77 void
79 {
80  message->ParseFromArray (m_data, m_dataSize);
81  delete[] m_data;
82 }
83 
84 //Kontruktor, ki sprejme protobuf object
86 {
87  SetData (message);
88 }
89 
90 uint32_t
92 {
93  if (m_dataSize < 0)
94  {
95  std::cout << "message not set " << std::endl;
96  }
97  //4B where the size of the serialization is stored
98  return m_dataSize + 4;
99 }
100 
101 void
102 SerializationWrapper::Serialize (Buffer::Iterator start) const
103 {
104 
105  Buffer::Iterator i = start;
106  i.Write (m_data, m_dataSize + 4);
107  delete[] m_data;
108 }
109 uint32_t
110 SerializationWrapper::Deserialize (Buffer::Iterator start)
111 {
112 
113  Buffer::Iterator i = start;
114  uint8_t *size_arr = new uint8_t[4];
115  int size = 0;
116  i.Read (size_arr, 4);
117 
118  memcpy (&size, &size_arr[0], 4);
119  delete[] size_arr;
120 
121  m_data = new uint8_t[size];
122  i.Read (m_data, size);
123  m_dataSize = size;
124  return size + 4;
125 }
126 
127 //Ne implementirano
128 void
129 SerializationWrapper::Print (std::ostream &os) const
130 {
131  os << "not printing the header";
132 }
133 
134 } // namespace ns3
ns3::SerializationWrapper::m_dataSize
int m_dataSize
holds the size of the serialized data in bytes
Definition: serializationwrapper.h:140
ns3::SerializationWrapper::Serialize
virtual void Serialize(Buffer::Iterator start) const
serialize the data
Definition: serializationwrapper.cc:102
ns3
Definition: sensornode-helper.cc:26
ns3::SerializationWrapper::m_data
uint8_t * m_data
the serialized data
Definition: serializationwrapper.h:142
ns3::SerializationWrapper::SetData
void SetData(protomessage::ProtoPacket message)
Setter of the data in the protocol header.
Definition: serializationwrapper.cc:58
ns3::SerializationWrapper::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
compute the serialized size of the data
Definition: serializationwrapper.cc:91
ns3::SerializationWrapper::GetTypeId
static TypeId GetTypeId(void)
Register this type.
Definition: serializationwrapper.cc:31
serializationwrapper.h
ns3::SerializationWrapper::~SerializationWrapper
virtual ~SerializationWrapper()
Definition: serializationwrapper.cc:51
protomessage::ProtoPacket::ByteSizeLong
size_t ByteSizeLong() const final
Definition: proto-packet.pb.cc:1134
ns3::SerializationWrapper::SerializationWrapper
SerializationWrapper()
Default constructor.
Definition: serializationwrapper.cc:47
ns3::SerializationWrapper::Print
virtual void Print(std::ostream &os) const
dummy printing of the serialized data
Definition: serializationwrapper.cc:129
ns3::SerializationWrapper::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)
deserialize the data
Definition: serializationwrapper.cc:110
protomessage::ProtoPacket
Definition: proto-packet.pb.h:598
ns3::SerializationWrapper::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Definition: serializationwrapper.cc:42
ns3::SerializationWrapper::GetData
void GetData(protomessage::ProtoPacket *message)
Getter of the data in the protocol header.
Definition: serializationwrapper.cc:78