David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame^] | 1 | %% |
| 2 | %% Licensed to the Apache Software Foundation (ASF) under one |
| 3 | %% or more contributor license agreements. See the NOTICE file |
| 4 | %% distributed with this work for additional information |
| 5 | %% regarding copyright ownership. The ASF licenses this file |
| 6 | %% to you under the Apache License, Version 2.0 (the |
| 7 | %% "License"); you may not use this file except in compliance |
| 8 | %% with the License. You may obtain a copy of the License at |
| 9 | %% |
| 10 | %% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | %% |
| 12 | %% Unless required by applicable law or agreed to in writing, |
| 13 | %% software distributed under the License is distributed on an |
| 14 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | %% KIND, either express or implied. See the License for the |
| 16 | %% specific language governing permissions and limitations |
| 17 | %% under the License. |
| 18 | %% |
| 19 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 20 | -module(thrift_processor). |
| 21 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 22 | -export([init/1]). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 23 | |
| 24 | -include("thrift_constants.hrl"). |
| 25 | -include("thrift_protocol.hrl"). |
| 26 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 27 | -record(thrift_processor, {handler, in_protocol, out_protocol, service}). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 28 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 29 | init({Server, ProtoGen, Service, Handler}) when is_function(ProtoGen, 0) -> |
| 30 | {ok, IProt, OProt} = ProtoGen(), |
| 31 | loop(#thrift_processor{in_protocol = IProt, |
| 32 | out_protocol = OProt, |
| 33 | service = Service, |
| 34 | handler = Handler}). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 35 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 36 | loop(State = #thrift_processor{in_protocol = IProto, |
| 37 | out_protocol = OProto}) -> |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 38 | case thrift_protocol:read(IProto, message_begin) of |
| 39 | #protocol_message_begin{name = Function, |
| 40 | type = ?tMessageType_CALL} -> |
David Reiss | 80664fe | 2008-06-11 01:00:30 +0000 | [diff] [blame] | 41 | ok = handle_function(State, list_to_atom(Function)), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 42 | loop(State); |
David Reiss | c49dd1e | 2008-06-11 01:02:39 +0000 | [diff] [blame] | 43 | {error, timeout} -> |
| 44 | thrift_protocol:close_transport(OProto), |
| 45 | ok; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 46 | {error, closed} -> |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 47 | %% error_logger:info_msg("Client disconnected~n"), |
David Reiss | 80664fe | 2008-06-11 01:00:30 +0000 | [diff] [blame] | 48 | thrift_protocol:close_transport(OProto), |
| 49 | exit(shutdown) |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 50 | end. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 51 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 52 | handle_function(State=#thrift_processor{in_protocol = IProto, |
| 53 | out_protocol = OProto, |
| 54 | handler = Handler, |
| 55 | service = Service}, |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 56 | Function) -> |
| 57 | InParams = Service:function_info(Function, params_type), |
| 58 | |
| 59 | {ok, Params} = thrift_protocol:read(IProto, InParams), |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 60 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 61 | try |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 62 | Result = Handler:handle_function(Function, Params), |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 63 | %% {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]), |
| 64 | %% error_logger:info_msg("Processed ~p(~p) in ~.4fms~n", |
| 65 | %% [Function, Params, Micro/1000.0]), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 66 | handle_success(State, Function, Result) |
| 67 | catch |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 68 | Type:Data -> |
| 69 | handle_function_catch(State, Function, Type, Data) |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 70 | end, |
| 71 | after_reply(OProto). |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 72 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 73 | handle_function_catch(State = #thrift_processor{service = Service}, |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 74 | Function, ErrType, ErrData) -> |
David Reiss | fe931d1 | 2009-03-24 20:02:08 +0000 | [diff] [blame] | 75 | IsOneway = Service:function_info(Function, reply_type) =:= oneway_void, |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 76 | |
| 77 | case {ErrType, ErrData} of |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 78 | _ when IsOneway -> |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame] | 79 | Stack = erlang:get_stacktrace(), |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 80 | error_logger:warning_msg( |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 81 | "oneway void ~p threw error which must be ignored: ~p", |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame] | 82 | [Function, {ErrType, ErrData, Stack}]), |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 83 | ok; |
| 84 | |
| 85 | {throw, Exception} when is_tuple(Exception), size(Exception) > 0 -> |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 86 | error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]), |
| 87 | handle_exception(State, Function, Exception), |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 88 | ok; % we still want to accept more requests from this client |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 89 | |
| 90 | {error, Error} -> |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 91 | ok = handle_error(State, Function, Error) |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 92 | end. |
| 93 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 94 | handle_success(State = #thrift_processor{out_protocol = OProto, |
| 95 | service = Service}, |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 96 | Function, |
| 97 | Result) -> |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 98 | ReplyType = Service:function_info(Function, reply_type), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 99 | StructName = atom_to_list(Function) ++ "_result", |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 100 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 101 | ok = case Result of |
| 102 | {reply, ReplyData} -> |
| 103 | Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}}, |
| 104 | send_reply(OProto, Function, ?tMessageType_REPLY, Reply); |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 105 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 106 | ok when ReplyType == {struct, []} -> |
| 107 | send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}}); |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 108 | |
David Reiss | fe931d1 | 2009-03-24 20:02:08 +0000 | [diff] [blame] | 109 | ok when ReplyType == oneway_void -> |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 110 | %% no reply for oneway void |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 111 | ok |
| 112 | end. |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 113 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 114 | handle_exception(State = #thrift_processor{out_protocol = OProto, |
| 115 | service = Service}, |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 116 | Function, |
| 117 | Exception) -> |
| 118 | ExceptionType = element(1, Exception), |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 119 | %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}}, |
| 120 | %% {-3, {struct, {Module, Type}}}]} |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 121 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 122 | ReplySpec = Service:function_info(Function, exceptions), |
| 123 | {struct, XInfo} = ReplySpec, |
| 124 | |
| 125 | true = is_list(XInfo), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 126 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 127 | %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined] |
| 128 | %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}] |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 129 | ExceptionList = [case Type of |
| 130 | ExceptionType -> Exception; |
| 131 | _ -> undefined |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 132 | end |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 133 | || {_Fid, {struct, {_Module, Type}}} <- XInfo], |
| 134 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 135 | ExceptionTuple = list_to_tuple([Function | ExceptionList]), |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 136 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 137 | % Make sure we got at least one defined |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 138 | case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of |
| 139 | true -> |
| 140 | ok = handle_unknown_exception(State, Function, Exception); |
| 141 | false -> |
| 142 | ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple}) |
| 143 | end. |
| 144 | |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 145 | %% |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 146 | %% Called when an exception has been explicitly thrown by the service, but it was |
| 147 | %% not one of the exceptions that was defined for the function. |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 148 | %% |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 149 | handle_unknown_exception(State, Function, Exception) -> |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 150 | handle_error(State, Function, {exception_not_declared_as_thrown, |
| 151 | Exception}). |
| 152 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 153 | handle_error(#thrift_processor{out_protocol = OProto}, Function, Error) -> |
David Reiss | da07067 | 2008-06-11 00:56:42 +0000 | [diff] [blame] | 154 | Stack = erlang:get_stacktrace(), |
| 155 | error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]), |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 156 | |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 157 | Message = |
| 158 | case application:get_env(thrift, exceptions_include_traces) of |
| 159 | {ok, true} -> |
| 160 | lists:flatten(io_lib:format("An error occurred: ~p~n", |
David Reiss | da07067 | 2008-06-11 00:56:42 +0000 | [diff] [blame] | 161 | [{Error, Stack}])); |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 162 | _ -> |
| 163 | "An unknown handler error occurred." |
| 164 | end, |
| 165 | Reply = {?TApplicationException_Structure, |
| 166 | #'TApplicationException'{ |
| 167 | message = Message, |
| 168 | type = ?TApplicationException_UNKNOWN}}, |
| 169 | send_reply(OProto, Function, ?tMessageType_EXCEPTION, Reply). |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 170 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 171 | send_reply(OProto, Function, ReplyMessageType, Reply) -> |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 172 | ok = thrift_protocol:write(OProto, #protocol_message_begin{ |
| 173 | name = atom_to_list(Function), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 174 | type = ReplyMessageType, |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 175 | seqid = 0}), |
| 176 | ok = thrift_protocol:write(OProto, Reply), |
| 177 | ok = thrift_protocol:write(OProto, message_end), |
David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 178 | ok = thrift_protocol:flush_transport(OProto), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 179 | ok. |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 180 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 181 | after_reply(OProto) -> |
David Reiss | 7255ed4 | 2008-06-11 01:00:04 +0000 | [diff] [blame] | 182 | ok = thrift_protocol:flush_transport(OProto) |
| 183 | %% ok = thrift_protocol:close_transport(OProto) |
| 184 | . |