blob: f3af9c81e9b52fb19ef088bb2959b96c6d376f53 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07004 */
5
Jeff Thompson4c89ad62013-06-28 12:50:13 -07006#include <stdexcept>
Jeff Thompson6cb56f92013-07-01 15:38:09 -07007#include "../c/encoding/BinaryXMLName.h"
Jeff Thompson7c30eda2013-07-03 18:37:07 -07008#include "../c/encoding/BinaryXMLInterest.h"
9#include "../Interest.hpp"
Jeff Thompson1f3f5172013-07-01 19:02:36 -070010#include "BinaryXMLEncoder.hpp"
Jeff Thompsone2c232d2013-07-03 18:47:29 -070011#include "../c/encoding/BinaryXMLDecoder.h"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070012#include "BinaryXMLWireFormat.hpp"
Jeff Thompson4c89ad62013-06-28 12:50:13 -070013
Jeff Thompson1f3f5172013-07-01 19:02:36 -070014using namespace std;
15
Jeff Thompson4c89ad62013-06-28 12:50:13 -070016namespace ndn {
17
18BinaryXMLWireFormat BinaryXMLWireFormat::instance_;
19
Jeff Thompsond345a5b2013-07-08 16:18:23 -070020void BinaryXMLWireFormat::encodeName(const Name &name, vector<unsigned char> &output)
Jeff Thompson1f3f5172013-07-01 19:02:36 -070021{
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 Thompson58d798f2013-07-02 14:16:25 -070029
30 encoder.appendTo(output);
Jeff Thompson1f3f5172013-07-01 19:02:36 -070031}
32
Jeff Thompson4c89ad62013-06-28 12:50:13 -070033void 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 Thompsone2c232d2013-07-03 18:47:29 -070039 struct ndn_BinaryXMLDecoder decoder;
40 ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength);
41
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070042 ndn_Error error;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070043 if (error = ndn_decodeBinaryXMLName(&nameStruct, &decoder))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070044 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson1f3f5172013-07-01 19:02:36 -070045
46 name.set(nameStruct);
Jeff Thompson4c89ad62013-06-28 12:50:13 -070047}
48
Jeff Thompsond345a5b2013-07-08 16:18:23 -070049void BinaryXMLWireFormat::encodeInterest(const Interest &interest, vector<unsigned char> &output)
Jeff Thompson214c7be2013-07-08 15:23:00 -070050{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070051 struct ndn_NameComponent nameComponents[100];
52 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson214c7be2013-07-08 15:23:00 -070053 struct ndn_Interest interestStruct;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070054 ndn_Interest_init
55 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
56 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson214c7be2013-07-08 15:23:00 -070057 interest.get(interestStruct);
58
59 BinaryXMLEncoder encoder;
60 ndn_encodeBinaryXMLInterest(&interestStruct, encoder.getEncoder());
61
62 encoder.appendTo(output);
63}
64
Jeff Thompson7c30eda2013-07-03 18:37:07 -070065void BinaryXMLWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength)
66{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070067 struct ndn_NameComponent nameComponents[100];
68 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -070069 struct ndn_Interest interestStruct;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070070 ndn_Interest_init
71 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
72 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070073
Jeff Thompsone2c232d2013-07-03 18:47:29 -070074 struct ndn_BinaryXMLDecoder decoder;
75 ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength);
76
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070077 ndn_Error error;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070078 if (error = ndn_decodeBinaryXMLInterest(&interestStruct, &decoder))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070079 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070080
81 interest.set(interestStruct);
82}
83
Jeff Thompson4c89ad62013-06-28 12:50:13 -070084}