blob: 1a40cd1aa309c85d8ba6b5d8ba1d13fd588dea14 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07006 */
7
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07008#ifndef NDN_WIREFORMAT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_WIREFORMAT_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070010
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070011#include "../common.hpp"
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070012#include "../util/blob.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070013
14namespace ndn {
15
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070016class Interest;
Jeff Thompson56ec9e22013-08-02 11:34:07 -070017class Data;
Jeff Thompson990599b2013-08-27 15:14:25 -070018class ForwardingEntry;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070019
20class WireFormat {
21public:
Jeff Thompson990599b2013-08-27 15:14:25 -070022 /**
23 * Encode interest and return the encoding. Your derived class should override.
24 * @param interest The Interest object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070025 * @return A Blob containing the encoding.
Jeff Thompson990599b2013-08-27 15:14:25 -070026 * @throw logic_error for unimplemented if the derived class does not override.
27 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070028 virtual Blob
29 encodeInterest(const Interest& interest);
Jeff Thompson990599b2013-08-27 15:14:25 -070030
31 /**
32 * Decode input as an interest and set the fields of the interest object. Your derived class should override.
33 * @param interest The Interest object whose fields are updated.
34 * @param input A pointer to the input buffer to decode.
35 * @param inputLength The number of bytes in input.
36 * @throw logic_error for unimplemented if the derived class does not override.
37 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070038 virtual void
Jeff Thompson97223af2013-09-24 17:01:27 -070039 decodeInterest(Interest& interest, const uint8_t *input, size_t inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070040
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070041 /**
42 * Encode data and return the encoding. Your derived class should override.
43 * @param data The Data object to encode.
Jeff Thompson9c661702013-09-13 14:35:44 -070044 * @param signedPortionBeginOffset Return the offset in the encoding of the beginning of the signed portion.
Jeff Thompson1656e6a2013-08-29 18:01:48 -070045 * 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 -070046 * @param signedPortionEndOffset Return the offset in the encoding of the end of the signed portion.
Jeff Thompson1656e6a2013-08-29 18:01:48 -070047 * 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 -070048 * @return A Blob containing the encoding.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070049 * @throw logic_error for unimplemented if the derived class does not override.
50 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070051 virtual Blob
52 encodeData
Jeff Thompson97223af2013-09-24 17:01:27 -070053 (const Data& data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset);
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070054
55 /**
56 * Encode data and return the encoding.
57 * @param data The Data object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070058 * @return A Blob containing the encoding.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070059 * @throw logic_error for unimplemented if the derived class does not override.
60 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070061 Blob
62 encodeData(const Data& data)
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070063 {
Jeff Thompson97223af2013-09-24 17:01:27 -070064 size_t dummyBeginOffset, dummyEndOffset;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070065 return encodeData(data, &dummyBeginOffset, &dummyEndOffset);
66 }
67
68 /**
69 * Decode input as a data packet and set the fields in the data object. Your derived class should override.
70 * @param data The Data object whose fields are updated.
71 * @param input A pointer to the input buffer to decode.
72 * @param inputLength The number of bytes in input.
Jeff Thompson9c661702013-09-13 14:35:44 -070073 * @param signedPortionBeginOffset Return the offset in the input buffer of the beginning of the signed portion.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070074 * If you are not decoding in order to verify, you can call
Jeff Thompson97223af2013-09-24 17:01:27 -070075 * decodeData(Data& data, const uint8_t *input, size_t inputLength) to ignore this returned value.
Jeff Thompson9c661702013-09-13 14:35:44 -070076 * @param signedPortionEndOffset Return the offset in the input buffer of the end of the signed portion.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070077 * If you are not decoding in order to verify, you can call
Jeff Thompson97223af2013-09-24 17:01:27 -070078 * decodeData(Data& data, const uint8_t *input, size_t inputLength) to ignore this returned value.
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070079 * @throw logic_error for unimplemented if the derived class does not override.
80 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070081 virtual void
82 decodeData
Jeff Thompson97223af2013-09-24 17:01:27 -070083 (Data& data, const uint8_t *input, size_t inputLength, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset);
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070084
Jeff Thompson0050abe2013-09-17 12:50:25 -070085 void
Jeff Thompson97223af2013-09-24 17:01:27 -070086 decodeData(Data& data, const uint8_t *input, size_t inputLength)
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070087 {
Jeff Thompson97223af2013-09-24 17:01:27 -070088 size_t dummyBeginOffset, dummyEndOffset;
Jeff Thompsonc87f39a2013-08-12 11:55:11 -070089 decodeData(data, input, inputLength, &dummyBeginOffset, &dummyEndOffset);
90 }
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070091
92 /**
Jeff Thompson990599b2013-08-27 15:14:25 -070093 * Encode forwardingEntry and return the encoding. Your derived class should override.
94 * @param forwardingEntry The ForwardingEntry object to encode.
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070095 * @return A Blob containing the encoding.
Jeff Thompson990599b2013-08-27 15:14:25 -070096 * @throw logic_error for unimplemented if the derived class does not override.
97 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070098 virtual Blob
99 encodeForwardingEntry(const ForwardingEntry& forwardingEntry);
Jeff Thompson990599b2013-08-27 15:14:25 -0700100
101 /**
102 * Decode input as a forwarding entry and set the fields of the forwardingEntry object. Your derived class should override.
103 * @param forwardingEntry The ForwardingEntry object whose fields are updated.
104 * @param input A pointer to the input buffer to decode.
105 * @param inputLength The number of bytes in input.
106 * @throw logic_error for unimplemented if the derived class does not override.
107 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700108 virtual void
Jeff Thompson97223af2013-09-24 17:01:27 -0700109 decodeForwardingEntry(ForwardingEntry& forwardingEntry, const uint8_t *input, size_t inputLength);
Jeff Thompson990599b2013-08-27 15:14:25 -0700110
111 /**
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700112 * Set the static default WireFormat used by default encoding and decoding methods.
Jeff Thompson72fd81d2013-08-20 12:34:51 -0700113 * @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 -0700114 * the caller must ensure that the object remains allocated.
115 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700116 static void
117 setDefaultWireFormat(WireFormat *wireFormat)
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700118 {
119 defaultWireFormat_ = wireFormat;
120 }
121
122 /**
123 * Return the default WireFormat used by default encoding and decoding methods which was set with
124 * setDefaultWireFormat.
125 * @return A pointer to the WireFormat object.
126 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700127 static WireFormat*
128 getDefaultWireFormat();
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700129
130private:
131 /**
132 * This is implemented by only one of the subclasses of WireFormat to return a new object used
133 * as the initial value for the default WireFormat. If the application doesn't include that class, then the application
134 * needs to include another subclass which defines WireFormat::newInitialDefaultWireFormat.
135 * @return a new object, which is held by a shared_ptr and freed when the application exits.
136 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700137 static WireFormat*
138 newInitialDefaultWireFormat();
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700139
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700140 static WireFormat *defaultWireFormat_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700141};
142
143}
144
145#endif
146