Class: Thrift::BaseProtocol

Inherits:
Object
  • Object
show all
Defined in:
lib/thrift/protocol/base_protocol.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trans) ⇒ BaseProtocol

Returns a new instance of BaseProtocol.



47
48
49
# File 'lib/thrift/protocol/base_protocol.rb', line 47

def initialize(trans)
  @trans = trans
end

Instance Attribute Details

#transObject (readonly)

Returns the value of attribute trans.



45
46
47
# File 'lib/thrift/protocol/base_protocol.rb', line 45

def trans
  @trans
end

Instance Method Details

#native?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/thrift/protocol/base_protocol.rb', line 51

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

#read_binaryObject

Reads a Thrift Binary (Thrift String without encoding). All Strings will be returned with an Encoding of BINARY.

Returns a String.

Raises:

  • (NotImplementedError)


221
222
223
# File 'lib/thrift/protocol/base_protocol.rb', line 221

def read_binary
  raise NotImplementedError
end

#read_boolObject

Raises:

  • (NotImplementedError)


186
187
188
# File 'lib/thrift/protocol/base_protocol.rb', line 186

def read_bool
  raise NotImplementedError
end

#read_byteObject

Raises:

  • (NotImplementedError)


190
191
192
# File 'lib/thrift/protocol/base_protocol.rb', line 190

def read_byte
  raise NotImplementedError
end

#read_doubleObject

Raises:

  • (NotImplementedError)


206
207
208
# File 'lib/thrift/protocol/base_protocol.rb', line 206

def read_double
  raise NotImplementedError
end

#read_field_beginObject

Raises:

  • (NotImplementedError)


162
163
164
# File 'lib/thrift/protocol/base_protocol.rb', line 162

def read_field_begin
  raise NotImplementedError
end

#read_field_endObject



166
# File 'lib/thrift/protocol/base_protocol.rb', line 166

def read_field_end; nil; end

#read_i16Object

Raises:

  • (NotImplementedError)


194
195
196
# File 'lib/thrift/protocol/base_protocol.rb', line 194

def read_i16
  raise NotImplementedError
end

#read_i32Object

Raises:

  • (NotImplementedError)


198
199
200
# File 'lib/thrift/protocol/base_protocol.rb', line 198

def read_i32
  raise NotImplementedError
end

#read_i64Object

Raises:

  • (NotImplementedError)


202
203
204
# File 'lib/thrift/protocol/base_protocol.rb', line 202

def read_i64
  raise NotImplementedError
end

#read_list_beginObject

Raises:

  • (NotImplementedError)


174
175
176
# File 'lib/thrift/protocol/base_protocol.rb', line 174

def read_list_begin
  raise NotImplementedError
end

#read_list_endObject



178
# File 'lib/thrift/protocol/base_protocol.rb', line 178

def read_list_end; nil; end

#read_map_beginObject

Raises:

  • (NotImplementedError)


168
169
170
# File 'lib/thrift/protocol/base_protocol.rb', line 168

def read_map_begin
  raise NotImplementedError
end

#read_map_endObject



172
# File 'lib/thrift/protocol/base_protocol.rb', line 172

def read_map_end; nil; end

#read_message_beginObject

Raises:

  • (NotImplementedError)


150
151
152
# File 'lib/thrift/protocol/base_protocol.rb', line 150

def read_message_begin
  raise NotImplementedError
end

#read_message_endObject



154
# File 'lib/thrift/protocol/base_protocol.rb', line 154

def read_message_end; nil; end

#read_set_beginObject

Raises:

  • (NotImplementedError)


180
181
182
# File 'lib/thrift/protocol/base_protocol.rb', line 180

def read_set_begin
  raise NotImplementedError
end

#read_set_endObject



184
# File 'lib/thrift/protocol/base_protocol.rb', line 184

def read_set_end; nil; end

#read_stringObject

Reads a Thrift String. All Strings will be returned with an Encoding of UTF-8.

Returns a String.

Raises:

  • (NotImplementedError)


213
214
215
# File 'lib/thrift/protocol/base_protocol.rb', line 213

def read_string
  raise NotImplementedError
end

#read_struct_beginObject

Raises:

  • (NotImplementedError)


156
157
158
# File 'lib/thrift/protocol/base_protocol.rb', line 156

def read_struct_begin
  raise NotImplementedError
end

#read_struct_endObject



160
# File 'lib/thrift/protocol/base_protocol.rb', line 160

def read_struct_end; nil; end

#read_type(field_info) ⇒ Object

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.



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
# File 'lib/thrift/protocol/base_protocol.rb', line 312

def read_type(field_info)
  # if field_info is a Integer, assume it is a Thrift::Types constant
  # convert it into a field_info Hash for backwards compatibility
  if field_info.is_a? Integer
    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
  when Types::UUID
    read_uuid
  else
    raise NotImplementedError
  end
end

#read_uuidObject

Reads a UUID as 16 bytes and returns it as a string.

Returns a String (e.g. "550e8400-e29b-41d4-a716-446655440000").

Raises:

  • (NotImplementedError)


228
229
230
# File 'lib/thrift/protocol/base_protocol.rb', line 228

def read_uuid
  raise NotImplementedError
end

#skip(type, max_depth = 64) ⇒ Object

Raises:



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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/thrift/protocol/base_protocol.rb', line 345

