[newbie] accept()-ing process under supervision (was:Erlangish way of Iterator pattern)
Gaspar Chilingarov
nm@REDACTED
Wed Jan 26 12:06:54 CET 2005
Yeah, i know, you have some tasty things on your pages :)
but i wish to run process which does gen_tcp:accept under supervision --
just to be sure that it always listening. processes which serve each
connections are out of my interest (for now) -- they can crash when they
want :)
Joe Armstrong (AL/EAB) wrote:
> To solve problems like this I almost always use a module called tcp_server which is
> available at
>
> http://www.sics.se/~joe/tutorials/web_server/tcp_server.erl
>
> To use it call:
>
> tcp_server:start(Port, Fun, Max)
>
> This starts a server which listens to Port up to Max simultaneous connections on Port are allowed.
>
> The *first* time a connection is made to Port Then Fun(Socket) is called.
> Thereafter messages to the socket result in messages to the handler.
>
> A typical server is usually written like this:
>
> start_server(Port) ->
> S = self(),
> process_flag(trap_exit, true),
> tcp_server:start_raw_server(Port,
> fun(Socket) -> input_handler(Socket, S) end,
> 15,
> 0)
> loop().
>
> The loop() process is a central controller that all processes can use to synchronize amongst themselves if necessary It ends up as the variable "Controller" in the input_handler
>
> A typical server is written like this:
>
> input_handler(Socket, Controller) ->
> receive
> {tcp, Socket, Bin} ->
> ...
> gen_tcp:send(Socket, ...)
> {tcp_closed, Socket} ->
>
> Any ->
> ...
> end.
>
> tcp_server can be used to build many different things. For example a web server or wiki system.
>
> If you want to see how to make a web server using tcp_server read my second "spitting in the dust tutorial" on how to
> make a web server.
>
> This is at
>
> http://www.sics.se/~joe/tutorials/web_server/web_server.html
>
> This has all the code for a simple web server - the web server itself is split into a http parser (222 lines of code)
> a server (104 lines) and the tcp_server (175) lines.
>
> I'll show you the code for the web server - it's like this:
>
> server(Client, Master) ->
> receive
> {Client, closed} ->
> true;
> {Client, Request} ->
> Response = generate_response(Request),
> Client ! {self(), Response},
> server(Client, Master)
> after 5000 ->
> true
> end.
>
> That's all :-)
>
> The structure used in the web server provides a useful model of how to build these type of things. It has
>
> - a connection handler (tcp_server) which manages tcp sessions
> - a device driver that parses HTTP messages and turns them into Erlang messages
> - a pure Erlang server
>
> The general philosophy is "pretend that all things in the outside world are Erlang processes" - thus to program a web
> server we model the browser as an Erlang process - it sends a {get, File} message to the server. The job of the
> connection handler and device driver is to take the HTTP protocol message and turn them into Erlang terms.
> Having done so, writing the server is a doodle.
>
> Cheers
>
> /Joe
>
>
>
>
>>-----Original Message-----
>>From: owner-erlang-questions@REDACTED
>>[mailto:owner-erlang-questions@REDACTED]On Behalf Of Gaspar
>>Chilingarov
>>Sent: den 25 januari 2005 15:20
>>To: erlang-questions@REDACTED
>>Subject: [newbie] Erlangish way of Iterator pattern
>>
>>
>>Hello!
>>
>>I'm beginning to program in erlang and hit the following problem.
>>in mainstream language i will do the following -- create
>>Iterator class
>>to read data from socket or file
>>i.e. class with Open/GetNext functions. Open should just do
>>all work for
>>preparing data source and GetNext will return lines of data or
>>EOF(freeing resources when reaching EOF).
>>
>>
>>what i have in erlang -- i have erlang process, which can accept data
>>messages and eof messages and do processing. another process hangs
>>around, waiting for TCP connection and feeding first process
>>with data
>>read from TCP. in the same time i have shell script which runs once a
>>minute and feeds data from some file(or piped command line)
>>to TCP port.
>>
>>now i want to get rid of shell script, but i wish to have separate
>>module which will do this -- i.e. 1. running periodically external
>>command, 2. feeding to another process.
>>
>>are there any idioms in Erlang for this or it should be
>>implemented in
>>straight way -- i.e. just another process sitting there and
>>doing it's
>>job -- file reading/parsing/feeding to another process ?
>>
>>
>>
>>--
>>Gaspar Chilingarov
>>System Administrator
>>
>>t +3749 419763
>>w www.web.am
>>e nm@REDACTED
>>
>
>
--
Gaspar Chilingarov
System Administrator
t +3749 419763
w www.web.am
e nm@REDACTED
More information about the erlang-questions
mailing list