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 | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 27 | virtual Blob encodeInterest(const Interest& interest); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Decode input as an interest and set the fields of the interest object. Your derived class should override. |
| 31 | * @param interest The Interest object whose fields are updated. |
| 32 | * @param input A pointer to the input buffer to decode. |
| 33 | * @param inputLength The number of bytes in input. |
| 34 | * @throw logic_error for unimplemented if the derived class does not override. |
| 35 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 36 | virtual void decodeInterest(Interest& interest, const unsigned char *input, unsigned int inputLength); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 37 | |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Encode data and return the encoding. Your derived class should override. |
| 40 | * @param data The Data object to encode. |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame^] | 41 | * @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] | 42 | * 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^] | 43 | * @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] | 44 | * 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] | 45 | * @return A Blob containing the encoding. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 46 | * @throw logic_error for unimplemented if the derived class does not override. |
| 47 | */ |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 48 | virtual Blob encodeData |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame^] | 49 | (const Data& data, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset); |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Encode data and return the encoding. |
| 53 | * @param data The Data object to encode. |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 54 | * @return A Blob containing the encoding. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 55 | * @throw logic_error for unimplemented if the derived class does not override. |
| 56 | */ |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 57 | Blob encodeData(const Data& data) |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 58 | { |
| 59 | unsigned int dummyBeginOffset, dummyEndOffset; |
| 60 | return encodeData(data, &dummyBeginOffset, &dummyEndOffset); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Decode input as a data packet and set the fields in the data object. Your derived class should override. |
| 65 | * @param data The Data object whose fields are updated. |
| 66 | * @param input A pointer to the input buffer to decode. |
| 67 | * @param inputLength The number of bytes in input. |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame^] | 68 | * @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] | 69 | * If you are not decoding in order to verify, you can call |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 70 | * decodeData(Data& data, const unsigned char *input, unsigned int inputLength) to ignore this returned value. |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame^] | 71 | * @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] | 72 | * If you are not decoding in order to verify, you can call |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 73 | * 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] | 74 | * @throw logic_error for unimplemented if the derived class does not override. |
| 75 | */ |
| 76 | virtual void decodeData |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame^] | 77 | (Data& data, const unsigned char *input, unsigned int inputLength, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset); |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 78 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 79 | void decodeData(Data& data, const unsigned char *input, unsigned int inputLength) |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 80 | { |
| 81 | unsigned int dummyBeginOffset, dummyEndOffset; |
| 82 | decodeData(data, input, inputLength, &dummyBeginOffset, &dummyEndOffset); |
| 83 | } |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 86 | * Encode forwardingEntry and return the encoding. Your derived class should override. |
| 87 | * @param forwardingEntry The ForwardingEntry object to encode. |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 88 | * @return A Blob containing the encoding. |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 89 | * @throw logic_error for unimplemented if the derived class does not override. |
| 90 | */ |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 91 | virtual Blob encodeForwardingEntry(const ForwardingEntry& forwardingEntry); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Decode input as a forwarding entry and set the fields of the forwardingEntry object. Your derived class should override. |
| 95 | * @param forwardingEntry The ForwardingEntry object whose fields are updated. |
| 96 | * @param input A pointer to the input buffer to decode. |
| 97 | * @param inputLength The number of bytes in input. |
| 98 | * @throw logic_error for unimplemented if the derived class does not override. |
| 99 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 100 | virtual void decodeForwardingEntry(ForwardingEntry& forwardingEntry, const unsigned char *input, unsigned int inputLength); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 103 | * Set the static default WireFormat used by default encoding and decoding methods. |
Jeff Thompson | 72fd81d | 2013-08-20 12:34:51 -0700 | [diff] [blame] | 104 | * @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] | 105 | * the caller must ensure that the object remains allocated. |
| 106 | */ |
| 107 | static void setDefaultWireFormat(WireFormat *wireFormat) |
| 108 | { |
| 109 | defaultWireFormat_ = wireFormat; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Return the default WireFormat used by default encoding and decoding methods which was set with |
| 114 | * setDefaultWireFormat. |
| 115 | * @return A pointer to the WireFormat object. |
| 116 | */ |
Jeff Thompson | 535f21a | 2013-08-05 18:25:46 -0700 | [diff] [blame] | 117 | static WireFormat *getDefaultWireFormat(); |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 118 | |
| 119 | private: |
| 120 | /** |
| 121 | * This is implemented by only one of the subclasses of WireFormat to return a new object used |
| 122 | * as the initial value for the default WireFormat. If the application doesn't include that class, then the application |
| 123 | * needs to include another subclass which defines WireFormat::newInitialDefaultWireFormat. |
| 124 | * @return a new object, which is held by a shared_ptr and freed when the application exits. |
| 125 | */ |
| 126 | static WireFormat *newInitialDefaultWireFormat(); |
| 127 | |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 128 | static WireFormat *defaultWireFormat_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | #endif |
| 134 | |