blob: d89b91c97a7191cdb1b8148a640aea66f7ded627 [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 io:format("Processor started~n"),
23 loop(#state{in_protocol = IProt,
24 out_protocol = OProt,
25 service = Service,
26 handler = Handler}).
27
28loop(State = #state{in_protocol = IProto,
29 out_protocol = OProto}) ->
David Reissae756f42008-06-10 22:57:11 +000030 case thrift_protocol:read(IProto, message_begin) of
31 #protocol_message_begin{name = Function,
32 type = ?tMessageType_CALL} ->
David Reissd93521b2008-06-10 22:58:07 +000033 ok= handle_function(State, list_to_atom(Function)),
David Reissae756f42008-06-10 22:57:11 +000034 loop(State);
35 {error, closed} ->
David Reiss76f0d112008-06-10 22:57:35 +000036 error_logger:info_msg("Client disconnected~n"),
David Reissae756f42008-06-10 22:57:11 +000037 ok
David Reissac549552008-06-10 22:56:59 +000038 end.
David Reissae756f42008-06-10 22:57:11 +000039
David Reiss1c1ca742008-06-10 22:57:21 +000040
41handle_function(State = #state{in_protocol = IProto,
42 out_protocol = OProto,
43 handler = Handler,
44 service = Service},
45 Function) ->
46 InParams = Service:function_info(Function, params_type),
47
48 {ok, Params} = thrift_protocol:read(IProto, InParams),
David Reiss76f0d112008-06-10 22:57:35 +000049
David Reiss982c72d2008-06-10 22:58:33 +000050 try
51 {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]),
52 error_logger:info_msg("Processed ~p(~p) in ~.4fms~n",
53 [Function, Params, Micro/1000.0]),
54 handle_success(State, Function, Result)
55 catch
56 throw:Exception when is_tuple(Exception), size(Exception) > 0 ->
57 error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]),
58 handle_exception(State, Function, Exception),
David Reiss920959a2008-06-11 00:56:35 +000059 ok; % we still want to accept more requests from this client
60 error:Error ->
61 ok = handle_error(State, Function, Error)
David Reiss982c72d2008-06-10 22:58:33 +000062 end.
63
64handle_success(State = #state{out_protocol = OProto,
65 service = Service},
66 Function,
67 Result) ->
David Reiss1c1ca742008-06-10 22:57:21 +000068 ReplyType = Service:function_info(Function, reply_type),
David Reisse1a79982008-06-10 22:58:14 +000069 StructName = atom_to_list(Function) ++ "_result",
David Reiss1c1ca742008-06-10 22:57:21 +000070
71 case Result of
David Reisse1a79982008-06-10 22:58:14 +000072 {reply, ReplyData} ->
73 Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}},
David Reiss982c72d2008-06-10 22:58:33 +000074 ok = send_reply(OProto, Function, ?tMessageType_REPLY, Reply);
David Reisse1a79982008-06-10 22:58:14 +000075
76 ok when ReplyType == {struct, []} ->
David Reiss982c72d2008-06-10 22:58:33 +000077 ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}})
David Reiss1c1ca742008-06-10 22:57:21 +000078 end,
79 ok.
David Reisse1a79982008-06-10 22:58:14 +000080
David Reiss982c72d2008-06-10 22:58:33 +000081handle_exception(State = #state{out_protocol = OProto,
82 service = Service},
83 Function,
84 Exception) ->
85 ExceptionType = element(1, Exception),
86 % Fetch a structure like {struct, [{-2, {struct, {Module, Type}}},
87 % {-3, {struct, {Module, Type}}}]}
David Reisse1a79982008-06-10 22:58:14 +000088
David Reiss982c72d2008-06-10 22:58:33 +000089 ReplySpec = Service:function_info(Function, exceptions),
90 {struct, XInfo} = ReplySpec,
91
92 true = is_list(XInfo),
93
94 % e.g.: [{-1, type0}, {-2, type1}, {-3, type2}]
95 XPairs = [{Fid, Type} || {Fid, {struct, {_Module, Type}}} <- XInfo],
96
97 Mapper = fun({Fid, Type}) ->
98 case Type of
99 ExceptionType ->
100 Exception;
101 _ ->
102 undefined
103 end
104 end,
105 % Assuming we had a type1 exception, we get: [undefined, Exception, undefined]
106 ExceptionList = lists:map(Mapper, XPairs),
107 ExceptionTuple = list_to_tuple([Function | ExceptionList]),
108
109 % Make sure we got at least one defined
110 case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of
111 true ->
112 ok = handle_unknown_exception(State, Function, Exception);
113 false ->
114 ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple})
115 end.
116
David Reiss920959a2008-06-11 00:56:35 +0000117%%
118% Called when an exception has been explicitly thrown by the service, but it was
119% not one of the exceptions that was defined for the function.
120%%
David Reiss982c72d2008-06-10 22:58:33 +0000121handle_unknown_exception(State, Function, Exception) ->
David Reiss920959a2008-06-11 00:56:35 +0000122 handle_error(State, Function, {exception_not_declared_as_thrown,
123 Exception}).
124
125handle_error(#state{out_protocol = OProto}, Function, Error) ->
David Reissda070672008-06-11 00:56:42 +0000126 Stack = erlang:get_stacktrace(),
127 error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]),
128
David Reiss920959a2008-06-11 00:56:35 +0000129 Message =
130 case application:get_env(thrift, exceptions_include_traces) of
131 {ok, true} ->
132 lists:flatten(io_lib:format("An error occurred: ~p~n",
David Reissda070672008-06-11 00:56:42 +0000133 [{Error, Stack}]));
David Reiss920959a2008-06-11 00:56:35 +0000134 _ ->
135 "An unknown handler error occurred."
136 end,
137 Reply = {?TApplicationException_Structure,
138 #'TApplicationException'{
139 message = Message,
140 type = ?TApplicationException_UNKNOWN}},
141 send_reply(OProto, Function, ?tMessageType_EXCEPTION, Reply).
David Reiss982c72d2008-06-10 22:58:33 +0000142
143
144send_reply(OProto, Function, ReplyMessageType, Reply) ->
David Reisse1a79982008-06-10 22:58:14 +0000145 ok = thrift_protocol:write(OProto, #protocol_message_begin{
146 name = atom_to_list(Function),
David Reiss982c72d2008-06-10 22:58:33 +0000147 type = ReplyMessageType,
David Reisse1a79982008-06-10 22:58:14 +0000148 seqid = 0}),
149 ok = thrift_protocol:write(OProto, Reply),
150 ok = thrift_protocol:write(OProto, message_end),
David Reiss90b40832008-06-10 22:58:52 +0000151 ok = thrift_protocol:flush_transport(OProto),
David Reisse1a79982008-06-10 22:58:14 +0000152 ok.
David Reiss982c72d2008-06-10 22:58:33 +0000153
154
155%%
156% This is the same as timer:tc except that timer:tc appears to catch
157% exceptions when it shouldn't!
158%%
159better_timer(Module, Function, Args) ->
160 T1 = erlang:now(),
161 Result = apply(Module, Function, Args),
162 T2 = erlang:now(),
163 {timer:now_diff(T2, T1), Result}.