Module: Thrift::Client

Included in:
BaseClient
Defined in:
lib/thrift/client.rb

Instance Method Summary collapse

Instance Method Details

#handle_exception(mtype) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/thrift/client.rb', line 76

def handle_exception(mtype)
  return if mtype != MessageTypes::EXCEPTION

  x = ApplicationException.new
  x.read(@iprot)
  @iprot.read_message_end
  raise x
end

#initialize(iprot, oprot = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/thrift/client.rb', line 23

def initialize(iprot, oprot = nil)
  @iprot = iprot
  @oprot = oprot || iprot
  @seqid = 0
  @mutex = Mutex.new
  @cond = ConditionVariable.new
  @ready = true
end

#receive_message(result_klass, name = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/thrift/client.rb', line 51

def receive_message(result_klass, name=nil)
  fname, mtype, seqid = @iprot.read_message_begin
  handle_exception(mtype)

  raise ApplicationException.new(
    ApplicationException::BAD_SEQUENCE_ID,
    'out of seq'
  ) if seqid != @seqid

  raise ApplicationException.new(
    ApplicationException::INVALID_MESSAGE_TYPE,
    'invalid message type'
  ) if mtype != MessageTypes::REPLY

  raise ApplicationException.new(
    ApplicationException::WRONG_METHOD_NAME,
    'wrong method name'
  ) if !name.nil? && name != fname

  result = result_klass.new
  result.read(@iprot)
  @iprot.read_message_end
  result
end

#send_message(name, args_class, args = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/thrift/client.rb', line 32

def send_message(name, args_class, args = {})
  @seqid += 1
  @oprot.write_message_begin(name, MessageTypes::CALL, @seqid)
  data = args_class.new
  args.each { |k, v| data.send("#{k}=", v) }
  send_message_instance(data)
end

#send_message_instance(data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/thrift/client.rb', line 40

def send_message_instance(data)
  begin
    data.write(@oprot)
  rescue StandardError => e
    @oprot.trans.close
    raise e
  end
  @oprot.write_message_end
  @oprot.trans.flush
end