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 | |
| 22 | -behaviour(gen_server). |
| 23 | |
| 24 | %% API |
David Reiss | 65cf720 | 2008-06-11 01:12:20 +0000 | [diff] [blame] | 25 | -export([start_link/2, start_link/3, start_link/4, call/3, send_call/3, close/1]). |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 26 | |
| 27 | %% gen_server callbacks |
| 28 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, |
| 29 | terminate/2, code_change/3]). |
| 30 | |
| 31 | |
| 32 | -include("thrift_constants.hrl"). |
| 33 | -include("thrift_protocol.hrl"). |
| 34 | |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 35 | -record(state, {service, protocol, seqid}). |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 36 | |
| 37 | %%==================================================================== |
| 38 | %% API |
| 39 | %%==================================================================== |
| 40 | %%-------------------------------------------------------------------- |
| 41 | %% Function: start_link() -> {ok,Pid} | ignore | {error,Error} |
| 42 | %% Description: Starts the server |
| 43 | %%-------------------------------------------------------------------- |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 44 | start_link(Host, Port, Service) when is_integer(Port), is_atom(Service) -> |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 45 | start_link(Host, Port, Service, []). |
David Reiss | 4fd7818 | 2008-06-11 01:01:13 +0000 | [diff] [blame] | 46 | |
David Reiss | fc427af | 2008-06-11 01:11:57 +0000 | [diff] [blame] | 47 | |
| 48 | %% |
| 49 | %% Splits client options into protocol options and transport options |
| 50 | %% |
| 51 | %% split_options([Options...]) -> {ProtocolOptions, TransportOptions} |
| 52 | %% |
| 53 | split_options(Options) -> |
| 54 | split_options(Options, [], []). |
| 55 | |
| 56 | split_options([], ProtoIn, TransIn) -> |
| 57 | {ProtoIn, TransIn}; |
| 58 | |
| 59 | split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn) |
| 60 | when OptKey =:= strict_read; |
| 61 | OptKey =:= strict_write -> |
| 62 | split_options(Rest, [Opt | ProtoIn], TransIn); |
| 63 | |
| 64 | split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn) |
| 65 | when OptKey =:= framed; |
| 66 | OptKey =:= connect_timeout; |
| 67 | OptKey =:= sockopts -> |
| 68 | split_options(Rest, ProtoIn, [Opt | TransIn]). |
| 69 | |
| 70 | |
| 71 | %% Backwards-compatible starter for the common-case of socket transports |
David Reiss | 2fe905e | 2008-06-11 01:02:23 +0000 | [diff] [blame] | 72 | start_link(Host, Port, Service, Options) |
| 73 | when is_integer(Port), is_atom(Service), is_list(Options) -> |
David Reiss | fc427af | 2008-06-11 01:11:57 +0000 | [diff] [blame] | 74 | {ProtoOpts, TransOpts} = split_options(Options), |
| 75 | |
| 76 | {ok, TransportFactory} = |
| 77 | thrift_socket_transport:new_transport_factory(Host, Port, TransOpts), |
| 78 | |
| 79 | {ok, ProtocolFactory} = thrift_binary_protocol:new_protocol_factory( |
| 80 | TransportFactory, ProtoOpts), |
| 81 | |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame] | 82 | start_link(ProtocolFactory, Service). |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 83 | |
David Reiss | fc427af | 2008-06-11 01:11:57 +0000 | [diff] [blame] | 84 | |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame] | 85 | %% ProtocolFactory :: fun() -> thrift_protocol() |
| 86 | start_link(ProtocolFactory, Service) |
| 87 | when is_function(ProtocolFactory), is_atom(Service) -> |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 88 | case gen_server:start_link(?MODULE, [Service], []) of |
David Reiss | e5a4d0c | 2008-06-11 01:02:10 +0000 | [diff] [blame] | 89 | {ok, Pid} -> |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame] | 90 | case gen_server:call(Pid, {connect, ProtocolFactory}) of |
David Reiss | e5a4d0c | 2008-06-11 01:02:10 +0000 | [diff] [blame] | 91 | ok -> |
| 92 | {ok, Pid}; |
| 93 | Error -> |
| 94 | Error |
| 95 | end; |
| 96 | Else -> |
| 97 | Else |
| 98 | end. |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 99 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 100 | call(Client, Function, Args) |
| 101 | when is_pid(Client), is_atom(Function), is_list(Args) -> |
| 102 | case gen_server:call(Client, {call, Function, Args}) of |
| 103 | R = {ok, _} -> R; |
| 104 | R = {error, _} -> R; |
| 105 | {exception, Exception} -> throw(Exception) |
| 106 | end. |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 107 | |
David Reiss | a2f4597 | 2008-06-11 01:13:33 +0000 | [diff] [blame] | 108 | cast(Client, Function, Args) |
| 109 | when is_pid(Client), is_atom(Function), is_list(Args) -> |
| 110 | gen_server:cast(Client, {call, Function, Args}). |
| 111 | |
David Reiss | 65cf720 | 2008-06-11 01:12:20 +0000 | [diff] [blame] | 112 | %% 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] | 113 | %% 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] | 114 | %% transports like thrift_disk_log_transport. |
| 115 | send_call(Client, Function, Args) |
| 116 | when is_pid(Client), is_atom(Function), is_list(Args) -> |
| 117 | gen_server:call(Client, {send_call, Function, Args}). |
| 118 | |
David Reiss | 464e300 | 2008-06-11 01:00:45 +0000 | [diff] [blame] | 119 | close(Client) when is_pid(Client) -> |
David Reiss | 6f1cd53 | 2008-06-11 01:01:21 +0000 | [diff] [blame] | 120 | gen_server:cast(Client, close). |
David Reiss | 464e300 | 2008-06-11 01:00:45 +0000 | [diff] [blame] | 121 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 122 | %%==================================================================== |
| 123 | %% gen_server callbacks |
| 124 | %%==================================================================== |
| 125 | |
| 126 | %%-------------------------------------------------------------------- |
| 127 | %% Function: init(Args) -> {ok, State} | |
| 128 | %% {ok, State, Timeout} | |
| 129 | %% ignore | |
| 130 | %% {stop, Reason} |
| 131 | %% Description: Initiates the server |
| 132 | %%-------------------------------------------------------------------- |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 133 | init([Service]) -> |
| 134 | {ok, #state{service = Service}}. |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 135 | |
| 136 | %%-------------------------------------------------------------------- |
| 137 | %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} | |
| 138 | %% {reply, Reply, State, Timeout} | |
| 139 | %% {noreply, State} | |
| 140 | %% {noreply, State, Timeout} | |
| 141 | %% {stop, Reason, Reply, State} | |
| 142 | %% {stop, Reason, State} |
| 143 | %% Description: Handling call messages |
| 144 | %%-------------------------------------------------------------------- |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame] | 145 | handle_call({connect, ProtocolFactory}, _From, |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 146 | State = #state{service = Service}) -> |
David Reiss | 44f785e | 2008-06-11 01:03:37 +0000 | [diff] [blame] | 147 | case ProtocolFactory() of |
David Reiss | ad74b32 | 2008-06-11 01:03:29 +0000 | [diff] [blame] | 148 | {ok, Protocol} -> |
| 149 | {reply, ok, State#state{protocol = Protocol, |
| 150 | seqid = 0}}; |
David Reiss | e5a4d0c | 2008-06-11 01:02:10 +0000 | [diff] [blame] | 151 | Error -> |
| 152 | {stop, normal, Error, State} |
| 153 | end; |
| 154 | |
David Reiss | 65cf720 | 2008-06-11 01:12:20 +0000 | [diff] [blame] | 155 | handle_call({call, Function, Args}, _From, State = #state{service = Service}) -> |
| 156 | Result = catch_function_exceptions( |
| 157 | fun() -> |
| 158 | ok = send_function_call(State, Function, Args), |
| 159 | receive_function_result(State, Function) |
| 160 | end, |
| 161 | Service), |
| 162 | {reply, Result, State}; |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 163 | |
David Reiss | 65cf720 | 2008-06-11 01:12:20 +0000 | [diff] [blame] | 164 | |
| 165 | handle_call({send_call, Function, Args}, _From, State = #state{service = Service}) -> |
| 166 | Result = catch_function_exceptions( |
| 167 | fun() -> |
| 168 | send_function_call(State, Function, Args) |
| 169 | end, |
| 170 | Service), |
David Reiss | 6f1cd53 | 2008-06-11 01:01:21 +0000 | [diff] [blame] | 171 | {reply, Result, State}. |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 172 | |
David Reiss | 65cf720 | 2008-06-11 01:12:20 +0000 | [diff] [blame] | 173 | |
| 174 | %% Helper function that catches exceptions thrown by sending or receiving |
| 175 | %% a function and returns the correct response for call or send_only above. |
| 176 | catch_function_exceptions(Fun, Service) -> |
| 177 | try |
| 178 | Fun() |
| 179 | catch |
| 180 | throw:{return, Return} -> |
| 181 | Return; |
| 182 | error:function_clause -> |
| 183 | ST = erlang:get_stacktrace(), |
| 184 | case hd(ST) of |
| 185 | {Service, function_info, [Function, _]} -> |
| 186 | {error, {no_function, Function}}; |
| 187 | _ -> throw({error, {function_clause, ST}}) |
| 188 | end |
| 189 | end. |
| 190 | |
| 191 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 192 | %%-------------------------------------------------------------------- |
| 193 | %% Function: handle_cast(Msg, State) -> {noreply, State} | |
| 194 | %% {noreply, State, Timeout} | |
| 195 | %% {stop, Reason, State} |
| 196 | %% Description: Handling cast messages |
| 197 | %%-------------------------------------------------------------------- |
David Reiss | a2f4597 | 2008-06-11 01:13:33 +0000 | [diff] [blame] | 198 | handle_cast({call, Function, Args}, State = #state{service = Service, |
| 199 | protocol = Protocol, |
| 200 | seqid = SeqId}) -> |
| 201 | _Result = |
| 202 | try |
| 203 | ok = send_function_call(State, Function, Args), |
| 204 | receive_function_result(State, Function) |
| 205 | catch |
| 206 | Class:Reason -> |
| 207 | error_logger:error_msg("error ignored in handle_cast({cast,...},...): ~p:~p~n", [Class, Reason]) |
| 208 | end, |
| 209 | |
| 210 | {noreply, State}; |
| 211 | |
David Reiss | 6f1cd53 | 2008-06-11 01:01:21 +0000 | [diff] [blame] | 212 | handle_cast(close, State=#state{protocol = Protocol}) -> |
| 213 | %% error_logger:info_msg("thrift_client ~p received close", [self()]), |
| 214 | {stop,normal,State}; |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 215 | handle_cast(_Msg, State) -> |
| 216 | {noreply, State}. |
| 217 | |
| 218 | %%-------------------------------------------------------------------- |
| 219 | %% Function: handle_info(Info, State) -> {noreply, State} | |
| 220 | %% {noreply, State, Timeout} | |
| 221 | %% {stop, Reason, State} |
| 222 | %% Description: Handling all non call/cast messages |
| 223 | %%-------------------------------------------------------------------- |
| 224 | handle_info(_Info, State) -> |
| 225 | {noreply, State}. |
| 226 | |
| 227 | %%-------------------------------------------------------------------- |
| 228 | %% Function: terminate(Reason, State) -> void() |
| 229 | %% Description: This function is called by a gen_server when it is about to |
| 230 | %% terminate. It should be the opposite of Module:init/1 and do any necessary |
| 231 | %% cleaning up. When it returns, the gen_server terminates with Reason. |
| 232 | %% The return value is ignored. |
| 233 | %%-------------------------------------------------------------------- |
David Reiss | e5a4d0c | 2008-06-11 01:02:10 +0000 | [diff] [blame] | 234 | terminate(Reason, State = #state{protocol=undefined}) -> |
| 235 | ok; |
| 236 | terminate(Reason, State = #state{protocol=Protocol}) -> |
David Reiss | 464e300 | 2008-06-11 01:00:45 +0000 | [diff] [blame] | 237 | thrift_protocol:close_transport(Protocol), |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 238 | ok. |
| 239 | |
| 240 | %%-------------------------------------------------------------------- |
| 241 | %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState} |
| 242 | %% Description: Convert process state when code is changed |
| 243 | %%-------------------------------------------------------------------- |
| 244 | code_change(_OldVsn, State, _Extra) -> |
| 245 | {ok, State}. |
| 246 | |
| 247 | %%-------------------------------------------------------------------- |
| 248 | %%% Internal functions |
| 249 | %%-------------------------------------------------------------------- |
| 250 | send_function_call(#state{protocol = Proto, |
| 251 | service = Service, |
| 252 | seqid = SeqId}, |
| 253 | Function, |
| 254 | Args) -> |
| 255 | Params = Service:function_info(Function, params_type), |
| 256 | {struct, PList} = Params, |
David Reiss | c525745 | 2008-06-11 00:59:27 +0000 | [diff] [blame] | 257 | if |
| 258 | length(PList) =/= length(Args) -> |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 259 | throw({return, {error, {bad_args, Function, Args}}}); |
David Reiss | c525745 | 2008-06-11 00:59:27 +0000 | [diff] [blame] | 260 | true -> ok |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 261 | end, |
| 262 | |
| 263 | Begin = #protocol_message_begin{name = atom_to_list(Function), |
| 264 | type = ?tMessageType_CALL, |
| 265 | seqid = SeqId}, |
| 266 | ok = thrift_protocol:write(Proto, Begin), |
| 267 | ok = thrift_protocol:write(Proto, {Params, list_to_tuple([Function | Args])}), |
| 268 | ok = thrift_protocol:write(Proto, message_end), |
| 269 | thrift_protocol:flush_transport(Proto), |
| 270 | ok. |
| 271 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 272 | receive_function_result(State = #state{protocol = Proto, |
| 273 | service = Service}, |
| 274 | Function) -> |
| 275 | ResultType = Service:function_info(Function, reply_type), |
| 276 | read_result(State, Function, ResultType). |
| 277 | |
| 278 | read_result(_State, |
| 279 | _Function, |
David Reiss | fe931d1 | 2009-03-24 20:02:08 +0000 | [diff] [blame] | 280 | oneway_void) -> |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 281 | {ok, ok}; |
| 282 | |
| 283 | read_result(State = #state{protocol = Proto, |
| 284 | seqid = SeqId}, |
| 285 | Function, |
| 286 | ReplyType) -> |
| 287 | case thrift_protocol:read(Proto, message_begin) of |
| 288 | #protocol_message_begin{seqid = RetSeqId} when RetSeqId =/= SeqId -> |
| 289 | {error, {bad_seq_id, SeqId}}; |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 290 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 291 | #protocol_message_begin{type = ?tMessageType_EXCEPTION} -> |
| 292 | handle_application_exception(State); |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 293 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 294 | #protocol_message_begin{type = ?tMessageType_REPLY} -> |
| 295 | handle_reply(State, Function, ReplyType) |
| 296 | end. |
| 297 | |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 298 | handle_reply(State = #state{protocol = Proto, |
| 299 | service = Service}, |
| 300 | Function, |
| 301 | ReplyType) -> |
| 302 | {struct, ExceptionFields} = Service:function_info(Function, exceptions), |
| 303 | ReplyStructDef = {struct, [{0, ReplyType}] ++ ExceptionFields}, |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 304 | {ok, Reply} = thrift_protocol:read(Proto, ReplyStructDef), |
| 305 | ReplyList = tuple_to_list(Reply), |
| 306 | true = length(ReplyList) == length(ExceptionFields) + 1, |
| 307 | ExceptionVals = tl(ReplyList), |
| 308 | Thrown = [X || X <- ExceptionVals, |
| 309 | X =/= undefined], |
David Reiss | 2c53403 | 2008-06-11 00:58:00 +0000 | [diff] [blame] | 310 | Result = |
| 311 | case Thrown of |
| 312 | [] when ReplyType == {struct, []} -> |
| 313 | {ok, ok}; |
| 314 | [] -> |
| 315 | {ok, hd(ReplyList)}; |
| 316 | [Exception] -> |
| 317 | {exception, Exception} |
| 318 | end, |
| 319 | ok = thrift_protocol:read(Proto, message_end), |
| 320 | Result. |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 321 | |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 322 | handle_application_exception(State = #state{protocol = Proto}) -> |
| 323 | {ok, Exception} = thrift_protocol:read(Proto, |
| 324 | ?TApplicationException_Structure), |
| 325 | ok = thrift_protocol:read(Proto, message_end), |
| 326 | XRecord = list_to_tuple( |
| 327 | ['TApplicationException' | tuple_to_list(Exception)]), |
David Reiss | 1af1868 | 2008-06-11 01:01:36 +0000 | [diff] [blame] | 328 | error_logger:error_msg("X: ~p~n", [XRecord]), |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 329 | true = is_record(XRecord, 'TApplicationException'), |
| 330 | {exception, XRecord}. |