blob: 0c1f4d28f3a726dc5687847a58409de99a27fed1 [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 Thompson7c30eda2013-07-03 18:37:07 -07007#include "../c/encoding/BinaryXMLInterest.h"
Jeff Thompson5cae5e52013-07-10 19:41:20 -07008#include "../c/encoding/BinaryXMLContentObject.h"
Jeff Thompson7c30eda2013-07-03 18:37:07 -07009#include "../Interest.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070010#include "../ContentObject.hpp"
Jeff Thompson1f3f5172013-07-01 19:02:36 -070011#include "BinaryXMLEncoder.hpp"
Jeff Thompson430a77a2013-07-15 17:17:56 -070012#include "BinaryXMLDecoder.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070013#include "BinaryXMLWireFormat.hpp"
Jeff Thompson4c89ad62013-06-28 12:50:13 -070014
Jeff Thompson1f3f5172013-07-01 19:02:36 -070015using namespace std;
16
Jeff Thompson4c89ad62013-06-28 12:50:13 -070017namespace ndn {
18
19BinaryXMLWireFormat BinaryXMLWireFormat::instance_;
20
Jeff Thompsond345a5b2013-07-08 16:18:23 -070021void BinaryXMLWireFormat::encodeInterest(const Interest &interest, vector<unsigned char> &output)
Jeff Thompson214c7be2013-07-08 15:23:00 -070022{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070023 struct ndn_NameComponent nameComponents[100];
24 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson214c7be2013-07-08 15:23:00 -070025 struct ndn_Interest interestStruct;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070026 ndn_Interest_init
27 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
28 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson214c7be2013-07-08 15:23:00 -070029 interest.get(interestStruct);
30
31 BinaryXMLEncoder encoder;
Jeff Thompson0adcbea2013-07-15 16:29:52 -070032 ndn_encodeBinaryXMLInterest(&interestStruct, &encoder);
Jeff Thompson214c7be2013-07-08 15:23:00 -070033
34 encoder.appendTo(output);
35}
36
Jeff Thompson7c30eda2013-07-03 18:37:07 -070037void BinaryXMLWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength)
38{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070039 struct ndn_NameComponent nameComponents[100];
40 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -070041 struct ndn_Interest interestStruct;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070042 ndn_Interest_init
43 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
44 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070045
Jeff Thompson430a77a2013-07-15 17:17:56 -070046 BinaryXMLDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070047 ndn_Error error;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070048 if (error = ndn_decodeBinaryXMLInterest(&interestStruct, &decoder))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070049 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070050
51 interest.set(interestStruct);
52}
53
Jeff Thompson5cae5e52013-07-10 19:41:20 -070054void BinaryXMLWireFormat::encodeContentObject(const ContentObject &contentObject, vector<unsigned char> &output)
55{
56 struct ndn_NameComponent nameComponents[100];
57 struct ndn_ContentObject contentObjectStruct;
58 ndn_ContentObject_init
59 (&contentObjectStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
60 contentObject.get(contentObjectStruct);
61
62 BinaryXMLEncoder encoder;
Jeff Thompson0adcbea2013-07-15 16:29:52 -070063 ndn_encodeBinaryXMLContentObject(&contentObjectStruct, &encoder);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070064
65 encoder.appendTo(output);
66}
67
68void BinaryXMLWireFormat::decodeContentObject(ContentObject &contentObject, const unsigned char *input, unsigned int inputLength)
69{
70 struct ndn_NameComponent nameComponents[100];
71 struct ndn_ContentObject contentObjectStruct;
72 ndn_ContentObject_init
73 (&contentObjectStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
74
Jeff Thompson430a77a2013-07-15 17:17:56 -070075 BinaryXMLDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070076 ndn_Error error;
77 if (error = ndn_decodeBinaryXMLContentObject(&contentObjectStruct, &decoder))
78 throw std::runtime_error(ndn_getErrorString(error));
79
80 contentObject.set(contentObjectStruct);
81}
82
Jeff Thompson4c89ad62013-06-28 12:50:13 -070083}