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 | |
| 10 | -export([start/4,init/4]). |
| 11 | |
| 12 | -include("thrift_constants.hrl"). |
| 13 | -include("thrift_protocol.hrl"). |
| 14 | |
| 15 | -record(state, {handler, in_protocol, out_protocol, service}). |
| 16 | |
| 17 | start(IProt, OProt, Service, Handler) -> |
| 18 | spawn(thrift_processor, init, [IProt, OProt, Service, Handler]). |
| 19 | |
| 20 | init(IProt, OProt, Service, Handler) -> |
| 21 | io:format("Processor started~n"), |
| 22 | loop(#state{in_protocol = IProt, |
| 23 | out_protocol = OProt, |
| 24 | service = Service, |
| 25 | handler = Handler}). |
| 26 | |
| 27 | loop(State = #state{in_protocol = IProto, |
| 28 | out_protocol = OProto}) -> |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 29 | case thrift_protocol:read(IProto, message_begin) of |
| 30 | #protocol_message_begin{name = Function, |
| 31 | type = ?tMessageType_CALL} -> |
David Reiss | d93521b | 2008-06-10 22:58:07 +0000 | [diff] [blame] | 32 | ok= handle_function(State, list_to_atom(Function)), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 33 | loop(State); |
| 34 | {error, closed} -> |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 35 | error_logger:info_msg("Client disconnected~n"), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 36 | ok |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 37 | end. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 38 | |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 39 | |
| 40 | handle_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 Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 48 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 49 | try |
| 50 | {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]), |
| 51 | error_logger:info_msg("Processed ~p(~p) in ~.4fms~n", |
| 52 | [Function, Params, Micro/1000.0]), |
| 53 | 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), |
| 58 | ok % we still want to accept more requests from this client |
| 59 | end. |
| 60 | |
| 61 | handle_success(State = #state{out_protocol = OProto, |
| 62 | service = Service}, |
| 63 | Function, |
| 64 | Result) -> |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 65 | ReplyType = Service:function_info(Function, reply_type), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 66 | StructName = atom_to_list(Function) ++ "_result", |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 67 | |
| 68 | case Result of |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 69 | {reply, ReplyData} -> |
| 70 | Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}}, |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 71 | ok = send_reply(OProto, Function, ?tMessageType_REPLY, Reply); |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 72 | |
| 73 | ok when ReplyType == {struct, []} -> |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 74 | ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}}) |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 75 | end, |
| 76 | ok. |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 77 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 78 | handle_exception(State = #state{out_protocol = OProto, |
| 79 | service = Service}, |
| 80 | Function, |
| 81 | Exception) -> |
| 82 | ExceptionType = element(1, Exception), |
| 83 | % Fetch a structure like {struct, [{-2, {struct, {Module, Type}}}, |
| 84 | % {-3, {struct, {Module, Type}}}]} |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 85 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 86 | ReplySpec = Service:function_info(Function, exceptions), |
| 87 | {struct, XInfo} = ReplySpec, |
| 88 | |
| 89 | true = is_list(XInfo), |
| 90 | |
| 91 | % e.g.: [{-1, type0}, {-2, type1}, {-3, type2}] |
| 92 | XPairs = [{Fid, Type} || {Fid, {struct, {_Module, Type}}} <- XInfo], |
| 93 | |
| 94 | Mapper = fun({Fid, Type}) -> |
| 95 | case Type of |
| 96 | ExceptionType -> |
| 97 | Exception; |
| 98 | _ -> |
| 99 | undefined |
| 100 | end |
| 101 | end, |
| 102 | % Assuming we had a type1 exception, we get: [undefined, Exception, undefined] |
| 103 | ExceptionList = lists:map(Mapper, XPairs), |
| 104 | ExceptionTuple = list_to_tuple([Function | ExceptionList]), |
| 105 | |
| 106 | % Make sure we got at least one defined |
| 107 | case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of |
| 108 | true -> |
| 109 | ok = handle_unknown_exception(State, Function, Exception); |
| 110 | false -> |
| 111 | ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple}) |
| 112 | end. |
| 113 | |
| 114 | handle_unknown_exception(State, Function, Exception) -> |
| 115 | io:format("Unknown exception!~n"), |
| 116 | ok. |
| 117 | |
| 118 | |
| 119 | send_reply(OProto, Function, ReplyMessageType, Reply) -> |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 120 | ok = thrift_protocol:write(OProto, #protocol_message_begin{ |
| 121 | name = atom_to_list(Function), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 122 | type = ReplyMessageType, |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 123 | seqid = 0}), |
| 124 | ok = thrift_protocol:write(OProto, Reply), |
| 125 | ok = thrift_protocol:write(OProto, message_end), |
David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame^] | 126 | ok = thrift_protocol:flush_transport(OProto), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 127 | ok. |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 128 | |
| 129 | |
| 130 | %% |
| 131 | % This is the same as timer:tc except that timer:tc appears to catch |
| 132 | % exceptions when it shouldn't! |
| 133 | %% |
| 134 | better_timer(Module, Function, Args) -> |
| 135 | T1 = erlang:now(), |
| 136 | Result = apply(Module, Function, Args), |
| 137 | T2 = erlang:now(), |
| 138 | {timer:now_diff(T2, T1), Result}. |