1 %%% Copyright (c) 2007- Facebook
2 %%% Distributed under the Thrift Software License
4 %%% See accompanying file LICENSE or visit the Thrift site at:
5 %%% http://developers.facebook.com/thrift/
11 -include("thrift.hrl").
12 -include("transport/tTransportException.hrl").
13 -include("server/tErlServer.hrl").
17 -export([attr
/4, super
/0, inspect
/1]).
19 -export([new
/6, new
/5, new
/4, effectful_serve
/1, effectful_new_acceptor
/1]).
23 %%% 'super' is required unless ?MODULE is a base class
27 ?
DEFINE_ATTR(acceptor
);
28 ?
DEFINE_ATTR(listenSocket
);
32 %%% behavior callbacks
35 %%% super() -> SuperModule = atom()
41 %%% inspect(This) -> string()
44 ?
FORMAT_ATTR(acceptor
) ++ ", " ++
45 ?
FORMAT_ATTR(listenSocket
) ++ ", " ++
52 new(Port
, Handler
, Processor
, ServerTransport
, TransportFactory
, ProtocolFactory
) ->
53 Super
= (super()):new(Handler
, Processor
, ServerTransport
, TransportFactory
, ProtocolFactory
),
54 #?MODULE
{super
=Super
, port
=Port
, listenSocket
=nil
, acceptor
=nil
}.
56 new(Port
, Handler
, Processor
, ServerTransport
) ->
57 new(Port
, Handler
, Processor
, ServerTransport
, nil
, nil
).
59 new(Port
, Handler
, Processor
, ServerTransport
, TransportFactory
) ->
60 new(Port
, Handler
, Processor
, ServerTransport
, TransportFactory
, nil
).
62 % listenSocket, acceptor, port
64 effectful_serve(This
) ->
65 Port
= oop:get(This
, port
),
67 Options
= [binary, {packet
, 0}, {active
, false
}],
70 case gen_tcp:listen(Port
, Options
) of
72 ?
INFO("thrift server listening on port ~p", [Port
]),
74 This1
= oop:set(This
, listenSocket
, ListenSocket
),
77 {_Acceptor
, This2
} = effectful_new_acceptor(This1
),
81 {error
, eaddrinuse
} ->
82 ?
ERROR("thrift couldn't bind port ~p, address in use", [Port
]),
83 {{error
, eaddrinuse
}, This
} %% state before the accept
86 effectful_new_acceptor(This
) ->
87 ListenSocket
= oop:get(This
, listenSocket
),
88 Processor
= oop:get(This
, processor
), %% cpiro: generated processor, not the "actual" processor
89 Handler
= oop:get(This
, handler
),
91 TF
= oop:get(This
, transportFactory
),
92 PF
= oop:get(This
, protocolFactory
),
94 tErlAcceptor
= oop:get(This
, serverTransport
), %% cpiro: only supported ServerTransport
97 Acceptor
= oop:start_new(tErlAcceptor
, [ServerPid
, TF
, PF
]),
98 ?
C3(Acceptor
, accept
, ListenSocket
, Processor
, Handler
),
100 This1
= oop:set(This
, acceptor
, Acceptor
),