Class: Thrift::HeaderProtocol

Inherits:
BaseProtocol show all
Defined in:
lib/thrift/protocol/header_protocol.rb

Overview

HeaderProtocol is a protocol that wraps HeaderTransport and delegates to either BinaryProtocol or CompactProtocol based on auto-detection.

It provides access to header management (get_headers, set_header, etc.) through the underlying HeaderTransport.

Example usage:

socket = Thrift::Socket.new('localhost', 9090)
protocol = Thrift::HeaderProtocol.new(socket)
client = MyService::Client.new(protocol)
protocol.trans.open
client.some_method()
protocol.trans.close

Instance Method Summary collapse

Methods inherited from BaseProtocol

#native?, #read_type, #skip, #validate_container_size, #write_field, #write_type

Constructor Details

#initialize(transport, allowed_client_types = nil, default_protocol = HeaderSubprotocolID::COMPACT) ⇒ HeaderProtocol

Creates a new HeaderProtocol.

Parameters:

  • transport (BaseTransport, HeaderTransport)

    The transport to wrap. If not already a HeaderTransport, it will be wrapped in one.

  • allowed_client_types (Array<Integer>) (defaults to: nil)

    Allowed client types for auto-detection

  • default_protocol (Integer) (defaults to: HeaderSubprotocolID::COMPACT)

    Default protocol ID (BINARY or COMPACT)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/thrift/protocol/header_protocol.rb', line 44

def initialize(transport, allowed_client_types = nil, default_protocol = HeaderSubprotocolID::COMPACT)
  # Wrap transport in HeaderTransport if not already wrapped
  if transport.is_a?(HeaderTransport)
    @header_transport = transport
  else
    @header_transport = HeaderTransport.new(transport, allowed_client_types, default_protocol)
  end

  @default_protocol = default_protocol
  @current_protocol_id = default_protocol

  # Create initial protocol
  @protocol = create_protocol(@current_protocol_id)
end

Instance Method Details

#add_transform(transform_id) ⇒ Object

Adds a transform (e.g., ZLIB compression)



80
81
82
# File 'lib/thrift/protocol/header_protocol.rb', line 80

def add_transform(transform_id)
  @header_transport.add_transform(transform_id)
end

#clear_headersObject

Clears all write headers



75
76
77
# File 'lib/thrift/protocol/header_protocol.rb', line 75

def clear_headers
  @header_transport.clear_headers
end

#get_headersObject

Returns headers read from the last message



65
66
67
# File 'lib/thrift/protocol/header_protocol.rb', line 65

def get_headers
  @header_transport.get_headers
end

#read_binaryObject



263
264
265
# File 'lib/thrift/protocol/header_protocol.rb', line 263

def read_binary
  @protocol.read_binary
end

#read_boolObject



235
236
237
# File 'lib/thrift/protocol/header_protocol.rb', line 235

def read_bool
  @protocol.read_bool
end

#read_byteObject



239
240
241
# File 'lib/thrift/protocol/header_protocol.rb', line 239

def read_byte
  @protocol.read_byte
end

#read_doubleObject



255
256
257
# File 'lib/thrift/protocol/header_protocol.rb', line 255

def read_double
  @protocol.read_double
end

#read_field_beginObject



203
204
205
# File 'lib/thrift/protocol/header_protocol.rb', line 203

def read_field_begin
  @protocol.read_field_begin
end

#read_field_endObject



207
208
209
# File 'lib/thrift/protocol/header_protocol.rb', line 207

def read_field_end
  @protocol.read_field_end
end

#read_i16Object



243
244
245
# File 'lib/thrift/protocol/header_protocol.rb', line 243

def read_i16
  @protocol.read_i16
end

#read_i32Object



247
248
249
# File 'lib/thrift/protocol/header_protocol.rb', line 247

def read_i32
  @protocol.read_i32
end

#read_i64Object



251
252
253
# File 'lib/thrift/protocol/header_protocol.rb', line 251

def read_i64
  @protocol.read_i64
end

#read_list_beginObject



219
220
221
# File 'lib/thrift/protocol/header_protocol.rb', line 219

def read_list_begin
  @protocol.read_list_begin
end

#read_list_endObject



223
224
225
# File 'lib/thrift/protocol/header_protocol.rb', line 223

def read_list_end
  @protocol.read_list_end
end

#read_map_beginObject



211
212
213
# File 'lib/thrift/protocol/header_protocol.rb', line 211

def read_map_begin
  @protocol.read_map_begin
end

#read_map_endObject



215
216
217
# File 'lib/thrift/protocol/header_protocol.rb', line 215

def read_map_end
  @protocol.read_map_end
end

#read_message_beginObject

Read methods - delegate to underlying protocol read_message_begin handles protocol switching after detection



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/thrift/protocol/header_protocol.rb', line 176

def read_message_begin
  begin
    @header_transport.reset_protocol
    reset_protocol_if_needed
  rescue ProtocolException => ex
    app_ex = ApplicationException.new(ApplicationException::INVALID_PROTOCOL, ex.message)
    write_message_begin("", MessageTypes::EXCEPTION, 0)
    app_ex.write(self)
    write_message_end
    @header_transport.flush
    raise ex
  end
  @protocol.read_message_begin
end

#read_message_endObject



191
192
193
# File 'lib/thrift/protocol/header_protocol.rb', line 191

def read_message_end
  @protocol.read_message_end
end

#read_set_beginObject



227
228
229
# File 'lib/thrift/protocol/header_protocol.rb', line 227

