]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | var util = require('util'); | |
20 | var TBinaryProtocol = require('./binary_protocol'); | |
21 | var TCompactProtocol = require('./compact_protocol'); | |
22 | var THeaderTransport = require('./header_transport'); | |
23 | ||
24 | var ProtocolMap = {}; | |
25 | ProtocolMap[THeaderTransport.SubprotocolId.BINARY] = TBinaryProtocol; | |
26 | ProtocolMap[THeaderTransport.SubprotocolId.COMPACT] = TCompactProtocol; | |
27 | ||
28 | module.exports = THeaderProtocol; | |
29 | ||
30 | function THeaderProtocolError(message) { | |
31 | Error.call(this); | |
32 | Error.captureStackTrace(this, this.constructor); | |
33 | this.name = this.constructor.name; | |
34 | this.message = message; | |
35 | } | |
36 | ||
37 | util.inherits(THeaderProtocolError, Error); | |
38 | ||
39 | /** | |
40 | * A framed protocol with headers. | |
41 | * | |
42 | * THeaderProtocol frames other Thrift protocols and adds support for | |
43 | * optional out-of-band headers. The currently supported subprotocols are | |
44 | * TBinaryProtocol and TCompactProtocol. It can currently only be used with | |
45 | * transports that inherit THeaderTransport. | |
46 | * | |
47 | * THeaderProtocol does not currently support THTTPServer, TNonblockingServer, | |
48 | * or TProcessPoolServer. | |
49 | * | |
50 | * See doc/specs/HeaderFormat.md for details of the wire format. | |
51 | */ | |
52 | function THeaderProtocol(trans) { | |
53 | if (!(trans instanceof THeaderTransport)) { | |
54 | throw new THeaderProtocolError( | |
55 | 'Only transports that inherit THeaderTransport can be' + | |
56 | ' used with THeaderProtocol' | |
57 | ); | |
58 | } | |
59 | this.trans = trans; | |
60 | this.setProtocol(); | |
61 | }; | |
62 | ||
63 | THeaderProtocol.prototype.flush = function() { | |
64 | // Headers must be written prior to flushing because because | |
65 | // you need to calculate the length of the payload for the length | |
66 | // field of the header | |
67 | this.trans.writeHeaders(); | |
68 | return this.trans.flush(); | |
69 | }; | |
70 | ||
71 | THeaderProtocol.prototype.writeMessageBegin = function(name, type, seqid) { | |
72 | return this.protocol.writeMessageBegin(name, type, seqid); | |
73 | }; | |
74 | ||
75 | THeaderProtocol.prototype.writeMessageEnd = function() { | |
76 | return this.protocol.writeMessageEnd(); | |
77 | }; | |
78 | ||
79 | THeaderProtocol.prototype.writeStructBegin = function(name) { | |
80 | return this.protocol.writeStructBegin(name); | |
81 | }; | |
82 | ||
83 | THeaderProtocol.prototype.writeStructEnd = function() { | |
84 | return this.protocol.writeStructEnd(); | |
85 | }; | |
86 | ||
87 | THeaderProtocol.prototype.writeFieldBegin = function(name, type, id) { | |
88 | return this.protocol.writeFieldBegin(name, type, id); | |
89 | } | |
90 | ||
91 | THeaderProtocol.prototype.writeFieldEnd = function() { | |
92 | return this.protocol.writeFieldEnd(); | |
93 | }; | |
94 | ||
95 | THeaderProtocol.prototype.writeFieldStop = function() { | |
96 | return this.protocol.writeFieldStop(); | |
97 | }; | |
98 | ||
99 | THeaderProtocol.prototype.writeMapBegin = function(ktype, vtype, size) { | |
100 | return this.protocol.writeMapBegin(ktype, vtype, size); | |
101 | }; | |
102 | ||
103 | THeaderProtocol.prototype.writeMapEnd = function() { | |
104 | return this.protocol.writeMapEnd(); | |
105 | }; | |
106 | ||
107 | THeaderProtocol.prototype.writeListBegin = function(etype, size) { | |
108 | return this.protocol.writeListBegin(etype, size); | |
109 | }; | |
110 | ||
111 | THeaderProtocol.prototype.writeListEnd = function() { | |
112 | return this.protocol.writeListEnd(); | |
113 | }; | |
114 | ||
115 | THeaderProtocol.prototype.writeSetBegin = function(etype, size) { | |
116 | return this.protocol.writeSetBegin(etype, size); | |
117 | }; | |
118 | ||
119 | THeaderProtocol.prototype.writeSetEnd = function() { | |
120 | return this.protocol.writeSetEnd(); | |
121 | }; | |
122 | ||
123 | THeaderProtocol.prototype.writeBool = function(b) { | |
124 | return this.protocol.writeBool(b); | |
125 | }; | |
126 | ||
127 | THeaderProtocol.prototype.writeByte = function(b) { | |
128 | return this.protocol.writeByte(b); | |
129 | }; | |
130 | ||
131 | THeaderProtocol.prototype.writeI16 = function(i16) { | |
132 | return this.protocol.writeI16(i16); | |
133 | }; | |
134 | ||
135 | THeaderProtocol.prototype.writeI32 = function(i32) { | |
136 | return this.protocol.writeI32(i32); | |
137 | }; | |
138 | ||
139 | THeaderProtocol.prototype.writeI64 = function(i64) { | |
140 | return this.protocol.writeI64(i64); | |
141 | }; | |
142 | ||
143 | THeaderProtocol.prototype.writeDouble = function(dub) { | |
144 | return this.protocol.writeDouble(dub); | |
145 | }; | |
146 | ||
147 | THeaderProtocol.prototype.writeStringOrBinary = function(name, encoding, arg) { | |
148 | return this.protocol.writeStringOrBinary(name, encoding, arg); | |
149 | }; | |
150 | ||
151 | THeaderProtocol.prototype.writeString = function(arg) { | |
152 | return this.protocol.writeString(arg); | |
153 | }; | |
154 | ||
155 | THeaderProtocol.prototype.writeBinary = function(arg) { | |
156 | return this.protocol.writeBinary(arg); | |
157 | }; | |
158 | ||
159 | THeaderProtocol.prototype.readMessageBegin = function() { | |
160 | this.trans.readHeaders(); | |
161 | this.setProtocol(); | |
162 | return this.protocol.readMessageBegin(); | |
163 | }; | |
164 | ||
165 | THeaderProtocol.prototype.readMessageEnd = function() { | |
166 | return this.protocol.readMessageEnd(); | |
167 | }; | |
168 | ||
169 | THeaderProtocol.prototype.readStructBegin = function() { | |
170 | return this.protocol.readStructBegin(); | |
171 | }; | |
172 | ||
173 | THeaderProtocol.prototype.readStructEnd = function() { | |
174 | return this.protocol.readStructEnd(); | |
175 | }; | |
176 | ||
177 | THeaderProtocol.prototype.readFieldBegin = function() { | |
178 | return this.protocol.readFieldBegin(); | |
179 | }; | |
180 | ||
181 | THeaderProtocol.prototype.readFieldEnd = function() { | |
182 | return this.protocol.readFieldEnd(); | |
183 | }; | |
184 | ||
185 | THeaderProtocol.prototype.readMapBegin = function() { | |
186 | return this.protocol.readMapBegin(); | |
187 | }; | |
188 | ||
189 | THeaderProtocol.prototype.readMapEnd = function() { | |
190 | return this.protocol.readMapEnd(); | |
191 | }; | |
192 | ||
193 | THeaderProtocol.prototype.readListBegin = function() { | |
194 | return this.protocol.readListBegin(); | |
195 | }; | |
196 | ||
197 | THeaderProtocol.prototype.readListEnd = function() { | |
198 | return this.protocol.readListEnd(); | |
199 | }; | |
200 | ||
201 | THeaderProtocol.prototype.readSetBegin = function() { | |
202 | return this.protocol.readSetBegin(); | |
203 | }; | |
204 | ||
205 | THeaderProtocol.prototype.readSetEnd = function() { | |
206 | return this.protocol.readSetEnd(); | |
207 | }; | |
208 | ||
209 | THeaderProtocol.prototype.readBool = function() { | |
210 | return this.protocol.readBool(); | |
211 | }; | |
212 | ||
213 | THeaderProtocol.prototype.readByte = function() { | |
214 | return this.protocol.readByte(); | |
215 | }; | |
216 | ||
217 | THeaderProtocol.prototype.readI16 = function() { | |
218 | return this.protocol.readI16(); | |
219 | }; | |
220 | ||
221 | THeaderProtocol.prototype.readI32 = function() { | |
222 | return this.protocol.readI32(); | |
223 | }; | |
224 | ||
225 | THeaderProtocol.prototype.readI64 = function() { | |
226 | return this.protocol.readI64(); | |
227 | }; | |
228 | ||
229 | THeaderProtocol.prototype.readDouble = function() { | |
230 | return this.protocol.readDouble(); | |
231 | }; | |
232 | ||
233 | THeaderProtocol.prototype.readBinary = function() { | |
234 | return this.protocol.readBinary(); | |
235 | }; | |
236 | ||
237 | THeaderProtocol.prototype.readString = function() { | |
238 | return this.protocol.readString(); | |
239 | }; | |
240 | ||
241 | THeaderProtocol.prototype.getTransport = function() { | |
242 | return this.trans; | |
243 | }; | |
244 | ||
245 | THeaderProtocol.prototype.skip = function(type) { | |
246 | return this.protocol.skip(type); | |
247 | }; | |
248 | ||
249 | THeaderProtocol.prototype.setProtocol = function(subProtocolId) { | |
250 | var subProtocolId = this.trans.getProtocolId(); | |
251 | if (!ProtocolMap[subProtocolId]) { | |
252 | throw new THeaderProtocolError('Headers not supported for protocol ' + subProtocolId); | |
253 | } | |
254 | ||
255 | this.protocol = new ProtocolMap[subProtocolId](this.trans); | |
256 | }; |