factor/extra/concurrency/distributed/distributed.factor

46 lines
1.2 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2005 Chris Double. All Rights Reserved.
! See http://factorcode.org/license.txt for BSD license.
2008-03-29 00:00:20 -04:00
USING: serialize sequences concurrency.messaging threads io
io.server qualified arrays namespaces kernel io.encodings.binary
2008-03-20 16:30:59 -04:00
accessors ;
FROM: io.sockets => host-name <inet> with-client ;
2007-09-20 18:09:08 -04:00
IN: concurrency.distributed
2008-03-11 22:01:39 -04:00
SYMBOL: local-node
2008-02-18 10:08:59 -05:00
2007-09-20 18:09:08 -04:00
: handle-node-client ( -- )
deserialize
[ first2 get-process send ]
[ stop-server ] if* ;
2007-09-20 18:09:08 -04:00
2008-02-18 06:07:40 -05:00
: (start-node) ( addrspecs addrspec -- )
local-node set-global
2008-02-18 06:07:40 -05:00
[
"concurrency.distributed"
binary
[ handle-node-client ] with-server
] curry "Distributed concurrency server" spawn drop ;
2007-09-20 18:09:08 -04:00
2008-02-18 06:07:40 -05:00
: start-node ( port -- )
[ internet-server ]
[ host-name swap <inet> ] bi
(start-node) ;
2008-02-18 10:08:59 -05:00
TUPLE: remote-process id node ;
2007-09-20 18:09:08 -04:00
2008-02-18 10:08:59 -05:00
C: <remote-process> remote-process
2007-09-20 18:09:08 -04:00
: send-remote-message ( message node -- )
binary [ serialize ] with-client ;
2008-02-18 10:08:59 -05:00
M: remote-process send ( message thread -- )
[ id>> 2array ] [ node>> ] bi
send-remote-message ;
2007-09-20 18:09:08 -04:00
2008-02-18 06:07:40 -05:00
M: thread (serialize) ( obj -- )
thread-id local-node get-global <remote-process>
2008-02-18 06:07:40 -05:00
(serialize) ;
: stop-node ( node -- )
f swap send-remote-message ;