blob: e0ffdf07fdd35a86fdba3132cc7709dc2915649c [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 Thompson9c41dfe2013-06-27 12:10:25 -070016
17class WireFormat {
18public:
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070019 virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeInterest(const Interest &interest);
Jeff Thompson42380712013-06-28 10:59:33 -070020 virtual void decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070021
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070022 /**
23 * Encode data and return the encoding. Your derived class should override.
24 * @param data The Data object to encode.
25 * @param signedFieldsBeginOffset Return the offset in the encoding of the beginning of the fields which are signed.
26 * If you are not encoding in order to sign, you can call encodeData(const Data &data) to ignore this returned value.
27 * @param signedFieldsEndOffset Return the offset in the encoding of the end of the fields which are signed.
28 * If you are not encoding in order to sign, you can call encodeData(const Data &data) to ignore this returned value.
29 * @return A shared_ptr with the vector<unsigned char> containing the encoding.
30 * @throw logic_error for unimplemented if the derived class does not override.
31 */
32 virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeData
33 (const Data &data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset);
34
35 /**
36 * Encode data and return the encoding.
37 * @param data The Data object to encode.
38 * @return A shared_ptr with the vector<unsigned char> containing the encoding.
39 * @throw logic_error for unimplemented if the derived class does not override.
40 */
41 ptr_lib::shared_ptr<std::vector<unsigned char> > encodeData(const Data &data)
42 {
43 unsigned int dummyBeginOffset, dummyEndOffset;
44 return encodeData(data, &dummyBeginOffset, &dummyEndOffset);
45 }
46
47 /**
48 * Decode input as a data packet and set the fields in the data object. Your derived class should override.
49 * @param data The Data object whose fields are updated.
50 * @param input A pointer to the input buffer to decode.
51 * @param inputLength The number of bytes in input.
52 * @param signedFieldsBeginOffset Return the offset in the input buffer of the beginning of the fields which are signed.
53 * If you are not decoding in order to verify, you can call
54 * decodeData(Data &data, const unsigned char *input, unsigned int inputLength) to ignore this returned value.
55 * @param signedFieldsEndOffset Return the offset in the input buffer of the end of the fields which are signed.
56 * If you are not decoding in order to verify, you can call
57 * decodeData(Data &data, const unsigned char *input, unsigned int inputLength) to ignore this returned value.
58 * @throw logic_error for unimplemented if the derived class does not override.
59 */
60 virtual void decodeData
61 (Data &data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset);
62
63 void decodeData(Data &data, const unsigned char *input, unsigned int inputLength)
64 {
65 unsigned int dummyBeginOffset, dummyEndOffset;
66 decodeData(data, input, inputLength, &dummyBeginOffset, &dummyEndOffset);
67 }
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070068
69 /**
70 * Set the static default WireFormat used by default encoding and decoding methods.
71 * @param wireFormat A Pointer to a object of a subclass of WireFormat. This does not make a copy and
72 * the caller must ensure that the object remains allocated.
73 */
74 static void setDefaultWireFormat(WireFormat *wireFormat)
75 {
76 defaultWireFormat_ = wireFormat;
77 }
78
79 /**
80 * Return the default WireFormat used by default encoding and decoding methods which was set with
81 * setDefaultWireFormat.
82 * @return A pointer to the WireFormat object.
83 */
Jeff Thompson535f21a2013-08-05 18:25:46 -070084 static WireFormat *getDefaultWireFormat();
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070085
86private:
87 /**
88 * This is implemented by only one of the subclasses of WireFormat to return a new object used
89 * as the initial value for the default WireFormat. If the application doesn't include that class, then the application
90 * needs to include another subclass which defines WireFormat::newInitialDefaultWireFormat.
91 * @return a new object, which is held by a shared_ptr and freed when the application exits.
92 */
93 static WireFormat *newInitialDefaultWireFormat();
94
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070095 static WireFormat *defaultWireFormat_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070096};
97
98}
99
100#endif
101