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, #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)



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

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)



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

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

#clear_headersObject

Clears all write headers



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

def clear_headers
  @header_transport.clear_headers
end

#get_headersObject

Returns headers read from the last message



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

def get_headers
  @header_transport.get_headers
end

#read_binaryObject



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

def read_binary
  @protocol.read_binary
end

#read_boolObject



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

def read_bool
  @protocol.read_bool
end

#read_byteObject



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

def read_byte
  @protocol.read_byte
end

#read_doubleObject



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

def read_double
  @protocol.read_double
end

#read_field_beginObject



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

def read_field_begin
  @protocol.read_field_begin
end

#read_field_endObject



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

def read_field_end
  @protocol.read_field_end
end

#read_i16Object



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

def read_i16
  @protocol.read_i16
end

#read_i32Object



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

def read_i32
  @protocol.read_i32
end

#read_i64Object



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

def read_i64
  @protocol.read_i64
end

#read_list_beginObject



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

def read_list_begin
  @protocol.read_list_begin
end

#read_list_endObject



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

def read_list_end
  @protocol.read_list_end
end

#read_map_beginObject



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

def read_map_begin
  @protocol.read_map_begin
end

#read_map_endObject



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

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



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

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



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

def read_message_end
  @protocol.read_message_end
end

#read_set_beginObject



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

def read_set_begin
  @protocol.read_set_begin
end

#read_set_endObject



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

def read_set_end
  @protocol.read_set_end
end

#read_stringObject



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

def read_string
  @protocol.read_string
end

#read_struct_beginObject



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

def read_struct_begin
  @protocol.read_struct_begin
end

#read_struct_endObject



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

def read_struct_end
  @protocol.read_struct_end
end

#read_uuidObject



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

def read_uuid
  @protocol.read_uuid
end

#set_header(key, value) ⇒ Object

Sets a header to be sent with the next message



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

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

#to_sObject



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

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

#transObject

Returns the HeaderTransport



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

def trans
  @header_transport
end

#write_binary(buf) ⇒ Object



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

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

#write_bool(bool) ⇒ Object



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

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

#write_byte(byte) ⇒ Object



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

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

#write_double(dub) ⇒ Object



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

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

#write_field_begin(name, type, id) ⇒ Object



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

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

#write_field_endObject



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

def write_field_end
  @protocol.write_field_end
end

#write_field_stopObject



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

def write_field_stop
  @protocol.write_field_stop
end

#write_i16(i16) ⇒ Object



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

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

#write_i32(i32) ⇒ Object



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

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

#write_i64(i64) ⇒ Object



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

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

#write_list_begin(etype, size) ⇒ Object



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

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

#write_list_endObject



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

def write_list_end
  @protocol.write_list_end
end

#write_map_begin(ktype, vtype, size) ⇒ Object



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

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

#write_map_endObject



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

def write_map_end
  @protocol.write_map_end
end

#write_message_begin(name, type, seqid) ⇒ Object

Write methods - delegate to underlying protocol



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

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

#write_message_endObject



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

def write_message_end
  @protocol.write_message_end
end

#write_set_begin(etype, size) ⇒ Object



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

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

#write_set_endObject



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

def write_set_end
  @protocol.write_set_end
end

#write_string(str) ⇒ Object



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

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

#write_struct_begin(name) ⇒ Object



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

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

#write_struct_endObject



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

def write_struct_end
  @protocol.write_struct_end
end

#write_uuid(uuid) ⇒ Object



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

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