def skip(type, max_depth = 64)
  raise ProtocolException.new(ProtocolException::DEPTH_LIMIT, 'Maximum skip depth exceeded') if max_depth <= 0
  case type
  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::UUID
    read_uuid
  when Types::STRUCT
    read_struct_begin
    while true
      name, type, id = read_field_begin
      break if type == Types::STOP
      skip(type, max_depth - 1)
      read_field_end
    end
    read_struct_end
  when Types::MAP
    ktype, vtype, size = read_map_begin
    validate_container_size(size)
    size.times do
      skip(ktype, max_depth - 1)
      skip(vtype, max_depth - 1)
    end
    read_map_end
  when Types::SET
    etype, size = read_set_begin
    validate_container_size(size)
    size.times do
      skip(etype, max_depth - 1)
    end
    read_set_end
  when Types::LIST
    etype, size = read_list_begin
    validate_container_size(size)
    size.times do
      skip(etype, max_depth - 1)
    end
    read_list_end
  else
    raise ProtocolException.new(ProtocolException::INVALID_DATA, 'Invalid data')
  end
end

#to_sObject



404
405
406
# File 'lib/thrift/protocol/base_protocol.rb', line 404

def to_s
  "#{trans.to_s}"
end

#validate_container_size(size) ⇒ Object

Raises:



400
401
402
# File 'lib/thrift/protocol/base_protocol.rb', line 400

def validate_container_size(size)
  raise ProtocolException.new(ProtocolException::NEGATIVE_SIZE, 'Negative size') unless size >= 0
end

#write_binary(buf) ⇒ Object

Writes a Thrift Binary (Thrift String with no encoding). The String passed will forced into BINARY encoding.

buf - The String to write.

Returns nothing.

Raises:

  • (NotImplementedError)


137
138
139
# File 'lib/thrift/protocol/base_protocol.rb', line 137

def write_binary(buf)
  raise NotImplementedError
end

#write_bool(bool) ⇒ Object

Raises:

  • (NotImplementedError)


96
97
98
# File 'lib/thrift/protocol/base_protocol.rb', line 96

def write_bool(bool)
  raise NotImplementedError
end

#write_byte(byte) ⇒ Object

Raises:

  • (NotImplementedError)


100
101
102
# File 'lib/thrift/protocol/base_protocol.rb', line 100

def write_byte(byte)
  raise NotImplementedError
end

#write_double(dub) ⇒ Object

Raises:

  • (NotImplementedError)


116
117
118
# File 'lib/thrift/protocol/base_protocol.rb', line 116

def write_double(dub)
  raise NotImplementedError
end

#write_field(*args) ⇒ Object

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.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/thrift/protocol/base_protocol.rb', line 242

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

#write_field_begin(name, type, id) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/thrift/protocol/base_protocol.rb', line 68

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

#write_field_endObject



72
# File 'lib/thrift/protocol/base_protocol.rb', line 72

def write_field_end; nil; end

#write_field_stopObject

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/thrift/protocol/base_protocol.rb', line 74

def write_field_stop
  raise NotImplementedError
end

#write_i16(i16) ⇒ Object

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/thrift/protocol/base_protocol.rb', line 104

def write_i16(i16)
  raise NotImplementedError
end

#write_i32(i32) ⇒ Object

Raises:

  • (NotImplementedError)


108
109
110
# File 'lib/thrift/protocol/base_protocol.rb', line 108

def write_i32(i32)
  raise NotImplementedError
end

#write_i64(i64) ⇒ Object

Raises:

  • (NotImplementedError)


112
113
114
# File 'lib/thrift/protocol/base_protocol.rb', line 112

def write_i64(i64)
  raise NotImplementedError
end

#write_list_begin(etype, size) ⇒ Object

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/thrift/protocol/base_protocol.rb', line 84

def write_list_begin(etype, size)
  raise NotImplementedError
end

#write_list_endObject



88
# File 'lib/thrift/protocol/base_protocol.rb', line 88

def write_list_end; nil; end

#write_map_begin(ktype, vtype, size) ⇒ Object

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/thrift/protocol/base_protocol.rb', line 78

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

#write_map_endObject



82
# File 'lib/thrift/protocol/base_protocol.rb', line 82

def write_map_end; nil; end

#write_message_begin(name, type, seqid) ⇒ Object

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/thrift/protocol/base_protocol.rb', line 56

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

#write_message_endObject



60
# File 'lib/thrift/protocol/base_protocol.rb', line 60

def write_message_end; nil; end

#write_set_begin(etype, size) ⇒ Object

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/thrift/protocol/base_protocol.rb', line 90

def write_set_begin(etype, size)
  raise NotImplementedError
end

#write_set_endObject



94
# File 'lib/thrift/protocol/base_protocol.rb', line 94

def write_set_end; nil; end

#write_string(str) ⇒ Object

Writes a Thrift String. 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.

Raises:

  • (NotImplementedError)


127
128
129
# File 'lib/thrift/protocol/base_protocol.rb', line 127

def write_string(str)
  raise NotImplementedError
end

#write_struct_begin(name) ⇒ Object

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/thrift/protocol/base_protocol.rb', line 62

def write_struct_begin(name)
  raise NotImplementedError
end

#write_struct_endObject



66
# File 'lib/thrift/protocol/base_protocol.rb', line 66

def write_struct_end; nil; end

#write_type(field_info, value) ⇒ Object

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.

Returns nothing.



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
# File 'lib/thrift/protocol/base_protocol.rb', line 270

def write_type(field_info, value)
  # if field_info is a Integer, assume it is a Thrift::Types constant
  # convert it into a field_info Hash for backwards compatibility
  if field_info.is_a? Integer
    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::UUID
    write_uuid(value)
  when Types::STRUCT
    value.write(self)
  else
    raise NotImplementedError
  end
end

#write_uuid(uuid) ⇒ Object

Writes a UUID as 16 bytes.

uuid - The UUID string to write (e.g. "550e8400-e29b-41d4-a716-446655440000").

Returns nothing.

Raises:

  • (NotImplementedError)


146
147
148
# File 'lib/thrift/protocol/base_protocol.rb', line 146

def write_uuid(uuid)
  raise NotImplementedError
end