blob: 71f74d5813200697b92565a171b7aa35eef7d340 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07005 */
6
Jeff Thompson4c89ad62013-06-28 12:50:13 -07007#include <stdexcept>
Jeff Thompson53412192013-08-06 13:35:50 -07008#include "../c/encoding/binary-xml-interest.h"
Jeff Thompson56ec9e22013-08-02 11:34:07 -07009#include "../c/encoding/binary-xml-data.h"
Jeff Thompson990599b2013-08-27 15:14:25 -070010#include "../c/encoding/binary-xml-forwarding-entry.h"
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "../interest.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070012#include "../data.hpp"
Jeff Thompson990599b2013-08-27 15:14:25 -070013#include "../forwarding-entry.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070014#include "binary-xml-encoder.hpp"
15#include "binary-xml-decoder.hpp"
16#include "binary-xml-wire-format.hpp"
Jeff Thompson4c89ad62013-06-28 12:50:13 -070017
Jeff Thompson1f3f5172013-07-01 19:02:36 -070018using namespace std;
19
Jeff Thompson4c89ad62013-06-28 12:50:13 -070020namespace ndn {
21
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070022// This is declared in the WireFormat class.
Jeff Thompson0050abe2013-09-17 12:50:25 -070023WireFormat*
24WireFormat::newInitialDefaultWireFormat()
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070025{
26 return new BinaryXmlWireFormat();
27}
28
Jeff Thompson0050abe2013-09-17 12:50:25 -070029Blob
30BinaryXmlWireFormat::encodeInterest(const Interest& interest)
Jeff Thompson214c7be2013-07-08 15:23:00 -070031{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070032 struct ndn_NameComponent nameComponents[100];
33 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson214c7be2013-07-08 15:23:00 -070034 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070035 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -070036 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
37 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson214c7be2013-07-08 15:23:00 -070038 interest.get(interestStruct);
39
Jeff Thompsonf0fea002013-07-30 17:22:42 -070040 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070041 ndn_Error error;
42 if ((error = ndn_encodeBinaryXmlInterest(&interestStruct, &encoder)))
43 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson214c7be2013-07-08 15:23:00 -070044
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070045 return encoder.getOutput();
Jeff Thompson214c7be2013-07-08 15:23:00 -070046}
47
Jeff Thompson0050abe2013-09-17 12:50:25 -070048void
49BinaryXmlWireFormat::decodeInterest(Interest& interest, const unsigned char *input, unsigned int inputLength)
Jeff Thompson7c30eda2013-07-03 18:37:07 -070050{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070051 struct ndn_NameComponent nameComponents[100];
52 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -070053 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070054 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -070055 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
56 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070057
Jeff Thompsonf0fea002013-07-30 17:22:42 -070058 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070059 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070060 if ((error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder)))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070061 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070062
63 interest.set(interestStruct);
64}
65
Jeff Thompson0050abe2013-09-17 12:50:25 -070066Blob
67BinaryXmlWireFormat::encodeData(const Data& data, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070068{
69 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -070070 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070071 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070072 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -070073 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
74 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson56ec9e22013-08-02 11:34:07 -070075 data.get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070076
Jeff Thompsonf0fea002013-07-30 17:22:42 -070077 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070078 ndn_Error error;
Jeff Thompson9c661702013-09-13 14:35:44 -070079 if ((error = ndn_encodeBinaryXmlData(&dataStruct, signedPortionBeginOffset, signedPortionEndOffset, &encoder)))
Jeff Thompsonaa020712013-08-08 21:20:06 -070080 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070081
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070082 return encoder.getOutput();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070083}
84
Jeff Thompson0050abe2013-09-17 12:50:25 -070085void
86BinaryXmlWireFormat::decodeData
Jeff Thompson9c661702013-09-13 14:35:44 -070087 (Data& data, const unsigned char *input, unsigned int inputLength, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070088{
89 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -070090 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070091 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070092 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -070093 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
94 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070095
Jeff Thompsonf0fea002013-07-30 17:22:42 -070096 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070097 ndn_Error error;
Jeff Thompson9c661702013-09-13 14:35:44 -070098 if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedPortionBeginOffset, signedPortionEndOffset, &decoder)))
Jeff Thompson5cae5e52013-07-10 19:41:20 -070099 throw std::runtime_error(ndn_getErrorString(error));
100
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700101 data.set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700102}
103
Jeff Thompson0050abe2013-09-17 12:50:25 -0700104Blob
105BinaryXmlWireFormat::encodeForwardingEntry(const ForwardingEntry& forwardingEntry)
Jeff Thompson990599b2013-08-27 15:14:25 -0700106{
107 struct ndn_NameComponent prefixNameComponents[100];
108 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700109 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700110 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
111 forwardingEntry.get(forwardingEntryStruct);
112
113 BinaryXmlEncoder encoder;
114 ndn_Error error;
115 if ((error = ndn_encodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &encoder)))
116 throw std::runtime_error(ndn_getErrorString(error));
117
118 return encoder.getOutput();
119}
120
Jeff Thompson0050abe2013-09-17 12:50:25 -0700121void
122BinaryXmlWireFormat::decodeForwardingEntry(ForwardingEntry& forwardingEntry, const unsigned char *input, unsigned int inputLength)
Jeff Thompson990599b2013-08-27 15:14:25 -0700123{
124 struct ndn_NameComponent prefixNameComponents[100];
125 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700126 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700127 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
128
129 BinaryXmlDecoder decoder(input, inputLength);
130 ndn_Error error;
131 if ((error = ndn_decodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &decoder)))
132 throw std::runtime_error(ndn_getErrorString(error));
133
134 forwardingEntry.set(forwardingEntryStruct);
135}
136
Jeff Thompson4c89ad62013-06-28 12:50:13 -0700137}