David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 1 | %%%------------------------------------------------------------------- |
| 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 Reiss | f4761e3 | 2008-06-11 00:56:49 +0000 | [diff] [blame] | 10 | -export([start/3,init/3]). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 11 | |
| 12 | -include("thrift_constants.hrl"). |
| 13 | -include("thrift_protocol.hrl"). |
| 14 | |
| 15 | -record(state, {handler, in_protocol, out_protocol, service}). |
| 16 | |
David Reiss | f4761e3 | 2008-06-11 00:56:49 +0000 | [diff] [blame] | 17 | start(ProtocolGenerator, Service, Handler) when is_function(ProtocolGenerator, 0) -> |
| 18 | spawn(thrift_processor, init, [ProtocolGenerator, Service, Handler]). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 19 | |
David Reiss | f4761e3 | 2008-06-11 00:56:49 +0000 | [diff] [blame] | 20 | init(ProtocolGenerator, Service, Handler) -> |
| 21 | {ok, IProt, OProt} = ProtocolGenerator(), |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 22 | io:format("Processor started~n"), |
| 23 | loop(#state{in_protocol = IProt, |
| 24 | out_protocol = OProt, |
| 25 | service = Service, |
| 26 | handler = Handler}). |
| 27 | |
| 28 | loop(State = #state{in_protocol = IProto, |
| 29 | out_protocol = OProto}) -> |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 30 | case thrift_protocol:read(IProto, message_begin) of |
| 31 | #protocol_message_begin{name = Function, |
| 32 | type = ?tMessageType_CALL} -> |
David Reiss | d93521b | 2008-06-10 22:58:07 +0000 | [diff] [blame] | 33 | ok= handle_function(State, list_to_atom(Function)), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 34 | loop(State); |
| 35 | {error, closed} -> |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 36 | error_logger:info_msg("Client disconnected~n"), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 37 | ok |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 38 | end. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 39 | |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 40 | |
| 41 | handle_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 Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 49 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 50 | 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 Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 59 | ok; % we still want to accept more requests from this client |
| 60 | error:Error -> |
| 61 | ok = handle_error(State, Function, Error) |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 62 | end. |
| 63 | |
| 64 | handle_success(State = #state{out_protocol = OProto, |
| 65 | service = Service}, |
| 66 | Function, |
| 67 | Result) -> |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 68 | ReplyType = Service:function_info(Function, reply_type), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 69 | StructName = atom_to_list(Function) ++ "_result", |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 70 | |
| 71 | case Result of |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 72 | {reply, ReplyData} -> |
| 73 | Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}}, |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 74 | ok = send_reply(OProto, Function, ?tMessageType_REPLY, Reply); |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 75 | |
| 76 | ok when ReplyType == {struct, []} -> |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 77 | ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}}) |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 78 | end, |
| 79 | ok. |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 80 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 81 | handle_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 Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 88 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 89 | 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 Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 117 | %% |
| 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 Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 121 | handle_unknown_exception(State, Function, Exception) -> |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 122 | handle_error(State, Function, {exception_not_declared_as_thrown, |
| 123 | Exception}). |
| 124 | |
| 125 | handle_error(#state{out_protocol = OProto}, Function, Error) -> |
David Reiss | da07067 | 2008-06-11 00:56:42 +0000 | [diff] [blame] | 126 | Stack = erlang:get_stacktrace(), |
| 127 | error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]), |
| 128 | |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 129 | 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 Reiss | da07067 | 2008-06-11 00:56:42 +0000 | [diff] [blame] | 133 | [{Error, Stack}])); |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 134 | _ -> |
| 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 Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 142 | |
| 143 | |
| 144 | send_reply(OProto, Function, ReplyMessageType, Reply) -> |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 145 | ok = thrift_protocol:write(OProto, #protocol_message_begin{ |
| 146 | name = atom_to_list(Function), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 147 | type = ReplyMessageType, |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 148 | seqid = 0}), |
| 149 | ok = thrift_protocol:write(OProto, Reply), |
| 150 | ok = thrift_protocol:write(OProto, message_end), |
David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 151 | ok = thrift_protocol:flush_transport(OProto), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 152 | ok. |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | %% |
| 156 | % This is the same as timer:tc except that timer:tc appears to catch |
| 157 | % exceptions when it shouldn't! |
| 158 | %% |
| 159 | better_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}. |