blob: 83261de06598e8dcdd58efc87794bcc5e581b2bd [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 Thompsone2c232d2013-07-03 18:47:29 -070013#include "../c/encoding/BinaryXMLDecoder.h"
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;
30 ndn_encodeBinaryXMLName(&nameStruct, encoder.getEncoder());
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 Thompsone2c232d2013-07-03 18:47:29 -070041 struct ndn_BinaryXMLDecoder decoder;
42 ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength);
43
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070044 ndn_Error error;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070045 if (error = ndn_decodeBinaryXMLName(&nameStruct, &decoder))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070046 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson1f3f5172013-07-01 19:02:36 -070047
48 name.set(nameStruct);
Jeff Thompson4c89ad62013-06-28 12:50:13 -070049}
50
Jeff Thompsond345a5b2013-07-08 16:18:23 -070051void BinaryXMLWireFormat::encodeInterest(const Interest &interest, vector<unsigned char> &output)
Jeff Thompson214c7be2013-07-08 15:23:00 -070052{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070053 struct ndn_NameComponent nameComponents[100];
54 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson214c7be2013-07-08 15:23:00 -070055 struct ndn_Interest interestStruct;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070056 ndn_Interest_init
57 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
58 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson214c7be2013-07-08 15:23:00 -070059 interest.get(interestStruct);
60
61 BinaryXMLEncoder encoder;
62 ndn_encodeBinaryXMLInterest(&interestStruct, encoder.getEncoder());
63
64 encoder.appendTo(output);
65}
66
Jeff Thompson7c30eda2013-07-03 18:37:07 -070067void BinaryXMLWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength)
68{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070069 struct ndn_NameComponent nameComponents[100];
70 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -070071 struct ndn_Interest interestStruct;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070072 ndn_Interest_init
73 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
74 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070075
Jeff Thompsone2c232d2013-07-03 18:47:29 -070076 struct ndn_BinaryXMLDecoder decoder;
77 ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength);
78
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070079 ndn_Error error;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070080 if (error = ndn_decodeBinaryXMLInterest(&interestStruct, &decoder))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070081 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070082
83 interest.set(interestStruct);
84}
85
Jeff Thompson5cae5e52013-07-10 19:41:20 -070086void 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
100void 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 Thompson4c89ad62013-06-28 12:50:13 -0700117}