blob: 65b3b6a17f1f9f4d493b73d47957fcd5d8bbdcb9 [file] [log] [blame]
Alexander Afanasyev834f35c2011-08-16 17:13:50 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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 *
Ilya Moiseenko332add02011-12-24 17:21:25 -080018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070019 */
20
21#ifndef _CCNX_ENCODING_HELPER_H_
22#define _CCNX_ENCODING_HELPER_H_
23
24#include <sys/types.h>
25
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080026#include "ccnb-parser/ccnb-parser-common.h"
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070027#include "ns3/ptr.h"
28#include "ns3/nstime.h"
29#include "ns3/buffer.h"
30
31namespace ns3 {
32
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -070033class CcnxNameComponents;
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070034
35class CcnxInterestHeader;
36class CcnxContentObjectHeader;
37
38/**
Ilya Moiseenko332add02011-12-24 17:21:25 -080039 * \brief Helper to encode/decode ccnb formatted CCNx message
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070040 *
41 */
42class CcnxEncodingHelper
43{
44public:
Ilya Moiseenko332add02011-12-24 17:21:25 -080045 /**
46 * \brief Serialize CcnxInterestHeader to Buffer::Iterator
47 * @param start Buffer to store serialized CcnxInterestHeader
48 * @param interest Pointer to CcnxInterestHeader to be serialized
49 * @return length of serialized CcnxInterestHeader
50 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070051 static size_t
52 Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest);
53
Ilya Moiseenko332add02011-12-24 17:21:25 -080054 /**
55 * \brief Compute the size of serialized CcnxInterestHeader
56 * @param interest Pointer to CcnxInterestHeader
57 * @return length
58 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070059 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070060 GetSerializedSize (const CcnxInterestHeader &interest);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070061
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070062public:
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070063 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070064 AppendBlockHeader (Buffer::Iterator &start, size_t value, CcnbParser::ccn_tt block_type);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070065
66 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070067 EstimateBlockHeader (size_t value);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070068
69 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070070 AppendNumber (Buffer::Iterator &start, uint32_t number);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070071
72 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070073 EstimateNumber (uint32_t number);
74
75 static size_t
76 AppendCloser (Buffer::Iterator &start);
77
78 static size_t
79 AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name);
80
81 static size_t
82 EstimateNameComponents (const CcnxNameComponents &name);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070083
84 /**
85 * Append a binary timestamp as a BLOB using the ccn binary
86 * Timestamp representation (12-bit fraction).
87 *
88 * @param start start iterator of the buffer to append to.
89 * @param time - Time object
90 *
91 * @returns written length
92 */
93 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070094 AppendTimestampBlob (Buffer::Iterator &start, const Time &time);
95
96 static size_t
97 EstimateTimestampBlob (const Time &time);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070098
99 /**
100 * Append a tagged BLOB
101 *
102 * This is a ccnb-encoded element with containing the BLOB as content
103 *
104 * @param start start iterator of the buffer to append to.
105 * @param dtag is the element's dtab
106 * @param data points to the binary data
107 * @param size is the size of the data, in bytes
108 *
109 * @returns written length
110 */
111 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700112 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700113 const uint8_t *data, size_t size);
114
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700115 static size_t
116 EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700117
118 /**
119 * Append value as a tagged BLOB (templated version)
120 *
121 * This is a ccnb-encoded element with containing the BLOB as content
122 *
123 * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof
124 *
125 * @param start start iterator of the buffer to append to.
126 * @param dtag is the element's dtab
127 * @param data a value to add
128 *
129 * @returns written length
130 */
131 template<class T>
132 static inline size_t
133 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data);
134
135 /**
136 * Append a tagged string (should be a valid UTF-8 coded string)
137 *
138 * This is a ccnb-encoded element with containing UDATA as content
139 *
140 * @param start start iterator of the buffer to append to.
141 * @param dtag is the element's dtab
142 * @param string UTF-8 string to be written
143 *
144 * @returns written length
145 */
146 static size_t
147 AppendString (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
148 const std::string &string);
149
150 static size_t
151 EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700152};
153
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700154
155template<class T>
156size_t
157CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data)
158{
159 return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data));
160}
161
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700162} // namespace ns3
163
164#endif // _CCNX_ENCODING_HELPER_H_
165