NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-wire.cc
1 
2 /*
3  * Copyright (c) 2013, Regents of the University of California
4  * Alexander Afanasyev
5  *
6  * GNU 3.0 license, See the LICENSE file for more information
7  *
8  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9  */
10 
11 #include "ndn-wire.h"
12 
13 #include "ns3/global-value.h"
14 #include "ns3/integer.h"
15 #include "ns3/ndn-common.h"
16 #include "ns3/ndn-interest.h"
17 #include "ns3/ndn-data.h"
18 #include "ns3/ndn-header-helper.h"
19 
20 #include "ndnsim.h"
21 #include "ndnsim/wire-ndnsim.h"
22 #include "ccnb.h"
23 #include "ccnb/wire-ccnb.h"
24 
25 NDN_NAMESPACE_BEGIN
26 
27 static
28 GlobalValue g_wireFormat ("ndn::WireFormat",
29  "Default wire format for ndnSIM. ndnSIM will be accepting packets "
30  "in any supported packet formats, but if encoding requested, it will "
31  "use default wire format to encode (0 for ndnSIM (default), 1 for CCNb)",
32  IntegerValue (Wire::WIRE_FORMAT_NDNSIM),
33  MakeIntegerChecker<int32_t> ());
34 
35 static inline uint32_t
36 GetWireFormat ()
37 {
38  static int32_t format = -1;
39  if (format >= 0)
40  return format;
41 
42  IntegerValue value;
43  g_wireFormat.GetValue (value);
44  format = value.Get ();
45 
46  return format;
47 }
48 
49 Ptr<Packet>
50 Wire::FromInterest (Ptr<const Interest> interest, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
51 {
52  if (wireFormat == WIRE_FORMAT_DEFAULT)
53  wireFormat = GetWireFormat ();
54 
55  if (wireFormat == WIRE_FORMAT_NDNSIM)
56  return wire::ndnSIM::Interest::ToWire (interest);
57  else if (wireFormat == WIRE_FORMAT_CCNB)
58  return wire::ccnb::Interest::ToWire (interest);
59  else
60  {
61  NS_FATAL_ERROR ("Unsupported format requested");
62  return 0;
63  }
64 }
65 
66 Ptr<Interest>
67 Wire::ToInterest (Ptr<Packet> packet, int8_t wireFormat/* = WIRE_FORMAT_AUTODETECT*/)
68 {
69  if (wireFormat == WIRE_FORMAT_AUTODETECT)
70  {
71  try
72  {
73  HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet);
74  switch (type)
75  {
76  case HeaderHelper::INTEREST_NDNSIM:
77  {
78  return wire::ndnSIM::Interest::FromWire (packet);
79  }
80  case HeaderHelper::INTEREST_CCNB:
81  {
82  return wire::ccnb::Interest::FromWire (packet);
83  }
84  case HeaderHelper::CONTENT_OBJECT_NDNSIM:
85  case HeaderHelper::CONTENT_OBJECT_CCNB:
86  NS_FATAL_ERROR ("Data packet supplied for InterestFromWire function");
87  break;
88  default:
89  NS_FATAL_ERROR ("Unsupported format");
90  return 0;
91  }
92 
93  // exception will be thrown if packet is not recognized
94  }
95  catch (UnknownHeaderException)
96  {
97  NS_FATAL_ERROR ("Unknown NDN header");
98  return 0;
99  }
100  }
101  else
102  {
103  if (wireFormat == WIRE_FORMAT_NDNSIM)
104  return wire::ndnSIM::Interest::FromWire (packet);
105  else if (wireFormat == WIRE_FORMAT_CCNB)
106  return wire::ccnb::Interest::FromWire (packet);
107  else
108  {
109  NS_FATAL_ERROR ("Unsupported format requested");
110  return 0;
111  }
112  }
113 }
114 
115 Ptr<Packet>
116 Wire::FromData (Ptr<const Data> data, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
117 {
118  if (wireFormat == WIRE_FORMAT_DEFAULT)
119  wireFormat = GetWireFormat ();
120 
121  if (wireFormat == WIRE_FORMAT_NDNSIM)
122  return wire::ndnSIM::Data::ToWire (data);
123  else if (wireFormat == WIRE_FORMAT_CCNB)
124  return wire::ccnb::Data::ToWire (data);
125  else
126  {
127  NS_FATAL_ERROR ("Unsupported format requested");
128  return 0;
129  }
130 }
131 
132 Ptr<Data>
133 Wire::ToData (Ptr<Packet> packet, int8_t wireFormat/* = WIRE_FORMAT_AUTODETECT*/)
134 {
135  if (wireFormat == WIRE_FORMAT_AUTODETECT)
136  {
137  try
138  {
139  HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet);
140  switch (type)
141  {
142  case HeaderHelper::CONTENT_OBJECT_NDNSIM:
143  {
144  return wire::ndnSIM::Data::FromWire (packet);
145  }
146  case HeaderHelper::CONTENT_OBJECT_CCNB:
147  {
148  return wire::ccnb::Data::FromWire (packet);
149  }
150  case HeaderHelper::INTEREST_NDNSIM:
151  case HeaderHelper::INTEREST_CCNB:
152  NS_FATAL_ERROR ("Interest supplied for DataFromWire function");
153  break;
154  default:
155  NS_FATAL_ERROR ("Unsupported format");
156  return 0;
157  }
158 
159  // exception will be thrown if packet is not recognized
160  }
161  catch (UnknownHeaderException)
162  {
163  NS_FATAL_ERROR ("Unknown NDN header");
164  return 0;
165  }
166  }
167  else
168  {
169  if (wireFormat == WIRE_FORMAT_NDNSIM)
170  return wire::ndnSIM::Data::FromWire (packet);
171  else if (wireFormat == WIRE_FORMAT_CCNB)
172  return wire::ccnb::Data::FromWire (packet);
173  else
174  {
175  NS_FATAL_ERROR ("Unsupported format requested");
176  return 0;
177  }
178  }
179 }
180 
181 
182 std::string
183 Wire::FromInterestStr (Ptr<const Interest> interest, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
184 {
185  Ptr<Packet> pkt = FromInterest (interest, wireFormat);
186  std::string wire;
187  wire.resize (pkt->GetSize ());
188  pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ());
189 
190  return wire;
191 }
192 
193 Ptr<Interest>
194 Wire::ToInterestStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/)
195 {
196  Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ());
197  return ToInterest (pkt, type);
198 }
199 
200 std::string
201 Wire::FromDataStr (Ptr<const Data> data, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
202 {
203  Ptr<Packet> pkt = FromData (data, wireFormat);
204  std::string wire;
205  wire.resize (pkt->GetSize ());
206  pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ());
207 
208  return wire;
209 }
210 
211 Ptr<Data>
212 Wire::ToDataStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/)
213 {
214  Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ());
215  return ToData (pkt, type);
216 }
217 
218 // uint32_t
219 // Wire::FromNameSize (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
220 // {
221 // if (wireFormat == WIRE_FORMAT_DEFAULT)
222 // wireFormat = GetWireFormat ();
223 
224 // if (wireFormat == WIRE_FORMAT_NDNSIM)
225 // {
226 // std::cout << wire::NdnSim::SerializedSizeName (*name) << std::endl;
227 // return wire::NdnSim::SerializedSizeName (*name);
228 // }
229 // else if (wireFormat == WIRE_FORMAT_CCNB)
230 // return wire::Ccnb::SerializedSizeName (*name);
231 // else
232 // {
233 // NS_FATAL_ERROR ("Unsupported format requested");
234 // return 0;
235 // }
236 // }
237 
238 std::string
239 Wire::FromName (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
240 {
241 
242  if (wireFormat == WIRE_FORMAT_DEFAULT)
243  wireFormat = GetWireFormat ();
244 
245  if (wireFormat == WIRE_FORMAT_NDNSIM)
246  {
247  Buffer buf;
248  buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name));
249  Buffer::Iterator i = buf.Begin ();
250 
251  wire::NdnSim::SerializeName (i, *name);
252 
253  std::string wire;
254  wire.resize (buf.GetSize ());
255  buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ());
256 
257  return wire;
258  }
259  else if (wireFormat == WIRE_FORMAT_CCNB)
260  {
261  Buffer buf;
262  buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name));
263  Buffer::Iterator i = buf.Begin ();
264 
265  wire::Ccnb::SerializeName (i, *name);
266 
267  std::string wire;
268  wire.resize (buf.GetSize ());
269  buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ());
270 
271  return wire;
272  }
273  else
274  {
275  NS_FATAL_ERROR ("Unsupported format requested");
276  }
277 }
278 
279 Ptr<Name>
280 Wire::ToName (const std::string &name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
281 {
282  Buffer buf;
283  buf.AddAtStart (name.size ());
284  Buffer::Iterator i = buf.Begin ();
285  i.Write (reinterpret_cast<const uint8_t *> (&name[0]), name.size ());
286 
287  i = buf.Begin ();
288 
289  if (wireFormat == WIRE_FORMAT_DEFAULT)
290  wireFormat = GetWireFormat ();
291 
292  if (wireFormat == WIRE_FORMAT_NDNSIM)
293  {
294  return wire::NdnSim::DeserializeName (i);
295  }
296  else if (wireFormat == WIRE_FORMAT_CCNB)
297  {
298  return wire::Ccnb::DeserializeName (i);
299  }
300  else
301  {
302  NS_FATAL_ERROR ("Unsupported format requested");
303  }
304 }
305 
306 NDN_NAMESPACE_END