Class: ThriftClient::Simple::ThriftService

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

Direct Known Subclasses

Gizzard::GizzmoService

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, framed = false) ⇒ ThriftService

Returns a new instance of ThriftService.



294
295
296
297
298
# File 'lib/vendor/thrift_client/simple.rb', line 294

def initialize(host, port, framed = false)
  @host = host
  @port = port
  @framed = framed
end

Class Method Details

._arg_structsObject



300
301
302
303
# File 'lib/vendor/thrift_client/simple.rb', line 300

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

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



305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/vendor/thrift_client/simple.rb', line 305

def self.thrift_method(name, rtype, *args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  fields = [ ThriftClient::Simple::Field.new(:rv, rtype, 0),
             (options[:throws] ? ThriftClient::Simple::Field.new(:ex, options[:throws], 1) : nil)
           ].compact

  arg_struct = ThriftClient::Simple.make_struct("Args__#{self.name}__#{name}", *args)
  rv_struct = ThriftClient::Simple.make_struct("Retval__#{self.name}__#{name}", *fields)

  _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



320
321
322
323
324
325
326
327
328
329
# File 'lib/vendor/thrift_client/simple.rb', line 320

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 = TCPSocket.new(@host, @port)
  sock.write(ThriftClient::Simple.pack_request(method_name, arg_struct, @framed))
  rv = ThriftClient::Simple.read_response(sock, rv_class, @framed)
  sock.close
  rv[2]
end