blob: 574a374757aca3cf96ff1e8402ab595d3ec38493 [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 Thompsonc87f39a2013-08-12 11:55:11 -070060ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeData
61 (const Data &data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070062{
63 struct ndn_NameComponent nameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070064 struct ndn_Data dataStruct;
65 ndn_Data_init
66 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
67 data.get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070068
Jeff Thompsonf0fea002013-07-30 17:22:42 -070069 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070070 ndn_Error error;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070071 if ((error = ndn_encodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &encoder)))
Jeff Thompsonaa020712013-08-08 21:20:06 -070072 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 Thompsonc87f39a2013-08-12 11:55:11 -070077void BinaryXmlWireFormat::decodeData
78 (Data &data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070079{
80 struct ndn_NameComponent nameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070081 struct ndn_Data dataStruct;
82 ndn_Data_init
83 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070084
Jeff Thompsonf0fea002013-07-30 17:22:42 -070085 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070086 ndn_Error error;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070087 if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &decoder)))
Jeff Thompson5cae5e52013-07-10 19:41:20 -070088 throw std::runtime_error(ndn_getErrorString(error));
89
Jeff Thompson56ec9e22013-08-02 11:34:07 -070090 data.set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070091}
92
Jeff Thompson4c89ad62013-06-28 12:50:13 -070093}