| 30 |
ashish |
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 |
module Thrift
|
|
|
21 |
module Client
|
|
|
22 |
def initialize(iprot, oprot=nil)
|
|
|
23 |
@iprot = iprot
|
|
|
24 |
@oprot = oprot || iprot
|
|
|
25 |
@seqid = 0
|
|
|
26 |
end
|
|
|
27 |
|
|
|
28 |
def send_message(name, args_class, args = {})
|
|
|
29 |
@oprot.write_message_begin(name, MessageTypes::CALL, @seqid)
|
|
|
30 |
data = args_class.new
|
|
|
31 |
args.each do |k, v|
|
|
|
32 |
data.send("#{k.to_s}=", v)
|
|
|
33 |
end
|
|
|
34 |
begin
|
|
|
35 |
data.write(@oprot)
|
|
|
36 |
rescue StandardError => e
|
|
|
37 |
@oprot.trans.close
|
|
|
38 |
raise e
|
|
|
39 |
end
|
|
|
40 |
@oprot.write_message_end
|
|
|
41 |
@oprot.trans.flush
|
|
|
42 |
end
|
|
|
43 |
|
|
|
44 |
def receive_message(result_klass)
|
|
|
45 |
fname, mtype, rseqid = @iprot.read_message_begin
|
|
|
46 |
handle_exception(mtype)
|
|
|
47 |
result = result_klass.new
|
|
|
48 |
result.read(@iprot)
|
|
|
49 |
@iprot.read_message_end
|
|
|
50 |
result
|
|
|
51 |
end
|
|
|
52 |
|
|
|
53 |
def handle_exception(mtype)
|
|
|
54 |
if mtype == MessageTypes::EXCEPTION
|
|
|
55 |
x = ApplicationException.new
|
|
|
56 |
x.read(@iprot)
|
|
|
57 |
@iprot.read_message_end
|
|
|
58 |
raise x
|
|
|
59 |
end
|
|
|
60 |
end
|
|
|
61 |
end
|
|
|
62 |
end
|