Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 8 | #ifndef NDN_WIREFORMAT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 9 | #define NDN_WIREFORMAT_HPP |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 11 | #include "../common.hpp" |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 12 | #include "../util/blob.hpp" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
| 15 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 16 | class Interest; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 17 | class Data; |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 18 | class ForwardingEntry; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 19 | |
| 20 | class WireFormat { |
| 21 | public: |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 22 | /** |
| 23 | * Encode interest and return the encoding. Your derived class should override. |
| 24 | * @param interest The Interest object to encode. |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 25 | * @return A Blob containing the encoding. |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 26 | * @throw logic_error for unimplemented if the derived class does not override. |
| 27 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 28 | virtual Blob |
| 29 | encodeInterest(const Interest& interest); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Decode input as an interest and set the fields of the interest object. Your derived class should override. |
| 33 | * @param interest The Interest object whose fields are updated. |
| 34 | * @param input A pointer to the input buffer to decode. |
| 35 | * @param inputLength The number of bytes in input. |
| 36 | * @throw logic_error for unimplemented if the derived class does not override. |
| 37 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 38 | virtual void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 39 | decodeInterest(Interest& interest, const uint8_t *input, size_t inputLength); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 40 | |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Encode data and return the encoding. Your derived class should override. |
| 43 | * @param data The Data object to encode. |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 44 | * @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] | 45 | * 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] | 46 | * @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] | 47 | * 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] | 48 | * @return A Blob containing the encoding. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 49 | * @throw logic_error for unimplemented if the derived class does not override. |
| 50 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 51 | virtual Blob |
| 52 | encodeData |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 53 | (const Data& data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset); |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Encode data and return the encoding. |
| 57 | * @param data The Data object to encode. |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 58 | * @return A Blob containing the encoding. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 59 | * @throw logic_error for unimplemented if the derived class does not override. |
| 60 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 61 | Blob |
| 62 | encodeData(const Data& data) |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 63 | { |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 64 | size_t dummyBeginOffset, dummyEndOffset; |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 65 | return encodeData(data, &dummyBeginOffset, &dummyEndOffset); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Decode input as a data packet and set the fields in the data object. Your derived class should override. |
| 70 | * @param data The Data object whose fields are updated. |
| 71 | * @param input A pointer to the input buffer to decode. |
| 72 | * @param inputLength The number of bytes in input. |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 73 | * @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] | 74 | * If you are not decoding in order to verify, you can call |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 75 | * decodeData(Data& data, const uint8_t *input, size_t inputLength) to ignore this returned value. |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 76 | * @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] | 77 | * If you are not decoding in order to verify, you can call |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 78 | * decodeData(Data& data, const uint8_t *input, size_t inputLength) to ignore this returned value. |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 79 | * @throw logic_error for unimplemented if the derived class does not override. |
| 80 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 81 | virtual void |
| 82 | decodeData |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 83 | (Data& data, const uint8_t *input, size_t inputLength, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset); |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 84 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 85 | void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 86 | decodeData(Data& data, const uint8_t *input, size_t inputLength) |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 87 | { |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 88 | size_t dummyBeginOffset, dummyEndOffset; |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 89 | decodeData(data, input, inputLength, &dummyBeginOffset, &dummyEndOffset); |
| 90 | } |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 93 | * Encode forwardingEntry and return the encoding. Your derived class should override. |
| 94 | * @param forwardingEntry The ForwardingEntry object to encode. |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 95 | * @return A Blob containing the encoding. |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 96 | * @throw logic_error for unimplemented if the derived class does not override. |
| 97 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 98 | virtual Blob |
| 99 | encodeForwardingEntry(const ForwardingEntry& forwardingEntry); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Decode input as a forwarding entry and set the fields of the forwardingEntry object. Your derived class should override. |
| 103 | * @param forwardingEntry The ForwardingEntry object whose fields are updated. |
| 104 | * @param input A pointer to the input buffer to decode. |
| 105 | * @param inputLength The number of bytes in input. |
| 106 | * @throw logic_error for unimplemented if the derived class does not override. |
| 107 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 108 | virtual void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 109 | decodeForwardingEntry(ForwardingEntry& forwardingEntry, const uint8_t *input, size_t inputLength); |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 112 | * Set the static default WireFormat used by default encoding and decoding methods. |
Jeff Thompson | 72fd81d | 2013-08-20 12:34:51 -0700 | [diff] [blame] | 113 | * @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] | 114 | * the caller must ensure that the object remains allocated. |
| 115 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 116 | static void |
| 117 | setDefaultWireFormat(WireFormat *wireFormat) |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 118 | { |
| 119 | defaultWireFormat_ = wireFormat; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Return the default WireFormat used by default encoding and decoding methods which was set with |
| 124 | * setDefaultWireFormat. |
| 125 | * @return A pointer to the WireFormat object. |
| 126 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 127 | static WireFormat* |
| 128 | getDefaultWireFormat(); |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | /** |
| 132 | * This is implemented by only one of the subclasses of WireFormat to return a new object used |
| 133 | * as the initial value for the default WireFormat. If the application doesn't include that class, then the application |
| 134 | * needs to include another subclass which defines WireFormat::newInitialDefaultWireFormat. |
| 135 | * @return a new object, which is held by a shared_ptr and freed when the application exits. |
| 136 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 137 | static WireFormat* |
| 138 | newInitialDefaultWireFormat(); |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 139 | |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 140 | static WireFormat *defaultWireFormat_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | #endif |
| 146 | |