Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 6 | #ifndef NDN_WIREFORMAT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 7 | #define NDN_WIREFORMAT_HPP |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 9 | #include "../common.hpp" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
| 12 | namespace ndn { |
| 13 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 14 | class Interest; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 15 | class Data; |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 16 | class ForwardingEntry; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 17 | |
| 18 | class WireFormat { |
| 19 | public: |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 20 | /** |
| 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 Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 26 | virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeInterest(const Interest& interest); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 27 | |
| 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 Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 35 | virtual void decodeInterest(Interest& interest, const unsigned char *input, unsigned int inputLength); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 36 | |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 37 | /** |
| 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 Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 41 | * If you are not encoding in order to sign, you can call encodeData(const Data& data) to ignore this returned value. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 42 | * @param signedFieldsEndOffset Return the offset in the encoding of the end of the fields which are signed. |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 43 | * If you are not encoding in order to sign, you can call encodeData(const Data& data) to ignore this returned value. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 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 |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 48 | (const Data& data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset); |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 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 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 56 | ptr_lib::shared_ptr<std::vector<unsigned char> > encodeData(const Data& data) |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 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 |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 69 | * decodeData(Data& data, const unsigned char *input, unsigned int inputLength) to ignore this returned value. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 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 |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 72 | * decodeData(Data& data, const unsigned char *input, unsigned int inputLength) to ignore this returned value. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 73 | * @throw logic_error for unimplemented if the derived class does not override. |
| 74 | */ |
| 75 | virtual void decodeData |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 76 | (Data& data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset); |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 77 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 78 | void decodeData(Data& data, const unsigned char *input, unsigned int inputLength) |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 79 | { |
| 80 | unsigned int dummyBeginOffset, dummyEndOffset; |
| 81 | decodeData(data, input, inputLength, &dummyBeginOffset, &dummyEndOffset); |
| 82 | } |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 85 | * 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 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 90 | virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeForwardingEntry(const ForwardingEntry& forwardingEntry); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 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 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 99 | virtual void decodeForwardingEntry(ForwardingEntry& forwardingEntry, const unsigned char *input, unsigned int inputLength); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 102 | * Set the static default WireFormat used by default encoding and decoding methods. |
Jeff Thompson | 72fd81d | 2013-08-20 12:34:51 -0700 | [diff] [blame] | 103 | * @param wireFormat A Pointer to an object of a subclass of WireFormat. This does not make a copy and |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 104 | * 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 Thompson | 535f21a | 2013-08-05 18:25:46 -0700 | [diff] [blame] | 116 | static WireFormat *getDefaultWireFormat(); |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 117 | |
| 118 | private: |
| 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 Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 127 | static WireFormat *defaultWireFormat_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | } |
| 131 | |
| 132 | #endif |
| 133 | |