blob: 7d383a459d83d0604f3e3dcc901cd199fd6e9c7d [file] [log] [blame]
Alexander Afanasyeva89bc102013-07-16 10:17:31 -07001/** -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
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 "wire-ndnsim.h"
12#include <boost/foreach.hpp>
13
14NDN_NAMESPACE_BEGIN
15
16namespace wire {
17
18//////////////////////////////////////////////////////////////////////
19//////////////////////////////////////////////////////////////////////
20//////////////////////////////////////////////////////////////////////
21
22size_t
23NdnSim::SerializeName (Buffer::Iterator &i, const Name &name)
24{
25 Buffer::Iterator start = i;
26
27 i.WriteU16 (static_cast<uint16_t> (SerializedSizeName (name)-2));
28
29 for (std::list<std::string>::const_iterator item = name.begin ();
30 item != name.end ();
31 item++)
32 {
33 i.WriteU16 (static_cast<uint16_t> (item->size ()));
34 i.Write (reinterpret_cast<const uint8_t*> (item->c_str ()), item->size ());
35 }
36
37 return i.GetDistanceFrom (start);
38}
39
40size_t
41NdnSim::SerializedSizeName (const Name &name)
42{
43 size_t nameSerializedSize = 2;
44
45 for (std::list<std::string>::const_iterator i = name.begin ();
46 i != name.end ();
47 i++)
48 {
49 nameSerializedSize += 2 + i->size ();
50 }
51 NS_ASSERT_MSG (nameSerializedSize < 30000, "Name is too long (> 30kbytes)");
52
53 return nameSerializedSize;
54}
55
56Ptr<Name>
57NdnSim::DeserializeName (Buffer::Iterator &i)
58{
59 Ptr<Name> name = Create<Name> ();
60
61 uint16_t nameLength = i.ReadU16 ();
62 while (nameLength > 0)
63 {
64 uint16_t length = i.ReadU16 ();
65 nameLength = nameLength - 2 - length;
66
67 uint8_t tmp[length];
68 i.Read (tmp, length);
69
70 name->Add (std::string (reinterpret_cast<const char*> (tmp), length));
71 }
72
73 return name;
74}
75
76} // wire
77
78NDN_NAMESPACE_END