Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 3 | %%% |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 4 | %%% See accompanying file LICENSE or visit the Thrift site at: |
| 5 | %%% http://developers.facebook.com/thrift/ |
| 6 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 7 | %%% 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 Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 10 | -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 Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 30 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 31 | %%% |
| 32 | %%% behavior callbacks |
| 33 | %%% |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 34 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 35 | %%% super() -> SuperModule = atom() |
| 36 | %%% | none |
| 37 | |
| 38 | super() -> |
| 39 | tServer. |
| 40 | |
| 41 | %%% inspect(This) -> string() |
| 42 | |
| 43 | inspect(_This) -> |
| 44 | "". |
| 45 | |
| 46 | %%% |
| 47 | %%% class methods |
| 48 | %%% |
| 49 | |
| 50 | new(Handler, Processor, ServerTransport, TransportFactory, ProtocolFactory) -> |
| 51 | Super = (super()):new(Handler, Processor, ServerTransport, TransportFactory, ProtocolFactory), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 52 | error_logger:warning_msg("tSimpleServer has an incompatable design and doesn't work. Promise."), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 53 | #?MODULE{super=Super}. |
| 54 | |
| 55 | new(Handler, Processor, ServerTransport) -> |
| 56 | new(Handler, Processor, ServerTransport, nil, nil). |
| 57 | |
| 58 | new(Handler, Processor, ServerTransport, TransportFactory) -> |
| 59 | new(Handler, Processor, ServerTransport, TransportFactory, nil). |
| 60 | |
| 61 | % |
| 62 | |
| 63 | serve(This) -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 64 | exit(tSimpleServer_doesnt_work), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 65 | ST = oop:get(This, serverTransport), |
| 66 | ?R0(ST, effectful_listen), |
| 67 | |
| 68 | serve_loop(This). |
| 69 | |
| 70 | serve_loop(This) -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 71 | error_logger:info_msg("ready.", []), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 72 | |
| 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 Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 82 | error_logger:info_msg("client accept()ed", []), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 83 | |
| 84 | serve_loop_loop(This, Prot), % giggle loop? |
| 85 | |
| 86 | ?R0(Trans, effectful_close), |
| 87 | |
| 88 | serve_loop(This). |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 89 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 90 | serve_loop_loop(This, Prot) -> |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 91 | Next = |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 92 | 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 Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 96 | error_logger:info_msg("request processed: rv=~p", [Val]), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 97 | loop |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 98 | catch |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 99 | %% 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 Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 102 | error_logger:info_msg("tTransportException (normal-ish?)", []), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 103 | close; |
| 104 | F -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 105 | error_logger:info_msg("EXCEPTION: ~p", [F]), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 106 | close |
| 107 | end, |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 108 | case Next of |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 109 | loop -> serve_loop_loop(This, Prot); |
| 110 | close -> ok |
| 111 | end. |