blob: 1c274052e2563e77fbc8a06bc4c8715a6139cac1 [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
Sergei Elin45764092022-09-23 23:21:31 +030028-record(tclient, {
29 service :: module(),
30 protocol :: thrift_protocol:state(),
31 seqid :: non_neg_integer()
32}).
33-type tclient() :: #tclient{}.
David Reissfc427af2008-06-11 01:11:57 +000034
Sergei Elin45764092022-09-23 23:21:31 +030035new(Protocol, Service) when
36 is_atom(Service)
37->
38 {ok, #tclient{
39 protocol = Protocol,
40 service = Service,
41 seqid = 0
42 }}.
David Reiss5e530af2009-06-04 02:01:24 +000043
David Reissf4494ee2010-08-30 22:06:03 +000044-spec call(#tclient{}, atom(), list()) -> {#tclient{}, {ok, any()} | {error, any()}}.
Sergei Elin45764092022-09-23 23:21:31 +030045call(Client = #tclient{}, Function, Args) when
46 is_atom(Function), is_list(Args)
47->
48 case send_function_call(Client, Function, Args) of
49 {ok, Client1} -> receive_function_result(Client1, Function);
50 {{error, X}, Client1} -> {Client1, {error, X}}
51 end.
David Reissa2f45972008-06-11 01:13:33 +000052
David Reiss65cf7202008-06-11 01:12:20 +000053%% Sends a function call but does not read the result. This is useful
David Reissc51986f2009-03-24 20:01:25 +000054%% if you're trying to log non-oneway function calls to write-only
David Reiss65cf7202008-06-11 01:12:20 +000055%% transports like thrift_disk_log_transport.
David Reiss3f660a42010-08-30 22:05:29 +000056-spec send_call(#tclient{}, atom(), list()) -> {#tclient{}, ok}.
Sergei Elin45764092022-09-23 23:21:31 +030057send_call(Client = #tclient{}, Function, Args) when
58 is_atom(Function), is_list(Args)
59->
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070060 case send_function_call(Client, Function, Args) of
Sergei Elin45764092022-09-23 23:21:31 +030061 {ok, Client1} -> {Client1, ok};
62 Else -> Else
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070063 end.
David Reiss65cf7202008-06-11 01:12:20 +000064
David Reiss3f660a42010-08-30 22:05:29 +000065-spec close(#tclient{}) -> ok.
Sergei Elin45764092022-09-23 23:21:31 +030066close(#tclient{protocol = Protocol}) ->
David Reiss3f660a42010-08-30 22:05:29 +000067 thrift_protocol:close_transport(Protocol).
David Reiss464e3002008-06-11 01:00:45 +000068
David Reiss2c534032008-06-11 00:58:00 +000069%%--------------------------------------------------------------------
70%%% Internal functions
71%%--------------------------------------------------------------------
Андрей Веселов54790992015-08-26 17:52:19 +030072-spec send_function_call(#tclient{}, atom(), list()) -> {ok | {error, any()}, #tclient{}}.
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070073send_function_call(Client = #tclient{service = Service}, Function, Args) ->
Sergei Elin45764092022-09-23 23:21:31 +030074 {Params, Reply} =
75 try
76 {
77 Service:function_info(Function, params_type),
78 Service:function_info(Function, reply_type)
79 }
80 catch
81 error:function_clause -> {no_function, 0}
82 end,
83 MsgType =
84 case Reply of
85 oneway_void -> ?tMessageType_ONEWAY;
86 _ -> ?tMessageType_CALL
87 end,
88 case Params of
89 no_function ->
90 {{error, {no_function, Function}}, Client};
91 {struct, PList} when length(PList) =/= length(Args) ->
92 {{error, {bad_args, Function, Args}}, Client};
93 {struct, _PList} ->
94 write_message(Client, Function, Args, Params, MsgType)
95 end.
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070096
97-spec write_message(#tclient{}, atom(), list(), {struct, list()}, integer()) ->
Sergei Elin45764092022-09-23 23:21:31 +030098 {ok | {error, any()}, #tclient{}}.
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070099write_message(Client = #tclient{protocol = P0, seqid = Seq}, Function, Args, Params, MsgType) ->
Sergei Elin45764092022-09-23 23:21:31 +0300100 try
101 {P1, ok} = thrift_protocol:write(P0, #protocol_message_begin{
102 name = atom_to_list(Function),
103 type = MsgType,
104 seqid = Seq
105 }),
106 {P2, ok} = thrift_protocol:write(P1, {Params, list_to_tuple([Function | Args])}),
107 {P3, ok} = thrift_protocol:write(P2, message_end),
108 {P4, ok} = thrift_protocol:flush_transport(P3),
109 {ok, Client#tclient{protocol = P4}}
110 catch
111 error:{badmatch, {_, {error, _} = Error}} -> {Error, Client}
112 end.
David Reiss2c534032008-06-11 00:58:00 +0000113
David Reissf4494ee2010-08-30 22:06:03 +0000114-spec receive_function_result(#tclient{}, atom()) -> {#tclient{}, {ok, any()} | {error, any()}}.
David Reiss3f660a42010-08-30 22:05:29 +0000115receive_function_result(Client = #tclient{service = Service}, Function) ->
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000116 ResultType = Service:function_info(Function, reply_type),
David Reiss3f660a42010-08-30 22:05:29 +0000117 read_result(Client, Function, ResultType).
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000118
David Reiss3f660a42010-08-30 22:05:29 +0000119read_result(Client, _Function, oneway_void) ->
120 {Client, {ok, ok}};
Sergei Elin45764092022-09-23 23:21:31 +0300121read_result(
122 Client = #tclient{
123 protocol = Proto0,
124 seqid = SeqId
125 },
126 Function,
127 ReplyType
128) ->
Jens Geyera6b328f2014-03-18 23:51:23 +0200129 case thrift_protocol:read(Proto0, message_begin) of
Sergei Elin45764092022-09-23 23:21:31 +0300130 {Proto1, {error, Reason}} ->
131 NewClient = Client#tclient{protocol = Proto1},
132 {NewClient, {error, Reason}};
133 {Proto1, MessageBegin} ->
134 NewClient = Client#tclient{protocol = Proto1},
135 case MessageBegin of
136 #protocol_message_begin{seqid = RetSeqId} when RetSeqId =/= SeqId ->
137 {NewClient, {error, {bad_seq_id, SeqId}}};
138 #protocol_message_begin{type = ?tMessageType_EXCEPTION} ->
139 handle_application_exception(NewClient);
140 #protocol_message_begin{type = ?tMessageType_REPLY} ->
141 handle_reply(NewClient, Function, ReplyType)
142 end
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000143 end.
144
Sergei Elin45764092022-09-23 23:21:31 +0300145handle_reply(
146 Client = #tclient{
147 protocol = Proto0,
148 service = Service
149 },
150 Function,
151 ReplyType
152) ->
David Reiss2c534032008-06-11 00:58:00 +0000153 {struct, ExceptionFields} = Service:function_info(Function, exceptions),
154 ReplyStructDef = {struct, [{0, ReplyType}] ++ ExceptionFields},
David Reiss82862952010-08-30 22:05:32 +0000155 {Proto1, {ok, Reply}} = thrift_protocol:read(Proto0, ReplyStructDef),
156 {Proto2, ok} = thrift_protocol:read(Proto1, message_end),
157 NewClient = Client#tclient{protocol = Proto2},
David Reiss2c534032008-06-11 00:58:00 +0000158 ReplyList = tuple_to_list(Reply),
159 true = length(ReplyList) == length(ExceptionFields) + 1,
160 ExceptionVals = tl(ReplyList),
Sergei Elin45764092022-09-23 23:21:31 +0300161 Thrown = [
162 X
163 || X <- ExceptionVals,
164 X =/= undefined
165 ],
David Reiss3f660a42010-08-30 22:05:29 +0000166 case Thrown of
167 [] when ReplyType == {struct, []} ->
David Reiss82862952010-08-30 22:05:32 +0000168 {NewClient, {ok, ok}};
David Reiss3f660a42010-08-30 22:05:29 +0000169 [] ->
David Reiss82862952010-08-30 22:05:32 +0000170 {NewClient, {ok, hd(ReplyList)}};
David Reiss3f660a42010-08-30 22:05:29 +0000171 [Exception] ->
David Reiss82862952010-08-30 22:05:32 +0000172 throw({NewClient, {exception, Exception}})
David Reiss3f660a42010-08-30 22:05:29 +0000173 end.
David Reiss6b3e40f2008-06-11 00:59:03 +0000174
Sergei Elin45764092022-09-23 23:21:31 +0300175-spec handle_application_exception(tclient()) -> no_return().
David Reiss82862952010-08-30 22:05:32 +0000176handle_application_exception(Client = #tclient{protocol = Proto0}) ->
177 {Proto1, {ok, Exception}} =
178 thrift_protocol:read(Proto0, ?TApplicationException_Structure),
179 {Proto2, ok} = thrift_protocol:read(Proto1, message_end),
David Reiss55ff70f2008-06-11 00:58:25 +0000180 XRecord = list_to_tuple(
Sergei Elin45764092022-09-23 23:21:31 +0300181 ['TApplicationException' | tuple_to_list(Exception)]
182 ),
David Reiss1af18682008-06-11 01:01:36 +0000183 error_logger:error_msg("X: ~p~n", [XRecord]),
David Reiss55ff70f2008-06-11 00:58:25 +0000184 true = is_record(XRecord, 'TApplicationException'),
David Reiss82862952010-08-30 22:05:32 +0000185 NewClient = Client#tclient{protocol = Proto2},
186 throw({NewClient, {exception, XRecord}}).