blob: e4fe45d745c777a2b6bf2392c99bffd4dc574a70 [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);
61
62 static size_t
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070063 Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject);
64
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070065 static size_t
66 GetSerializedSize (const CcnxContentObjectHeader &contentObject);
67
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070068private:
69 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070070 AppendBlockHeader (Buffer::Iterator &start, size_t value, CcnbParser::ccn_tt block_type);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070071
72 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070073 EstimateBlockHeader (size_t value);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070074
75 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070076 AppendNumber (Buffer::Iterator &start, uint32_t number);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070077
78 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070079 EstimateNumber (uint32_t number);
80
81 static size_t
82 AppendCloser (Buffer::Iterator &start);
83
84 static size_t
85 AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name);
86
87 static size_t
88 EstimateNameComponents (const CcnxNameComponents &name);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070089
90 /**
91 * Append a binary timestamp as a BLOB using the ccn binary
92 * Timestamp representation (12-bit fraction).
93 *
94 * @param start start iterator of the buffer to append to.
95 * @param time - Time object
96 *
97 * @returns written length
98 */
99 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700100 AppendTimestampBlob (Buffer::Iterator &start, const Time &time);
101
102 static size_t
103 EstimateTimestampBlob (const Time &time);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700104
105 /**
106 * Append a tagged BLOB
107 *
108 * This is a ccnb-encoded element with containing the BLOB as content
109 *
110 * @param start start iterator of the buffer to append to.
111 * @param dtag is the element's dtab
112 * @param data points to the binary data
113 * @param size is the size of the data, in bytes
114 *
115 * @returns written length
116 */
117 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700118 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700119 const uint8_t *data, size_t size);
120
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700121 static size_t
122 EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700123};
124
125} // namespace ns3
126
127#endif // _CCNX_ENCODING_HELPER_H_
128