]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | # |
2 | # Licensed to the Apache Software Foundation (ASF) under one | |
3 | # or more contributor license agreements. See the NOTICE file | |
4 | # distributed with this work for additional information | |
5 | # regarding copyright ownership. The ASF licenses this file | |
6 | # to you under the Apache License, Version 2.0 (the | |
7 | # "License"); you may not use this file except in compliance | |
8 | # with the License. You may obtain a copy of the License at | |
9 | # | |
10 | # http://www.apache.org/licenses/LICENSE-2.0 | |
11 | # | |
12 | # Unless required by applicable law or agreed to in writing, | |
13 | # software distributed under the License is distributed on an | |
14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | # KIND, either express or implied. See the License for the | |
16 | # specific language governing permissions and limitations | |
17 | # under the License. | |
18 | # | |
19 | ||
20 | require 'logger' | |
21 | ||
22 | module Thrift | |
23 | module Processor | |
24 | def initialize(handler, logger=nil) | |
25 | @handler = handler | |
26 | if logger.nil? | |
27 | @logger = Logger.new(STDERR) | |
28 | @logger.level = Logger::WARN | |
29 | else | |
30 | @logger = logger | |
31 | end | |
32 | end | |
33 | ||
34 | def process(iprot, oprot) | |
35 | name, type, seqid = iprot.read_message_begin | |
36 | if respond_to?("process_#{name}") | |
37 | begin | |
38 | send("process_#{name}", seqid, iprot, oprot) | |
39 | rescue => e | |
40 | x = ApplicationException.new(ApplicationException::INTERNAL_ERROR, 'Internal error') | |
41 | @logger.debug "Internal error : #{e.message}\n#{e.backtrace.join("\n")}" | |
42 | write_error(x, oprot, name, seqid) | |
43 | end | |
44 | true | |
45 | else | |
46 | iprot.skip(Types::STRUCT) | |
47 | iprot.read_message_end | |
48 | x = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, 'Unknown function '+name) | |
49 | write_error(x, oprot, name, seqid) | |
50 | false | |
51 | end | |
52 | end | |
53 | ||
54 | def read_args(iprot, args_class) | |
55 | args = args_class.new | |
56 | args.read(iprot) | |
57 | iprot.read_message_end | |
58 | args | |
59 | end | |
60 | ||
61 | def write_result(result, oprot, name, seqid) | |
62 | oprot.write_message_begin(name, MessageTypes::REPLY, seqid) | |
63 | result.write(oprot) | |
64 | oprot.write_message_end | |
65 | oprot.trans.flush | |
66 | end | |
67 | ||
68 | def write_error(err, oprot, name, seqid) | |
69 | oprot.write_message_begin(name, MessageTypes::EXCEPTION, seqid) | |
70 | err.write(oprot) | |
71 | oprot.write_message_end | |
72 | oprot.trans.flush | |
73 | end | |
74 | end | |
75 | end |