blob: e26fb3303553b35986d50f7e17d2cd0a85254d27 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
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 Reissac549552008-06-10 22:56:59 +000020-module(thrift_processor).
21
David Reissc11734e2008-06-11 00:59:48 +000022-export([init/1]).
David Reissac549552008-06-10 22:56:59 +000023
24-include("thrift_constants.hrl").
25-include("thrift_protocol.hrl").
26
David Reissc11734e2008-06-11 00:59:48 +000027-record(thrift_processor, {handler, in_protocol, out_protocol, service}).
David Reissac549552008-06-10 22:56:59 +000028
David Reissc11734e2008-06-11 00:59:48 +000029init({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 Reissac549552008-06-10 22:56:59 +000035
David Reissc11734e2008-06-11 00:59:48 +000036loop(State = #thrift_processor{in_protocol = IProto,
37 out_protocol = OProto}) ->
David Reissae756f42008-06-10 22:57:11 +000038 case thrift_protocol:read(IProto, message_begin) of
39 #protocol_message_begin{name = Function,
40 type = ?tMessageType_CALL} ->
David Reiss80664fe2008-06-11 01:00:30 +000041 ok = handle_function(State, list_to_atom(Function)),
David Reissae756f42008-06-10 22:57:11 +000042 loop(State);
David Reissdeda1412009-04-02 19:22:31 +000043 #protocol_message_begin{name = Function,
44 type = ?tMessageType_ONEWAY} ->
45 ok = handle_function(State, list_to_atom(Function)),
46 loop(State);
David Reissc49dd1e2008-06-11 01:02:39 +000047 {error, timeout} ->
48 thrift_protocol:close_transport(OProto),
49 ok;
David Reissae756f42008-06-10 22:57:11 +000050 {error, closed} ->
David Reissc11734e2008-06-11 00:59:48 +000051 %% error_logger:info_msg("Client disconnected~n"),
David Reiss80664fe2008-06-11 01:00:30 +000052 thrift_protocol:close_transport(OProto),
53 exit(shutdown)
David Reissac549552008-06-10 22:56:59 +000054 end.
David Reissae756f42008-06-10 22:57:11 +000055
David Reissc11734e2008-06-11 00:59:48 +000056handle_function(State=#thrift_processor{in_protocol = IProto,
57 out_protocol = OProto,
58 handler = Handler,
59 service = Service},
David Reiss1c1ca742008-06-10 22:57:21 +000060 Function) ->
61 InParams = Service:function_info(Function, params_type),
62
63 {ok, Params} = thrift_protocol:read(IProto, InParams),
David Reiss76f0d112008-06-10 22:57:35 +000064
David Reiss982c72d2008-06-10 22:58:33 +000065 try
David Reiss11d855c2008-06-11 00:59:12 +000066 Result = Handler:handle_function(Function, Params),
David Reissc11734e2008-06-11 00:59:48 +000067 %% {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]),
68 %% error_logger:info_msg("Processed ~p(~p) in ~.4fms~n",
69 %% [Function, Params, Micro/1000.0]),
David Reiss982c72d2008-06-10 22:58:33 +000070 handle_success(State, Function, Result)
71 catch
David Reiss5541d652008-06-11 00:57:42 +000072 Type:Data ->
73 handle_function_catch(State, Function, Type, Data)
David Reissc11734e2008-06-11 00:59:48 +000074 end,
75 after_reply(OProto).
David Reiss5541d652008-06-11 00:57:42 +000076
David Reissc11734e2008-06-11 00:59:48 +000077handle_function_catch(State = #thrift_processor{service = Service},
David Reiss5541d652008-06-11 00:57:42 +000078 Function, ErrType, ErrData) ->
David Reissfe931d12009-03-24 20:02:08 +000079 IsOneway = Service:function_info(Function, reply_type) =:= oneway_void,
David Reiss5541d652008-06-11 00:57:42 +000080
81 case {ErrType, ErrData} of
David Reiss6ce401d2009-03-24 20:01:58 +000082 _ when IsOneway ->
David Reiss4ec777e2008-06-11 01:01:29 +000083 Stack = erlang:get_stacktrace(),
David Reiss5541d652008-06-11 00:57:42 +000084 error_logger:warning_msg(
David Reiss6ce401d2009-03-24 20:01:58 +000085 "oneway void ~p threw error which must be ignored: ~p",
David Reiss4ec777e2008-06-11 01:01:29 +000086 [Function, {ErrType, ErrData, Stack}]),
David Reiss5541d652008-06-11 00:57:42 +000087 ok;
88
89 {throw, Exception} when is_tuple(Exception), size(Exception) > 0 ->
David Reiss982c72d2008-06-10 22:58:33 +000090 error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]),
91 handle_exception(State, Function, Exception),
David Reiss920959a2008-06-11 00:56:35 +000092 ok; % we still want to accept more requests from this client
David Reiss5541d652008-06-11 00:57:42 +000093
94 {error, Error} ->
David Reiss920959a2008-06-11 00:56:35 +000095 ok = handle_error(State, Function, Error)
David Reiss982c72d2008-06-10 22:58:33 +000096 end.
97
David Reissc11734e2008-06-11 00:59:48 +000098handle_success(State = #thrift_processor{out_protocol = OProto,
99 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +0000100 Function,
101 Result) ->
David Reiss11d855c2008-06-11 00:59:12 +0000102 ReplyType = Service:function_info(Function, reply_type),
David Reisse1a79982008-06-10 22:58:14 +0000103 StructName = atom_to_list(Function) ++ "_result",
David Reiss11d855c2008-06-11 00:59:12 +0000104
David Reissc11734e2008-06-11 00:59:48 +0000105 ok = case Result of
106 {reply, ReplyData} ->
107 Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}},
108 send_reply(OProto, Function, ?tMessageType_REPLY, Reply);
David Reisse1a79982008-06-10 22:58:14 +0000109
David Reissc11734e2008-06-11 00:59:48 +0000110 ok when ReplyType == {struct, []} ->
111 send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}});
David Reiss11d855c2008-06-11 00:59:12 +0000112
David Reissfe931d12009-03-24 20:02:08 +0000113 ok when ReplyType == oneway_void ->
David Reissc51986f2009-03-24 20:01:25 +0000114 %% no reply for oneway void
David Reissc11734e2008-06-11 00:59:48 +0000115 ok
116 end.
David Reisse1a79982008-06-10 22:58:14 +0000117
David Reissc11734e2008-06-11 00:59:48 +0000118handle_exception(State = #thrift_processor{out_protocol = OProto,
119 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +0000120 Function,
121 Exception) ->
122 ExceptionType = element(1, Exception),
David Reissc11734e2008-06-11 00:59:48 +0000123 %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}},
124 %% {-3, {struct, {Module, Type}}}]}
David Reisse1a79982008-06-10 22:58:14 +0000125
David Reiss982c72d2008-06-10 22:58:33 +0000126 ReplySpec = Service:function_info(Function, exceptions),
127 {struct, XInfo} = ReplySpec,
128
129 true = is_list(XInfo),
David Reiss982c72d2008-06-10 22:58:33 +0000130
David Reissc11734e2008-06-11 00:59:48 +0000131 %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined]
132 %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}]
David Reiss11d855c2008-06-11 00:59:12 +0000133 ExceptionList = [case Type of
134 ExceptionType -> Exception;
135 _ -> undefined
David Reiss982c72d2008-06-10 22:58:33 +0000136 end
David Reiss11d855c2008-06-11 00:59:12 +0000137 || {_Fid, {struct, {_Module, Type}}} <- XInfo],
138
David Reiss982c72d2008-06-10 22:58:33 +0000139 ExceptionTuple = list_to_tuple([Function | ExceptionList]),
David Reiss11d855c2008-06-11 00:59:12 +0000140
David Reissc11734e2008-06-11 00:59:48 +0000141 % Make sure we got at least one defined
David Reiss982c72d2008-06-10 22:58:33 +0000142 case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of
143 true ->
144 ok = handle_unknown_exception(State, Function, Exception);
145 false ->
146 ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple})
147 end.
148
David Reiss920959a2008-06-11 00:56:35 +0000149%%
David Reissc11734e2008-06-11 00:59:48 +0000150%% Called when an exception has been explicitly thrown by the service, but it was
151%% not one of the exceptions that was defined for the function.
David Reiss920959a2008-06-11 00:56:35 +0000152%%
David Reiss982c72d2008-06-10 22:58:33 +0000153handle_unknown_exception(State, Function, Exception) ->
David Reiss920959a2008-06-11 00:56:35 +0000154 handle_error(State, Function, {exception_not_declared_as_thrown,
155 Exception}).
156
David Reissc11734e2008-06-11 00:59:48 +0000157handle_error(#thrift_processor{out_protocol = OProto}, Function, Error) ->
David Reissda070672008-06-11 00:56:42 +0000158 Stack = erlang:get_stacktrace(),
159 error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]),
David Reiss11d855c2008-06-11 00:59:12 +0000160
David Reiss920959a2008-06-11 00:56:35 +0000161 Message =
162 case application:get_env(thrift, exceptions_include_traces) of
163 {ok, true} ->
164 lists:flatten(io_lib:format("An error occurred: ~p~n",
David Reissda070672008-06-11 00:56:42 +0000165 [{Error, Stack}]));
David Reiss920959a2008-06-11 00:56:35 +0000166 _ ->
167 "An unknown handler error occurred."
168 end,
169 Reply = {?TApplicationException_Structure,
170 #'TApplicationException'{
171 message = Message,
172 type = ?TApplicationException_UNKNOWN}},
173 send_reply(OProto, Function, ?tMessageType_EXCEPTION, Reply).
David Reiss982c72d2008-06-10 22:58:33 +0000174
David Reiss982c72d2008-06-10 22:58:33 +0000175send_reply(OProto, Function, ReplyMessageType, Reply) ->
David Reisse1a79982008-06-10 22:58:14 +0000176 ok = thrift_protocol:write(OProto, #protocol_message_begin{
177 name = atom_to_list(Function),
David Reiss982c72d2008-06-10 22:58:33 +0000178 type = ReplyMessageType,
David Reisse1a79982008-06-10 22:58:14 +0000179 seqid = 0}),
180 ok = thrift_protocol:write(OProto, Reply),
181 ok = thrift_protocol:write(OProto, message_end),
David Reiss90b40832008-06-10 22:58:52 +0000182 ok = thrift_protocol:flush_transport(OProto),
David Reisse1a79982008-06-10 22:58:14 +0000183 ok.
David Reiss982c72d2008-06-10 22:58:33 +0000184
David Reissc11734e2008-06-11 00:59:48 +0000185after_reply(OProto) ->
David Reiss7255ed42008-06-11 01:00:04 +0000186 ok = thrift_protocol:flush_transport(OProto)
187 %% ok = thrift_protocol:close_transport(OProto)
188 .