blob: 8cb564308e45e4356ec5e2e2338058b365b398e3 [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 Thompson7329a132013-08-16 15:57:37 -070064 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070065 struct ndn_Data dataStruct;
66 ndn_Data_init
Jeff Thompson7329a132013-08-16 15:57:37 -070067 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
68 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson56ec9e22013-08-02 11:34:07 -070069 data.get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070070
Jeff Thompsonf0fea002013-07-30 17:22:42 -070071 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070072 ndn_Error error;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070073 if ((error = ndn_encodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &encoder)))
Jeff Thompsonaa020712013-08-08 21:20:06 -070074 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070075
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070076 return encoder.getOutput();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070077}
78
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070079void BinaryXmlWireFormat::decodeData
80 (Data &data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070081{
82 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -070083 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070084 struct ndn_Data dataStruct;
85 ndn_Data_init
Jeff Thompson7329a132013-08-16 15:57:37 -070086 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
87 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070088
Jeff Thompsonf0fea002013-07-30 17:22:42 -070089 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070090 ndn_Error error;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070091 if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &decoder)))
Jeff Thompson5cae5e52013-07-10 19:41:20 -070092 throw std::runtime_error(ndn_getErrorString(error));
93
Jeff Thompson56ec9e22013-08-02 11:34:07 -070094 data.set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070095}
96
Jeff Thompson4c89ad62013-06-28 12:50:13 -070097}