blob: 7bf50a5c207a236b9b9620243caf50ecc23d1dc2 [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)
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070039when is_atom(Function), is_list(Args) ->
40 case send_function_call(Client, Function, Args) of
41 {ok, Client1} -> receive_function_result(Client1, Function);
David Robakowskiae971ce2013-08-02 12:16:00 +020042 {{error, X}, Client1} -> {Client1, {error, X}};
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070043 Else -> Else
44 end.
David Reiss2c534032008-06-11 00:58:00 +000045
David Reissa2f45972008-06-11 01:13:33 +000046
David Reiss65cf7202008-06-11 01:12:20 +000047%% Sends a function call but does not read the result. This is useful
David Reissc51986f2009-03-24 20:01:25 +000048%% if you're trying to log non-oneway function calls to write-only
David Reiss65cf7202008-06-11 01:12:20 +000049%% transports like thrift_disk_log_transport.
David Reiss3f660a42010-08-30 22:05:29 +000050-spec send_call(#tclient{}, atom(), list()) -> {#tclient{}, ok}.
51send_call(Client = #tclient{}, Function, Args)
52 when is_atom(Function), is_list(Args) ->
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070053 case send_function_call(Client, Function, Args) of
54 {ok, Client1} -> {Client1, ok};
55 Else -> Else
56 end.
David Reiss65cf7202008-06-11 01:12:20 +000057
David Reiss3f660a42010-08-30 22:05:29 +000058-spec close(#tclient{}) -> ok.
59close(#tclient{protocol=Protocol}) ->
60 thrift_protocol:close_transport(Protocol).
David Reiss464e3002008-06-11 01:00:45 +000061
David Reiss2c534032008-06-11 00:58:00 +000062
63%%--------------------------------------------------------------------
64%%% Internal functions
65%%--------------------------------------------------------------------
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070066-spec send_function_call(#tclient{}, atom(), list()) -> {sync | async | {error, any()}, #tclient{}}.
67send_function_call(Client = #tclient{service = Service}, Function, Args) ->
68 {Params, Reply} = try
69 {Service:function_info(Function, params_type), Service:function_info(Function, reply_type)}
David Robakowskiae971ce2013-08-02 12:16:00 +020070 catch error:function_clause -> {no_function, 0}
alisdair sullivan7bdba5c2014-09-30 22:03:34 -070071 end,
72 MsgType = case Reply of
73 oneway_void -> ?tMessageType_ONEWAY;
74 _ -> ?tMessageType_CALL
75 end,
76 case Params of
77 no_function ->
78 {{error, {no_function, Function}}, Client};
79 {struct, PList} when length(PList) =/= length(Args) ->
80 {{error, {bad_args, Function, Args}}, Client};
81 {struct, _PList} -> write_message(Client, Function, Args, Params, MsgType)
82 end.
83
84-spec write_message(#tclient{}, atom(), list(), {struct, list()}, integer()) ->
85 {ok, #tclient{}}.
86write_message(Client = #tclient{protocol = P0, seqid = Seq}, Function, Args, Params, MsgType) ->
87 {P1, ok} = thrift_protocol:write(P0, #protocol_message_begin{
88 name = atom_to_list(Function),
89 type = MsgType,
90 seqid = Seq
91 }),
92 {P2, ok} = thrift_protocol:write(P1, {Params, list_to_tuple([Function|Args])}),
93 {P3, ok} = thrift_protocol:write(P2, message_end),
94 {P4, ok} = thrift_protocol:flush_transport(P3),
95 {ok, Client#tclient{protocol = P4}}.
David Reiss2c534032008-06-11 00:58:00 +000096
David Reissf4494ee2010-08-30 22:06:03 +000097-spec receive_function_result(#tclient{}, atom()) -> {#tclient{}, {ok, any()} | {error, any()}}.
David Reiss3f660a42010-08-30 22:05:29 +000098receive_function_result(Client = #tclient{service = Service}, Function) ->
Bryan Duxburyd3879f82010-08-19 05:06:02 +000099 ResultType = Service:function_info(Function, reply_type),
David Reiss3f660a42010-08-30 22:05:29 +0000100 read_result(Client, Function, ResultType).
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000101
David Reiss3f660a42010-08-30 22:05:29 +0000102read_result(Client, _Function, oneway_void) ->
103 {Client, {ok, ok}};
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000104
David Reiss82862952010-08-30 22:05:32 +0000105read_result(Client = #tclient{protocol = Proto0,
David Reiss3f660a42010-08-30 22:05:29 +0000106 seqid = SeqId},
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000107 Function,
108 ReplyType) ->
Jens Geyera6b328f2014-03-18 23:51:23 +0200109 case thrift_protocol:read(Proto0, message_begin) of
110 {Proto1, {error, Reason}} ->
111 NewClient = Client#tclient{protocol = Proto1},
112 {NewClient, {error, Reason}};
113 {Proto1, MessageBegin} ->
114 NewClient = Client#tclient{protocol = Proto1},
115 case MessageBegin of
116 #protocol_message_begin{seqid = RetSeqId} when RetSeqId =/= SeqId ->
117 {NewClient, {error, {bad_seq_id, SeqId}}};
118 #protocol_message_begin{type = ?tMessageType_EXCEPTION} ->
119 handle_application_exception(NewClient);
120 #protocol_message_begin{type = ?tMessageType_REPLY} ->
121 handle_reply(NewClient, Function, ReplyType)
122 end
Bryan Duxburyd3879f82010-08-19 05:06:02 +0000123 end.
124
David Reiss3f660a42010-08-30 22:05:29 +0000125
David Reiss82862952010-08-30 22:05:32 +0000126handle_reply(Client = #tclient{protocol = Proto0,
David Reiss3f660a42010-08-30 22:05:29 +0000127 service = Service},
David Reiss2c534032008-06-11 00:58:00 +0000128 Function,
129 ReplyType) ->
130 {struct, ExceptionFields} = Service:function_info(Function, exceptions),
131 ReplyStructDef = {struct, [{0, ReplyType}] ++ ExceptionFields},
David Reiss82862952010-08-30 22:05:32 +0000132 {Proto1, {ok, Reply}} = thrift_protocol:read(Proto0, ReplyStructDef),
133 {Proto2, ok} = thrift_protocol:read(Proto1, message_end),
134 NewClient = Client#tclient{protocol = Proto2},
David Reiss2c534032008-06-11 00:58:00 +0000135 ReplyList = tuple_to_list(Reply),
136 true = length(ReplyList) == length(ExceptionFields) + 1,
137 ExceptionVals = tl(ReplyList),
138 Thrown = [X || X <- ExceptionVals,
139 X =/= undefined],
David Reiss3f660a42010-08-30 22:05:29 +0000140 case Thrown of
141 [] when ReplyType == {struct, []} ->
David Reiss82862952010-08-30 22:05:32 +0000142 {NewClient, {ok, ok}};
David Reiss3f660a42010-08-30 22:05:29 +0000143 [] ->
David Reiss82862952010-08-30 22:05:32 +0000144 {NewClient, {ok, hd(ReplyList)}};
David Reiss3f660a42010-08-30 22:05:29 +0000145 [Exception] ->
David Reiss82862952010-08-30 22:05:32 +0000146 throw({NewClient, {exception, Exception}})
David Reiss3f660a42010-08-30 22:05:29 +0000147 end.
David Reiss6b3e40f2008-06-11 00:59:03 +0000148
David Reiss82862952010-08-30 22:05:32 +0000149handle_application_exception(Client = #tclient{protocol = Proto0}) ->
150 {Proto1, {ok, Exception}} =
151 thrift_protocol:read(Proto0, ?TApplicationException_Structure),
152 {Proto2, ok} = thrift_protocol:read(Proto1, message_end),
David Reiss55ff70f2008-06-11 00:58:25 +0000153 XRecord = list_to_tuple(
154 ['TApplicationException' | tuple_to_list(Exception)]),
David Reiss1af18682008-06-11 01:01:36 +0000155 error_logger:error_msg("X: ~p~n", [XRecord]),
David Reiss55ff70f2008-06-11 00:58:25 +0000156 true = is_record(XRecord, 'TApplicationException'),
David Reiss82862952010-08-30 22:05:32 +0000157 NewClient = Client#tclient{protocol = Proto2},
158 throw({NewClient, {exception, XRecord}}).