blob: 6296aa6c35ccf2618a2f33640e648510fc404ffe [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;
88 }
89
90 // exception will be thrown if packet is not recognized
91 }
92 catch (UnknownHeaderException)
93 {
94 NS_FATAL_ERROR ("Unknown NDN header");
95 return 0;
96 }
97 }
98 else
99 {
100 if (wireFormat == WIRE_FORMAT_NDNSIM)
101 return wire::ndnSIM::Interest::FromWire (packet);
102 else if (wireFormat == WIRE_FORMAT_CCNB)
103 return wire::ccnb::Interest::FromWire (packet);
104 else
105 {
106 NS_FATAL_ERROR ("Unsupported format requested");
107 return 0;
108 }
109 }
110}
111
112Ptr<Packet>
113Wire::FromData (Ptr<const ContentObject> data, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
114{
115 if (wireFormat == WIRE_FORMAT_DEFAULT)
116 wireFormat = GetWireFormat ();
117
118 if (wireFormat == WIRE_FORMAT_NDNSIM)
119 return wire::ndnSIM::Data::ToWire (data);
120 else if (wireFormat == WIRE_FORMAT_CCNB)
121 return wire::ccnb::Data::ToWire (data);
122 else
123 {
124 NS_FATAL_ERROR ("Unsupported format requested");
125 return 0;
126 }
127}
128
129Ptr<ContentObject>
130Wire::ToData (Ptr<Packet> packet, int8_t wireFormat/* = WIRE_FORMAT_AUTODETECT*/)
131{
132 if (wireFormat == WIRE_FORMAT_AUTODETECT)
133 {
134 try
135 {
136 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet);
137 switch (type)
138 {
139 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
140 {
141 return wire::ndnSIM::Data::FromWire (packet);
142 }
143 case HeaderHelper::CONTENT_OBJECT_CCNB:
144 {
145 return wire::ccnb::Data::FromWire (packet);
146 }
147 case HeaderHelper::INTEREST_NDNSIM:
148 case HeaderHelper::INTEREST_CCNB:
149 NS_FATAL_ERROR ("Interest supplied for DataFromWire function");
150 break;
151 }
152
153 // exception will be thrown if packet is not recognized
154 }
155 catch (UnknownHeaderException)
156 {
157 NS_FATAL_ERROR ("Unknown NDN header");
158 return 0;
159 }
160 }
161 else
162 {
163 if (wireFormat == WIRE_FORMAT_NDNSIM)
164 return wire::ndnSIM::Data::FromWire (packet);
165 else if (wireFormat == WIRE_FORMAT_CCNB)
166 return wire::ccnb::Data::FromWire (packet);
167 else
168 {
169 NS_FATAL_ERROR ("Unsupported format requested");
170 return 0;
171 }
172 }
173}
174
175
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700176std::string
177Wire::FromInterestStr (Ptr<const Interest> interest, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700178{
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700179 Ptr<Packet> pkt = FromInterest (interest, wireFormat);
180 std::string wire;
181 wire.resize (pkt->GetSize ());
182 pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ());
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700183
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700184 return wire;
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700185}
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700186
187Ptr<Interest>
188Wire::ToInterestStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/)
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700189{
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700190 Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ());
191 return ToInterest (pkt, type);
192}
193
194std::string
195Wire::FromDataStr (Ptr<const ContentObject> data, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
196{
197 Ptr<Packet> pkt = FromData (data, wireFormat);
198 std::string wire;
199 wire.resize (pkt->GetSize ());
200 pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ());
201
202 return wire;
203}
204
205Ptr<ContentObject>
206Wire::ToDataStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/)
207{
208 Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ());
209 return ToData (pkt, type);
210}
211
212// uint32_t
213// Wire::FromNameSize (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
214// {
215// if (wireFormat == WIRE_FORMAT_DEFAULT)
216// wireFormat = GetWireFormat ();
217
218// if (wireFormat == WIRE_FORMAT_NDNSIM)
219// {
220// std::cout << wire::NdnSim::SerializedSizeName (*name) << std::endl;
221// return wire::NdnSim::SerializedSizeName (*name);
222// }
223// else if (wireFormat == WIRE_FORMAT_CCNB)
224// return wire::Ccnb::SerializedSizeName (*name);
225// else
226// {
227// NS_FATAL_ERROR ("Unsupported format requested");
228// return 0;
229// }
230// }
231
232std::string
233Wire::FromName (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
234{
235
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700236 if (wireFormat == WIRE_FORMAT_DEFAULT)
237 wireFormat = GetWireFormat ();
238
239 if (wireFormat == WIRE_FORMAT_NDNSIM)
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700240 {
241 Buffer buf;
242 buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name));
243 Buffer::Iterator i = buf.Begin ();
244
245 wire::NdnSim::SerializeName (i, *name);
246
247 std::string wire;
248 wire.resize (buf.GetSize ());
249 buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ());
250
251 return wire;
252 }
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700253 else if (wireFormat == WIRE_FORMAT_CCNB)
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700254 {
255 Buffer buf;
256 buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name));
257 Buffer::Iterator i = buf.Begin ();
258
259 wire::Ccnb::SerializeName (i, *name);
260
261 std::string wire;
262 wire.resize (buf.GetSize ());
263 buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ());
264
265 return wire;
266 }
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700267 else
268 {
269 NS_FATAL_ERROR ("Unsupported format requested");
270 }
271}
272
273Ptr<Name>
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700274Wire::ToName (const std::string &name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700275{
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700276 Buffer buf;
277 buf.AddAtStart (name.size ());
278 Buffer::Iterator i = buf.Begin ();
279 i.Write (reinterpret_cast<const uint8_t *> (&name[0]), name.size ());
280
281 i = buf.Begin ();
282
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700283 if (wireFormat == WIRE_FORMAT_DEFAULT)
284 wireFormat = GetWireFormat ();
285
286 if (wireFormat == WIRE_FORMAT_NDNSIM)
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700287 {
288 return wire::NdnSim::DeserializeName (i);
289 }
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700290 else if (wireFormat == WIRE_FORMAT_CCNB)
291 {
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700292 return wire::Ccnb::DeserializeName (i);
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700293 }
294 else
295 {
296 NS_FATAL_ERROR ("Unsupported format requested");
297 }
298}
299
300NDN_NAMESPACE_END