factor/extra/io/server/server.factor

73 lines
1.7 KiB
Factor
Raw Normal View History

2008-02-05 17:36:11 -05:00
! Copyright (C) 2003, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-02-07 18:07:43 -05:00
USING: io io.sockets io.files logging continuations kernel
2008-02-07 02:02:26 -05:00
math math.parser namespaces parser sequences strings
2008-02-18 08:30:16 -05:00
prettyprint debugger quotations calendar
threads concurrency.combinators assocs ;
2007-09-20 18:09:08 -04:00
IN: io.server
SYMBOL: servers
<PRIVATE
2008-02-07 18:07:43 -05:00
LOG: accepted-connection NOTICE
: with-client ( client quot -- )
[
over client-stream-addr accepted-connection
with-stream*
] curry with-disposal ; inline
\ with-client DEBUG add-error-logging
2007-09-20 18:09:08 -04:00
2008-02-01 23:47:01 -05:00
: accept-loop ( server quot -- )
2008-02-07 18:07:43 -05:00
[
>r accept r> [ with-client ] 2curry "Client" spawn drop
2008-02-07 18:07:43 -05:00
] 2keep accept-loop ; inline
2007-09-20 18:09:08 -04:00
: server-loop ( addrspec encoding quot -- )
>r <server> dup servers get push r>
2008-02-01 23:47:01 -05:00
[ accept-loop ] curry with-disposal ; inline
2007-09-20 18:09:08 -04:00
\ server-loop NOTICE add-error-logging
2008-02-07 18:07:43 -05:00
PRIVATE>
2007-09-20 18:09:08 -04:00
: local-server ( port -- seq )
"localhost" swap t resolve-host ;
: internet-server ( port -- seq )
f swap t resolve-host ;
: with-server ( seq service encoding quot -- )
V{ } clone [
swap servers [
[ server-loop ] 2curry with-logging
] with-variable
] 3curry curry parallel-each ; inline
2007-09-20 18:09:08 -04:00
2008-02-11 17:10:03 -05:00
: stop-server ( -- )
servers get [ dispose ] each ;
<PRIVATE
2008-02-07 18:07:43 -05:00
LOG: received-datagram NOTICE
2007-09-20 18:09:08 -04:00
: datagram-loop ( quot datagram -- )
[
2008-02-07 18:07:43 -05:00
[ receive dup received-datagram >r swap call r> ] keep
2007-11-12 01:41:27 -05:00
pick [ send ] [ 3drop ] keep
2007-09-20 18:09:08 -04:00
] 2keep datagram-loop ; inline
: spawn-datagrams ( quot addrspec -- )
<datagram> [ datagram-loop ] with-disposal ; inline
2007-09-20 18:09:08 -04:00
2008-02-07 18:07:43 -05:00
\ spawn-datagrams NOTICE add-input-logging
PRIVATE>
2007-09-20 18:09:08 -04:00
: with-datagrams ( seq service quot -- )
[
2008-02-18 08:30:16 -05:00
[ swap spawn-datagrams ] curry parallel-each
2007-11-12 01:41:27 -05:00
] curry with-logging ; inline