def read_set_begin
  @protocol.read_set_begin
end

#read_set_endObject



231
232
233
# File 'lib/thrift/protocol/header_protocol.rb', line 231

def read_set_end
  @protocol.read_set_end
end

#read_stringObject



259
260
261
# File 'lib/thrift/protocol/header_protocol.rb', line 259

def read_string
  @protocol.read_string
end

#read_struct_beginObject



195
196
197
# File 'lib/thrift/protocol/header_protocol.rb', line 195

def read_struct_begin
  @protocol.read_struct_begin
end

#read_struct_endObject



199
200
201
# File 'lib/thrift/protocol/header_protocol.rb', line 199

def read_struct_end
  @protocol.read_struct_end
end

#read_uuidObject



267
268
269
# File 'lib/thrift/protocol/header_protocol.rb', line 267

def read_uuid
  @protocol.read_uuid
end

#set_header(key, value) ⇒ Object

Sets a header to be sent with the next message



70
71
72
# File 'lib/thrift/protocol/header_protocol.rb', line 70

def set_header(key, value)
  @header_transport.set_header(key, value)
end

#to_sObject



271
272
273
# File 'lib/thrift/protocol/header_protocol.rb', line 271

def to_s
  "header(#{@protocol.to_s})"
end

#transObject

Returns the HeaderTransport



60
61
62
# File 'lib/thrift/protocol/header_protocol.rb', line 60

def trans
  @header_transport
end

#write_binary(buf) ⇒ Object



166
167
168
# File 'lib/thrift/protocol/header_protocol.rb', line 166

def write_binary(buf)
  @protocol.write_binary(buf)
end

#write_bool(bool) ⇒ Object



138
139
140
# File 'lib/thrift/protocol/header_protocol.rb', line 138

def write_bool(bool)
  @protocol.write_bool(bool)
end

#write_byte(byte) ⇒ Object



142
143
144
# File 'lib/thrift/protocol/header_protocol.rb', line 142

def write_byte(byte)
  @protocol.write_byte(byte)
end

#write_double(dub) ⇒ Object



158
159
160
# File 'lib/thrift/protocol/header_protocol.rb', line 158

def write_double(dub)
  @protocol.write_double(dub)
end

#write_field_begin(name, type, id) ⇒ Object



102
103
104
# File 'lib/thrift/protocol/header_protocol.rb', line 102

def write_field_begin(name, type, id)
  @protocol.write_field_begin(name, type, id)
end

#write_field_endObject



106
107
108
# File 'lib/thrift/protocol/header_protocol.rb', line 106

def write_field_end
  @protocol.write_field_end
end

#write_field_stopObject



110
111
112
# File 'lib/thrift/protocol/header_protocol.rb', line 110

def write_field_stop
  @protocol.write_field_stop
end

#write_i16(i16) ⇒ Object



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

def write_i16(i16)
  @protocol.write_i16(i16)
end

#write_i32(i32) ⇒ Object



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

def write_i32(i32)
  @protocol.write_i32(i32)
end

#write_i64(i64) ⇒ Object



154
155
156
# File 'lib/thrift/protocol/header_protocol.rb', line 154

def write_i64(i64)
  @protocol.write_i64(i64)
end

#write_list_begin(etype, size) ⇒ Object



122
123
124
# File 'lib/thrift/protocol/header_protocol.rb', line 122

def write_list_begin(etype, size)
  @protocol.write_list_begin(etype, size)
end

#write_list_endObject



126
127
128
# File 'lib/thrift/protocol/header_protocol.rb', line 126

def write_list_end
  @protocol.write_list_end
end

#write_map_begin(ktype, vtype, size) ⇒ Object



114
115
116
# File 'lib/thrift/protocol/header_protocol.rb', line 114

def write_map_begin(ktype, vtype, size)
  @protocol.write_map_begin(ktype, vtype, size)
end

#write_map_endObject



118
119
120
# File 'lib/thrift/protocol/header_protocol.rb', line 118

def write_map_end
  @protocol.write_map_end
end

#write_message_begin(name, type, seqid) ⇒ Object

Write methods - delegate to underlying protocol



85
86
87
88
# File 'lib/thrift/protocol/header_protocol.rb', line 85

def write_message_begin(name, type, seqid)
  @header_transport.sequence_id = seqid
  @protocol.write_message_begin(name, type, seqid)
end

#write_message_endObject



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

def write_message_end
  @protocol.write_message_end
end

#write_set_begin(etype, size) ⇒ Object



130
131
132
# File 'lib/thrift/protocol/header_protocol.rb', line 130

def write_set_begin(etype, size)
  @protocol.write_set_begin(etype, size)
end

#write_set_endObject



134
135
136
# File 'lib/thrift/protocol/header_protocol.rb', line 134

def write_set_end
  @protocol.write_set_end
end

#write_string(str) ⇒ Object



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

def write_string(str)
  @protocol.write_string(str)
end

#write_struct_begin(name) ⇒ Object



94
95
96
# File 'lib/thrift/protocol/header_protocol.rb', line 94

def write_struct_begin(name)
  @protocol.write_struct_begin(name)
end

#write_struct_endObject



98
99
100
# File 'lib/thrift/protocol/header_protocol.rb', line 98

def write_struct_end
  @protocol.write_struct_end
end

#write_uuid(uuid) ⇒ Object



170
171
172
# File 'lib/thrift/protocol/header_protocol.rb', line 170

def write_uuid(uuid)
  @protocol.write_uuid(uuid)
end