blob: 1adea67c78c990ca822b1763c78b0f69acfa80f6 [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 Reissc49dd1e2008-06-11 01:02:39 +000043 {error, timeout} ->
44 thrift_protocol:close_transport(OProto),
45 ok;
David Reissae756f42008-06-10 22:57:11 +000046 {error, closed} ->
David Reissc11734e2008-06-11 00:59:48 +000047 %% error_logger:info_msg("Client disconnected~n"),
David Reiss80664fe2008-06-11 01:00:30 +000048 thrift_protocol:close_transport(OProto),
49 exit(shutdown)
David Reissac549552008-06-10 22:56:59 +000050 end.
David Reissae756f42008-06-10 22:57:11 +000051
David Reissc11734e2008-06-11 00:59:48 +000052handle_function(State=#thrift_processor{in_protocol = IProto,
53 out_protocol = OProto,
54 handler = Handler,
55 service = Service},
David Reiss1c1ca742008-06-10 22:57:21 +000056 Function) ->
57 InParams = Service:function_info(Function, params_type),
58
59 {ok, Params} = thrift_protocol:read(IProto, InParams),
David Reiss76f0d112008-06-10 22:57:35 +000060
David Reiss982c72d2008-06-10 22:58:33 +000061 try
David Reiss11d855c2008-06-11 00:59:12 +000062 Result = Handler:handle_function(Function, Params),
David Reissc11734e2008-06-11 00:59:48 +000063 %% {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 Reiss982c72d2008-06-10 22:58:33 +000066 handle_success(State, Function, Result)
67 catch
David Reiss5541d652008-06-11 00:57:42 +000068 Type:Data ->
69 handle_function_catch(State, Function, Type, Data)
David Reissc11734e2008-06-11 00:59:48 +000070 end,
71 after_reply(OProto).
David Reiss5541d652008-06-11 00:57:42 +000072
David Reissc11734e2008-06-11 00:59:48 +000073handle_function_catch(State = #thrift_processor{service = Service},
David Reiss5541d652008-06-11 00:57:42 +000074 Function, ErrType, ErrData) ->
David Reissfe931d12009-03-24 20:02:08 +000075 IsOneway = Service:function_info(Function, reply_type) =:= oneway_void,
David Reiss5541d652008-06-11 00:57:42 +000076
77 case {ErrType, ErrData} of
David Reiss6ce401d2009-03-24 20:01:58 +000078 _ when IsOneway ->
David Reiss4ec777e2008-06-11 01:01:29 +000079 Stack = erlang:get_stacktrace(),
David Reiss5541d652008-06-11 00:57:42 +000080 error_logger:warning_msg(
David Reiss6ce401d2009-03-24 20:01:58 +000081 "oneway void ~p threw error which must be ignored: ~p",
David Reiss4ec777e2008-06-11 01:01:29 +000082 [Function, {ErrType, ErrData, Stack}]),
David Reiss5541d652008-06-11 00:57:42 +000083 ok;
84
85 {throw, Exception} when is_tuple(Exception), size(Exception) > 0 ->
David Reiss982c72d2008-06-10 22:58:33 +000086 error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]),
87 handle_exception(State, Function, Exception),
David Reiss920959a2008-06-11 00:56:35 +000088 ok; % we still want to accept more requests from this client
David Reiss5541d652008-06-11 00:57:42 +000089
90 {error, Error} ->
David Reiss920959a2008-06-11 00:56:35 +000091 ok = handle_error(State, Function, Error)
David Reiss982c72d2008-06-10 22:58:33 +000092 end.
93
David Reissc11734e2008-06-11 00:59:48 +000094handle_success(State = #thrift_processor{out_protocol = OProto,
95 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +000096 Function,
97 Result) ->
David Reiss11d855c2008-06-11 00:59:12 +000098 ReplyType = Service:function_info(Function, reply_type),
David Reisse1a79982008-06-10 22:58:14 +000099 StructName = atom_to_list(Function) ++ "_result",
David Reiss11d855c2008-06-11 00:59:12 +0000100
David Reissc11734e2008-06-11 00:59:48 +0000101 ok = case Result of
102 {reply, ReplyData} ->
103 Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}},
104 send_reply(OProto, Function, ?tMessageType_REPLY, Reply);
David Reisse1a79982008-06-10 22:58:14 +0000105
David Reissc11734e2008-06-11 00:59:48 +0000106 ok when ReplyType == {struct, []} ->
107 send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}});
David Reiss11d855c2008-06-11 00:59:12 +0000108
David Reissfe931d12009-03-24 20:02:08 +0000109 ok when ReplyType == oneway_void ->
David Reissc51986f2009-03-24 20:01:25 +0000110 %% no reply for oneway void
David Reissc11734e2008-06-11 00:59:48 +0000111 ok
112 end.
David Reisse1a79982008-06-10 22:58:14 +0000113
David Reissc11734e2008-06-11 00:59:48 +0000114handle_exception(State = #thrift_processor{out_protocol = OProto,
115 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +0000116 Function,
117 Exception) ->
118 ExceptionType = element(1, Exception),
David Reissc11734e2008-06-11 00:59:48 +0000119 %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}},
120 %% {-3, {struct, {Module, Type}}}]}
David Reisse1a79982008-06-10 22:58:14 +0000121
David Reiss982c72d2008-06-10 22:58:33 +0000122 ReplySpec = Service:function_info(Function, exceptions),
123 {struct, XInfo} = ReplySpec,
124
125 true = is_list(XInfo),
David Reiss982c72d2008-06-10 22:58:33 +0000126
David Reissc11734e2008-06-11 00:59:48 +0000127 %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined]
128 %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}]
David Reiss11d855c2008-06-11 00:59:12 +0000129 ExceptionList = [case Type of
130 ExceptionType -> Exception;
131 _ -> undefined
David Reiss982c72d2008-06-10 22:58:33 +0000132 end
David Reiss11d855c2008-06-11 00:59:12 +0000133 || {_Fid, {struct, {_Module, Type}}} <- XInfo],
134
David Reiss982c72d2008-06-10 22:58:33 +0000135 ExceptionTuple = list_to_tuple([Function | ExceptionList]),
David Reiss11d855c2008-06-11 00:59:12 +0000136
David Reissc11734e2008-06-11 00:59:48 +0000137 % Make sure we got at least one defined
David Reiss982c72d2008-06-10 22:58:33 +0000138 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 Reiss920959a2008-06-11 00:56:35 +0000145%%
David Reissc11734e2008-06-11 00:59:48 +0000146%% 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 Reiss920959a2008-06-11 00:56:35 +0000148%%
David Reiss982c72d2008-06-10 22:58:33 +0000149handle_unknown_exception(State, Function, Exception) ->
David Reiss920959a2008-06-11 00:56:35 +0000150 handle_error(State, Function, {exception_not_declared_as_thrown,
151 Exception}).
152
David Reissc11734e2008-06-11 00:59:48 +0000153handle_error(#thrift_processor{out_protocol = OProto}, Function, Error) ->
David Reissda070672008-06-11 00:56:42 +0000154 Stack = erlang:get_stacktrace(),
155 error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]),
David Reiss11d855c2008-06-11 00:59:12 +0000156
David Reiss920959a2008-06-11 00:56:35 +0000157 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 Reissda070672008-06-11 00:56:42 +0000161 [{Error, Stack}]));
David Reiss920959a2008-06-11 00:56:35 +0000162 _ ->
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 Reiss982c72d2008-06-10 22:58:33 +0000170
David Reiss982c72d2008-06-10 22:58:33 +0000171send_reply(OProto, Function, ReplyMessageType, Reply) ->
David Reisse1a79982008-06-10 22:58:14 +0000172 ok = thrift_protocol:write(OProto, #protocol_message_begin{
173 name = atom_to_list(Function),
David Reiss982c72d2008-06-10 22:58:33 +0000174 type = ReplyMessageType,
David Reisse1a79982008-06-10 22:58:14 +0000175 seqid = 0}),
176 ok = thrift_protocol:write(OProto, Reply),
177 ok = thrift_protocol:write(OProto, message_end),
David Reiss90b40832008-06-10 22:58:52 +0000178 ok = thrift_protocol:flush_transport(OProto),
David Reisse1a79982008-06-10 22:58:14 +0000179 ok.
David Reiss982c72d2008-06-10 22:58:33 +0000180
David Reissc11734e2008-06-11 00:59:48 +0000181after_reply(OProto) ->
David Reiss7255ed42008-06-11 01:00:04 +0000182 ok = thrift_protocol:flush_transport(OProto)
183 %% ok = thrift_protocol:close_transport(OProto)
184 .