1 ! Copyright (C) 2007 Chris Double. All Rights Reserved.
2 ! See http://factorcode.org/license.txt for BSD license.
5 USING: kernel init namespaces make assocs arrays random
6 sequences channels match concurrency.messaging
7 concurrency.distributed threads accessors ;
12 : remote-channels ( -- hash )
13 \ remote-channels get-global ;
16 : publish ( channel -- id )
17 256 random-bits dup [ remote-channels set-at ] dip ;
19 : get-channel ( id -- channel )
23 remote-channels delete-at ;
27 MATCH-VARS: ?from ?tag ?id ?value ;
31 : channel-process ( -- )
35 [ ?value ?id get-channel dup [ to f ] [ 2drop no-channel ] if ] }
37 [ ?id get-channel [ from ] [ no-channel ] if* ] }
39 ] handle-synchronous ;
43 : start-channel-node ( -- )
44 "remote-channels" get-process [
46 [ channel-process t ] "Remote channels" spawn-server
50 TUPLE: remote-channel node id ;
52 C: <remote-channel> remote-channel
54 M: remote-channel to ( value remote-channel -- )
55 [ [ \ to , id>> , , ] { } make ] keep
56 node>> "remote-channels" <remote-process>
57 send-synchronous no-channel = [ no-channel throw ] when ;
59 M: remote-channel from ( remote-channel -- value )
60 [ [ \ from , id>> , ] { } make ] keep
61 node>> "remote-channels" <remote-process>
62 send-synchronous dup no-channel = [ no-channel throw ] when* ;
65 H{ } clone \ remote-channels set-global
67 ] "channel-registry" add-init-hook