Class: ThriftClient::Simple::ThriftService

Inherits:
Object
  • Object
show all
Defined in:
lib/thrift_client/simple.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock) ⇒ ThriftService

Returns a new instance of ThriftService.



236
237
238
# File 'lib/thrift_client/simple.rb', line 236

def initialize(sock)
  @sock = sock
end

Class Method Details

._arg_structsObject



240
241
242
243
# File 'lib/thrift_client/simple.rb', line 240

def self._arg_structs
  @_arg_structs = {} if @_arg_structs.nil?
  @_arg_structs
end

.thrift_method(name, rtype, *args) ⇒ Object



245
246
247
248
249
250
251
252
# File 'lib/thrift_client/simple.rb', line 245

def self.thrift_method(name, rtype, *args)
  arg_struct = ThriftClient::Simple.make_struct("Args__#{self.name}__#{name}", *args)
  rv_struct = ThriftClient::Simple.make_struct("Retval__#{self.name}__#{name}", ThriftClient::Simple::Field.new(:rv, rtype, 0))
  _arg_structs[name.to_sym] = [ arg_struct, rv_struct ]

  arg_names = args.map { |a| a.name.to_s }.join(", ")
  class_eval "def #{name}(#{arg_names}); _proxy(:#{name}#{args.size > 0 ? ', ' : ''}#{arg_names}); end"
end

Instance Method Details

#_proxy(method_name, *args) ⇒ Object



254
255
256
257
258
259
260
261
# File 'lib/thrift_client/simple.rb', line 254

def _proxy(method_name, *args)
  cls = self.class.ancestors.find { |cls| cls.respond_to?(:_arg_structs) and cls._arg_structs[method_name.to_sym] }
  arg_class, rv_class = cls._arg_structs[method_name.to_sym]
  arg_struct = arg_class.new(*args)
  @sock.write(ThriftClient::Simple.pack_request(method_name, arg_struct))
  rv = ThriftClient::Simple.read_response(@sock, rv_class)
  rv[2]
end