blob: 1c6b7b19a5208d635a500f1f1f0e9041e07ac32a [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 Thompson56ec9e22013-08-02 11:34:07 -07008#include "../c/encoding/binary-xml-data.h"
Jeff Thompson7c30eda2013-07-03 18:37:07 -07009#include "../Interest.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070010#include "../data.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 Thompson56ec9e22013-08-02 11:34:07 -070054ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeData(const Data &data)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070055{
56 struct ndn_NameComponent nameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070057 struct ndn_Data dataStruct;
58 ndn_Data_init
59 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
60 data.get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070061
Jeff Thompsonf0fea002013-07-30 17:22:42 -070062 BinaryXmlEncoder encoder;
Jeff Thompson56ec9e22013-08-02 11:34:07 -070063 ndn_encodeBinaryXmlData(&dataStruct, &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 Thompson56ec9e22013-08-02 11:34:07 -070068void BinaryXmlWireFormat::decodeData(Data &data, const unsigned char *input, unsigned int inputLength)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070069{
70 struct ndn_NameComponent nameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070071 struct ndn_Data dataStruct;
72 ndn_Data_init
73 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070074
Jeff Thompsonf0fea002013-07-30 17:22:42 -070075 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070076 ndn_Error error;
Jeff Thompson56ec9e22013-08-02 11:34:07 -070077 if (error = ndn_decodeBinaryXmlData(&dataStruct, &decoder))
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078 throw std::runtime_error(ndn_getErrorString(error));
79
Jeff Thompson56ec9e22013-08-02 11:34:07 -070080 data.set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070081}
82
Jeff Thompson4c89ad62013-06-28 12:50:13 -070083}