blob: 3ef731cbb223f84b5a658042a1d1348a37fb4047 [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 Thompson53412192013-08-06 13:35:50 -07007#include "../c/encoding/binary-xml-interest.h"
Jeff Thompson56ec9e22013-08-02 11:34:07 -07008#include "../c/encoding/binary-xml-data.h"
Jeff Thompson53412192013-08-06 13:35:50 -07009#include "../interest.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070010#include "../data.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "binary-xml-encoder.hpp"
12#include "binary-xml-decoder.hpp"
13#include "binary-xml-wire-format.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;
Jeff Thompsonaa020712013-08-08 21:20:06 -070036 ndn_Error error;
37 if ((error = ndn_encodeBinaryXmlInterest(&interestStruct, &encoder)))
38 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson214c7be2013-07-08 15:23:00 -070039
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070040 return encoder.getOutput();
Jeff Thompson214c7be2013-07-08 15:23:00 -070041}
42
Jeff Thompsonf0fea002013-07-30 17:22:42 -070043void BinaryXmlWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength)
Jeff Thompson7c30eda2013-07-03 18:37:07 -070044{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070045 struct ndn_NameComponent nameComponents[100];
46 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -070047 struct ndn_Interest interestStruct;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070048 ndn_Interest_init
49 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
50 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070051
Jeff Thompsonf0fea002013-07-30 17:22:42 -070052 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070053 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070054 if ((error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder)))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070055 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070056
57 interest.set(interestStruct);
58}
59
Jeff Thompson56ec9e22013-08-02 11:34:07 -070060ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeData(const Data &data)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070061{
62 struct ndn_NameComponent nameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070063 struct ndn_Data dataStruct;
64 ndn_Data_init
65 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
66 data.get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070067
Jeff Thompsonf0fea002013-07-30 17:22:42 -070068 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070069 unsigned int dummyBeginOffset, dummyEndOffset;
70 ndn_Error error;
71 if ((error = ndn_encodeBinaryXmlData(&dataStruct, &dummyBeginOffset, &dummyEndOffset, &encoder)))
72 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070073
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070074 return encoder.getOutput();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070075}
76
Jeff Thompson56ec9e22013-08-02 11:34:07 -070077void BinaryXmlWireFormat::decodeData(Data &data, const unsigned char *input, unsigned int inputLength)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078{
79 struct ndn_NameComponent nameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070080 struct ndn_Data dataStruct;
81 ndn_Data_init
82 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070083
Jeff Thompsonf0fea002013-07-30 17:22:42 -070084 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070085 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070086 if ((error = ndn_decodeBinaryXmlData(&dataStruct, &decoder)))
Jeff Thompson5cae5e52013-07-10 19:41:20 -070087 throw std::runtime_error(ndn_getErrorString(error));
88
Jeff Thompson56ec9e22013-08-02 11:34:07 -070089 data.set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070090}
91
Jeff Thompson4c89ad62013-06-28 12:50:13 -070092}