summaryrefslogtreecommitdiff
path: root/lib/rb/lib/thrift/protocol/base_protocol.rb
blob: 88f44d46df0ad925ff8fc6c7bb276a4bedde33c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# 
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# 

# this require is to make generated struct definitions happy
require 'set'

module Thrift
  class ProtocolException < Exception

    UNKNOWN = 0
    INVALID_DATA = 1
    NEGATIVE_SIZE = 2
    SIZE_LIMIT = 3
    BAD_VERSION = 4
    NOT_IMPLEMENTED = 5
    DEPTH_LIMIT = 6

    attr_reader :type

    def initialize(type=UNKNOWN, message=nil)
      super(message)
      @type = type
    end
  end

  class BaseProtocol

    attr_reader :trans

    def initialize(trans)
      @trans = trans
    end

    def native?
      puts "wrong method is being called!"
      false
    end

    def write_message_begin(name, type, seqid)
      raise NotImplementedError
    end

    def write_message_end; nil; end

    def write_struct_begin(name)
      raise NotImplementedError
    end

    def write_struct_end; nil; end

    def write_field_begin(name, type, id)
      raise NotImplementedError
    end

    def write_field_end; nil; end

    def write_field_stop
      raise NotImplementedError
    end

    def write_map_begin(ktype, vtype, size)
      raise NotImplementedError
    end

    def write_map_end; nil; end

    def write_list_begin(etype, size)
      raise NotImplementedError
    end

    def write_list_end; nil; end

    def write_set_begin(etype, size)
      raise NotImplementedError
    end

    def write_set_end; nil; end

    def write_bool(bool)
      raise NotImplementedError
    end

    def write_byte(byte)
      raise NotImplementedError
    end

    def write_i16(i16)
      raise NotImplementedError
    end

    def write_i32(i32)
      raise NotImplementedError
    end

    def write_i64(i64)
      raise NotImplementedError
    end

    def write_double(dub)
      raise NotImplementedError
    end

    # Writes a Thrift String. In Ruby 1.9+, the String passed will be transcoded to UTF-8.
    #
    # str - The String to write.
    #
    # Raises EncodingError if the transcoding to UTF-8 fails.
    #
    # Returns nothing.
    def write_string(str)
      raise NotImplementedError
    end

    # Writes a Thrift Binary (Thrift String with no encoding). In Ruby 1.9+, the String passed
    # will forced into BINARY encoding.
    #
    # buf - The String to write.
    #
    # Returns nothing.
    def write_binary(buf)
      raise NotImplementedError
    end

    def read_message_begin
      raise NotImplementedError
    end

    def read_message_end; nil; end

    def read_struct_begin
      raise NotImplementedError
    end

    def read_struct_end; nil; end

    def read_field_begin
      raise NotImplementedError
    end

    def read_field_end; nil; end

    def read_map_begin
      raise NotImplementedError
    end

    def read_map_end; nil; end

    def read_list_begin
      raise NotImplementedError
    end

    def read_list_end; nil; end

    def read_set_begin
      raise NotImplementedError
    end

    def read_set_end; nil; end

    def read_bool
      raise NotImplementedError
    end

    def read_byte
      raise NotImplementedError
    end

    def read_i16
      raise NotImplementedError
    end

    def read_i32
      raise NotImplementedError
    end

    def read_i64
      raise NotImplementedError
    end

    def read_double
      raise NotImplementedError
    end

    # Reads a Thrift String. In Ruby 1.9+, all Strings will be returned with an Encoding of UTF-8.
    #
    # Returns a String.
    def read_string
      raise NotImplementedError
    end

    # Reads a Thrift Binary (Thrift String without encoding). In Ruby 1.9+, all Strings will be returned
    # with an Encoding of BINARY.
    #
    # Returns a String.
    def read_binary
      raise NotImplementedError
    end

    # Writes a field based on the field information, field ID and value.
    #
    # field_info - A Hash containing the definition of the field:
    #              :name   - The name of the field.
    #              :type   - The type of the field, which must be a Thrift::Types constant.
    #              :binary - A Boolean flag that indicates if Thrift::Types::STRING is a binary string (string without encoding).
    # fid        - The ID of the field.
    # value      - The field's value to write; object type varies based on :type.
    #
    # Returns nothing.
    def write_field(*args)
      if args.size == 3
        # handles the documented method signature - write_field(field_info, fid, value)
        field_info = args[0]
        fid = args[1]
        value = args[2]
      elsif args.size == 4
        # handles the deprecated method signature - write_field(name, type, fid, value)
        field_info = {:name => args[0], :type => args[1]}
        fid = args[2]
        value = args[3]
      else
        raise ArgumentError, "wrong number of arguments (#{args.size} for 3)"
      end

      write_field_begin(field_info[:name], field_info[:type], fid)
      write_type(field_info, value)
      write_field_end
    end

    # Writes a field value based on the field information.
    #
    # field_info - A Hash containing the definition of the field:
    #              :type   - The Thrift::Types constant that determines how the value is written.
    #              :binary - A Boolean flag that indicates if Thrift::Types::STRING is a binary string (string without encoding).
    # value      - The field's value to write; object type varies based on field_info[:type].
    #
    # Returns nothing.
    def write_type(field_info, value)
      # if field_info is a Fixnum, assume it is a Thrift::Types constant
      # convert it into a field_info Hash for backwards compatibility
      if field_info.is_a? Fixnum
        field_info = {:type => field_info}
      end

      case field_info[:type]
      when Types::BOOL
        write_bool(value)
      when Types::BYTE
        write_byte(value)
      when Types::DOUBLE
        write_double(value)
      when Types::I16
        write_i16(value)
      when Types::I32
        write_i32(value)
      when Types::I64
        write_i64(value)
      when Types::STRING
        if field_info[:binary]
          write_binary(value)
        else
          write_string(value)
        end
      when Types::STRUCT
        value.write(self)
      else
        raise NotImplementedError
      end
    end

    # Reads a field value based on the field information.
    #
    # field_info - A Hash containing the pertinent data to write:
    #              :type   - The Thrift::Types constant that determines how the value is written.
    #              :binary - A flag that indicates if Thrift::Types::STRING is a binary string (string without encoding).
    #
    # Returns the value read; object type varies based on field_info[:type].
    def read_type(field_info)
      # if field_info is a Fixnum, assume it is a Thrift::Types constant
      # convert it into a field_info Hash for backwards compatibility
      if field_info.is_a? Fixnum
        field_info = {:type => field_info}
      end

      case field_info[:type]
      when Types::BOOL
        read_bool
      when Types::BYTE
        read_byte
      when Types::DOUBLE
        read_double
      when Types::I16
        read_i16
      when Types::I32
        read_i32
      when Types::I64
        read_i64
      when Types::STRING
        if field_info[:binary]
          read_binary
        else
          read_string
        end
      else
        raise NotImplementedError
      end
    end

    def skip(type)
      case type
      when Types::STOP
        nil
      when Types::BOOL
        read_bool
      when Types::BYTE
        read_byte
      when Types::I16
        read_i16
      when Types::I32
        read_i32
      when Types::I64
        read_i64
      when Types::DOUBLE
        read_double
      when Types::STRING
        read_string
      when Types::STRUCT
        read_struct_begin
        while true
          name, type, id = read_field_begin
          break if type == Types::STOP
          skip(type)
          read_field_end
        end
        read_struct_end
      when Types::MAP
        ktype, vtype, size = read_map_begin
        size.times do
          skip(ktype)
          skip(vtype)
        end
        read_map_end
      when Types::SET
        etype, size = read_set_begin
        size.times do
          skip(etype)
        end
        read_set_end
      when Types::LIST
        etype, size = read_list_begin
        size.times do
          skip(etype)
        end
        read_list_end
      end
    end
  end

  class BaseProtocolFactory
    def get_protocol(trans)
      raise NotImplementedError
    end
  end
end