David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 1 | %%%------------------------------------------------------------------- |
| 2 | %%% File : thrift_client.erl |
| 3 | %%% Author : Todd Lipcon <todd@lipcon.org> |
| 4 | %%% Description : A client which connects to a thrift service |
| 5 | %%% |
| 6 | %%% Created : 24 Feb 2008 by Todd Lipcon <todd@lipcon.org> |
| 7 | %%%------------------------------------------------------------------- |
| 8 | -module(thrift_client). |
| 9 | |
| 10 | -behaviour(gen_server). |
| 11 | |
| 12 | %% API |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 13 | -export([start_link/2, start_link/3, start_link/4, call/3, close/1]). |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 14 | |
| 15 | %% gen_server callbacks |
| 16 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, |
| 17 | terminate/2, code_change/3]). |
| 18 | |
| 19 | |
| 20 | -include("thrift_constants.hrl"). |
| 21 | -include("thrift_protocol.hrl"). |
| 22 | |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 23 | -record(state, {service, protocol, seqid}). |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 24 | |
| 25 | %%==================================================================== |
| 26 | %% API |
| 27 | %%==================================================================== |
| 28 | %%-------------------------------------------------------------------- |
| 29 | %% Function: start_link() -> {ok,Pid} | ignore | {error,Error} |
| 30 | %% Description: Starts the server |
| 31 | %%-------------------------------------------------------------------- |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 32 | start_link(Host, Port, Service) when is_integer(Port), is_atom(Service) -> |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 33 | start_link(Host, Port, Service, []). |
David Reiss | 4fd7818 | 2008-06-11 01:01:13 +0000 | [diff] [blame] | 34 | |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 35 | %% Backwards-compatible starter for the usual case of socket transports |
David Reiss | 2fe905e | 2008-06-11 01:02:23 +0000 | [diff] [blame] | 36 | start_link(Host, Port, Service, Options) |
| 37 | when is_integer(Port), is_atom(Service), is_list(Options) -> |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame^] | 38 | {ok, ProtocolFactory} = thrift_socket_transport:new_protocol_factory(Host, Port, Options), |
| 39 | start_link(ProtocolFactory, Service). |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 40 | |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame^] | 41 | %% ProtocolFactory :: fun() -> thrift_protocol() |
| 42 | start_link(ProtocolFactory, Service) |
| 43 | when is_function(ProtocolFactory), is_atom(Service) -> |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 44 | case gen_server:start_link(?MODULE, [Service], []) of |
David Reiss | e5a4d0c | 2008-06-11 01:02:10 +0000 | [diff] [blame] | 45 | {ok, Pid} -> |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame^] | 46 | case gen_server:call(Pid, {connect, ProtocolFactory}) of |
David Reiss | e5a4d0c | 2008-06-11 01:02:10 +0000 | [diff] [blame] | 47 | ok -> |
| 48 | {ok, Pid}; |
| 49 | Error -> |
| 50 | Error |
| 51 | end; |
| 52 | Else -> |
| 53 | Else |
| 54 | end. |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 55 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 56 | call(Client, Function, Args) |
| 57 | when is_pid(Client), is_atom(Function), is_list(Args) -> |
| 58 | case gen_server:call(Client, {call, Function, Args}) of |
| 59 | R = {ok, _} -> R; |
| 60 | R = {error, _} -> R; |
| 61 | {exception, Exception} -> throw(Exception) |
| 62 | end. |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 63 | |
David Reiss | 464e300 | 2008-06-11 01:00:45 +0000 | [diff] [blame] | 64 | close(Client) when is_pid(Client) -> |
David Reiss | 6f1cd53 | 2008-06-11 01:01:21 +0000 | [diff] [blame] | 65 | gen_server:cast(Client, close). |
David Reiss | 464e300 | 2008-06-11 01:00:45 +0000 | [diff] [blame] | 66 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 67 | %%==================================================================== |
| 68 | %% gen_server callbacks |
| 69 | %%==================================================================== |
| 70 | |
| 71 | %%-------------------------------------------------------------------- |
| 72 | %% Function: init(Args) -> {ok, State} | |
| 73 | %% {ok, State, Timeout} | |
| 74 | %% ignore | |
| 75 | %% {stop, Reason} |
| 76 | %% Description: Initiates the server |
| 77 | %%-------------------------------------------------------------------- |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 78 | init([Service]) -> |
| 79 | {ok, #state{service = Service}}. |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 80 | |
| 81 | %%-------------------------------------------------------------------- |
| 82 | %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} | |
| 83 | %% {reply, Reply, State, Timeout} | |
| 84 | %% {noreply, State} | |
| 85 | %% {noreply, State, Timeout} | |
| 86 | %% {stop, Reason, Reply, State} | |
| 87 | %% {stop, Reason, State} |
| 88 | %% Description: Handling call messages |
| 89 | %%-------------------------------------------------------------------- |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame^] | 90 | handle_call({connect, ProtocolFactory}, _From, |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 91 | State = #state{service = Service}) -> |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame^] | 92 | case ProtocolFactory() of |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 93 | {ok, Protocol} -> |
| 94 | {reply, ok, State#state{protocol = Protocol, |
| 95 | seqid = 0}}; |
David Reiss | e5a4d0c | 2008-06-11 01:02:10 +0000 | [diff] [blame] | 96 | Error -> |
| 97 | {stop, normal, Error, State} |
| 98 | end; |
| 99 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 100 | handle_call({call, Function, Args}, _From, State = #state{service = Service, |
| 101 | protocol = Protocol, |
| 102 | seqid = SeqId}) -> |
| 103 | Result = |
| 104 | try |
| 105 | ok = send_function_call(State, Function, Args), |
| 106 | receive_function_result(State, Function) |
| 107 | catch |
| 108 | throw:{return, Return} -> |
| 109 | Return; |
| 110 | error:function_clause -> |
| 111 | ST = erlang:get_stacktrace(), |
| 112 | case hd(ST) of |
| 113 | {Service, function_info, [Function, _]} -> |
| 114 | {error, {no_function, Function}}; |
| 115 | _ -> throw({error, {function_clause, ST}}) |
| 116 | end |
| 117 | end, |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 118 | |
David Reiss | 6f1cd53 | 2008-06-11 01:01:21 +0000 | [diff] [blame] | 119 | {reply, Result, State}. |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 120 | |
| 121 | %%-------------------------------------------------------------------- |
| 122 | %% Function: handle_cast(Msg, State) -> {noreply, State} | |
| 123 | %% {noreply, State, Timeout} | |
| 124 | %% {stop, Reason, State} |
| 125 | %% Description: Handling cast messages |
| 126 | %%-------------------------------------------------------------------- |
David Reiss | 6f1cd53 | 2008-06-11 01:01:21 +0000 | [diff] [blame] | 127 | handle_cast(close, State=#state{protocol = Protocol}) -> |
| 128 | %% error_logger:info_msg("thrift_client ~p received close", [self()]), |
| 129 | {stop,normal,State}; |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 130 | handle_cast(_Msg, State) -> |
| 131 | {noreply, State}. |
| 132 | |
| 133 | %%-------------------------------------------------------------------- |
| 134 | %% Function: handle_info(Info, State) -> {noreply, State} | |
| 135 | %% {noreply, State, Timeout} | |
| 136 | %% {stop, Reason, State} |
| 137 | %% Description: Handling all non call/cast messages |
| 138 | %%-------------------------------------------------------------------- |
| 139 | handle_info(_Info, State) -> |
| 140 | {noreply, State}. |
| 141 | |
| 142 | %%-------------------------------------------------------------------- |
| 143 | %% Function: terminate(Reason, State) -> void() |
| 144 | %% Description: This function is called by a gen_server when it is about to |
| 145 | %% terminate. It should be the opposite of Module:init/1 and do any necessary |
| 146 | %% cleaning up. When it returns, the gen_server terminates with Reason. |
| 147 | %% The return value is ignored. |
| 148 | %%-------------------------------------------------------------------- |
David Reiss | e5a4d0c | 2008-06-11 01:02:10 +0000 | [diff] [blame] | 149 | terminate(Reason, State = #state{protocol=undefined}) -> |
| 150 | ok; |
| 151 | terminate(Reason, State = #state{protocol=Protocol}) -> |
David Reiss | 464e300 | 2008-06-11 01:00:45 +0000 | [diff] [blame] | 152 | thrift_protocol:close_transport(Protocol), |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 153 | ok. |
| 154 | |
| 155 | %%-------------------------------------------------------------------- |
| 156 | %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState} |
| 157 | %% Description: Convert process state when code is changed |
| 158 | %%-------------------------------------------------------------------- |
| 159 | code_change(_OldVsn, State, _Extra) -> |
| 160 | {ok, State}. |
| 161 | |
| 162 | %%-------------------------------------------------------------------- |
| 163 | %%% Internal functions |
| 164 | %%-------------------------------------------------------------------- |
| 165 | send_function_call(#state{protocol = Proto, |
| 166 | service = Service, |
| 167 | seqid = SeqId}, |
| 168 | Function, |
| 169 | Args) -> |
| 170 | Params = Service:function_info(Function, params_type), |
| 171 | {struct, PList} = Params, |
David Reiss | c525745 | 2008-06-11 00:59:27 +0000 | [diff] [blame] | 172 | if |
| 173 | length(PList) =/= length(Args) -> |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 174 | throw({return, {error, {bad_args, Function, Args}}}); |
David Reiss | c525745 | 2008-06-11 00:59:27 +0000 | [diff] [blame] | 175 | true -> ok |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 176 | end, |
| 177 | |
| 178 | Begin = #protocol_message_begin{name = atom_to_list(Function), |
| 179 | type = ?tMessageType_CALL, |
| 180 | seqid = SeqId}, |
| 181 | ok = thrift_protocol:write(Proto, Begin), |
| 182 | ok = thrift_protocol:write(Proto, {Params, list_to_tuple([Function | Args])}), |
| 183 | ok = thrift_protocol:write(Proto, message_end), |
| 184 | thrift_protocol:flush_transport(Proto), |
| 185 | ok. |
| 186 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 187 | receive_function_result(State = #state{protocol = Proto, |
| 188 | service = Service}, |
| 189 | Function) -> |
| 190 | ResultType = Service:function_info(Function, reply_type), |
| 191 | read_result(State, Function, ResultType). |
| 192 | |
| 193 | read_result(_State, |
| 194 | _Function, |
| 195 | async_void) -> |
| 196 | {ok, ok}; |
| 197 | |
| 198 | read_result(State = #state{protocol = Proto, |
| 199 | seqid = SeqId}, |
| 200 | Function, |
| 201 | ReplyType) -> |
| 202 | case thrift_protocol:read(Proto, message_begin) of |
| 203 | #protocol_message_begin{seqid = RetSeqId} when RetSeqId =/= SeqId -> |
| 204 | {error, {bad_seq_id, SeqId}}; |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 205 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 206 | #protocol_message_begin{type = ?tMessageType_EXCEPTION} -> |
| 207 | handle_application_exception(State); |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 208 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 209 | #protocol_message_begin{type = ?tMessageType_REPLY} -> |
| 210 | handle_reply(State, Function, ReplyType) |
| 211 | end. |
| 212 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 213 | handle_reply(State = #state{protocol = Proto, |
| 214 | service = Service}, |
| 215 | Function, |
| 216 | ReplyType) -> |
| 217 | {struct, ExceptionFields} = Service:function_info(Function, exceptions), |
| 218 | ReplyStructDef = {struct, [{0, ReplyType}] ++ ExceptionFields}, |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 219 | {ok, Reply} = thrift_protocol:read(Proto, ReplyStructDef), |
| 220 | ReplyList = tuple_to_list(Reply), |
| 221 | true = length(ReplyList) == length(ExceptionFields) + 1, |
| 222 | ExceptionVals = tl(ReplyList), |
| 223 | Thrown = [X || X <- ExceptionVals, |
| 224 | X =/= undefined], |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 225 | Result = |
| 226 | case Thrown of |
| 227 | [] when ReplyType == {struct, []} -> |
| 228 | {ok, ok}; |
| 229 | [] -> |
| 230 | {ok, hd(ReplyList)}; |
| 231 | [Exception] -> |
| 232 | {exception, Exception} |
| 233 | end, |
| 234 | ok = thrift_protocol:read(Proto, message_end), |
| 235 | Result. |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 236 | |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 237 | handle_application_exception(State = #state{protocol = Proto}) -> |
| 238 | {ok, Exception} = thrift_protocol:read(Proto, |
| 239 | ?TApplicationException_Structure), |
| 240 | ok = thrift_protocol:read(Proto, message_end), |
| 241 | XRecord = list_to_tuple( |
| 242 | ['TApplicationException' | tuple_to_list(Exception)]), |
David Reiss | 1af1868 | 2008-06-11 01:01:36 +0000 | [diff] [blame] | 243 | error_logger:error_msg("X: ~p~n", [XRecord]), |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 244 | true = is_record(XRecord, 'TApplicationException'), |
| 245 | {exception, XRecord}. |