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 | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 7 | #include "../c/encoding/BinaryXMLInterest.h" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 8 | #include "../c/encoding/binary-xml-data.h" |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 9 | #include "../Interest.hpp" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 10 | #include "../data.hpp" |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 11 | #include "BinaryXMLEncoder.hpp" |
Jeff Thompson | 430a77a | 2013-07-15 17:17:56 -0700 | [diff] [blame] | 12 | #include "BinaryXMLDecoder.hpp" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 13 | #include "BinaryXMLWireFormat.hpp" |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 15 | using namespace std; |
| 16 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 17 | namespace ndn { |
| 18 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 19 | BinaryXmlWireFormat BinaryXmlWireFormat::instance_; |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 20 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 21 | ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeInterest(const Interest &interest) |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 22 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 23 | struct ndn_NameComponent nameComponents[100]; |
| 24 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 25 | struct ndn_Interest interestStruct; |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 26 | ndn_Interest_init |
| 27 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 28 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 29 | interest.get(interestStruct); |
| 30 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 31 | BinaryXmlEncoder encoder; |
| 32 | ndn_encodeBinaryXmlInterest(&interestStruct, &encoder); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 33 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 34 | return encoder.getOutput(); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 37 | void BinaryXmlWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength) |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 38 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 39 | struct ndn_NameComponent nameComponents[100]; |
| 40 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 41 | struct ndn_Interest interestStruct; |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 42 | ndn_Interest_init |
| 43 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 44 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 45 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 46 | BinaryXmlDecoder decoder(input, inputLength); |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 47 | ndn_Error error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 48 | if (error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder)) |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 49 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 50 | |
| 51 | interest.set(interestStruct); |
| 52 | } |
| 53 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 54 | ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeData(const Data &data) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 55 | { |
| 56 | struct ndn_NameComponent nameComponents[100]; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 57 | struct ndn_Data dataStruct; |
| 58 | ndn_Data_init |
| 59 | (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0])); |
| 60 | data.get(dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 61 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 62 | BinaryXmlEncoder encoder; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 63 | ndn_encodeBinaryXmlData(&dataStruct, &encoder); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 64 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 65 | return encoder.getOutput(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 68 | void BinaryXmlWireFormat::decodeData(Data &data, const unsigned char *input, unsigned int inputLength) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 69 | { |
| 70 | struct ndn_NameComponent nameComponents[100]; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 71 | struct ndn_Data dataStruct; |
| 72 | ndn_Data_init |
| 73 | (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0])); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 74 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 75 | BinaryXmlDecoder decoder(input, inputLength); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 76 | ndn_Error error; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 77 | if (error = ndn_decodeBinaryXmlData(&dataStruct, &decoder)) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 78 | throw std::runtime_error(ndn_getErrorString(error)); |
| 79 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 80 | data.set(dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 83 | } |