blob: 833fe5db80b7f760821e2fe3c13f4938823c1e3c [file] [log] [blame]
Christopher Piro094823a2007-07-18 00:26:12 +00001%%% Copyright (c) 2007- Facebook
2%%% Distributed under the Thrift Software License
David Reiss0c90f6f2008-02-06 22:18:40 +00003%%%
Christopher Piro094823a2007-07-18 00:26:12 +00004%%% See accompanying file LICENSE or visit the Thrift site at:
5%%% http://developers.facebook.com/thrift/
6
Christopher Piro5b3a8f72007-08-01 22:27:37 +00007%%% NOTE: tSimpleServer's design isn't compatible with our concurrency model.
8%%% It won't work in principle, and certainly not in practice. YMMV.
9
Christopher Piro094823a2007-07-18 00:26:12 +000010-module(tSimpleServer).
11
12-include("oop.hrl").
13
14-include("thrift.hrl").
15-include("transport/tTransportException.hrl").
16-include("server/tSimpleServer.hrl").
17
18-behavior(oop).
19
20-export([attr/4, super/0, inspect/1]).
21
22-export([new/5, new/4, new/3, serve/1]).
23
24%%%
25%%% define attributes
26%%% 'super' is required unless ?MODULE is a base class
27%%%
28
29?DEFINE_ATTR(super).
David Reiss0c90f6f2008-02-06 22:18:40 +000030
Christopher Piro094823a2007-07-18 00:26:12 +000031%%%
32%%% behavior callbacks
33%%%
David Reiss0c90f6f2008-02-06 22:18:40 +000034
Christopher Piro094823a2007-07-18 00:26:12 +000035%%% super() -> SuperModule = atom()
36%%% | none
37
38super() ->
39 tServer.
40
41%%% inspect(This) -> string()
42
43inspect(_This) ->
44 "".
45
46%%%
47%%% class methods
48%%%
49
50new(Handler, Processor, ServerTransport, TransportFactory, ProtocolFactory) ->
51 Super = (super()):new(Handler, Processor, ServerTransport, TransportFactory, ProtocolFactory),
Christopher Piro5b3a8f72007-08-01 22:27:37 +000052 error_logger:warning_msg("tSimpleServer has an incompatable design and doesn't work. Promise."),
Christopher Piro094823a2007-07-18 00:26:12 +000053 #?MODULE{super=Super}.
54
55new(Handler, Processor, ServerTransport) ->
56 new(Handler, Processor, ServerTransport, nil, nil).
57
58new(Handler, Processor, ServerTransport, TransportFactory) ->
59 new(Handler, Processor, ServerTransport, TransportFactory, nil).
60
61%
62
63serve(This) ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000064 exit(tSimpleServer_doesnt_work),
Christopher Piro094823a2007-07-18 00:26:12 +000065 ST = oop:get(This, serverTransport),
66 ?R0(ST, effectful_listen),
67
68 serve_loop(This).
69
70serve_loop(This) ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000071 error_logger:info_msg("ready.", []),
Christopher Piro094823a2007-07-18 00:26:12 +000072
73 ST = oop:get(This, serverTransport),
74 Client = ?RT0(ST, accept, infinity),
75
76 TF = oop:get(This, transportFactory),
77 Trans = ?F1(TF, getTransport, Client), %% cpiro: OPAQUE!! Trans = Client
78
79 PF = oop:get(This, protocolFactory),
80 Prot = ?F1(PF, getProtocol, Trans), %% cpiro: OPAQUE!! Prot = start_new(tBinaryProtocol, [Trans])
81
Christopher Piro5b3a8f72007-08-01 22:27:37 +000082 error_logger:info_msg("client accept()ed", []),
Christopher Piro094823a2007-07-18 00:26:12 +000083
84 serve_loop_loop(This, Prot), % giggle loop?
85
86 ?R0(Trans, effectful_close),
87
88 serve_loop(This).
David Reiss0c90f6f2008-02-06 22:18:40 +000089
Christopher Piro094823a2007-07-18 00:26:12 +000090serve_loop_loop(This, Prot) ->
David Reiss0c90f6f2008-02-06 22:18:40 +000091 Next =
Christopher Piro094823a2007-07-18 00:26:12 +000092 try
93 Handler = oop:get(This, handler),
94 Processor = oop:get(This, processor),
95 Val = apply(Processor, process, [Handler, Prot, Prot]), %% TODO(cpiro): make processor a gen_server instance
Christopher Piro5b3a8f72007-08-01 22:27:37 +000096 error_logger:info_msg("request processed: rv=~p", [Val]),
Christopher Piro094823a2007-07-18 00:26:12 +000097 loop
David Reiss0c90f6f2008-02-06 22:18:40 +000098 catch
Christopher Piro094823a2007-07-18 00:26:12 +000099 %% TODO(cpiro) case when is_record(...) to pick out our exception
100 %% records vs. normal erlang throws
101 E when is_record(E, tTransportException) ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +0000102 error_logger:info_msg("tTransportException (normal-ish?)", []),
Christopher Piro094823a2007-07-18 00:26:12 +0000103 close;
104 F ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +0000105 error_logger:info_msg("EXCEPTION: ~p", [F]),
Christopher Piro094823a2007-07-18 00:26:12 +0000106 close
107 end,
David Reiss0c90f6f2008-02-06 22:18:40 +0000108 case Next of
Christopher Piro094823a2007-07-18 00:26:12 +0000109 loop -> serve_loop_loop(This, Prot);
110 close -> ok
111 end.