blob: bfd1a1d24b5221126affa6f586329333edb50af4 [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 Thompson9c41dfe2013-06-27 12:10:25 -070010#include <vector>
11
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.
23 * @return A shared_ptr with the vector<unsigned char> containing the encoding.
24 * @throw logic_error for unimplemented if the derived class does not override.
25 */
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070026 virtual ptr_lib::shared_ptr<std::vector<unsigned char> > 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 Thompson42380712013-06-28 10:59:33 -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.
41 * If you are not encoding in order to sign, you can call encodeData(const Data &data) to ignore this returned value.
42 * @param signedFieldsEndOffset Return the offset in the encoding of the end of the fields which are signed.
43 * If you are not encoding in order to sign, you can call encodeData(const Data &data) to ignore this returned value.
44 * @return A shared_ptr with the vector<unsigned char> containing the encoding.
45 * @throw logic_error for unimplemented if the derived class does not override.
46 */
47 virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeData
48 (const Data &data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset);
49
50 /**
51 * Encode data and return the encoding.
52 * @param data The Data object to encode.
53 * @return A shared_ptr with the vector<unsigned char> containing the encoding.
54 * @throw logic_error for unimplemented if the derived class does not override.
55 */
56 ptr_lib::shared_ptr<std::vector<unsigned char> > encodeData(const Data &data)
57 {
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
69 * decodeData(Data &data, const unsigned char *input, unsigned int inputLength) to ignore this returned value.
70 * @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
72 * decodeData(Data &data, const unsigned char *input, unsigned int inputLength) to ignore this returned value.
73 * @throw logic_error for unimplemented if the derived class does not override.
74 */
75 virtual void decodeData
76 (Data &data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset);
77
78 void decodeData(Data &data, const unsigned char *input, unsigned int inputLength)
79 {
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.
87 * @return A shared_ptr with the vector<unsigned char> containing the encoding.
88 * @throw logic_error for unimplemented if the derived class does not override.
89 */
90 virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeForwardingEntry(const ForwardingEntry &forwardingEntry);
91
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 */
99 virtual void decodeForwardingEntry(ForwardingEntry &forwardingEntry, const unsigned char *input, unsigned int inputLength);
100
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