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} -> |
| 32 | ok = handle_function(State, list_to_atom(binary_to_list(Function))), |
| 33 | loop(State); |
| 34 | {error, closed} -> |
| 35 | error_logger:info_msg("Processor finished~n"), |
| 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 | %handle_function(State = #state{in_protocol = IProto, |
| 40 | % out_protocol = OProto}, |
| 41 | % add) -> |
| 42 | % io:format("Reading struct~n"), |
| 43 | % {ok, Struct} = thrift_protocol:read(IProto, |
| 44 | % {struct, [{1, i32}, |
| 45 | % {2, i32}]}), |
| 46 | % io:format("Struct: ~p~n", [Struct]), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 47 | |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame^] | 48 | % {A, B} = Struct, |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 49 | |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame^] | 50 | % thrift_protocol:write(OProto, #protocol_message_begin{ |
| 51 | % name = "addResult", |
| 52 | % type = ?tMessageType_REPLY, |
| 53 | % seqid = 0}), |
| 54 | % thrift_protocol:write(OProto, {{struct, [{0, i32}]}, |
| 55 | % {A + B}}), |
| 56 | % thrift_protocol:write(OProto, message_end); |
| 57 | |
| 58 | %handle_function(State = #state{in_protocol = IProto, |
| 59 | % out_protocol = OProto}, |
| 60 | % complexTest) -> |
| 61 | % io:format("Reading struct~n"), |
| 62 | % Struct = thrift_protocol:read( |
| 63 | % IProto, |
| 64 | % {struct, [{1, {struct, |
| 65 | % [{1, {list, i32}}, |
| 66 | % {2, {map, string, {struct, [{1, i16}]}}}]}}]}), |
| 67 | |
| 68 | % io:format("Struct: ~p~n", [Struct]); |
| 69 | |
| 70 | |
| 71 | handle_function(State = #state{in_protocol = IProto, |
| 72 | out_protocol = OProto, |
| 73 | handler = Handler, |
| 74 | service = Service}, |
| 75 | Function) -> |
| 76 | InParams = Service:function_info(Function, params_type), |
| 77 | |
| 78 | {ok, Params} = thrift_protocol:read(IProto, InParams), |
| 79 | Result = Handler:handle_function(Function, Params), |
| 80 | |
| 81 | ReplyType = Service:function_info(Function, reply_type), |
| 82 | |
| 83 | case Result of |
| 84 | {reply, Reply} -> |
| 85 | thrift_protocol:write(OProto, #protocol_message_begin{ |
| 86 | name = atom_to_list(Function) ++ "_result", |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 87 | type = ?tMessageType_REPLY, |
| 88 | seqid = 0}), |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame^] | 89 | thrift_protocol:write(OProto, {{struct, [{0, ReplyType}]}, {Reply}}), |
| 90 | thrift_protocol:write(OProto, message_end) |
| 91 | end, |
| 92 | ok. |