factor/basis/channels/remote/remote.factor

68 lines
1.7 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2007 Chris Double. All Rights Reserved.
! See http://factorcode.org/license.txt for BSD license.
!
! Remote Channels
USING: kernel init namespaces assocs arrays random
2008-02-18 08:30:16 -05:00
sequences channels match concurrency.messaging
2008-02-18 10:08:59 -05:00
concurrency.distributed threads ;
2007-09-20 18:09:08 -04:00
IN: channels.remote
<PRIVATE
: remote-channels ( -- hash )
\ remote-channels get-global ;
PRIVATE>
: publish ( channel -- id )
2008-03-19 17:19:37 -04:00
256 random-bits dup >r remote-channels set-at r> ;
2007-09-20 18:09:08 -04:00
: get-channel ( id -- channel )
remote-channels at ;
: unpublish ( id -- )
remote-channels delete-at ;
<PRIVATE
2008-02-18 10:08:59 -05:00
MATCH-VARS: ?from ?tag ?id ?value ;
2007-09-20 18:09:08 -04:00
SYMBOL: no-channel
: channel-process ( -- )
2008-02-20 00:17:59 -05:00
[
2008-02-18 10:08:59 -05:00
{
{ { to ?id ?value }
[ ?value ?id get-channel dup [ to f ] [ 2drop no-channel ] if ] }
2008-02-18 10:08:59 -05:00
{ { from ?id }
[ ?id get-channel [ from ] [ no-channel ] if* ] }
} match-cond
2008-02-20 00:17:59 -05:00
] handle-synchronous ;
2007-09-20 18:09:08 -04:00
PRIVATE>
: start-channel-node ( -- )
2008-02-18 08:30:16 -05:00
"remote-channels" get-process [
2008-02-18 10:08:59 -05:00
"remote-channels"
[ channel-process t ] "Remote channels" spawn-server
2008-02-18 10:08:59 -05:00
register-process
2007-09-20 18:09:08 -04:00
] unless ;
TUPLE: remote-channel node id ;
C: <remote-channel> remote-channel
M: remote-channel to ( value remote-channel -- )
2008-02-18 10:08:59 -05:00
[ [ \ to , remote-channel-id , , ] { } make ] keep
2007-09-20 18:09:08 -04:00
remote-channel-node "remote-channels" <remote-process>
send-synchronous no-channel = [ no-channel throw ] when ;
M: remote-channel from ( remote-channel -- value )
2008-02-18 10:08:59 -05:00
[ [ \ from , remote-channel-id , ] { } make ] keep
2007-09-20 18:09:08 -04:00
remote-channel-node "remote-channels" <remote-process>
send-synchronous dup no-channel = [ no-channel throw ] when* ;
[
H{ } clone \ remote-channels set-global
start-channel-node
] "channel-registry" add-init-hook