blob: b9164e48a738130690eba66660a6b7e1a4903ff0 [file] [log] [blame]
Alexander Afanasyev7b923f32013-07-16 13:23:04 -07001/** -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev76b11572013-07-16 21:49:50 -07002/*
Alexander Afanasyev7b923f32013-07-16 13:23:04 -07003 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
Alexander Afanasyev76b11572013-07-16 21:49:50 -07005 *
Alexander Afanasyev7b923f32013-07-16 13:23:04 -07006 * GNU 3.0 license, See the LICENSE file for more information
Alexander Afanasyev76b11572013-07-16 21:49:50 -07007 *
Alexander Afanasyev7b923f32013-07-16 13:23:04 -07008 * 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-content-object.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
25NDN_NAMESPACE_BEGIN
26
27static
Alexander Afanasyev76b11572013-07-16 21:49:50 -070028GlobalValue g_wireFormat ("ndn::WireFormat",
Alexander Afanasyev7b923f32013-07-16 13:23:04 -070029 "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
35static inline uint32_t
36GetWireFormat ()
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
49Ptr<Packet>
50Wire::FromInterest (Ptr<const Interest> interest, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
51{
52 if (wireFormat == WIRE_FORMAT_DEFAULT)
53 wireFormat = GetWireFormat ();
Alexander Afanasyev76b11572013-07-16 21:49:50 -070054
Alexander Afanasyev7b923f32013-07-16 13:23:04 -070055 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
66Ptr<Interest>
67Wire::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;
Saran Tarnoi0bb088a2013-07-27 19:51:20 +090088 default:
89 NS_FATAL_ERROR ("Unsupported format");
90 return 0;
Alexander Afanasyev7b923f32013-07-16 13:23:04 -070091 }
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
115Ptr<Packet>
116Wire::FromData (Ptr<const ContentObject> 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
132Ptr<ContentObject>
133Wire::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;
Saran Tarnoi0bb088a2013-07-27 19:51:20 +0900154 default:
155 NS_FATAL_ERROR ("Unsupported format");
156 return 0;
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700157 }
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
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700182std::string
183Wire::FromInterestStr (Ptr<const Interest> interest, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700184{
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700185 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 ());
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700189
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700190 return wire;
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700191}
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700192
193Ptr<Interest>
194Wire::ToInterestStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/)
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700195{
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700196 Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ());
197 return ToInterest (pkt, type);
198}
199
200std::string
201Wire::FromDataStr (Ptr<const ContentObject> 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
211Ptr<ContentObject>
212Wire::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
238std::string
239Wire::FromName (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
240{
241
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700242 if (wireFormat == WIRE_FORMAT_DEFAULT)
243 wireFormat = GetWireFormat ();
244
245 if (wireFormat == WIRE_FORMAT_NDNSIM)
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700246 {
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 }
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700259 else if (wireFormat == WIRE_FORMAT_CCNB)
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700260 {
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 }
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700273 else
274 {
275 NS_FATAL_ERROR ("Unsupported format requested");
276 }
277}
278
279Ptr<Name>
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700280Wire::ToName (const std::string &name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700281{
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700282 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
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700289 if (wireFormat == WIRE_FORMAT_DEFAULT)
290 wireFormat = GetWireFormat ();
291
292 if (wireFormat == WIRE_FORMAT_NDNSIM)
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700293 {
294 return wire::NdnSim::DeserializeName (i);
295 }
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700296 else if (wireFormat == WIRE_FORMAT_CCNB)
297 {
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700298 return wire::Ccnb::DeserializeName (i);
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700299 }
300 else
301 {
302 NS_FATAL_ERROR ("Unsupported format requested");
303 }
304}
305
306NDN_NAMESPACE_END