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 | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 27 | -record(thrift_processor, {handler, protocol, service}). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 28 | |
David Reiss | 5ed313d | 2010-08-30 22:05:57 +0000 | [diff] [blame] | 29 | init({_Server, ProtoGen, Service, Handler}) when is_function(ProtoGen, 0) -> |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 30 | {ok, Proto} = ProtoGen(), |
| 31 | loop(#thrift_processor{protocol = Proto, |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 32 | service = Service, |
| 33 | handler = Handler}). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 34 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 35 | loop(State0 = #thrift_processor{protocol = Proto0}) -> |
| 36 | {Proto1, MessageBegin} = thrift_protocol:read(Proto0, message_begin), |
| 37 | State1 = State0#thrift_processor{protocol = Proto1}, |
| 38 | case MessageBegin of |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 39 | #protocol_message_begin{name = Function, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 40 | type = ?tMessageType_CALL, |
| 41 | seqid = Seqid} -> |
| 42 | {State2, ok} = handle_function(State1, list_to_atom(Function), Seqid), |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 43 | loop(State2); |
David Reiss | deda141 | 2009-04-02 19:22:31 +0000 | [diff] [blame] | 44 | #protocol_message_begin{name = Function, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 45 | type = ?tMessageType_ONEWAY, |
| 46 | seqid = Seqid} -> |
| 47 | {State2, ok} = handle_function(State1, list_to_atom(Function), Seqid), |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 48 | loop(State2); |
David Reiss | c49dd1e | 2008-06-11 01:02:39 +0000 | [diff] [blame] | 49 | {error, timeout} -> |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 50 | thrift_protocol:close_transport(Proto1), |
David Reiss | c49dd1e | 2008-06-11 01:02:39 +0000 | [diff] [blame] | 51 | ok; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 52 | {error, closed} -> |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 53 | %% error_logger:info_msg("Client disconnected~n"), |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 54 | thrift_protocol:close_transport(Proto1), |
David Reiss | 80664fe | 2008-06-11 01:00:30 +0000 | [diff] [blame] | 55 | exit(shutdown) |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 56 | end. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 57 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 58 | handle_function(State0=#thrift_processor{protocol = Proto0, |
| 59 | handler = Handler, |
| 60 | service = Service}, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 61 | Function, |
| 62 | Seqid) -> |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 63 | InParams = Service:function_info(Function, params_type), |
| 64 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 65 | {Proto1, {ok, Params}} = thrift_protocol:read(Proto0, InParams), |
| 66 | State1 = State0#thrift_processor{protocol = Proto1}, |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 67 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 68 | try |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 69 | Result = Handler:handle_function(Function, Params), |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 70 | %% {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]), |
| 71 | %% error_logger:info_msg("Processed ~p(~p) in ~.4fms~n", |
| 72 | %% [Function, Params, Micro/1000.0]), |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 73 | handle_success(State1, Function, Result, Seqid) |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 74 | catch |
David Reiss | b9561c8 | 2010-08-30 22:05:36 +0000 | [diff] [blame] | 75 | Type:Data when Type =:= throw orelse Type =:= error -> |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 76 | handle_function_catch(State1, Function, Type, Data, Seqid) |
David Reiss | 4b0534a | 2010-08-30 22:05:37 +0000 | [diff] [blame] | 77 | end. |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 78 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 79 | handle_function_catch(State = #thrift_processor{service = Service}, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 80 | Function, ErrType, ErrData, Seqid) -> |
David Reiss | fe931d1 | 2009-03-24 20:02:08 +0000 | [diff] [blame] | 81 | IsOneway = Service:function_info(Function, reply_type) =:= oneway_void, |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 82 | |
| 83 | case {ErrType, ErrData} of |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 84 | _ when IsOneway -> |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame] | 85 | Stack = erlang:get_stacktrace(), |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 86 | error_logger:warning_msg( |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 87 | "oneway void ~p threw error which must be ignored: ~p", |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame] | 88 | [Function, {ErrType, ErrData, Stack}]), |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 89 | {State, ok}; |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 90 | |
| 91 | {throw, Exception} when is_tuple(Exception), size(Exception) > 0 -> |
David Reiss | c0e4b5b | 2010-08-30 22:05:12 +0000 | [diff] [blame] | 92 | %error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]), |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 93 | handle_exception(State, Function, Exception, Seqid); |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 94 | % we still want to accept more requests from this client |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 95 | |
| 96 | {error, Error} -> |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 97 | handle_error(State, Function, Error, Seqid) |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 98 | end. |
| 99 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 100 | handle_success(State = #thrift_processor{service = Service}, |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 101 | Function, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 102 | Result, |
| 103 | Seqid) -> |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 104 | ReplyType = Service:function_info(Function, reply_type), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 105 | StructName = atom_to_list(Function) ++ "_result", |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 106 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 107 | case Result of |
| 108 | {reply, ReplyData} -> |
| 109 | Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}}, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 110 | send_reply(State, Function, ?tMessageType_REPLY, Reply, Seqid); |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 111 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 112 | ok when ReplyType == {struct, []} -> |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 113 | send_reply(State, Function, ?tMessageType_REPLY, {ReplyType, {StructName}}, Seqid); |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 114 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 115 | ok when ReplyType == oneway_void -> |
| 116 | %% no reply for oneway void |
| 117 | {State, ok} |
| 118 | end. |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 119 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 120 | handle_exception(State = #thrift_processor{service = Service}, |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 121 | Function, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 122 | Exception, |
| 123 | Seqid) -> |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 124 | ExceptionType = element(1, Exception), |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 125 | %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}}, |
| 126 | %% {-3, {struct, {Module, Type}}}]} |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 127 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 128 | ReplySpec = Service:function_info(Function, exceptions), |
| 129 | {struct, XInfo} = ReplySpec, |
| 130 | |
| 131 | true = is_list(XInfo), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 132 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 133 | %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined] |
| 134 | %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}] |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 135 | ExceptionList = [case Type of |
| 136 | ExceptionType -> Exception; |
| 137 | _ -> undefined |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 138 | end |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 139 | || {_Fid, {struct, {_Module, Type}}} <- XInfo], |
| 140 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 141 | ExceptionTuple = list_to_tuple([Function | ExceptionList]), |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 142 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 143 | % Make sure we got at least one defined |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 144 | case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of |
| 145 | true -> |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 146 | handle_unknown_exception(State, Function, Exception, Seqid); |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 147 | false -> |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 148 | send_reply(State, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple}, Seqid) |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 149 | end. |
| 150 | |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 151 | %% |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 152 | %% Called when an exception has been explicitly thrown by the service, but it was |
| 153 | %% not one of the exceptions that was defined for the function. |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 154 | %% |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 155 | handle_unknown_exception(State, Function, Exception, Seqid) -> |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 156 | handle_error(State, Function, {exception_not_declared_as_thrown, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 157 | Exception}, Seqid). |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 158 | |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 159 | handle_error(State, Function, Error, Seqid) -> |
David Reiss | da07067 | 2008-06-11 00:56:42 +0000 | [diff] [blame] | 160 | Stack = erlang:get_stacktrace(), |
| 161 | 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] | 162 | |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 163 | Message = |
| 164 | case application:get_env(thrift, exceptions_include_traces) of |
| 165 | {ok, true} -> |
| 166 | lists:flatten(io_lib:format("An error occurred: ~p~n", |
David Reiss | da07067 | 2008-06-11 00:56:42 +0000 | [diff] [blame] | 167 | [{Error, Stack}])); |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 168 | _ -> |
| 169 | "An unknown handler error occurred." |
| 170 | end, |
| 171 | Reply = {?TApplicationException_Structure, |
| 172 | #'TApplicationException'{ |
| 173 | message = Message, |
| 174 | type = ?TApplicationException_UNKNOWN}}, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 175 | send_reply(State, Function, ?tMessageType_EXCEPTION, Reply, Seqid). |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 176 | |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 177 | send_reply(State = #thrift_processor{protocol = Proto0}, Function, ReplyMessageType, Reply, Seqid) -> |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 178 | {Proto1, ok} = thrift_protocol:write(Proto0, #protocol_message_begin{ |
| 179 | name = atom_to_list(Function), |
| 180 | type = ReplyMessageType, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame^] | 181 | seqid = Seqid}), |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 182 | {Proto2, ok} = thrift_protocol:write(Proto1, Reply), |
| 183 | {Proto3, ok} = thrift_protocol:write(Proto2, message_end), |
| 184 | {Proto4, ok} = thrift_protocol:flush_transport(Proto3), |
| 185 | {State#thrift_processor{protocol = Proto4}, ok}. |