blob: e0b2b1c0ee90be87dca119457e22a3cd6ac0390b [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
Jeff Thompsonf0fea002013-07-30 17:22:42 -070019BinaryXmlWireFormat BinaryXmlWireFormat::instance_;
Jeff Thompson4c89ad62013-06-28 12:50:13 -070020
Jeff Thompsonf0fea002013-07-30 17:22:42 -070021ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeInterest(const Interest &interest)
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
Jeff Thompsonf0fea002013-07-30 17:22:42 -070031 BinaryXmlEncoder encoder;
32 ndn_encodeBinaryXmlInterest(&interestStruct, &encoder);
Jeff Thompson214c7be2013-07-08 15:23:00 -070033
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070034 return encoder.getOutput();
Jeff Thompson214c7be2013-07-08 15:23:00 -070035}
36
Jeff Thompsonf0fea002013-07-30 17:22:42 -070037void BinaryXmlWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength)
Jeff Thompson7c30eda2013-07-03 18:37:07 -070038{
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 Thompsonf0fea002013-07-30 17:22:42 -070046 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070047 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -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 Thompsonf0fea002013-07-30 17:22:42 -070054ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeContentObject(const ContentObject &contentObject)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070055{
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
Jeff Thompsonf0fea002013-07-30 17:22:42 -070062 BinaryXmlEncoder encoder;
63 ndn_encodeBinaryXmlContentObject(&contentObjectStruct, &encoder);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070064
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070065 return encoder.getOutput();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070066}
67
Jeff Thompsonf0fea002013-07-30 17:22:42 -070068void BinaryXmlWireFormat::decodeContentObject(ContentObject &contentObject, const unsigned char *input, unsigned int inputLength)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070069{
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 Thompsonf0fea002013-07-30 17:22:42 -070075 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070076 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070077 if (error = ndn_decodeBinaryXmlContentObject(&contentObjectStruct, &decoder))
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078 throw std::runtime_error(ndn_getErrorString(error));
79
80 contentObject.set(contentObjectStruct);
81}
82
Jeff Thompson4c89ad62013-06-28 12:50:13 -070083}