blob: 91d3d9e4070bfd8394d03391e02927fbc571f159 [file] [log] [blame]
Alexander Afanasyev834f35c2011-08-16 17:13:50 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev1043c702013-07-15 16:21:09 -07003 * Copyright (c) 2011-2013 University of California, Los Angeles
Alexander Afanasyev834f35c2011-08-16 17:13:50 -07004 *
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
Alexander Afanasyev1043c702013-07-15 16:21:09 -070021#ifndef NDN_WIRE_CCNB_SYNTAX_H
22#define NDN_WIRE_CCNB_SYNTAX_H
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070023
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070024#include "ns3/ptr.h"
25#include "ns3/nstime.h"
26#include "ns3/buffer.h"
27
Alexander Afanasyev1043c702013-07-15 16:21:09 -070028#include "ns3/ndn-common.h"
29#include "ns3/ndn-name.h"
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070030
Alexander Afanasyev1043c702013-07-15 16:21:09 -070031NDN_NAMESPACE_BEGIN
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070032
Alexander Afanasyev1043c702013-07-15 16:21:09 -070033namespace wire {
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070034
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070035/**
Alexander Afanasyev1043c702013-07-15 16:21:09 -070036 * \brief Helper to encode CCNb blocks
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070037 */
Alexander Afanasyev1043c702013-07-15 16:21:09 -070038class Ccnb
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070039{
40public:
Ilya Moiseenko332add02011-12-24 17:21:25 -080041 /**
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070042 * @brief Append CCNB block header
Alexander Afanasyev1043c702013-07-15 16:21:09 -070043 * @param start Buffer to store serialized
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070044 * @param value dictionary id of the block header
45 * @param block_type Type of CCNB block
46 *
47 * @returns written length
48 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070049 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -070050 AppendBlockHeader (Buffer::Iterator &start, size_t value, uint32_t block_type);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070051
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070052 /**
53 * @brief Estimate size of the CCNB block header
54 * @param value dictionary id of the block header
55 * @returns estimated length
56 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070057 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070058 EstimateBlockHeader (size_t value);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070059
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070060 /**
61 * @brief Add number in CCNB encoding
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070062 * @param start Buffer to store serialized NdnInterest
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070063 * @param number Number to be written
64 *
65 * @returns written length
66 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070067 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070068 AppendNumber (Buffer::Iterator &start, uint32_t number);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070069
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070070 /**
71 * @brief Estimate size of the number in CCNB encoding
72 * @param number Number to be written
73 * @returns estimated length
74 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070075 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070076 EstimateNumber (uint32_t number);
77
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070078 /**
79 * @brief Append CCNB closer tag (estimated size is 1)
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070080 * @param start Buffer to store serialized Interest
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070081 *
82 * @returns written length
83 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070084 static size_t
85 AppendCloser (Buffer::Iterator &start);
86
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070087 /**
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070088 * Append a binary timestamp as a BLOB using the ccn binary
89 * Timestamp representation (12-bit fraction).
90 *
91 * @param start start iterator of the buffer to append to.
92 * @param time - Time object
93 *
94 * @returns written length
95 */
96 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070097 AppendTimestampBlob (Buffer::Iterator &start, const Time &time);
98
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070099 /**
100 * @brief Estimate size of a binary timestamp as a BLOB using CCNB enconding
101 * @param time - Time object
102 * @returns estimated length
103 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700104 static size_t
105 EstimateTimestampBlob (const Time &time);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700106
107 /**
108 * Append a tagged BLOB
109 *
110 * This is a ccnb-encoded element with containing the BLOB as content
111 *
112 * @param start start iterator of the buffer to append to.
113 * @param dtag is the element's dtab
114 * @param data points to the binary data
115 * @param size is the size of the data, in bytes
116 *
117 * @returns written length
118 */
119 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700120 AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag,
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700121 const uint8_t *data, size_t size);
122
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700123 /**
124 * @brief Estimate size of a tagged BLOB in CCNB enconding
125 * @param dtag is the element's dtab
126 * @param size is the size of the data, in bytes
127 * @returns estimated length
128 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700129 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700130 EstimateTaggedBlob (uint32_t dtag, size_t size);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700131
132 /**
133 * Append value as a tagged BLOB (templated version)
134 *
135 * This is a ccnb-encoded element with containing the BLOB as content
136 *
137 * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof
138 *
139 * @param start start iterator of the buffer to append to.
140 * @param dtag is the element's dtab
141 * @param data a value to add
142 *
143 * @returns written length
144 */
145 template<class T>
146 static inline size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700147 AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700148
149 /**
150 * Append a tagged string (should be a valid UTF-8 coded string)
151 *
152 * This is a ccnb-encoded element with containing UDATA as content
153 *
154 * @param start start iterator of the buffer to append to.
155 * @param dtag is the element's dtab
156 * @param string UTF-8 string to be written
157 *
158 * @returns written length
159 */
160 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700161 AppendString (Buffer::Iterator &start, uint32_t dtag,
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700162 const std::string &string);
163
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700164 /**
165 * @brief Estimate size of the string in CCNB encoding
166 * @param dtag is the element's dtab
167 * @param string UTF-8 string to be written
168 * @returns estimated length
169 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700170 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700171 EstimateString (uint32_t dtag, const std::string &string);
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700172
173 ////////////////////////////////
174 // General use wire formatters
175 ////////////////////////////////
176
177 /**
178 * @brief Append Name in CCNB encoding
179 * @param start Buffer to store serialized Interest
180 * @param name constant reference to Name object
181 *
182 * @returns written length
183 */
184 static size_t
185 SerializeName (Buffer::Iterator &start, const Name &name);
186
187 /**
188 * @brief Estimate size of Name in CCNB encoding
189 * @param name constant reference to Name object
190 * @returns estimated length
191 */
192 static size_t
193 SerializedSizeName (const Name &name);
194
195 /**
196 * @brief Deserialize Name from CCNB encodeing
197 * @param start Buffer that stores serialized Interest
198 * @param name Name object
199 */
200 static Ptr<Name>
201 DeserializeName (Buffer::Iterator &start);
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700202}; // Ccnb
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700203
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700204
205template<class T>
206size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700207Ccnb::AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700208{
209 return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data));
210}
211
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700212} // wire
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700213
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700214NDN_NAMESPACE_END
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700215
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700216#endif // NDN_WIRE_CCNB_SYNTAX_H