blob: 5c74adc75b85386b3951f224ae5858e027c04991 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
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
David Reiss2c534032008-06-11 00:58:00 +000020-module(thrift_client).
21
David Reiss2c534032008-06-11 00:58:00 +000022%% API
David Reiss3f660a42010-08-30 22:05:29 +000023-export([new/2, call/3, send_call/3, close/1]).
David Reiss2c534032008-06-11 00:58:00 +000024
25-include("thrift_constants.hrl").
26-include("thrift_protocol.hrl").
27
David Reiss3f660a42010-08-30 22:05:29 +000028-record(tclient, {service, protocol, seqid}).
David Reissfc427af2008-06-11 01:11:57 +000029
30
David Reiss3f660a42010-08-30 22:05:29 +000031new(Protocol, Service)
32 when is_atom(Service) ->
33 {ok, #tclient{protocol = Protocol,
34 service = Service,
35 seqid = 0}}.
David Reiss5e530af2009-06-04 02:01:24 +000036
David Reissf4494ee2010-08-30 22:06:03 +000037-spec call(#tclient{}, atom(), list()) -> {#tclient{}, {ok, any()} | {error, any()}}.
David Reiss3f660a42010-08-30 22:05:29 +000038call(Client = #tclient{}, Function, Args)
39 when is_atom(Function), is_list(Args) ->
40 case send_function_call(Client, Function, Args) of
41 {Client1, ok} ->
42 receive_function_result(Client1, Function);
43 Else ->
44 Else
David Reisse5a4d0c2008-06-11 01:02:10 +000045 end.
David Reiss2c534032008-06-11 00:58:00 +000046
David Reissa2f45972008-06-11 01:13:33 +000047
David Reiss65cf7202008-06-11 01:12:20 +000048%% Sends a function call but does not read the result. This is useful
David Reissc51986f2009-03-24 20:01:25 +000049%% if you're trying to log non-oneway function calls to write-only
David Reiss65cf7202008-06-11 01:12:20 +000050%% transports like thrift_disk_log_transport.
David Reiss3f660a42010-08-30 22:05:29 +000051-spec send_call(#tclient{}, atom(), list()) -> {#tclient{}, ok}.
52send_call(Client = #tclient{}, Function, Args)
53 when is_atom(Function), is_list(Args) ->
54 send_function_call(Client, Function, Args).
David Reiss65cf7202008-06-11 01:12:20 +000055
David Reiss3f660a42010-08-30 22:05:29 +000056-spec close(#tclient{}) -> ok.
57close(#tclient{protocol=Protocol}) ->
58 thrift_protocol:close_transport(Protocol).
David Reiss464e3002008-06-11 01:00:45 +000059
David Reiss2c534032008-06-11 00:58:00 +000060
61%%--------------------------------------------------------------------
62%%% Internal functions
63%%--------------------------------------------------------------------
David Reissf4494ee2010-08-30 22:06:03 +000064-spec send_function_call(#tclient{}, atom(), list()) -> {#tclient{}, ok | {error, any()}}.
David Reissc4657992010-08-30 22:05:31 +000065send_function_call(Client = #tclient{protocol = Proto0,
David Reiss3f660a42010-08-30 22:05:29 +000066 service = Service,
67 seqid = SeqId},
David Reiss2c534032008-06-11 00:58:00 +000068 Function,
69 Args) ->
70 Params = Service:function_info(Function, params_type),
David Reiss3f660a42010-08-30 22:05:29 +000071 case Params of
72 no_function ->
73 {Client, {error, {no_function, Function}}};
74 {struct, PList} when length(PList) =/= length(Args) ->
75 {Client, {error, {bad_args, Function, Args}}};
76 {struct, _PList} ->
77 Begin = #protocol_message_begin{name = atom_to_list(Function),
78 type = ?tMessageType_CALL,
79 seqid = SeqId},
David Reissc4657992010-08-30 22:05:31 +000080 {Proto1, ok} = thrift_protocol:write(Proto0, Begin),
81 {Proto2, ok} = thrift_protocol:write(Proto1, {Params, list_to_tuple([Function | Args])}),
82 {Proto3, ok} = thrift_protocol:write(Proto2, message_end),
83 {Proto4, ok} = thrift_protocol:flush_transport(Proto3),
84 {Client#tclient{protocol = Proto4}, ok}
David Reiss3f660a42010-08-30 22:05:29 +000085 end.
David Reiss2c534032008-06-11 00:58:00 +000086
David Reissf4494ee2010-08-30 22:06:03 +000087-spec receive_function_result(#tclient{}, atom()) -> {#tclient{}, {ok, any()} | {error, any()}}.
David Reiss3f660a42010-08-30 22:05:29 +000088receive_function_result(Client = #tclient{service = Service}, Function) ->
Bryan Duxburyd3879f82010-08-19 05:06:02 +000089 ResultType = Service:function_info(Function, reply_type),
David Reiss3f660a42010-08-30 22:05:29 +000090 read_result(Client, Function, ResultType).
Bryan Duxburyd3879f82010-08-19 05:06:02 +000091
David Reiss3f660a42010-08-30 22:05:29 +000092read_result(Client, _Function, oneway_void) ->
93 {Client, {ok, ok}};
Bryan Duxburyd3879f82010-08-19 05:06:02 +000094
David Reiss82862952010-08-30 22:05:32 +000095read_result(Client = #tclient{protocol = Proto0,
David Reiss3f660a42010-08-30 22:05:29 +000096 seqid = SeqId},
Bryan Duxburyd3879f82010-08-19 05:06:02 +000097 Function,
98 ReplyType) ->
David Reiss82862952010-08-30 22:05:32 +000099 {Proto1, MessageBegin} = thrift_protocol:read(Proto0, message_begin),
100 NewClient = Client#tclient{protocol = Proto1},
101 case MessageBegin of
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000102 #protocol_message_begin{seqid = RetSeqId} when RetSeqId =/= SeqId ->
David Reiss82862952010-08-30 22:05:32 +0000103 {NewClient, {error, {bad_seq_id, SeqId}}};
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000104
105 #protocol_message_begin{type = ?tMessageType_EXCEPTION} ->
David Reiss82862952010-08-30 22:05:32 +0000106 handle_application_exception(NewClient);
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000107
108 #protocol_message_begin{type = ?tMessageType_REPLY} ->
David Reiss82862952010-08-30 22:05:32 +0000109 handle_reply(NewClient, Function, ReplyType)
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000110 end.
111
David Reiss3f660a42010-08-30 22:05:29 +0000112
David Reiss82862952010-08-30 22:05:32 +0000113handle_reply(Client = #tclient{protocol = Proto0,
David Reiss3f660a42010-08-30 22:05:29 +0000114 service = Service},
David Reiss2c534032008-06-11 00:58:00 +0000115 Function,
116 ReplyType) ->
117 {struct, ExceptionFields} = Service:function_info(Function, exceptions),
118 ReplyStructDef = {struct, [{0, ReplyType}] ++ ExceptionFields},
David Reiss82862952010-08-30 22:05:32 +0000119 {Proto1, {ok, Reply}} = thrift_protocol:read(Proto0, ReplyStructDef),
120 {Proto2, ok} = thrift_protocol:read(Proto1, message_end),
121 NewClient = Client#tclient{protocol = Proto2},
David Reiss2c534032008-06-11 00:58:00 +0000122 ReplyList = tuple_to_list(Reply),
123 true = length(ReplyList) == length(ExceptionFields) + 1,
124 ExceptionVals = tl(ReplyList),
125 Thrown = [X || X <- ExceptionVals,
126 X =/= undefined],
David Reiss3f660a42010-08-30 22:05:29 +0000127 case Thrown of
128 [] when ReplyType == {struct, []} ->
David Reiss82862952010-08-30 22:05:32 +0000129 {NewClient, {ok, ok}};
David Reiss3f660a42010-08-30 22:05:29 +0000130 [] ->
David Reiss82862952010-08-30 22:05:32 +0000131 {NewClient, {ok, hd(ReplyList)}};
David Reiss3f660a42010-08-30 22:05:29 +0000132 [Exception] ->
David Reiss82862952010-08-30 22:05:32 +0000133 throw({NewClient, {exception, Exception}})
David Reiss3f660a42010-08-30 22:05:29 +0000134 end.
David Reiss6b3e40f2008-06-11 00:59:03 +0000135
David Reiss82862952010-08-30 22:05:32 +0000136handle_application_exception(Client = #tclient{protocol = Proto0}) ->
137 {Proto1, {ok, Exception}} =
138 thrift_protocol:read(Proto0, ?TApplicationException_Structure),
139 {Proto2, ok} = thrift_protocol:read(Proto1, message_end),
David Reiss55ff70f2008-06-11 00:58:25 +0000140 XRecord = list_to_tuple(
141 ['TApplicationException' | tuple_to_list(Exception)]),
David Reiss1af18682008-06-11 01:01:36 +0000142 error_logger:error_msg("X: ~p~n", [XRecord]),
David Reiss55ff70f2008-06-11 00:58:25 +0000143 true = is_record(XRecord, 'TApplicationException'),
David Reiss82862952010-08-30 22:05:32 +0000144 NewClient = Client#tclient{protocol = Proto2},
145 throw({NewClient, {exception, XRecord}}).