blob: 6899924a4c585ed47f5197027af10563abe50b7b [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"
Jeff Thompson5cae5e52013-07-10 19:41:20 -07009#include "../c/encoding/BinaryXMLContentObject.h"
Jeff Thompson7c30eda2013-07-03 18:37:07 -070010#include "../Interest.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070011#include "../ContentObject.hpp"
Jeff Thompson1f3f5172013-07-01 19:02:36 -070012#include "BinaryXMLEncoder.hpp"
Jeff Thompson430a77a2013-07-15 17:17:56 -070013#include "BinaryXMLDecoder.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070014#include "BinaryXMLWireFormat.hpp"
Jeff Thompson4c89ad62013-06-28 12:50:13 -070015
Jeff Thompson1f3f5172013-07-01 19:02:36 -070016using namespace std;
17
Jeff Thompson4c89ad62013-06-28 12:50:13 -070018namespace ndn {
19
20BinaryXMLWireFormat BinaryXMLWireFormat::instance_;
21
Jeff Thompsond345a5b2013-07-08 16:18:23 -070022void BinaryXMLWireFormat::encodeName(const Name &name, vector<unsigned char> &output)
Jeff Thompson1f3f5172013-07-01 19:02:36 -070023{
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;
Jeff Thompson0adcbea2013-07-15 16:29:52 -070030 ndn_encodeBinaryXMLName(&nameStruct, &encoder);
Jeff Thompson58d798f2013-07-02 14:16:25 -070031
32 encoder.appendTo(output);
Jeff Thompson1f3f5172013-07-01 19:02:36 -070033}
34
Jeff Thompson4c89ad62013-06-28 12:50:13 -070035void 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 Thompson430a77a2013-07-15 17:17:56 -070041 BinaryXMLDecoder decoder(input, inputLength);
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;
Jeff Thompson0adcbea2013-07-15 16:29:52 -070060 ndn_encodeBinaryXMLInterest(&interestStruct, &encoder);
Jeff Thompson214c7be2013-07-08 15:23:00 -070061
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 Thompson430a77a2013-07-15 17:17:56 -070074 BinaryXMLDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070075 ndn_Error error;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070076 if (error = ndn_decodeBinaryXMLInterest(&interestStruct, &decoder))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070077 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070078
79 interest.set(interestStruct);
80}
81
Jeff Thompson5cae5e52013-07-10 19:41:20 -070082void BinaryXMLWireFormat::encodeContentObject(const ContentObject &contentObject, vector<unsigned char> &output)
83{
84 struct ndn_NameComponent nameComponents[100];
85 struct ndn_ContentObject contentObjectStruct;
86 ndn_ContentObject_init
87 (&contentObjectStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
88 contentObject.get(contentObjectStruct);
89
90 BinaryXMLEncoder encoder;
Jeff Thompson0adcbea2013-07-15 16:29:52 -070091 ndn_encodeBinaryXMLContentObject(&contentObjectStruct, &encoder);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070092
93 encoder.appendTo(output);
94}
95
96void BinaryXMLWireFormat::decodeContentObject(ContentObject &contentObject, const unsigned char *input, unsigned int inputLength)
97{
98 struct ndn_NameComponent nameComponents[100];
99 struct ndn_ContentObject contentObjectStruct;
100 ndn_ContentObject_init
101 (&contentObjectStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
102
Jeff Thompson430a77a2013-07-15 17:17:56 -0700103 BinaryXMLDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700104 ndn_Error error;
105 if (error = ndn_decodeBinaryXMLContentObject(&contentObjectStruct, &decoder))
106 throw std::runtime_error(ndn_getErrorString(error));
107
108 contentObject.set(contentObjectStruct);
109}
110
Jeff Thompson4c89ad62013-06-28 12:50:13 -0700111}