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 | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 6 | #include <stdexcept> |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 7 | #include "../c/encoding/binary-xml-interest.h" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 8 | #include "../c/encoding/binary-xml-data.h" |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 9 | #include "../c/encoding/binary-xml-forwarding-entry.h" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "../interest.hpp" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 11 | #include "../data.hpp" |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 12 | #include "../forwarding-entry.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 13 | #include "binary-xml-encoder.hpp" |
| 14 | #include "binary-xml-decoder.hpp" |
| 15 | #include "binary-xml-wire-format.hpp" |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 16 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 17 | using namespace std; |
| 18 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 19 | namespace ndn { |
| 20 | |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 21 | // This is declared in the WireFormat class. |
| 22 | WireFormat *WireFormat::newInitialDefaultWireFormat() |
| 23 | { |
| 24 | return new BinaryXmlWireFormat(); |
| 25 | } |
| 26 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 27 | ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeInterest(const Interest& interest) |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 28 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 29 | struct ndn_NameComponent nameComponents[100]; |
| 30 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 31 | struct ndn_Interest interestStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 32 | ndn_Interest_initialize |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 33 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 34 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 35 | interest.get(interestStruct); |
| 36 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 37 | BinaryXmlEncoder encoder; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 38 | ndn_Error error; |
| 39 | if ((error = ndn_encodeBinaryXmlInterest(&interestStruct, &encoder))) |
| 40 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 41 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 42 | return encoder.getOutput(); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 45 | void BinaryXmlWireFormat::decodeInterest(Interest& interest, const unsigned char *input, unsigned int inputLength) |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 46 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 47 | struct ndn_NameComponent nameComponents[100]; |
| 48 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 49 | struct ndn_Interest interestStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 50 | ndn_Interest_initialize |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 51 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 52 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 53 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 54 | BinaryXmlDecoder decoder(input, inputLength); |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 55 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 56 | if ((error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder))) |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 57 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 58 | |
| 59 | interest.set(interestStruct); |
| 60 | } |
| 61 | |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 62 | ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeData |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 63 | (const Data& data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 64 | { |
| 65 | struct ndn_NameComponent nameComponents[100]; |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 66 | struct ndn_NameComponent keyNameComponents[100]; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 67 | struct ndn_Data dataStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 68 | ndn_Data_initialize |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 69 | (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 70 | keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0])); |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 71 | data.get(dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 72 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 73 | BinaryXmlEncoder encoder; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 74 | ndn_Error error; |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 75 | if ((error = ndn_encodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &encoder))) |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 76 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 77 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 78 | return encoder.getOutput(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 81 | void BinaryXmlWireFormat::decodeData |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 82 | (Data& data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 83 | { |
| 84 | struct ndn_NameComponent nameComponents[100]; |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 85 | struct ndn_NameComponent keyNameComponents[100]; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 86 | struct ndn_Data dataStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 87 | ndn_Data_initialize |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 88 | (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 89 | keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0])); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 90 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 91 | BinaryXmlDecoder decoder(input, inputLength); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 92 | ndn_Error error; |
Jeff Thompson | c87f39a | 2013-08-12 11:55:11 -0700 | [diff] [blame] | 93 | if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &decoder))) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 94 | throw std::runtime_error(ndn_getErrorString(error)); |
| 95 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 96 | data.set(dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 99 | ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeForwardingEntry(const ForwardingEntry& forwardingEntry) |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 100 | { |
| 101 | struct ndn_NameComponent prefixNameComponents[100]; |
| 102 | struct ndn_ForwardingEntry forwardingEntryStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 103 | ndn_ForwardingEntry_initialize |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 104 | (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0])); |
| 105 | forwardingEntry.get(forwardingEntryStruct); |
| 106 | |
| 107 | BinaryXmlEncoder encoder; |
| 108 | ndn_Error error; |
| 109 | if ((error = ndn_encodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &encoder))) |
| 110 | throw std::runtime_error(ndn_getErrorString(error)); |
| 111 | |
| 112 | return encoder.getOutput(); |
| 113 | } |
| 114 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 115 | void BinaryXmlWireFormat::decodeForwardingEntry(ForwardingEntry& forwardingEntry, const unsigned char *input, unsigned int inputLength) |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 116 | { |
| 117 | struct ndn_NameComponent prefixNameComponents[100]; |
| 118 | struct ndn_ForwardingEntry forwardingEntryStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 119 | ndn_ForwardingEntry_initialize |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 120 | (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0])); |
| 121 | |
| 122 | BinaryXmlDecoder decoder(input, inputLength); |
| 123 | ndn_Error error; |
| 124 | if ((error = ndn_decodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &decoder))) |
| 125 | throw std::runtime_error(ndn_getErrorString(error)); |
| 126 | |
| 127 | forwardingEntry.set(forwardingEntryStruct); |
| 128 | } |
| 129 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 130 | } |