blob: a892659cae843eaed98695de111fb84eaaadd6b9 [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 Thompson9c41dfe2013-06-27 12:10:25 -07007#ifndef NDN_WIREFORMAT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_WIREFORMAT_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07009
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070010#include "../common.hpp"
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070011#include "../util/blob.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070012
13namespace ndn {
14
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070015class Interest;
Jeff Thompson56ec9e22013-08-02 11:34:07 -070016class Data;
Jeff Thompson990599b2013-08-27 15:14:25 -070017class ForwardingEntry;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070018
19class WireFormat {
20public:
Jeff Thompson990599b2013-08-27 15:14:25 -070021 /**
22 * Encode interest and return the encoding. Your derived class should override.
23 * @param interest The Interest object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070024 * @return A Blob containing the encoding.
Jeff Thompson990599b2013-08-27 15:14:25 -070025 * @throw logic_error for unimplemented if the derived class does not override.
26 */
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070027 virtual Blob encodeInterest(const Interest& interest);
Jeff Thompson990599b2013-08-27 15:14:25 -070028
29 /**
30 * Decode input as an interest and set the fields of the interest object. Your derived class should override.
31 * @param interest The Interest object whose fields are updated.
32 * @param input A pointer to the input buffer to decode.
33 * @param inputLength The number of bytes in input.
34 * @throw logic_error for unimplemented if the derived class does not override.
35 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070036 virtual void decodeInterest(Interest& interest, const unsigned char *input, unsigned int inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070037
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070038 /**
39 * Encode data and return the encoding. Your derived class should override.
40 * @param data The Data object to encode.
Jeff Thompson9c661702013-09-13 14:35:44 -070041 * @param signedPortionBeginOffset Return the offset in the encoding of the beginning of the signed portion.
Jeff Thompson1656e6a2013-08-29 18:01:48 -070042 * If you are not encoding in order to sign, you can call encodeData(const Data& data) to ignore this returned value.
Jeff Thompson9c661702013-09-13 14:35:44 -070043 * @param signedPortionEndOffset Return the offset in the encoding of the end of the signed portion.
Jeff Thompson1656e6a2013-08-29 18:01:48 -070044 * If you are not encoding in order to sign, you can call encodeData(const Data& data) to ignore this returned value.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070045 * @return A Blob containing the encoding.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070046 * @throw logic_error for unimplemented if the derived class does not override.
47 */
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070048 virtual Blob encodeData
Jeff Thompson9c661702013-09-13 14:35:44 -070049 (const Data& data, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset);
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070050
51 /**
52 * Encode data and return the encoding.
53 * @param data The Data object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070054 * @return A Blob containing the encoding.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070055 * @throw logic_error for unimplemented if the derived class does not override.
56 */
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070057 Blob encodeData(const Data& data)
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070058 {
59 unsigned int dummyBeginOffset, dummyEndOffset;
60 return encodeData(data, &dummyBeginOffset, &dummyEndOffset);
61 }
62
63 /**
64 * Decode input as a data packet and set the fields in the data object. Your derived class should override.
65 * @param data The Data object whose fields are updated.
66 * @param input A pointer to the input buffer to decode.
67 * @param inputLength The number of bytes in input.
Jeff Thompson9c661702013-09-13 14:35:44 -070068 * @param signedPortionBeginOffset Return the offset in the input buffer of the beginning of the signed portion.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070069 * If you are not decoding in order to verify, you can call
Jeff Thompson1656e6a2013-08-29 18:01:48 -070070 * decodeData(Data& data, const unsigned char *input, unsigned int inputLength) to ignore this returned value.
Jeff Thompson9c661702013-09-13 14:35:44 -070071 * @param signedPortionEndOffset Return the offset in the input buffer of the end of the signed portion.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070072 * If you are not decoding in order to verify, you can call
Jeff Thompson1656e6a2013-08-29 18:01:48 -070073 * decodeData(Data& data, const unsigned char *input, unsigned int inputLength) to ignore this returned value.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070074 * @throw logic_error for unimplemented if the derived class does not override.
75 */
76 virtual void decodeData
Jeff Thompson9c661702013-09-13 14:35:44 -070077 (Data& data, const unsigned char *input, unsigned int inputLength, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset);
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070078
Jeff Thompson1656e6a2013-08-29 18:01:48 -070079 void decodeData(Data& data, const unsigned char *input, unsigned int inputLength)
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070080 {
81 unsigned int dummyBeginOffset, dummyEndOffset;
82 decodeData(data, input, inputLength, &dummyBeginOffset, &dummyEndOffset);
83 }
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070084
85 /**
Jeff Thompson990599b2013-08-27 15:14:25 -070086 * Encode forwardingEntry and return the encoding. Your derived class should override.
87 * @param forwardingEntry The ForwardingEntry object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070088 * @return A Blob containing the encoding.
Jeff Thompson990599b2013-08-27 15:14:25 -070089 * @throw logic_error for unimplemented if the derived class does not override.
90 */
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070091 virtual Blob encodeForwardingEntry(const ForwardingEntry& forwardingEntry);
Jeff Thompson990599b2013-08-27 15:14:25 -070092
93 /**
94 * Decode input as a forwarding entry and set the fields of the forwardingEntry object. Your derived class should override.
95 * @param forwardingEntry The ForwardingEntry object whose fields are updated.
96 * @param input A pointer to the input buffer to decode.
97 * @param inputLength The number of bytes in input.
98 * @throw logic_error for unimplemented if the derived class does not override.
99 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700100 virtual void decodeForwardingEntry(ForwardingEntry& forwardingEntry, const unsigned char *input, unsigned int inputLength);
Jeff Thompson990599b2013-08-27 15:14:25 -0700101
102 /**
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700103 * Set the static default WireFormat used by default encoding and decoding methods.
Jeff Thompson72fd81d2013-08-20 12:34:51 -0700104 * @param wireFormat A Pointer to an object of a subclass of WireFormat. This does not make a copy and
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700105 * the caller must ensure that the object remains allocated.
106 */
107 static void setDefaultWireFormat(WireFormat *wireFormat)
108 {
109 defaultWireFormat_ = wireFormat;
110 }
111
112 /**
113 * Return the default WireFormat used by default encoding and decoding methods which was set with
114 * setDefaultWireFormat.
115 * @return A pointer to the WireFormat object.
116 */
Jeff Thompson535f21a2013-08-05 18:25:46 -0700117 static WireFormat *getDefaultWireFormat();
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700118
119private:
120 /**
121 * This is implemented by only one of the subclasses of WireFormat to return a new object used
122 * as the initial value for the default WireFormat. If the application doesn't include that class, then the application
123 * needs to include another subclass which defines WireFormat::newInitialDefaultWireFormat.
124 * @return a new object, which is held by a shared_ptr and freed when the application exits.
125 */
126 static WireFormat *newInitialDefaultWireFormat();
127
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700128 static WireFormat *defaultWireFormat_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700129};
130
131}
132
133#endif
134