blob: 88af05a8d9805b6d997116218e5878a2f08575b6 [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 Reiss035979f2010-08-30 22:05:38 +000027-record(thrift_processor, {handler, protocol, service}).
David Reissac549552008-06-10 22:56:59 +000028
David Reiss5ed313d2010-08-30 22:05:57 +000029init({_Server, ProtoGen, Service, Handler}) when is_function(ProtoGen, 0) ->
David Reiss035979f2010-08-30 22:05:38 +000030 {ok, Proto} = ProtoGen(),
31 loop(#thrift_processor{protocol = Proto,
David Reissc11734e2008-06-11 00:59:48 +000032 service = Service,
33 handler = Handler}).
David Reissac549552008-06-10 22:56:59 +000034
David Reiss035979f2010-08-30 22:05:38 +000035loop(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 Reissae756f42008-06-10 22:57:11 +000039 #protocol_message_begin{name = Function,
Bryan Duxburya2cceb42011-03-02 18:25:24 +000040 type = ?tMessageType_CALL,
41 seqid = Seqid} ->
42 {State2, ok} = handle_function(State1, list_to_atom(Function), Seqid),
David Reiss035979f2010-08-30 22:05:38 +000043 loop(State2);
David Reissdeda1412009-04-02 19:22:31 +000044 #protocol_message_begin{name = Function,
Bryan Duxburya2cceb42011-03-02 18:25:24 +000045 type = ?tMessageType_ONEWAY,
46 seqid = Seqid} ->
47 {State2, ok} = handle_function(State1, list_to_atom(Function), Seqid),
David Reiss035979f2010-08-30 22:05:38 +000048 loop(State2);
David Reissc49dd1e2008-06-11 01:02:39 +000049 {error, timeout} ->
David Reiss035979f2010-08-30 22:05:38 +000050 thrift_protocol:close_transport(Proto1),
David Reissc49dd1e2008-06-11 01:02:39 +000051 ok;
David Reissae756f42008-06-10 22:57:11 +000052 {error, closed} ->
David Reissc11734e2008-06-11 00:59:48 +000053 %% error_logger:info_msg("Client disconnected~n"),
David Reiss035979f2010-08-30 22:05:38 +000054 thrift_protocol:close_transport(Proto1),
David Reiss80664fe2008-06-11 01:00:30 +000055 exit(shutdown)
David Reissac549552008-06-10 22:56:59 +000056 end.
David Reissae756f42008-06-10 22:57:11 +000057
David Reiss035979f2010-08-30 22:05:38 +000058handle_function(State0=#thrift_processor{protocol = Proto0,
59 handler = Handler,
60 service = Service},
Bryan Duxburya2cceb42011-03-02 18:25:24 +000061 Function,
62 Seqid) ->
David Reiss1c1ca742008-06-10 22:57:21 +000063 InParams = Service:function_info(Function, params_type),
64
David Reiss035979f2010-08-30 22:05:38 +000065 {Proto1, {ok, Params}} = thrift_protocol:read(Proto0, InParams),
66 State1 = State0#thrift_processor{protocol = Proto1},
David Reiss76f0d112008-06-10 22:57:35 +000067
David Reiss982c72d2008-06-10 22:58:33 +000068 try
David Reiss11d855c2008-06-11 00:59:12 +000069 Result = Handler:handle_function(Function, Params),
David Reissc11734e2008-06-11 00:59:48 +000070 %% {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 Duxburya2cceb42011-03-02 18:25:24 +000073 handle_success(State1, Function, Result, Seqid)
David Reiss982c72d2008-06-10 22:58:33 +000074 catch
David Reissb9561c82010-08-30 22:05:36 +000075 Type:Data when Type =:= throw orelse Type =:= error ->
Bryan Duxburya2cceb42011-03-02 18:25:24 +000076 handle_function_catch(State1, Function, Type, Data, Seqid)
David Reiss4b0534a2010-08-30 22:05:37 +000077 end.
David Reiss5541d652008-06-11 00:57:42 +000078
David Reissc11734e2008-06-11 00:59:48 +000079handle_function_catch(State = #thrift_processor{service = Service},
Bryan Duxburya2cceb42011-03-02 18:25:24 +000080 Function, ErrType, ErrData, Seqid) ->
David Reissfe931d12009-03-24 20:02:08 +000081 IsOneway = Service:function_info(Function, reply_type) =:= oneway_void,
David Reiss5541d652008-06-11 00:57:42 +000082
83 case {ErrType, ErrData} of
David Reiss6ce401d2009-03-24 20:01:58 +000084 _ when IsOneway ->
David Reiss4ec777e2008-06-11 01:01:29 +000085 Stack = erlang:get_stacktrace(),
David Reiss5541d652008-06-11 00:57:42 +000086 error_logger:warning_msg(
David Reiss6ce401d2009-03-24 20:01:58 +000087 "oneway void ~p threw error which must be ignored: ~p",
David Reiss4ec777e2008-06-11 01:01:29 +000088 [Function, {ErrType, ErrData, Stack}]),
David Reiss035979f2010-08-30 22:05:38 +000089 {State, ok};
David Reiss5541d652008-06-11 00:57:42 +000090
91 {throw, Exception} when is_tuple(Exception), size(Exception) > 0 ->
David Reissc0e4b5b2010-08-30 22:05:12 +000092 %error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]),
Bryan Duxburya2cceb42011-03-02 18:25:24 +000093 handle_exception(State, Function, Exception, Seqid);
David Reiss035979f2010-08-30 22:05:38 +000094 % we still want to accept more requests from this client
David Reiss5541d652008-06-11 00:57:42 +000095
96 {error, Error} ->
Bryan Duxburya2cceb42011-03-02 18:25:24 +000097 handle_error(State, Function, Error, Seqid)
David Reiss982c72d2008-06-10 22:58:33 +000098 end.
99
David Reiss035979f2010-08-30 22:05:38 +0000100handle_success(State = #thrift_processor{service = Service},
David Reiss982c72d2008-06-10 22:58:33 +0000101 Function,
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000102 Result,
103 Seqid) ->
David Reiss11d855c2008-06-11 00:59:12 +0000104 ReplyType = Service:function_info(Function, reply_type),
David Reisse1a79982008-06-10 22:58:14 +0000105 StructName = atom_to_list(Function) ++ "_result",
David Reiss11d855c2008-06-11 00:59:12 +0000106
David Reiss035979f2010-08-30 22:05:38 +0000107 case Result of
108 {reply, ReplyData} ->
109 Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}},
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000110 send_reply(State, Function, ?tMessageType_REPLY, Reply, Seqid);
David Reisse1a79982008-06-10 22:58:14 +0000111
David Reiss035979f2010-08-30 22:05:38 +0000112 ok when ReplyType == {struct, []} ->
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000113 send_reply(State, Function, ?tMessageType_REPLY, {ReplyType, {StructName}}, Seqid);
David Reiss11d855c2008-06-11 00:59:12 +0000114
David Reiss035979f2010-08-30 22:05:38 +0000115 ok when ReplyType == oneway_void ->
116 %% no reply for oneway void
117 {State, ok}
118 end.
David Reisse1a79982008-06-10 22:58:14 +0000119
David Reiss035979f2010-08-30 22:05:38 +0000120handle_exception(State = #thrift_processor{service = Service},
David Reiss982c72d2008-06-10 22:58:33 +0000121 Function,
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000122 Exception,
123 Seqid) ->
David Reiss982c72d2008-06-10 22:58:33 +0000124 ExceptionType = element(1, Exception),
David Reissc11734e2008-06-11 00:59:48 +0000125 %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}},
126 %% {-3, {struct, {Module, Type}}}]}
David Reisse1a79982008-06-10 22:58:14 +0000127
David Reiss982c72d2008-06-10 22:58:33 +0000128 ReplySpec = Service:function_info(Function, exceptions),
129 {struct, XInfo} = ReplySpec,
130
131 true = is_list(XInfo),
David Reiss982c72d2008-06-10 22:58:33 +0000132
David Reissc11734e2008-06-11 00:59:48 +0000133 %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined]
134 %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}]
David Reiss11d855c2008-06-11 00:59:12 +0000135 ExceptionList = [case Type of
136 ExceptionType -> Exception;
137 _ -> undefined
David Reiss982c72d2008-06-10 22:58:33 +0000138 end
David Reiss11d855c2008-06-11 00:59:12 +0000139 || {_Fid, {struct, {_Module, Type}}} <- XInfo],
140
David Reiss982c72d2008-06-10 22:58:33 +0000141 ExceptionTuple = list_to_tuple([Function | ExceptionList]),
David Reiss11d855c2008-06-11 00:59:12 +0000142
David Reissc11734e2008-06-11 00:59:48 +0000143 % Make sure we got at least one defined
David Reiss982c72d2008-06-10 22:58:33 +0000144 case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of
145 true ->
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000146 handle_unknown_exception(State, Function, Exception, Seqid);
David Reiss982c72d2008-06-10 22:58:33 +0000147 false ->
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000148 send_reply(State, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple}, Seqid)
David Reiss982c72d2008-06-10 22:58:33 +0000149 end.
150
David Reiss920959a2008-06-11 00:56:35 +0000151%%
David Reissc11734e2008-06-11 00:59:48 +0000152%% 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 Reiss920959a2008-06-11 00:56:35 +0000154%%
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000155handle_unknown_exception(State, Function, Exception, Seqid) ->
David Reiss920959a2008-06-11 00:56:35 +0000156 handle_error(State, Function, {exception_not_declared_as_thrown,
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000157 Exception}, Seqid).
David Reiss920959a2008-06-11 00:56:35 +0000158
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000159handle_error(State, Function, Error, Seqid) ->
David Reissda070672008-06-11 00:56:42 +0000160 Stack = erlang:get_stacktrace(),
161 error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]),
David Reiss11d855c2008-06-11 00:59:12 +0000162
David Reiss920959a2008-06-11 00:56:35 +0000163 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 Reissda070672008-06-11 00:56:42 +0000167 [{Error, Stack}]));
David Reiss920959a2008-06-11 00:56:35 +0000168 _ ->
169 "An unknown handler error occurred."
170 end,
171 Reply = {?TApplicationException_Structure,
172 #'TApplicationException'{
173 message = Message,
174 type = ?TApplicationException_UNKNOWN}},
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000175 send_reply(State, Function, ?tMessageType_EXCEPTION, Reply, Seqid).
David Reiss982c72d2008-06-10 22:58:33 +0000176
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000177send_reply(State = #thrift_processor{protocol = Proto0}, Function, ReplyMessageType, Reply, Seqid) ->
David Reiss035979f2010-08-30 22:05:38 +0000178 {Proto1, ok} = thrift_protocol:write(Proto0, #protocol_message_begin{
179 name = atom_to_list(Function),
180 type = ReplyMessageType,
Bryan Duxburya2cceb42011-03-02 18:25:24 +0000181 seqid = Seqid}),
David Reiss035979f2010-08-30 22:05:38 +0000182 {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}.