Module: Thrift::Processor

Defined in:
lib/thrift/processor.rb

Defined Under Namespace

Classes: BaseProcessorFunction, BinaryProcessorFunction, UnaryProcessorFunction

Instance Method Summary collapse

Instance Method Details

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/thrift/processor.rb', line 98

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

  @functions = if self.class.const_defined?(:METHODS)
    self.class::METHODS.reduce({}) do |acc, (key, args)|
      klass = args[:oneway] ? UnaryProcessorFunction : BinaryProcessorFunction

      acc.merge key => klass.new(
        key,
        @middleware,
        args[:args_klass],
        method("execute_#{key}")
      )
    end
  end || {}
end

#process(iprot, oprot) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/thrift/processor.rb', line 116

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

  func = @functions[name]

  return func.process(seqid, iprot, oprot) if func

  # TODO: once all the stubs will be generated w thrift >=2.5 the next lines
  # can be deleted
  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



148
149
150
151
152
153
# File 'lib/thrift/processor.rb', line 148

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



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

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



170
171
172
173
174
175
# File 'lib/thrift/processor.rb', line 170

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