blob: 2bb2093ab03558b2f381093919dfc1a283e0b8d5 [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 Thompson1656e6a2013-08-29 18:01:48 -070027ptr_lib::shared_ptr<vector<unsigned char> > 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 Thompsonc87f39a2013-08-12 11:55:11 -070062ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeData
Jeff Thompson1656e6a2013-08-29 18:01:48 -070063 (const Data& data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070064{
65 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -070066 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070067 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070068 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -070069 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
70 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson56ec9e22013-08-02 11:34:07 -070071 data.get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070072
Jeff Thompsonf0fea002013-07-30 17:22:42 -070073 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070074 ndn_Error error;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070075 if ((error = ndn_encodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &encoder)))
Jeff Thompsonaa020712013-08-08 21:20:06 -070076 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070077
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070078 return encoder.getOutput();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070079}
80
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070081void BinaryXmlWireFormat::decodeData
Jeff Thompson1656e6a2013-08-29 18:01:48 -070082 (Data& data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070083{
84 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -070085 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070086 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070087 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -070088 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
89 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070090
Jeff Thompsonf0fea002013-07-30 17:22:42 -070091 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070092 ndn_Error error;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070093 if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedFieldsBeginOffset, signedFieldsEndOffset, &decoder)))
Jeff Thompson5cae5e52013-07-10 19:41:20 -070094 throw std::runtime_error(ndn_getErrorString(error));
95
Jeff Thompson56ec9e22013-08-02 11:34:07 -070096 data.set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070097}
98
Jeff Thompson1656e6a2013-08-29 18:01:48 -070099ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeForwardingEntry(const ForwardingEntry& forwardingEntry)
Jeff Thompson990599b2013-08-27 15:14:25 -0700100{
101 struct ndn_NameComponent prefixNameComponents[100];
102 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700103 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700104 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
105 forwardingEntry.get(forwardingEntryStruct);
106
107 BinaryXmlEncoder encoder;
108 ndn_Error error;
109 if ((error = ndn_encodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &encoder)))
110 throw std::runtime_error(ndn_getErrorString(error));
111
112 return encoder.getOutput();
113}
114
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700115void BinaryXmlWireFormat::decodeForwardingEntry(ForwardingEntry& forwardingEntry, const unsigned char *input, unsigned int inputLength)
Jeff Thompson990599b2013-08-27 15:14:25 -0700116{
117 struct ndn_NameComponent prefixNameComponents[100];
118 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700119 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700120 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
121
122 BinaryXmlDecoder decoder(input, inputLength);
123 ndn_Error error;
124 if ((error = ndn_decodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &decoder)))
125 throw std::runtime_error(ndn_getErrorString(error));
126
127 forwardingEntry.set(forwardingEntryStruct);
128}
129
Jeff Thompson4c89ad62013-06-28 12:50:13 -0700130}