blob: 42764c43686ba98f8a225f552078c51749a2fcfa [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 Afanasyevcfdc14f2013-03-15 14:38:44 -070088 * @brief Append Name in CCNB encoding
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070089 * @param start Buffer to store serialized Interest
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070090 * @param name constant reference to Name object
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070091 *
92 * @returns written length
93 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070094 static size_t
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070095 AppendName (Buffer::Iterator &start, const Name &name);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070096
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070097 /**
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070098 * @brief Estimate size of Name in CCNB encoding
99 * @param name constant reference to Name object
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700100 * @returns estimated length
101 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700102 static size_t
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700103 EstimateName (const Name &name);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700104
105 /**
106 * Append a binary timestamp as a BLOB using the ccn binary
107 * Timestamp representation (12-bit fraction).
108 *
109 * @param start start iterator of the buffer to append to.
110 * @param time - Time object
111 *
112 * @returns written length
113 */
114 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700115 AppendTimestampBlob (Buffer::Iterator &start, const Time &time);
116
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700117 /**
118 * @brief Estimate size of a binary timestamp as a BLOB using CCNB enconding
119 * @param time - Time object
120 * @returns estimated length
121 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700122 static size_t
123 EstimateTimestampBlob (const Time &time);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700124
125 /**
126 * Append a tagged BLOB
127 *
128 * This is a ccnb-encoded element with containing the BLOB as content
129 *
130 * @param start start iterator of the buffer to append to.
131 * @param dtag is the element's dtab
132 * @param data points to the binary data
133 * @param size is the size of the data, in bytes
134 *
135 * @returns written length
136 */
137 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700138 AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag,
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700139 const uint8_t *data, size_t size);
140
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700141 /**
142 * @brief Estimate size of a tagged BLOB in CCNB enconding
143 * @param dtag is the element's dtab
144 * @param size is the size of the data, in bytes
145 * @returns estimated length
146 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700147 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700148 EstimateTaggedBlob (uint32_t dtag, size_t size);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700149
150 /**
151 * Append value as a tagged BLOB (templated version)
152 *
153 * This is a ccnb-encoded element with containing the BLOB as content
154 *
155 * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof
156 *
157 * @param start start iterator of the buffer to append to.
158 * @param dtag is the element's dtab
159 * @param data a value to add
160 *
161 * @returns written length
162 */
163 template<class T>
164 static inline size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700165 AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700166
167 /**
168 * Append a tagged string (should be a valid UTF-8 coded string)
169 *
170 * This is a ccnb-encoded element with containing UDATA as content
171 *
172 * @param start start iterator of the buffer to append to.
173 * @param dtag is the element's dtab
174 * @param string UTF-8 string to be written
175 *
176 * @returns written length
177 */
178 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700179 AppendString (Buffer::Iterator &start, uint32_t dtag,
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700180 const std::string &string);
181
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700182 /**
183 * @brief Estimate size of the string in CCNB encoding
184 * @param dtag is the element's dtab
185 * @param string UTF-8 string to be written
186 * @returns estimated length
187 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700188 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700189 EstimateString (uint32_t dtag, const std::string &string);
190}; // Ccnb
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700191
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700192
193template<class T>
194size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700195Ccnb::AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700196{
197 return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data));
198}
199
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700200} // wire
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700201
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700202NDN_NAMESPACE_END
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700203
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700204#endif // NDN_WIRE_CCNB_SYNTAX_H