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-thread ( -- )
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-remote-thread [
45 [ channel-thread t ] "Remote channels" spawn-server
46 "remote-channels" register-remote-thread
49 TUPLE: remote-channel node id ;
51 C: <remote-channel> remote-channel
53 M: remote-channel to ( value remote-channel -- )
54 [ [ \ to , id>> , , ] { } make ] keep
55 node>> "remote-channels" <remote-thread>
56 send-synchronous no-channel = [ no-channel throw ] when ;
58 M: remote-channel from ( remote-channel -- value )
59 [ [ \ from , id>> , ] { } make ] keep
60 node>> "remote-channels" <remote-thread>
61 send-synchronous dup no-channel = [ no-channel throw ] when* ;
64 H{ } clone \ remote-channels set-global
66 ] "channel-registry" add-init-hook