blob: 39f7b4af4f2f71f3d582d23a6b886bc07db8ec85 [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 Thompson990599b2013-08-27 15:14:25 -07009#include "../c/encoding/binary-xml-forwarding-entry.h"
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "../interest.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070011#include "../data.hpp"
Jeff Thompson990599b2013-08-27 15:14:25 -070012#include "../forwarding-entry.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070013#include "binary-xml-encoder.hpp"
14#include "binary-xml-decoder.hpp"
15#include "binary-xml-wire-format.hpp"
Jeff Thompson4c89ad62013-06-28 12:50:13 -070016
Jeff Thompson1f3f5172013-07-01 19:02:36 -070017using namespace std;
18
Jeff Thompson4c89ad62013-06-28 12:50:13 -070019namespace ndn {
20
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070021// This is declared in the WireFormat class.
22WireFormat *WireFormat::newInitialDefaultWireFormat()
23{
24 return new BinaryXmlWireFormat();
25}
26
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070027Blob BinaryXmlWireFormat::encodeInterest(const Interest& interest)
Jeff Thompson214c7be2013-07-08 15:23:00 -070028{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070029 struct ndn_NameComponent nameComponents[100];
30 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson214c7be2013-07-08 15:23:00 -070031 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070032 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -070033 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
34 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson214c7be2013-07-08 15:23:00 -070035 interest.get(interestStruct);
36
Jeff Thompsonf0fea002013-07-30 17:22:42 -070037 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070038 ndn_Error error;
39 if ((error = ndn_encodeBinaryXmlInterest(&interestStruct, &encoder)))
40 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson214c7be2013-07-08 15:23:00 -070041
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070042 return encoder.getOutput();
Jeff Thompson214c7be2013-07-08 15:23:00 -070043}
44
Jeff Thompson1656e6a2013-08-29 18:01:48 -070045void BinaryXmlWireFormat::decodeInterest(Interest& interest, const unsigned char *input, unsigned int inputLength)
Jeff Thompson7c30eda2013-07-03 18:37:07 -070046{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070047 struct ndn_NameComponent nameComponents[100];
48 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -070049 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070050 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -070051 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
52 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070053
Jeff Thompsonf0fea002013-07-30 17:22:42 -070054 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070055 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070056 if ((error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder)))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070057 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070058
59 interest.set(interestStruct);
60}
61
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070062Blob BinaryXmlWireFormat::encodeData(const Data& data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070063{
64 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -070065 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070066 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070067 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -070068 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
69 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson56ec9e22013-08-02 11:34:07 -070070 data.get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070071
Jeff Thompsonf0fea002013-07-30 17:22:42 -070072 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070073 ndn_Error error;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070074 if ((error = ndn_encodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &encoder)))
Jeff Thompsonaa020712013-08-08 21:20:06 -070075 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070076
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070077 return encoder.getOutput();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078}
79
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070080void BinaryXmlWireFormat::decodeData
Jeff Thompson1656e6a2013-08-29 18:01:48 -070081 (Data& data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070082{
83 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -070084 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070085 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070086 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -070087 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
88 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070089
Jeff Thompsonf0fea002013-07-30 17:22:42 -070090 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070091 ndn_Error error;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070092 if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &decoder)))
Jeff Thompson5cae5e52013-07-10 19:41:20 -070093 throw std::runtime_error(ndn_getErrorString(error));
94
Jeff Thompson56ec9e22013-08-02 11:34:07 -070095 data.set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070096}
97
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070098Blob BinaryXmlWireFormat::encodeForwardingEntry(const ForwardingEntry& forwardingEntry)
Jeff Thompson990599b2013-08-27 15:14:25 -070099{
100 struct ndn_NameComponent prefixNameComponents[100];
101 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700102 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700103 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
104 forwardingEntry.get(forwardingEntryStruct);
105
106 BinaryXmlEncoder encoder;
107 ndn_Error error;
108 if ((error = ndn_encodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &encoder)))
109 throw std::runtime_error(ndn_getErrorString(error));
110
111 return encoder.getOutput();
112}
113
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700114void BinaryXmlWireFormat::decodeForwardingEntry(ForwardingEntry& forwardingEntry, const unsigned char *input, unsigned int inputLength)
Jeff Thompson990599b2013-08-27 15:14:25 -0700115{
116 struct ndn_NameComponent prefixNameComponents[100];
117 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700118 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700119 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
120
121 BinaryXmlDecoder decoder(input, inputLength);
122 ndn_Error error;
123 if ((error = ndn_decodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &decoder)))
124 throw std::runtime_error(ndn_getErrorString(error));
125
126 forwardingEntry.set(forwardingEntryStruct);
127}
128
Jeff Thompson4c89ad62013-06-28 12:50:13 -0700129}