Module: Thrift::Client

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

Instance Method Summary collapse

Instance Method Details

#handle_exception(mtype) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/thrift/client.rb', line 87

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
# File 'lib/thrift/client.rb', line 23

def initialize(iprot, oprot = nil)
  @iprot = iprot
  @oprot = oprot || iprot
  @seqid = 0
end

#receive_message(result_klass, name = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/thrift/client.rb', line 62

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



29
30
31
32
33
# File 'lib/thrift/client.rb', line 29

def send_message(name, args_class, args = {})
  @seqid += 1
  @oprot.write_message_begin(name, MessageTypes::CALL, @seqid)
  send_message_args(args_class, args)
end

#send_message_args(args_class, args) ⇒ Object



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

def send_message_args(args_class, args)
  data = args_class.new

  args.each do |k, v|
    data.send("#{k.to_s}=", v)
  end

  send_message_instance(data)
end

#send_message_instance(data) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/thrift/client.rb', line 51

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

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



35
36
37
38
39
# File 'lib/thrift/client.rb', line 35

def send_oneway_message(name, args_class, args = {})
  @seqid += 1
  @oprot.write_message_begin(name, MessageTypes::ONEWAY, @seqid)
  send_message_args(args_class, args)
end