Module: Thrift::Processor

Defined in:
lib/thrift/processor.rb

Instance Method Summary collapse

Instance Method Details

#initialize(handler, logger = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/thrift/processor.rb', line 24

def initialize(handler, logger=nil)
  @handler = handler
  if logger.nil?
    @logger = Logger.new(STDERR)
    @logger.level = Logger::WARN
  else
    @logger = logger
  end
end

#process(iprot, oprot) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/thrift/processor.rb', line 34

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
      x = ApplicationException.new(ApplicationException::INTERNAL_ERROR, 'Internal error')
      @logger.debug "Internal error : #{e.message}\n#{e.backtrace.join("\n")}"
      write_error(x, oprot, name, seqid)
    end
    true
  else
    iprot.skip(Types::STRUCT)
    iprot.read_message_end
    x = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, 'Unknown function '+name)
    write_error(x, oprot, name, seqid)
    false
  end
end

#read_args(iprot, args_class) ⇒ Object



54
55
56
57
58
59
# File 'lib/thrift/processor.rb', line 54

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

#write_error(err, oprot, name, seqid) ⇒ Object



68
69
70
71
72
73
# File 'lib/thrift/processor.rb', line 68

def write_error(err, oprot, name, seqid)
  oprot.write_message_begin(name, MessageTypes::EXCEPTION, seqid)
  err.write(oprot)
  oprot.write_message_end
  oprot.trans.flush
end

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



61
62
63
64
65
66
# File 'lib/thrift/processor.rb', line 61

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