blob: fd6972dbbec607448308b9f71e5972f9fdc027ae [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
39handle_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]),
47
48 {A, B} = Struct,
49
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
58handle_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