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 | 6cb56f9 | 2013-07-01 15:38:09 -0700 | [diff] [blame] | 7 | #include "../c/encoding/BinaryXMLName.h" |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 8 | #include "../c/encoding/BinaryXMLInterest.h" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame^] | 9 | #include "../c/encoding/BinaryXMLContentObject.h" |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 10 | #include "../Interest.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame^] | 11 | #include "../ContentObject.hpp" |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 12 | #include "BinaryXMLEncoder.hpp" |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 13 | #include "../c/encoding/BinaryXMLDecoder.h" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 14 | #include "BinaryXMLWireFormat.hpp" |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 15 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 16 | using namespace std; |
| 17 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 18 | namespace ndn { |
| 19 | |
| 20 | BinaryXMLWireFormat BinaryXMLWireFormat::instance_; |
| 21 | |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 22 | void BinaryXMLWireFormat::encodeName(const Name &name, vector<unsigned char> &output) |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 23 | { |
| 24 | struct ndn_Name nameStruct; |
| 25 | struct ndn_NameComponent components[100]; |
| 26 | ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0])); |
| 27 | name.get(nameStruct); |
| 28 | |
| 29 | BinaryXMLEncoder encoder; |
| 30 | ndn_encodeBinaryXMLName(&nameStruct, encoder.getEncoder()); |
Jeff Thompson | 58d798f | 2013-07-02 14:16:25 -0700 | [diff] [blame] | 31 | |
| 32 | encoder.appendTo(output); |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 35 | void BinaryXMLWireFormat::decodeName(Name &name, const unsigned char *input, unsigned int inputLength) |
| 36 | { |
| 37 | struct ndn_NameComponent components[100]; |
| 38 | struct ndn_Name nameStruct; |
| 39 | ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0])); |
| 40 | |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 41 | struct ndn_BinaryXMLDecoder decoder; |
| 42 | ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength); |
| 43 | |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 44 | ndn_Error error; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 45 | if (error = ndn_decodeBinaryXMLName(&nameStruct, &decoder)) |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 46 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 47 | |
| 48 | name.set(nameStruct); |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 51 | void BinaryXMLWireFormat::encodeInterest(const Interest &interest, vector<unsigned char> &output) |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 52 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 53 | struct ndn_NameComponent nameComponents[100]; |
| 54 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 55 | struct ndn_Interest interestStruct; |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 56 | ndn_Interest_init |
| 57 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 58 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 59 | interest.get(interestStruct); |
| 60 | |
| 61 | BinaryXMLEncoder encoder; |
| 62 | ndn_encodeBinaryXMLInterest(&interestStruct, encoder.getEncoder()); |
| 63 | |
| 64 | encoder.appendTo(output); |
| 65 | } |
| 66 | |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 67 | void BinaryXMLWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength) |
| 68 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 69 | struct ndn_NameComponent nameComponents[100]; |
| 70 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 71 | struct ndn_Interest interestStruct; |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 72 | ndn_Interest_init |
| 73 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 74 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 75 | |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 76 | struct ndn_BinaryXMLDecoder decoder; |
| 77 | ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength); |
| 78 | |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 79 | ndn_Error error; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 80 | if (error = ndn_decodeBinaryXMLInterest(&interestStruct, &decoder)) |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 81 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 82 | |
| 83 | interest.set(interestStruct); |
| 84 | } |
| 85 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame^] | 86 | void BinaryXMLWireFormat::encodeContentObject(const ContentObject &contentObject, vector<unsigned char> &output) |
| 87 | { |
| 88 | struct ndn_NameComponent nameComponents[100]; |
| 89 | struct ndn_ContentObject contentObjectStruct; |
| 90 | ndn_ContentObject_init |
| 91 | (&contentObjectStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0])); |
| 92 | contentObject.get(contentObjectStruct); |
| 93 | |
| 94 | BinaryXMLEncoder encoder; |
| 95 | ndn_encodeBinaryXMLContentObject(&contentObjectStruct, encoder.getEncoder()); |
| 96 | |
| 97 | encoder.appendTo(output); |
| 98 | } |
| 99 | |
| 100 | void BinaryXMLWireFormat::decodeContentObject(ContentObject &contentObject, const unsigned char *input, unsigned int inputLength) |
| 101 | { |
| 102 | struct ndn_NameComponent nameComponents[100]; |
| 103 | struct ndn_ContentObject contentObjectStruct; |
| 104 | ndn_ContentObject_init |
| 105 | (&contentObjectStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0])); |
| 106 | |
| 107 | struct ndn_BinaryXMLDecoder decoder; |
| 108 | ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength); |
| 109 | |
| 110 | ndn_Error error; |
| 111 | if (error = ndn_decodeBinaryXMLContentObject(&contentObjectStruct, &decoder)) |
| 112 | throw std::runtime_error(ndn_getErrorString(error)); |
| 113 | |
| 114 | contentObject.set(contentObjectStruct); |
| 115 | } |
| 116 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 117 | } |