blob: 546bcfe263a740733be9344e5f63c59323cff98c [file] [log] [blame]
David Reissac549552008-06-10 22:56:59 +00001%%%-------------------------------------------------------------------
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
17start(IProt, OProt, Service, Handler) ->
18 spawn(thrift_processor, init, [IProt, OProt, Service, Handler]).
19
20init(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
27loop(State = #state{in_protocol = IProto,
28 out_protocol = OProto}) ->
David Reissae756f42008-06-10 22:57:11 +000029 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 Reissac549552008-06-10 22:56:59 +000037 end.
David Reissae756f42008-06-10 22:57:11 +000038
David Reiss1c1ca742008-06-10 22:57:21 +000039%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 Reissae756f42008-06-10 22:57:11 +000047
David Reiss1c1ca742008-06-10 22:57:21 +000048% {A, B} = Struct,
David Reissae756f42008-06-10 22:57:11 +000049
David Reiss1c1ca742008-06-10 22:57:21 +000050% 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
71handle_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 Reissae756f42008-06-10 22:57:11 +000087 type = ?tMessageType_REPLY,
88 seqid = 0}),
David Reiss1c1ca742008-06-10 22:57:21 +000089 thrift_protocol:write(OProto, {{struct, [{0, ReplyType}]}, {Reply}}),
90 thrift_protocol:write(OProto, message_end)
91 end,
92 ok.