factor/basis/channels/remote/remote.factor

73 lines
1.8 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
2009-10-29 21:19:34 -04:00
USING: kernel init namespaces 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-11-29 14:47:45 -05:00
256 random-bits dup [ remote-channels set-at ] dip ;
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
2009-10-29 21:19:34 -04:00
TUPLE: to-message id value ;
TUPLE: from-message id ;
2007-09-20 18:09:08 -04:00
: channel-thread ( -- )
2008-02-20 00:17:59 -05:00
[
2008-02-18 10:08:59 -05:00
{
2009-10-29 21:19:34 -04:00
{ T{ to-message f ?id ?value }
[ ?value ?id get-channel dup [ to f ] [ 2drop no-channel ] if ] }
2009-10-29 21:19:34 -04:00
{ T{ from-message f ?id }
2008-02-18 10:08:59 -05:00
[ ?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
: start-channel-node ( -- )
"remote-channels" get-remote-thread [
[ channel-thread t ] "Remote channels" spawn-server
"remote-channels" register-remote-thread
2007-09-20 18:09:08 -04:00
] unless ;
2009-10-29 21:19:34 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
TUPLE: remote-channel node id ;
C: <remote-channel> remote-channel
2009-10-29 21:19:34 -04:00
<PRIVATE
2007-09-20 18:09:08 -04:00
2009-10-29 21:19:34 -04:00
: send-message ( message remote-channel -- value )
node>> "remote-channels" <remote-thread>
2007-09-20 18:09:08 -04:00
send-synchronous dup no-channel = [ no-channel throw ] when* ;
2009-10-29 21:19:34 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
M: remote-channel to ( value remote-channel -- )
2009-10-29 21:19:34 -04:00
[ id>> swap to-message boa ] keep send-message drop ;
2007-09-20 18:09:08 -04:00
M: remote-channel from ( remote-channel -- value )
2009-10-29 21:19:34 -04:00
[ id>> from-message boa ] keep send-message ;
2007-09-20 18:09:08 -04:00
[
H{ } clone \ remote-channels set-global
start-channel-node
] "channel-registry" add-startup-hook