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 | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 8 | #include "../c/encoding/BinaryXMLContentObject.h" |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 9 | #include "../Interest.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 10 | #include "../ContentObject.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 | |
| 19 | BinaryXMLWireFormat BinaryXMLWireFormat::instance_; |
| 20 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -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 | |
| 31 | BinaryXMLEncoder encoder; |
Jeff Thompson | 0adcbea | 2013-07-15 16:29:52 -0700 | [diff] [blame] | 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 | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 37 | void BinaryXMLWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength) |
| 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 | 430a77a | 2013-07-15 17:17:56 -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 | e2c232d | 2013-07-03 18:47:29 -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 | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame^] | 54 | ptr_lib::shared_ptr<vector<unsigned char> > BinaryXMLWireFormat::encodeContentObject(const ContentObject &contentObject) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 55 | { |
| 56 | struct ndn_NameComponent nameComponents[100]; |
| 57 | struct ndn_ContentObject contentObjectStruct; |
| 58 | ndn_ContentObject_init |
| 59 | (&contentObjectStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0])); |
| 60 | contentObject.get(contentObjectStruct); |
| 61 | |
| 62 | BinaryXMLEncoder encoder; |
Jeff Thompson | 0adcbea | 2013-07-15 16:29:52 -0700 | [diff] [blame] | 63 | ndn_encodeBinaryXMLContentObject(&contentObjectStruct, &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 | |
| 68 | void BinaryXMLWireFormat::decodeContentObject(ContentObject &contentObject, const unsigned char *input, unsigned int inputLength) |
| 69 | { |
| 70 | struct ndn_NameComponent nameComponents[100]; |
| 71 | struct ndn_ContentObject contentObjectStruct; |
| 72 | ndn_ContentObject_init |
| 73 | (&contentObjectStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0])); |
| 74 | |
Jeff Thompson | 430a77a | 2013-07-15 17:17:56 -0700 | [diff] [blame] | 75 | BinaryXMLDecoder decoder(input, inputLength); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 76 | ndn_Error error; |
| 77 | if (error = ndn_decodeBinaryXMLContentObject(&contentObjectStruct, &decoder)) |
| 78 | throw std::runtime_error(ndn_getErrorString(error)); |
| 79 | |
| 80 | contentObject.set(contentObjectStruct); |
| 81 | } |
| 82 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 83 | } |