David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 20 | -module(thrift_client). |
| 21 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 22 | %% API |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 23 | -export([new/2, call/3, send_call/3, close/1]). |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 24 | |
| 25 | -include("thrift_constants.hrl"). |
| 26 | -include("thrift_protocol.hrl"). |
| 27 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 28 | -record(tclient, { |
| 29 | service :: module(), |
| 30 | protocol :: thrift_protocol:state(), |
| 31 | seqid :: non_neg_integer() |
| 32 | }). |
| 33 | -type tclient() :: #tclient{}. |
Björn Svensson | d966d66 | 2025-06-04 16:51:05 +0200 | [diff] [blame] | 34 | -export_type([tclient/0]). |
David Reiss | fc427af | 2008-06-11 01:11:57 +0000 | [diff] [blame] | 35 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 36 | new(Protocol, Service) when |
| 37 | is_atom(Service) |
| 38 | -> |
| 39 | {ok, #tclient{ |
| 40 | protocol = Protocol, |
| 41 | service = Service, |
| 42 | seqid = 0 |
| 43 | }}. |
David Reiss | 5e530af | 2009-06-04 02:01:24 +0000 | [diff] [blame] | 44 | |
David Reiss | f4494ee | 2010-08-30 22:06:03 +0000 | [diff] [blame] | 45 | -spec call(#tclient{}, atom(), list()) -> {#tclient{}, {ok, any()} | {error, any()}}. |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 46 | call(Client = #tclient{}, Function, Args) when |
| 47 | is_atom(Function), is_list(Args) |
| 48 | -> |
| 49 | case send_function_call(Client, Function, Args) of |
| 50 | {ok, Client1} -> receive_function_result(Client1, Function); |
| 51 | {{error, X}, Client1} -> {Client1, {error, X}} |
| 52 | end. |
David Reiss | a2f4597 | 2008-06-11 01:13:33 +0000 | [diff] [blame] | 53 | |
David Reiss | 65cf720 | 2008-06-11 01:12:20 +0000 | [diff] [blame] | 54 | %% Sends a function call but does not read the result. This is useful |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 55 | %% if you're trying to log non-oneway function calls to write-only |
David Reiss | 65cf720 | 2008-06-11 01:12:20 +0000 | [diff] [blame] | 56 | %% transports like thrift_disk_log_transport. |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 57 | -spec send_call(#tclient{}, atom(), list()) -> {#tclient{}, ok}. |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 58 | send_call(Client = #tclient{}, Function, Args) when |
| 59 | is_atom(Function), is_list(Args) |
| 60 | -> |
alisdair sullivan | 7bdba5c | 2014-09-30 22:03:34 -0700 | [diff] [blame] | 61 | case send_function_call(Client, Function, Args) of |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 62 | {ok, Client1} -> {Client1, ok}; |
| 63 | Else -> Else |
alisdair sullivan | 7bdba5c | 2014-09-30 22:03:34 -0700 | [diff] [blame] | 64 | end. |
David Reiss | 65cf720 | 2008-06-11 01:12:20 +0000 | [diff] [blame] | 65 | |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 66 | -spec close(#tclient{}) -> ok. |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 67 | close(#tclient{protocol = Protocol}) -> |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 68 | thrift_protocol:close_transport(Protocol). |
David Reiss | 464e300 | 2008-06-11 01:00:45 +0000 | [diff] [blame] | 69 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 70 | %%-------------------------------------------------------------------- |
| 71 | %%% Internal functions |
| 72 | %%-------------------------------------------------------------------- |
Андрей Веселов | 5479099 | 2015-08-26 17:52:19 +0300 | [diff] [blame] | 73 | -spec send_function_call(#tclient{}, atom(), list()) -> {ok | {error, any()}, #tclient{}}. |
alisdair sullivan | 7bdba5c | 2014-09-30 22:03:34 -0700 | [diff] [blame] | 74 | send_function_call(Client = #tclient{service = Service}, Function, Args) -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 75 | {Params, Reply} = |
| 76 | try |
| 77 | { |
| 78 | Service:function_info(Function, params_type), |
| 79 | Service:function_info(Function, reply_type) |
| 80 | } |
| 81 | catch |
| 82 | error:function_clause -> {no_function, 0} |
| 83 | end, |
| 84 | MsgType = |
| 85 | case Reply of |
| 86 | oneway_void -> ?tMessageType_ONEWAY; |
| 87 | _ -> ?tMessageType_CALL |
| 88 | end, |
| 89 | case Params of |
| 90 | no_function -> |
| 91 | {{error, {no_function, Function}}, Client}; |
| 92 | {struct, PList} when length(PList) =/= length(Args) -> |
| 93 | {{error, {bad_args, Function, Args}}, Client}; |
| 94 | {struct, _PList} -> |
| 95 | write_message(Client, Function, Args, Params, MsgType) |
| 96 | end. |
alisdair sullivan | 7bdba5c | 2014-09-30 22:03:34 -0700 | [diff] [blame] | 97 | |
| 98 | -spec write_message(#tclient{}, atom(), list(), {struct, list()}, integer()) -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 99 | {ok | {error, any()}, #tclient{}}. |
alisdair sullivan | 7bdba5c | 2014-09-30 22:03:34 -0700 | [diff] [blame] | 100 | write_message(Client = #tclient{protocol = P0, seqid = Seq}, Function, Args, Params, MsgType) -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 101 | try |
| 102 | {P1, ok} = thrift_protocol:write(P0, #protocol_message_begin{ |
| 103 | name = atom_to_list(Function), |
| 104 | type = MsgType, |
| 105 | seqid = Seq |
| 106 | }), |
| 107 | {P2, ok} = thrift_protocol:write(P1, {Params, list_to_tuple([Function | Args])}), |
| 108 | {P3, ok} = thrift_protocol:write(P2, message_end), |
| 109 | {P4, ok} = thrift_protocol:flush_transport(P3), |
| 110 | {ok, Client#tclient{protocol = P4}} |
| 111 | catch |
| 112 | error:{badmatch, {_, {error, _} = Error}} -> {Error, Client} |
| 113 | end. |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 114 | |
David Reiss | f4494ee | 2010-08-30 22:06:03 +0000 | [diff] [blame] | 115 | -spec receive_function_result(#tclient{}, atom()) -> {#tclient{}, {ok, any()} | {error, any()}}. |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 116 | receive_function_result(Client = #tclient{service = Service}, Function) -> |
Bryan Duxbury | d3879f8 | 2010-08-19 05:06:02 +0000 | [diff] [blame] | 117 | ResultType = Service:function_info(Function, reply_type), |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 118 | read_result(Client, Function, ResultType). |
Bryan Duxbury | d3879f8 | 2010-08-19 05:06:02 +0000 | [diff] [blame] | 119 | |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 120 | read_result(Client, _Function, oneway_void) -> |
| 121 | {Client, {ok, ok}}; |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 122 | read_result( |
| 123 | Client = #tclient{ |
| 124 | protocol = Proto0, |
| 125 | seqid = SeqId |
| 126 | }, |
| 127 | Function, |
| 128 | ReplyType |
| 129 | ) -> |
Jens Geyer | a6b328f | 2014-03-18 23:51:23 +0200 | [diff] [blame] | 130 | case thrift_protocol:read(Proto0, message_begin) of |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 131 | {Proto1, {error, Reason}} -> |
| 132 | NewClient = Client#tclient{protocol = Proto1}, |
| 133 | {NewClient, {error, Reason}}; |
| 134 | {Proto1, MessageBegin} -> |
| 135 | NewClient = Client#tclient{protocol = Proto1}, |
| 136 | case MessageBegin of |
| 137 | #protocol_message_begin{seqid = RetSeqId} when RetSeqId =/= SeqId -> |
| 138 | {NewClient, {error, {bad_seq_id, SeqId}}}; |
| 139 | #protocol_message_begin{type = ?tMessageType_EXCEPTION} -> |
| 140 | handle_application_exception(NewClient); |
| 141 | #protocol_message_begin{type = ?tMessageType_REPLY} -> |
| 142 | handle_reply(NewClient, Function, ReplyType) |
| 143 | end |
Bryan Duxbury | d3879f8 | 2010-08-19 05:06:02 +0000 | [diff] [blame] | 144 | end. |
| 145 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 146 | handle_reply( |
| 147 | Client = #tclient{ |
| 148 | protocol = Proto0, |
| 149 | service = Service |
| 150 | }, |
| 151 | Function, |
| 152 | ReplyType |
| 153 | ) -> |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 154 | {struct, ExceptionFields} = Service:function_info(Function, exceptions), |
| 155 | ReplyStructDef = {struct, [{0, ReplyType}] ++ ExceptionFields}, |
David Reiss | 8286295 | 2010-08-30 22:05:32 +0000 | [diff] [blame] | 156 | {Proto1, {ok, Reply}} = thrift_protocol:read(Proto0, ReplyStructDef), |
| 157 | {Proto2, ok} = thrift_protocol:read(Proto1, message_end), |
| 158 | NewClient = Client#tclient{protocol = Proto2}, |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 159 | ReplyList = tuple_to_list(Reply), |
| 160 | true = length(ReplyList) == length(ExceptionFields) + 1, |
| 161 | ExceptionVals = tl(ReplyList), |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 162 | Thrown = [ |
| 163 | X |
| 164 | || X <- ExceptionVals, |
| 165 | X =/= undefined |
| 166 | ], |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 167 | case Thrown of |
| 168 | [] when ReplyType == {struct, []} -> |
David Reiss | 8286295 | 2010-08-30 22:05:32 +0000 | [diff] [blame] | 169 | {NewClient, {ok, ok}}; |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 170 | [] -> |
David Reiss | 8286295 | 2010-08-30 22:05:32 +0000 | [diff] [blame] | 171 | {NewClient, {ok, hd(ReplyList)}}; |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 172 | [Exception] -> |
David Reiss | 8286295 | 2010-08-30 22:05:32 +0000 | [diff] [blame] | 173 | throw({NewClient, {exception, Exception}}) |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 174 | end. |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 175 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 176 | -spec handle_application_exception(tclient()) -> no_return(). |
David Reiss | 8286295 | 2010-08-30 22:05:32 +0000 | [diff] [blame] | 177 | handle_application_exception(Client = #tclient{protocol = Proto0}) -> |
| 178 | {Proto1, {ok, Exception}} = |
| 179 | thrift_protocol:read(Proto0, ?TApplicationException_Structure), |
| 180 | {Proto2, ok} = thrift_protocol:read(Proto1, message_end), |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 181 | XRecord = list_to_tuple( |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 182 | ['TApplicationException' | tuple_to_list(Exception)] |
| 183 | ), |
David Reiss | 1af1868 | 2008-06-11 01:01:36 +0000 | [diff] [blame] | 184 | error_logger:error_msg("X: ~p~n", [XRecord]), |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 185 | true = is_record(XRecord, 'TApplicationException'), |
David Reiss | 8286295 | 2010-08-30 22:05:32 +0000 | [diff] [blame] | 186 | NewClient = Client#tclient{protocol = Proto2}, |
| 187 | throw({NewClient, {exception, XRecord}}). |