Module: Thrift::Processor

Included in:
Types::Plugin::Plugin::Processor, Types::Plugin::Plugin::Processor
Defined in:
lib/thrift/processor.rb

Defined Under Namespace

Classes: BaseProcessor, BaseProcessorFunction, BaseStreamProcessor, BidiStreamProcessor, BinaryProcessor, BinaryProcessorFunction, InboundStreamProcessor, OutboundStreamProcessor, UnaryProcessor, UnaryProcessorFunction, UnkwonFunctionProcessor

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/thrift/processor.rb', line 139

def self.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_internal_error(exception, oprot, name, seqid) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/thrift/processor.rb', line 154

def self.write_internal_error(exception, oprot, name, seqid)
  write_exception(
    ApplicationException.new(
      ApplicationException::INTERNAL_ERROR,
      "Internal error processing #{name}: #{exception.class}: #{exception}"
    ),
    oprot,
    name,
    seqid
  )
end

Instance Method Details

#build_processor(name, info) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/thrift/processor.rb', line 166

def build_processor(name, info)
  if info[:result_klass].nil? && info[:oneway]
    UnaryProcessor
  elsif info[:stream_klass].nil? && info[:sink_klass].nil?
    BinaryProcessor
  elsif info[:sink_klass].nil?
    OutboundStreamProcessor
  elsif info[:stream_klass].nil?
    InboundStreamProcessor
  else
    BidiStreamProcessor
  end.new(name, info, @middleware, @handler)
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/thrift/processor.rb', line 85

def initialize(handler, middlewares = [], logger=nil)
  @handler = handler
  if logger.nil?
    @logger = Logger.new(STDERR)
    @logger.level = Logger::WARN
  else
    @logger = logger
  end
  @middleware = Middleware.wrap(middlewares)

  @processors = if self.class.const_defined? :METHODS
                  self.class::METHODS.reduce({}) do |acc, (name, info)|
                    acc.merge(name => build_processor(name, info))
                  end
                else
                  {}
                end
end

#process(iprot, oprot) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/thrift/processor.rb', line 118

def process(iprot, oprot)
  name, type, seqid = iprot.read_message_begin

  mth = "process_#{name}"
  if respond_to?(mth)
    begin
      send(mth, seqid, iprot, oprot)
    rescue StandardError => e
      raise e if type == MessageTypes::ONEWAY

      Processor.write_internal_error(e, oprot, name, seqid)
    end

    return true
  end

  (
    @processors[name] || UnkwonFunctionProcessor.new(name)
  ).process(seqid, iprot, oprot)
end

#read_args(iprot, args_class) ⇒ Object



104
105
106
107
108
109
# File 'lib/thrift/processor.rb', line 104

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

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



111
112
113
114
115
116
# File 'lib/thrift/processor.rb', line 111

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