blob: 489415bbaac412ac4b24c3dcb1f7394b54ea565d [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07005 */
6
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07007#ifndef NDN_WIREFORMAT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_WIREFORMAT_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07009
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070010#include "../common.hpp"
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070011#include "../util/blob.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070012
13namespace ndn {
14
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070015class Interest;
Jeff Thompson56ec9e22013-08-02 11:34:07 -070016class Data;
Jeff Thompson990599b2013-08-27 15:14:25 -070017class ForwardingEntry;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070018
19class WireFormat {
20public:
Jeff Thompson990599b2013-08-27 15:14:25 -070021 /**
22 * Encode interest and return the encoding. Your derived class should override.
23 * @param interest The Interest object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070024 * @return A Blob containing the encoding.
Jeff Thompson990599b2013-08-27 15:14:25 -070025 * @throw logic_error for unimplemented if the derived class does not override.
26 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070027 virtual Blob
28 encodeInterest(const Interest& interest);
Jeff Thompson990599b2013-08-27 15:14:25 -070029
30 /**
31 * Decode input as an interest and set the fields of the interest object. Your derived class should override.
32 * @param interest The Interest object whose fields are updated.
33 * @param input A pointer to the input buffer to decode.
34 * @param inputLength The number of bytes in input.
35 * @throw logic_error for unimplemented if the derived class does not override.
36 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070037 virtual void
Jeff Thompson10ad12a2013-09-24 16:19:11 -070038 decodeInterest(Interest& interest, const uint8_t *input, unsigned int inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070039
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070040 /**
41 * Encode data and return the encoding. Your derived class should override.
42 * @param data The Data object to encode.
Jeff Thompson9c661702013-09-13 14:35:44 -070043 * @param signedPortionBeginOffset Return the offset in the encoding of the beginning of the signed portion.
Jeff Thompson1656e6a2013-08-29 18:01:48 -070044 * If you are not encoding in order to sign, you can call encodeData(const Data& data) to ignore this returned value.
Jeff Thompson9c661702013-09-13 14:35:44 -070045 * @param signedPortionEndOffset Return the offset in the encoding of the end of the signed portion.
Jeff Thompson1656e6a2013-08-29 18:01:48 -070046 * If you are not encoding in order to sign, you can call encodeData(const Data& data) to ignore this returned value.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070047 * @return A Blob containing the encoding.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070048 * @throw logic_error for unimplemented if the derived class does not override.
49 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070050 virtual Blob
51 encodeData
Jeff Thompson9c661702013-09-13 14:35:44 -070052 (const Data& data, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset);
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070053
54 /**
55 * Encode data and return the encoding.
56 * @param data The Data object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070057 * @return A Blob containing the encoding.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070058 * @throw logic_error for unimplemented if the derived class does not override.
59 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070060 Blob
61 encodeData(const Data& data)
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070062 {
63 unsigned int dummyBeginOffset, dummyEndOffset;
64 return encodeData(data, &dummyBeginOffset, &dummyEndOffset);
65 }
66
67 /**
68 * Decode input as a data packet and set the fields in the data object. Your derived class should override.
69 * @param data The Data object whose fields are updated.
70 * @param input A pointer to the input buffer to decode.
71 * @param inputLength The number of bytes in input.
Jeff Thompson9c661702013-09-13 14:35:44 -070072 * @param signedPortionBeginOffset Return the offset in the input buffer of the beginning of the signed portion.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070073 * If you are not decoding in order to verify, you can call
Jeff Thompson10ad12a2013-09-24 16:19:11 -070074 * decodeData(Data& data, const uint8_t *input, unsigned int inputLength) to ignore this returned value.
Jeff Thompson9c661702013-09-13 14:35:44 -070075 * @param signedPortionEndOffset Return the offset in the input buffer of the end of the signed portion.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070076 * If you are not decoding in order to verify, you can call
Jeff Thompson10ad12a2013-09-24 16:19:11 -070077 * decodeData(Data& data, const uint8_t *input, unsigned int inputLength) to ignore this returned value.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070078 * @throw logic_error for unimplemented if the derived class does not override.
79 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070080 virtual void
81 decodeData
Jeff Thompson10ad12a2013-09-24 16:19:11 -070082 (Data& data, const uint8_t *input, unsigned int inputLength, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset);
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070083
Jeff Thompson0050abe2013-09-17 12:50:25 -070084 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -070085 decodeData(Data& data, const uint8_t *input, unsigned int inputLength)
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070086 {
87 unsigned int dummyBeginOffset, dummyEndOffset;
88 decodeData(data, input, inputLength, &dummyBeginOffset, &dummyEndOffset);
89 }
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070090
91 /**
Jeff Thompson990599b2013-08-27 15:14:25 -070092 * Encode forwardingEntry and return the encoding. Your derived class should override.
93 * @param forwardingEntry The ForwardingEntry object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070094 * @return A Blob containing the encoding.
Jeff Thompson990599b2013-08-27 15:14:25 -070095 * @throw logic_error for unimplemented if the derived class does not override.
96 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070097 virtual Blob
98 encodeForwardingEntry(const ForwardingEntry& forwardingEntry);
Jeff Thompson990599b2013-08-27 15:14:25 -070099
100 /**
101 * Decode input as a forwarding entry and set the fields of the forwardingEntry object. Your derived class should override.
102 * @param forwardingEntry The ForwardingEntry object whose fields are updated.
103 * @param input A pointer to the input buffer to decode.
104 * @param inputLength The number of bytes in input.
105 * @throw logic_error for unimplemented if the derived class does not override.
106 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700107 virtual void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700108 decodeForwardingEntry(ForwardingEntry& forwardingEntry, const uint8_t *input, unsigned int inputLength);
Jeff Thompson990599b2013-08-27 15:14:25 -0700109
110 /**
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700111 * Set the static default WireFormat used by default encoding and decoding methods.
Jeff Thompson72fd81d2013-08-20 12:34:51 -0700112 * @param wireFormat A Pointer to an object of a subclass of WireFormat. This does not make a copy and
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700113 * the caller must ensure that the object remains allocated.
114 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700115 static void
116 setDefaultWireFormat(WireFormat *wireFormat)
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700117 {
118 defaultWireFormat_ = wireFormat;
119 }
120
121 /**
122 * Return the default WireFormat used by default encoding and decoding methods which was set with
123 * setDefaultWireFormat.
124 * @return A pointer to the WireFormat object.
125 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700126 static WireFormat*
127 getDefaultWireFormat();
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700128
129private:
130 /**
131 * This is implemented by only one of the subclasses of WireFormat to return a new object used
132 * as the initial value for the default WireFormat. If the application doesn't include that class, then the application
133 * needs to include another subclass which defines WireFormat::newInitialDefaultWireFormat.
134 * @return a new object, which is held by a shared_ptr and freed when the application exits.
135 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700136 static WireFormat*
137 newInitialDefaultWireFormat();
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700138
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700139 static WireFormat *defaultWireFormat_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700140};
141
142}
143
144#endif
145