Module: Thrift::Processor

Defined in:
lib/thrift/processor.rb

Instance Method Summary collapse

Instance Method Details

#initialize(handler, middlewares = []) ⇒ Object



22
23
24
25
# File 'lib/thrift/processor.rb', line 22

def initialize(handler, middlewares = [])
  @handler = handler
  @middleware = Middleware.wrap(middlewares)
end

#process(iprot, oprot) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/thrift/processor.rb', line 27

def process(iprot, oprot)
  name, _type, seqid = iprot.read_message_begin
  if respond_to?("process_#{name}")
    begin
      send("process_#{name}", seqid, iprot, oprot)
    rescue => e
      write_exception(e, oprot, name, seqid)
    end
    true
  else
    iprot.skip(Types::STRUCT)
    iprot.read_message_end
    write_exception(
      ApplicationException.new(
        ApplicationException::UNKNOWN_METHOD,
        'Unknown function ' + name,
      ),
      oprot,
      name,
      seqid
    )
    false
  end
end

#read_args(iprot, args_class) ⇒ Object



52
53
54
55
56
57
# File 'lib/thrift/processor.rb', line 52

def read_args(iprot, args_class)
  args = args_class.new
  args.read(iprot)
  iprot.read_message_end
  args
end

#write_exception(exception, oprot, name, seqid) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/thrift/processor.rb', line 59

def write_exception(exception, oprot, name, seqid)
  oprot.write_message_begin(name, MessageTypes::EXCEPTION, seqid)

  unless exception.is_a? ApplicationException
    exception = ApplicationException.new(
      ApplicationException::INTERNAL_ERROR,
      "Internal error processing #{name}: #{exception.class}: #{exception}"
    )
  end

  exception.write(oprot)
  oprot.write_message_end
  oprot.trans.flush
end

#write_result(result, oprot, name, seqid) ⇒ Object



74
75
76
77
78
79
# File 'lib/thrift/processor.rb', line 74

def write_result(result, oprot, name, seqid)
  oprot.write_message_begin(name, MessageTypes::REPLY, seqid)
  result.write(oprot)
  oprot.write_message_end
  oprot.trans.flush
end