David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 1 | %%%------------------------------------------------------------------- |
| 2 | %%% File : thrift_processor.erl |
| 3 | %%% Author : <todd@lipcon.org> |
| 4 | %%% Description : |
| 5 | %%% |
| 6 | %%% Created : 28 Jan 2008 by <todd@lipcon.org> |
| 7 | %%%------------------------------------------------------------------- |
| 8 | -module(thrift_processor). |
| 9 | |
| 10 | -export([start/4,init/4]). |
| 11 | |
| 12 | -include("thrift_constants.hrl"). |
| 13 | -include("thrift_protocol.hrl"). |
| 14 | |
| 15 | -record(state, {handler, in_protocol, out_protocol, service}). |
| 16 | |
| 17 | start(IProt, OProt, Service, Handler) -> |
| 18 | spawn(thrift_processor, init, [IProt, OProt, Service, Handler]). |
| 19 | |
| 20 | init(IProt, OProt, Service, Handler) -> |
| 21 | io:format("Processor started~n"), |
| 22 | loop(#state{in_protocol = IProt, |
| 23 | out_protocol = OProt, |
| 24 | service = Service, |
| 25 | handler = Handler}). |
| 26 | |
| 27 | loop(State = #state{in_protocol = IProto, |
| 28 | out_protocol = OProto}) -> |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 29 | case thrift_protocol:read(IProto, message_begin) of |
| 30 | #protocol_message_begin{name = Function, |
| 31 | type = ?tMessageType_CALL} -> |
David Reiss | d93521b | 2008-06-10 22:58:07 +0000 | [diff] [blame^] | 32 | ok= handle_function(State, list_to_atom(Function)), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 33 | loop(State); |
| 34 | {error, closed} -> |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 35 | error_logger:info_msg("Client disconnected~n"), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 36 | ok |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 37 | end. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 38 | |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 39 | |
| 40 | handle_function(State = #state{in_protocol = IProto, |
| 41 | out_protocol = OProto, |
| 42 | handler = Handler, |
| 43 | service = Service}, |
| 44 | Function) -> |
| 45 | InParams = Service:function_info(Function, params_type), |
| 46 | |
| 47 | {ok, Params} = thrift_protocol:read(IProto, InParams), |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 48 | |
| 49 | {Micro, Result} = timer:tc(Handler, handle_function, [Function, Params]), |
| 50 | error_logger:info_msg("Processed ~p(~p) in ~.4fms~n", |
| 51 | [Function, Params, Micro/1000.0]), |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 52 | |
| 53 | ReplyType = Service:function_info(Function, reply_type), |
| 54 | |
| 55 | case Result of |
| 56 | {reply, Reply} -> |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 57 | StructName = atom_to_list(Function) ++ "_result", |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 58 | thrift_protocol:write(OProto, #protocol_message_begin{ |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 59 | name = StructName, |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 60 | type = ?tMessageType_REPLY, |
| 61 | seqid = 0}), |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 62 | thrift_protocol:write(OProto, {{struct, [{0, ReplyType}]}, {StructName, Reply}}), |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 63 | thrift_protocol:write(OProto, message_end) |
| 64 | end, |
| 65 | ok. |