blob: 2038b564b705e795206c7c3694b726ed1f8b8def [file] [log] [blame]
David Reissac549552008-06-10 22:56:59 +00001%%%-------------------------------------------------------------------
2%%% File : thrift_processor.erl
3%%% Author : <todd@lipcon.org>
David Reiss11d855c2008-06-11 00:59:12 +00004%%% Description :
David Reissac549552008-06-10 22:56:59 +00005%%%
6%%% Created : 28 Jan 2008 by <todd@lipcon.org>
7%%%-------------------------------------------------------------------
8-module(thrift_processor).
David Reissc11734e2008-06-11 00:59:48 +00009-author('todd@lipcon.org').
10-author('eletuchy@facebook.com').
David Reissac549552008-06-10 22:56:59 +000011
David Reissc11734e2008-06-11 00:59:48 +000012-export([init/1]).
David Reissac549552008-06-10 22:56:59 +000013
14-include("thrift_constants.hrl").
15-include("thrift_protocol.hrl").
16
David Reissc11734e2008-06-11 00:59:48 +000017-record(thrift_processor, {handler, in_protocol, out_protocol, service}).
David Reissac549552008-06-10 22:56:59 +000018
David Reissc11734e2008-06-11 00:59:48 +000019init({Server, ProtoGen, Service, Handler}) when is_function(ProtoGen, 0) ->
20 {ok, IProt, OProt} = ProtoGen(),
21 loop(#thrift_processor{in_protocol = IProt,
22 out_protocol = OProt,
23 service = Service,
24 handler = Handler}).
David Reissac549552008-06-10 22:56:59 +000025
David Reissc11734e2008-06-11 00:59:48 +000026loop(State = #thrift_processor{in_protocol = IProto,
27 out_protocol = OProto}) ->
28 error_logger:info_msg("loop: ~p", [State]),
David Reissae756f42008-06-10 22:57:11 +000029 case thrift_protocol:read(IProto, message_begin) of
30 #protocol_message_begin{name = Function,
31 type = ?tMessageType_CALL} ->
David Reissc11734e2008-06-11 00:59:48 +000032 ok=handle_function(State, list_to_atom(Function)),
David Reissae756f42008-06-10 22:57:11 +000033 loop(State);
34 {error, closed} ->
David Reissc11734e2008-06-11 00:59:48 +000035 %% error_logger:info_msg("Client disconnected~n"),
David Reissa5a53db2008-06-11 00:59:34 +000036 exit(protocol_closed)
David Reissac549552008-06-10 22:56:59 +000037 end.
David Reissae756f42008-06-10 22:57:11 +000038
David Reissc11734e2008-06-11 00:59:48 +000039handle_function(State=#thrift_processor{in_protocol = IProto,
40 out_protocol = OProto,
41 handler = Handler,
42 service = Service},
David Reiss1c1ca742008-06-10 22:57:21 +000043 Function) ->
44 InParams = Service:function_info(Function, params_type),
45
46 {ok, Params} = thrift_protocol:read(IProto, InParams),
David Reiss76f0d112008-06-10 22:57:35 +000047
David Reiss982c72d2008-06-10 22:58:33 +000048 try
David Reissc11734e2008-06-11 00:59:48 +000049 error_logger:info_msg("calling: ~p(~p)", [Function, Params]),
David Reiss11d855c2008-06-11 00:59:12 +000050 Result = Handler:handle_function(Function, Params),
David Reissc11734e2008-06-11 00:59:48 +000051 error_logger:info_msg("result: ~p", [Result]),
52 %% {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]),
53 %% error_logger:info_msg("Processed ~p(~p) in ~.4fms~n",
54 %% [Function, Params, Micro/1000.0]),
David Reiss982c72d2008-06-10 22:58:33 +000055 handle_success(State, Function, Result)
56 catch
David Reiss5541d652008-06-11 00:57:42 +000057 Type:Data ->
David Reissc11734e2008-06-11 00:59:48 +000058 error_logger:info_msg("handle_function oh noes: ~p ~p", [Type, Data]),
David Reiss5541d652008-06-11 00:57:42 +000059 handle_function_catch(State, Function, Type, Data)
David Reissc11734e2008-06-11 00:59:48 +000060 end,
61 after_reply(OProto).
David Reiss5541d652008-06-11 00:57:42 +000062
David Reissc11734e2008-06-11 00:59:48 +000063handle_function_catch(State = #thrift_processor{service = Service},
David Reiss5541d652008-06-11 00:57:42 +000064 Function, ErrType, ErrData) ->
65 IsAsync = Service:function_info(Function, reply_type) =:= async_void,
66
67 case {ErrType, ErrData} of
68 _ when IsAsync ->
69 error_logger:warning_msg(
70 "async void ~p threw error which must be ignored: ~p",
71 [Function, {ErrType, ErrData}]),
72 ok;
73
74 {throw, Exception} when is_tuple(Exception), size(Exception) > 0 ->
David Reiss982c72d2008-06-10 22:58:33 +000075 error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]),
76 handle_exception(State, Function, Exception),
David Reiss920959a2008-06-11 00:56:35 +000077 ok; % we still want to accept more requests from this client
David Reiss5541d652008-06-11 00:57:42 +000078
79 {error, Error} ->
David Reiss920959a2008-06-11 00:56:35 +000080 ok = handle_error(State, Function, Error)
David Reiss982c72d2008-06-10 22:58:33 +000081 end.
82
David Reissc11734e2008-06-11 00:59:48 +000083handle_success(State = #thrift_processor{out_protocol = OProto,
84 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +000085 Function,
86 Result) ->
David Reiss11d855c2008-06-11 00:59:12 +000087 ReplyType = Service:function_info(Function, reply_type),
David Reisse1a79982008-06-10 22:58:14 +000088 StructName = atom_to_list(Function) ++ "_result",
David Reiss11d855c2008-06-11 00:59:12 +000089
David Reissc11734e2008-06-11 00:59:48 +000090 ok = case Result of
91 {reply, ReplyData} ->
92 Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}},
93 send_reply(OProto, Function, ?tMessageType_REPLY, Reply);
David Reisse1a79982008-06-10 22:58:14 +000094
David Reissc11734e2008-06-11 00:59:48 +000095 ok when ReplyType == {struct, []} ->
96 send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}});
David Reiss11d855c2008-06-11 00:59:12 +000097
David Reissc11734e2008-06-11 00:59:48 +000098 ok when ReplyType == async_void ->
99 %% no reply for async void
100 ok
101 end.
David Reisse1a79982008-06-10 22:58:14 +0000102
David Reissc11734e2008-06-11 00:59:48 +0000103handle_exception(State = #thrift_processor{out_protocol = OProto,
104 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +0000105 Function,
106 Exception) ->
107 ExceptionType = element(1, Exception),
David Reissc11734e2008-06-11 00:59:48 +0000108 %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}},
109 %% {-3, {struct, {Module, Type}}}]}
David Reisse1a79982008-06-10 22:58:14 +0000110
David Reiss982c72d2008-06-10 22:58:33 +0000111 ReplySpec = Service:function_info(Function, exceptions),
112 {struct, XInfo} = ReplySpec,
113
114 true = is_list(XInfo),
David Reiss982c72d2008-06-10 22:58:33 +0000115
David Reissc11734e2008-06-11 00:59:48 +0000116 %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined]
117 %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}]
David Reiss11d855c2008-06-11 00:59:12 +0000118 ExceptionList = [case Type of
119 ExceptionType -> Exception;
120 _ -> undefined
David Reiss982c72d2008-06-10 22:58:33 +0000121 end
David Reiss11d855c2008-06-11 00:59:12 +0000122 || {_Fid, {struct, {_Module, Type}}} <- XInfo],
123
David Reiss982c72d2008-06-10 22:58:33 +0000124 ExceptionTuple = list_to_tuple([Function | ExceptionList]),
David Reiss11d855c2008-06-11 00:59:12 +0000125
David Reissc11734e2008-06-11 00:59:48 +0000126 % Make sure we got at least one defined
David Reiss982c72d2008-06-10 22:58:33 +0000127 case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of
128 true ->
129 ok = handle_unknown_exception(State, Function, Exception);
130 false ->
131 ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple})
132 end.
133
David Reiss920959a2008-06-11 00:56:35 +0000134%%
David Reissc11734e2008-06-11 00:59:48 +0000135%% Called when an exception has been explicitly thrown by the service, but it was
136%% not one of the exceptions that was defined for the function.
David Reiss920959a2008-06-11 00:56:35 +0000137%%
David Reiss982c72d2008-06-10 22:58:33 +0000138handle_unknown_exception(State, Function, Exception) ->
David Reiss920959a2008-06-11 00:56:35 +0000139 handle_error(State, Function, {exception_not_declared_as_thrown,
140 Exception}).
141
David Reissc11734e2008-06-11 00:59:48 +0000142handle_error(#thrift_processor{out_protocol = OProto}, Function, Error) ->
David Reissda070672008-06-11 00:56:42 +0000143 Stack = erlang:get_stacktrace(),
144 error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]),
David Reiss11d855c2008-06-11 00:59:12 +0000145
David Reiss920959a2008-06-11 00:56:35 +0000146 Message =
147 case application:get_env(thrift, exceptions_include_traces) of
148 {ok, true} ->
149 lists:flatten(io_lib:format("An error occurred: ~p~n",
David Reissda070672008-06-11 00:56:42 +0000150 [{Error, Stack}]));
David Reiss920959a2008-06-11 00:56:35 +0000151 _ ->
152 "An unknown handler error occurred."
153 end,
154 Reply = {?TApplicationException_Structure,
155 #'TApplicationException'{
156 message = Message,
157 type = ?TApplicationException_UNKNOWN}},
158 send_reply(OProto, Function, ?tMessageType_EXCEPTION, Reply).
David Reiss982c72d2008-06-10 22:58:33 +0000159
David Reiss982c72d2008-06-10 22:58:33 +0000160send_reply(OProto, Function, ReplyMessageType, Reply) ->
David Reisse1a79982008-06-10 22:58:14 +0000161 ok = thrift_protocol:write(OProto, #protocol_message_begin{
162 name = atom_to_list(Function),
David Reiss982c72d2008-06-10 22:58:33 +0000163 type = ReplyMessageType,
David Reisse1a79982008-06-10 22:58:14 +0000164 seqid = 0}),
165 ok = thrift_protocol:write(OProto, Reply),
166 ok = thrift_protocol:write(OProto, message_end),
David Reiss90b40832008-06-10 22:58:52 +0000167 ok = thrift_protocol:flush_transport(OProto),
David Reisse1a79982008-06-10 22:58:14 +0000168 ok.
David Reiss982c72d2008-06-10 22:58:33 +0000169
David Reissc11734e2008-06-11 00:59:48 +0000170after_reply(OProto) ->
171 ok = thrift_protocol:close_transport(OProto).