blob: 2b435acb149f0fdf40f46af69b92142072cc9a9f [file] [log] [blame]
David Reissac549552008-06-10 22:56:59 +00001%%%-------------------------------------------------------------------
2%%% File : thrift_processor.erl
3%%% Author : <todd@lipcon.org>
4%%% Description :
5%%%
6%%% Created : 28 Jan 2008 by <todd@lipcon.org>
7%%%-------------------------------------------------------------------
8-module(thrift_processor).
9
David Reissf4761e32008-06-11 00:56:49 +000010-export([start/3,init/3]).
David Reissac549552008-06-10 22:56:59 +000011
12-include("thrift_constants.hrl").
13-include("thrift_protocol.hrl").
14
15-record(state, {handler, in_protocol, out_protocol, service}).
16
David Reissf4761e32008-06-11 00:56:49 +000017start(ProtocolGenerator, Service, Handler) when is_function(ProtocolGenerator, 0) ->
18 spawn(thrift_processor, init, [ProtocolGenerator, Service, Handler]).
David Reissac549552008-06-10 22:56:59 +000019
David Reissf4761e32008-06-11 00:56:49 +000020init(ProtocolGenerator, Service, Handler) ->
21 {ok, IProt, OProt} = ProtocolGenerator(),
David Reissac549552008-06-10 22:56:59 +000022 loop(#state{in_protocol = IProt,
23 out_protocol = OProt,
24 service = Service,
25 handler = Handler}).
26
27loop(State = #state{in_protocol = IProto,
28 out_protocol = OProto}) ->
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 Reissd93521b2008-06-10 22:58:07 +000032 ok= handle_function(State, list_to_atom(Function)),
David Reissae756f42008-06-10 22:57:11 +000033 loop(State);
34 {error, closed} ->
David Reissfc44c412008-06-11 00:57:19 +000035 % error_logger:info_msg("Client disconnected~n"),
David Reissae756f42008-06-10 22:57:11 +000036 ok
David Reissac549552008-06-10 22:56:59 +000037 end.
David Reissae756f42008-06-10 22:57:11 +000038
David Reiss1c1ca742008-06-10 22:57:21 +000039
40handle_function(State = #state{in_protocol = IProto,
41 out_protocol = OProto,
42 handler = Handler,
43 service = Service},
44 Function) ->
45 InParams = Service:function_info(Function, params_type),
46
47 {ok, Params} = thrift_protocol:read(IProto, InParams),
David Reiss76f0d112008-06-10 22:57:35 +000048
David Reiss982c72d2008-06-10 22:58:33 +000049 try
50 {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]),
David Reissfc44c412008-06-11 00:57:19 +000051 % error_logger:info_msg("Processed ~p(~p) in ~.4fms~n",
52 % [Function, Params, Micro/1000.0]),
David Reiss982c72d2008-06-10 22:58:33 +000053 handle_success(State, Function, Result)
54 catch
55 throw:Exception when is_tuple(Exception), size(Exception) > 0 ->
56 error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]),
57 handle_exception(State, Function, Exception),
David Reiss920959a2008-06-11 00:56:35 +000058 ok; % we still want to accept more requests from this client
59 error:Error ->
60 ok = handle_error(State, Function, Error)
David Reiss982c72d2008-06-10 22:58:33 +000061 end.
62
63handle_success(State = #state{out_protocol = OProto,
64 service = Service},
65 Function,
66 Result) ->
David Reiss1c1ca742008-06-10 22:57:21 +000067 ReplyType = Service:function_info(Function, reply_type),
David Reisse1a79982008-06-10 22:58:14 +000068 StructName = atom_to_list(Function) ++ "_result",
David Reiss1c1ca742008-06-10 22:57:21 +000069
70 case Result of
David Reisse1a79982008-06-10 22:58:14 +000071 {reply, ReplyData} ->
72 Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}},
David Reiss982c72d2008-06-10 22:58:33 +000073 ok = send_reply(OProto, Function, ?tMessageType_REPLY, Reply);
David Reisse1a79982008-06-10 22:58:14 +000074
75 ok when ReplyType == {struct, []} ->
David Reisscdf8d192008-06-11 00:57:35 +000076 ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}});
77
78 ok when ReplyType == async_void ->
79 % no reply for async void
80 ok
David Reiss1c1ca742008-06-10 22:57:21 +000081 end,
82 ok.
David Reisse1a79982008-06-10 22:58:14 +000083
David Reiss982c72d2008-06-10 22:58:33 +000084handle_exception(State = #state{out_protocol = OProto,
85 service = Service},
86 Function,
87 Exception) ->
88 ExceptionType = element(1, Exception),
89 % Fetch a structure like {struct, [{-2, {struct, {Module, Type}}},
90 % {-3, {struct, {Module, Type}}}]}
David Reisse1a79982008-06-10 22:58:14 +000091
David Reiss982c72d2008-06-10 22:58:33 +000092 ReplySpec = Service:function_info(Function, exceptions),
93 {struct, XInfo} = ReplySpec,
94
95 true = is_list(XInfo),
96
97 % e.g.: [{-1, type0}, {-2, type1}, {-3, type2}]
98 XPairs = [{Fid, Type} || {Fid, {struct, {_Module, Type}}} <- XInfo],
99
100 Mapper = fun({Fid, Type}) ->
101 case Type of
102 ExceptionType ->
103 Exception;
104 _ ->
105 undefined
106 end
107 end,
108 % Assuming we had a type1 exception, we get: [undefined, Exception, undefined]
109 ExceptionList = lists:map(Mapper, XPairs),
110 ExceptionTuple = list_to_tuple([Function | ExceptionList]),
111
112 % Make sure we got at least one defined
113 case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of
114 true ->
115 ok = handle_unknown_exception(State, Function, Exception);
116 false ->
117 ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple})
118 end.
119
David Reiss920959a2008-06-11 00:56:35 +0000120%%
121% Called when an exception has been explicitly thrown by the service, but it was
122% not one of the exceptions that was defined for the function.
123%%
David Reiss982c72d2008-06-10 22:58:33 +0000124handle_unknown_exception(State, Function, Exception) ->
David Reiss920959a2008-06-11 00:56:35 +0000125 handle_error(State, Function, {exception_not_declared_as_thrown,
126 Exception}).
127
128handle_error(#state{out_protocol = OProto}, Function, Error) ->
David Reissda070672008-06-11 00:56:42 +0000129 Stack = erlang:get_stacktrace(),
130 error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]),
131
David Reiss920959a2008-06-11 00:56:35 +0000132 Message =
133 case application:get_env(thrift, exceptions_include_traces) of
134 {ok, true} ->
135 lists:flatten(io_lib:format("An error occurred: ~p~n",
David Reissda070672008-06-11 00:56:42 +0000136 [{Error, Stack}]));
David Reiss920959a2008-06-11 00:56:35 +0000137 _ ->
138 "An unknown handler error occurred."
139 end,
140 Reply = {?TApplicationException_Structure,
141 #'TApplicationException'{
142 message = Message,
143 type = ?TApplicationException_UNKNOWN}},
144 send_reply(OProto, Function, ?tMessageType_EXCEPTION, Reply).
David Reiss982c72d2008-06-10 22:58:33 +0000145
146
147send_reply(OProto, Function, ReplyMessageType, Reply) ->
David Reisse1a79982008-06-10 22:58:14 +0000148 ok = thrift_protocol:write(OProto, #protocol_message_begin{
149 name = atom_to_list(Function),
David Reiss982c72d2008-06-10 22:58:33 +0000150 type = ReplyMessageType,
David Reisse1a79982008-06-10 22:58:14 +0000151 seqid = 0}),
152 ok = thrift_protocol:write(OProto, Reply),
153 ok = thrift_protocol:write(OProto, message_end),
David Reiss90b40832008-06-10 22:58:52 +0000154 ok = thrift_protocol:flush_transport(OProto),
David Reisse1a79982008-06-10 22:58:14 +0000155 ok.
David Reiss982c72d2008-06-10 22:58:33 +0000156
157
158%%
159% This is the same as timer:tc except that timer:tc appears to catch
160% exceptions when it shouldn't!
161%%
162better_timer(Module, Function, Args) ->
163 T1 = erlang:now(),
164 Result = apply(Module, Function, Args),
165 T2 = erlang:now(),
166 {timer:now_diff(T2, T1), Result}.