Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 7 | #ifndef NDN_WIREFORMAT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 8 | #define NDN_WIREFORMAT_HPP |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 10 | #include "../common.hpp" |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 11 | #include "../util/blob.hpp" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
| 14 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 15 | class Interest; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 16 | class Data; |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 17 | class ForwardingEntry; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 18 | |
| 19 | class WireFormat { |
| 20 | public: |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Encode interest and return the encoding. Your derived class should override. |
| 23 | * @param interest The Interest object to encode. |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 24 | * @return A Blob containing the encoding. |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 25 | * @throw logic_error for unimplemented if the derived class does not override. |
| 26 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 27 | virtual Blob |
| 28 | encodeInterest(const Interest& interest); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 29 | |
| 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 Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 37 | virtual void |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 38 | decodeInterest(Interest& interest, const uint8_t *input, unsigned int inputLength); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 39 | |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Encode data and return the encoding. Your derived class should override. |
| 42 | * @param data The Data object to encode. |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 43 | * @param signedPortionBeginOffset Return the offset in the encoding of the beginning of the signed portion. |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 44 | * If you are not encoding in order to sign, you can call encodeData(const Data& data) to ignore this returned value. |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 45 | * @param signedPortionEndOffset Return the offset in the encoding of the end of the signed portion. |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 46 | * If you are not encoding in order to sign, you can call encodeData(const Data& data) to ignore this returned value. |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 47 | * @return A Blob containing the encoding. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 48 | * @throw logic_error for unimplemented if the derived class does not override. |
| 49 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 50 | virtual Blob |
| 51 | encodeData |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 52 | (const Data& data, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset); |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Encode data and return the encoding. |
| 56 | * @param data The Data object to encode. |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 57 | * @return A Blob containing the encoding. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 58 | * @throw logic_error for unimplemented if the derived class does not override. |
| 59 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 60 | Blob |
| 61 | encodeData(const Data& data) |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 62 | { |
| 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 Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 72 | * @param signedPortionBeginOffset Return the offset in the input buffer of the beginning of the signed portion. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 73 | * If you are not decoding in order to verify, you can call |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 74 | * decodeData(Data& data, const uint8_t *input, unsigned int inputLength) to ignore this returned value. |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 75 | * @param signedPortionEndOffset Return the offset in the input buffer of the end of the signed portion. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 76 | * If you are not decoding in order to verify, you can call |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 77 | * decodeData(Data& data, const uint8_t *input, unsigned int inputLength) to ignore this returned value. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 78 | * @throw logic_error for unimplemented if the derived class does not override. |
| 79 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 80 | virtual void |
| 81 | decodeData |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 82 | (Data& data, const uint8_t *input, unsigned int inputLength, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset); |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 83 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 84 | void |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 85 | decodeData(Data& data, const uint8_t *input, unsigned int inputLength) |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 86 | { |
| 87 | unsigned int dummyBeginOffset, dummyEndOffset; |
| 88 | decodeData(data, input, inputLength, &dummyBeginOffset, &dummyEndOffset); |
| 89 | } |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 92 | * Encode forwardingEntry and return the encoding. Your derived class should override. |
| 93 | * @param forwardingEntry The ForwardingEntry object to encode. |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 94 | * @return A Blob containing the encoding. |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 95 | * @throw logic_error for unimplemented if the derived class does not override. |
| 96 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 97 | virtual Blob |
| 98 | encodeForwardingEntry(const ForwardingEntry& forwardingEntry); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 99 | |
| 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 Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 107 | virtual void |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 108 | decodeForwardingEntry(ForwardingEntry& forwardingEntry, const uint8_t *input, unsigned int inputLength); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 111 | * Set the static default WireFormat used by default encoding and decoding methods. |
Jeff Thompson | 72fd81d | 2013-08-20 12:34:51 -0700 | [diff] [blame] | 112 | * @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] | 113 | * the caller must ensure that the object remains allocated. |
| 114 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 115 | static void |
| 116 | setDefaultWireFormat(WireFormat *wireFormat) |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 117 | { |
| 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 Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 126 | static WireFormat* |
| 127 | getDefaultWireFormat(); |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 128 | |
| 129 | private: |
| 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 Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 136 | static WireFormat* |
| 137 | newInitialDefaultWireFormat(); |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 138 | |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 139 | static WireFormat *defaultWireFormat_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | } |
| 143 | |
| 144 | #endif |
| 145 | |