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" |
| 9 | #include "../Interest.hpp" |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 10 | #include "BinaryXMLEncoder.hpp" |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 11 | #include "../c/encoding/BinaryXMLDecoder.h" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 12 | #include "BinaryXMLWireFormat.hpp" |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 13 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 14 | using namespace std; |
| 15 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 16 | namespace ndn { |
| 17 | |
| 18 | BinaryXMLWireFormat BinaryXMLWireFormat::instance_; |
| 19 | |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 20 | void BinaryXMLWireFormat::encodeName(const Name &name, vector<unsigned char> &output) |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 21 | { |
| 22 | struct ndn_Name nameStruct; |
| 23 | struct ndn_NameComponent components[100]; |
| 24 | ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0])); |
| 25 | name.get(nameStruct); |
| 26 | |
| 27 | BinaryXMLEncoder encoder; |
| 28 | ndn_encodeBinaryXMLName(&nameStruct, encoder.getEncoder()); |
Jeff Thompson | 58d798f | 2013-07-02 14:16:25 -0700 | [diff] [blame] | 29 | |
| 30 | encoder.appendTo(output); |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 33 | void BinaryXMLWireFormat::decodeName(Name &name, const unsigned char *input, unsigned int inputLength) |
| 34 | { |
| 35 | struct ndn_NameComponent components[100]; |
| 36 | struct ndn_Name nameStruct; |
| 37 | ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0])); |
| 38 | |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 39 | struct ndn_BinaryXMLDecoder decoder; |
| 40 | ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength); |
| 41 | |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 42 | ndn_Error error; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 43 | if (error = ndn_decodeBinaryXMLName(&nameStruct, &decoder)) |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 44 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 45 | |
| 46 | name.set(nameStruct); |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 49 | void BinaryXMLWireFormat::encodeInterest(const Interest &interest, vector<unsigned char> &output) |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 50 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 51 | struct ndn_NameComponent nameComponents[100]; |
| 52 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 53 | struct ndn_Interest interestStruct; |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 54 | ndn_Interest_init |
| 55 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 56 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 57 | interest.get(interestStruct); |
| 58 | |
| 59 | BinaryXMLEncoder encoder; |
| 60 | ndn_encodeBinaryXMLInterest(&interestStruct, encoder.getEncoder()); |
| 61 | |
| 62 | encoder.appendTo(output); |
| 63 | } |
| 64 | |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 65 | void BinaryXMLWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength) |
| 66 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 67 | struct ndn_NameComponent nameComponents[100]; |
| 68 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 69 | struct ndn_Interest interestStruct; |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 70 | ndn_Interest_init |
| 71 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 72 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 73 | |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 74 | struct ndn_BinaryXMLDecoder decoder; |
| 75 | ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength); |
| 76 | |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 77 | ndn_Error error; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 78 | if (error = ndn_decodeBinaryXMLInterest(&interestStruct, &decoder)) |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 79 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 80 | |
| 81 | interest.set(interestStruct); |
| 82 | } |
| 83 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 84 | } |