blob: 55a6d77a08b7172bbe4f3f95d35718e1dc7f2338 [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.
23WireFormat *WireFormat::newInitialDefaultWireFormat()
24{
25 return new BinaryXmlWireFormat();
26}
27
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070028Blob BinaryXmlWireFormat::encodeInterest(const Interest& interest)
Jeff Thompson214c7be2013-07-08 15:23:00 -070029{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070030 struct ndn_NameComponent nameComponents[100];
31 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson214c7be2013-07-08 15:23:00 -070032 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070033 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -070034 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
35 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson214c7be2013-07-08 15:23:00 -070036 interest.get(interestStruct);
37
Jeff Thompsonf0fea002013-07-30 17:22:42 -070038 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070039 ndn_Error error;
40 if ((error = ndn_encodeBinaryXmlInterest(&interestStruct, &encoder)))
41 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson214c7be2013-07-08 15:23:00 -070042
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070043 return encoder.getOutput();
Jeff Thompson214c7be2013-07-08 15:23:00 -070044}
45
Jeff Thompson1656e6a2013-08-29 18:01:48 -070046void BinaryXmlWireFormat::decodeInterest(Interest& interest, const unsigned char *input, unsigned int inputLength)
Jeff Thompson7c30eda2013-07-03 18:37:07 -070047{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070048 struct ndn_NameComponent nameComponents[100];
49 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -070050 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070051 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -070052 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
53 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070054
Jeff Thompsonf0fea002013-07-30 17:22:42 -070055 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070056 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070057 if ((error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder)))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070058 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070059
60 interest.set(interestStruct);
61}
62
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070063Blob BinaryXmlWireFormat::encodeData(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 Thompsonc2b7b142013-09-12 15:29:04 -070099Blob 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}