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
|
2008-09-10 23:11:40 -04:00
|
|
|
USING: kernel init namespaces make assocs arrays random
|
2008-02-18 08:30:16 -05:00
|
|
|
sequences channels match concurrency.messaging
|
2008-09-02 13:48:45 -04:00
|
|
|
concurrency.distributed threads accessors ;
|
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 }
|
2008-02-18 17:20:18 -05:00
|
|
|
[ ?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"
|
2008-02-18 17:20:18 -05:00
|
|
|
[ 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-09-02 13:48:45 -04:00
|
|
|
[ [ \ to , id>> , , ] { } make ] keep
|
|
|
|
node>> "remote-channels" <remote-process>
|
2007-09-20 18:09:08 -04:00
|
|
|
send-synchronous no-channel = [ no-channel throw ] when ;
|
|
|
|
|
|
|
|
M: remote-channel from ( remote-channel -- value )
|
2008-09-02 13:48:45 -04:00
|
|
|
[ [ \ from , id>> , ] { } make ] keep
|
|
|
|
node>> "remote-channels" <remote-process>
|
2007-09-20 18:09:08 -04:00
|
|
|
send-synchronous dup no-channel = [ no-channel throw ] when* ;
|
|
|
|
|
|
|
|
[
|
|
|
|
H{ } clone \ remote-channels set-global
|
|
|
|
start-channel-node
|
|
|
|
] "channel-registry" add-init-hook
|