blob: 575b441adda1bcf0ee38b029545a388b0bdb8c3c [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07004 */
5
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07006#ifndef NDN_WIREFORMAT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_WIREFORMAT_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07008
Jeff Thompsonb0979fd2013-07-30 15:48:21 -07009#include "../common.hpp"
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070010#include "../util/blob.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070011
12namespace ndn {
13
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070014class Interest;
Jeff Thompson56ec9e22013-08-02 11:34:07 -070015class Data;
Jeff Thompson990599b2013-08-27 15:14:25 -070016class ForwardingEntry;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070017
18class WireFormat {
19public:
Jeff Thompson990599b2013-08-27 15:14:25 -070020 /**
21 * Encode interest and return the encoding. Your derived class should override.
22 * @param interest The Interest object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070023 * @return A Blob containing the encoding.
Jeff Thompson990599b2013-08-27 15:14:25 -070024 * @throw logic_error for unimplemented if the derived class does not override.
25 */
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070026 virtual Blob encodeInterest(const Interest& interest);
Jeff Thompson990599b2013-08-27 15:14:25 -070027
28 /**
29 * Decode input as an interest and set the fields of the interest object. Your derived class should override.
30 * @param interest The Interest object whose fields are updated.
31 * @param input A pointer to the input buffer to decode.
32 * @param inputLength The number of bytes in input.
33 * @throw logic_error for unimplemented if the derived class does not override.
34 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070035 virtual void decodeInterest(Interest& interest, const unsigned char *input, unsigned int inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070036
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070037 /**
38 * Encode data and return the encoding. Your derived class should override.
39 * @param data The Data object to encode.
40 * @param signedFieldsBeginOffset Return the offset in the encoding of the beginning of the fields which are signed.
Jeff Thompson1656e6a2013-08-29 18:01:48 -070041 * If you are not encoding in order to sign, you can call encodeData(const Data& data) to ignore this returned value.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070042 * @param signedFieldsEndOffset Return the offset in the encoding of the end of the fields which are signed.
Jeff Thompson1656e6a2013-08-29 18:01:48 -070043 * 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 -070044 * @return A Blob containing the encoding.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070045 * @throw logic_error for unimplemented if the derived class does not override.
46 */
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070047 virtual Blob encodeData
Jeff Thompson1656e6a2013-08-29 18:01:48 -070048 (const Data& data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset);
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070049
50 /**
51 * Encode data and return the encoding.
52 * @param data The Data object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070053 * @return A Blob containing the encoding.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070054 * @throw logic_error for unimplemented if the derived class does not override.
55 */
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070056 Blob encodeData(const Data& data)
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070057 {
58 unsigned int dummyBeginOffset, dummyEndOffset;
59 return encodeData(data, &dummyBeginOffset, &dummyEndOffset);
60 }
61
62 /**
63 * Decode input as a data packet and set the fields in the data object. Your derived class should override.
64 * @param data The Data object whose fields are updated.
65 * @param input A pointer to the input buffer to decode.
66 * @param inputLength The number of bytes in input.
67 * @param signedFieldsBeginOffset Return the offset in the input buffer of the beginning of the fields which are signed.
68 * If you are not decoding in order to verify, you can call
Jeff Thompson1656e6a2013-08-29 18:01:48 -070069 * decodeData(Data& data, const unsigned char *input, unsigned int inputLength) to ignore this returned value.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070070 * @param signedFieldsEndOffset Return the offset in the input buffer of the end of the fields which are signed.
71 * If you are not decoding in order to verify, you can call
Jeff Thompson1656e6a2013-08-29 18:01:48 -070072 * decodeData(Data& data, const unsigned char *input, unsigned int inputLength) to ignore this returned value.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070073 * @throw logic_error for unimplemented if the derived class does not override.
74 */
75 virtual void decodeData
Jeff Thompson1656e6a2013-08-29 18:01:48 -070076 (Data& data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset);
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070077
Jeff Thompson1656e6a2013-08-29 18:01:48 -070078 void decodeData(Data& data, const unsigned char *input, unsigned int inputLength)
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070079 {
80 unsigned int dummyBeginOffset, dummyEndOffset;
81 decodeData(data, input, inputLength, &dummyBeginOffset, &dummyEndOffset);
82 }
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070083
84 /**
Jeff Thompson990599b2013-08-27 15:14:25 -070085 * Encode forwardingEntry and return the encoding. Your derived class should override.
86 * @param forwardingEntry The ForwardingEntry object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070087 * @return A Blob containing the encoding.
Jeff Thompson990599b2013-08-27 15:14:25 -070088 * @throw logic_error for unimplemented if the derived class does not override.
89 */
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070090 virtual Blob encodeForwardingEntry(const ForwardingEntry& forwardingEntry);
Jeff Thompson990599b2013-08-27 15:14:25 -070091
92 /**
93 * Decode input as a forwarding entry and set the fields of the forwardingEntry object. Your derived class should override.
94 * @param forwardingEntry The ForwardingEntry object whose fields are updated.
95 * @param input A pointer to the input buffer to decode.
96 * @param inputLength The number of bytes in input.
97 * @throw logic_error for unimplemented if the derived class does not override.
98 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070099 virtual void decodeForwardingEntry(ForwardingEntry& forwardingEntry, const unsigned char *input, unsigned int inputLength);
Jeff Thompson990599b2013-08-27 15:14:25 -0700100
101 /**
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700102 * Set the static default WireFormat used by default encoding and decoding methods.
Jeff Thompson72fd81d2013-08-20 12:34:51 -0700103 * @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 -0700104 * the caller must ensure that the object remains allocated.
105 */
106 static void setDefaultWireFormat(WireFormat *wireFormat)
107 {
108 defaultWireFormat_ = wireFormat;
109 }
110
111 /**
112 * Return the default WireFormat used by default encoding and decoding methods which was set with
113 * setDefaultWireFormat.
114 * @return A pointer to the WireFormat object.
115 */
Jeff Thompson535f21a2013-08-05 18:25:46 -0700116 static WireFormat *getDefaultWireFormat();
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700117
118private:
119 /**
120 * This is implemented by only one of the subclasses of WireFormat to return a new object used
121 * as the initial value for the default WireFormat. If the application doesn't include that class, then the application
122 * needs to include another subclass which defines WireFormat::newInitialDefaultWireFormat.
123 * @return a new object, which is held by a shared_ptr and freed when the application exits.
124 */
125 static WireFormat *newInitialDefaultWireFormat();
126
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700127 static WireFormat *defaultWireFormat_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700128};
129
130}
131
132#endif
133