blob: ffda086b77771cc2df0d3ea4225fe3f671c4723c [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 Thompsonfa181ac2013-08-02 19:00:51 -070019// This is declared in the WireFormat class.
20WireFormat *WireFormat::newInitialDefaultWireFormat()
21{
22 return new BinaryXmlWireFormat();
23}
24
Jeff Thompsonf0fea002013-07-30 17:22:42 -070025ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeInterest(const Interest &interest)
Jeff Thompson214c7be2013-07-08 15:23:00 -070026{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070027 struct ndn_NameComponent nameComponents[100];
28 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson214c7be2013-07-08 15:23:00 -070029 struct ndn_Interest interestStruct;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070030 ndn_Interest_init
31 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
32 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson214c7be2013-07-08 15:23:00 -070033 interest.get(interestStruct);
34
Jeff Thompsonf0fea002013-07-30 17:22:42 -070035 BinaryXmlEncoder encoder;
36 ndn_encodeBinaryXmlInterest(&interestStruct, &encoder);
Jeff Thompson214c7be2013-07-08 15:23:00 -070037
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070038 return encoder.getOutput();
Jeff Thompson214c7be2013-07-08 15:23:00 -070039}
40
Jeff Thompsonf0fea002013-07-30 17:22:42 -070041void BinaryXmlWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength)
Jeff Thompson7c30eda2013-07-03 18:37:07 -070042{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070043 struct ndn_NameComponent nameComponents[100];
44 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -070045 struct ndn_Interest interestStruct;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070046 ndn_Interest_init
47 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
48 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070049
Jeff Thompsonf0fea002013-07-30 17:22:42 -070050 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070051 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070052 if (error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070053 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070054
55 interest.set(interestStruct);
56}
57
Jeff Thompson56ec9e22013-08-02 11:34:07 -070058ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeData(const Data &data)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070059{
60 struct ndn_NameComponent nameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070061 struct ndn_Data dataStruct;
62 ndn_Data_init
63 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
64 data.get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070065
Jeff Thompsonf0fea002013-07-30 17:22:42 -070066 BinaryXmlEncoder encoder;
Jeff Thompson56ec9e22013-08-02 11:34:07 -070067 ndn_encodeBinaryXmlData(&dataStruct, &encoder);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070068
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070069 return encoder.getOutput();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070070}
71
Jeff Thompson56ec9e22013-08-02 11:34:07 -070072void BinaryXmlWireFormat::decodeData(Data &data, const unsigned char *input, unsigned int inputLength)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070073{
74 struct ndn_NameComponent nameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070075 struct ndn_Data dataStruct;
76 ndn_Data_init
77 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078
Jeff Thompsonf0fea002013-07-30 17:22:42 -070079 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070080 ndn_Error error;
Jeff Thompson56ec9e22013-08-02 11:34:07 -070081 if (error = ndn_decodeBinaryXmlData(&dataStruct, &decoder))
Jeff Thompson5cae5e52013-07-10 19:41:20 -070082 throw std::runtime_error(ndn_getErrorString(error));
83
Jeff Thompson56ec9e22013-08-02 11:34:07 -070084 data.set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070085}
86
Jeff Thompson4c89ad62013-06-28 12:50:13 -070087}