]>
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 | require 'thrift/protocol/protocol_decorator' | |
20 | require 'thrift/protocol/base_protocol' | |
21 | ||
22 | module Thrift | |
23 | class MultiplexedProcessor | |
24 | def initialize | |
25 | @actual_processors = {} | |
26 | end | |
27 | ||
28 | def register_processor(service_name, processor) | |
29 | @actual_processors[service_name] = processor | |
30 | end | |
31 | ||
32 | def process(iprot, oprot) | |
33 | name, type, seqid = iprot.read_message_begin | |
34 | check_type(type) | |
35 | check_separator(name) | |
36 | service_name, method = name.split(':') | |
37 | processor(service_name).process(StoredMessageProtocol.new(iprot, [method, type, seqid]), oprot) | |
38 | end | |
39 | ||
40 | protected | |
41 | ||
42 | def processor(service_name) | |
43 | if @actual_processors.has_key?(service_name) | |
44 | @actual_processors[service_name] | |
45 | else | |
46 | raise Thrift::Exception.new("Service name not found: #{service_name}. Did you forget to call #{self.class.name}#register_processor?") | |
47 | end | |
48 | end | |
49 | ||
50 | def check_type(type) | |
51 | unless [MessageTypes::CALL, MessageTypes::ONEWAY].include?(type) | |
52 | raise Thrift::Exception.new('This should not have happened!?') | |
53 | end | |
54 | end | |
55 | ||
56 | def check_separator(name) | |
57 | if name.count(':') < 1 | |
58 | raise Thrift::Exception.new("Service name not found in message name: #{name}. Did you forget to use a Thrift::Protocol::MultiplexedProtocol in your client?") | |
59 | end | |
60 | end | |
61 | end | |
62 | ||
63 | class StoredMessageProtocol < BaseProtocol | |
64 | ||
65 | include ProtocolDecorator | |
66 | ||
67 | def initialize(protocol, message_begin) | |
68 | super(protocol) | |
69 | @message_begin = message_begin | |
70 | end | |
71 | ||
72 | def read_message_begin | |
73 | @message_begin | |
74 | end | |
75 | end | |
76 | end |