]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/channels/remote/remote.factor
factor: trim using lists
[factor.git] / basis / channels / remote / remote.factor
index 1a7addac12583fcb5646e529951d40336f76db7a..ef0b3b15f1b70d2ca1785dfba52ea579569c0874 100644 (file)
@@ -2,9 +2,9 @@
 ! See http://factorcode.org/license.txt for BSD license.
 !
 ! Remote Channels
-USING: kernel init namespaces make assocs arrays random
-sequences channels match concurrency.messaging
-concurrency.distributed threads accessors ;
+USING: accessors assocs channels concurrency.distributed
+concurrency.messaging init kernel match namespaces random
+threads ;
 IN: channels.remote
 
 <PRIVATE
@@ -14,54 +14,59 @@ IN: channels.remote
 PRIVATE>
 
 : publish ( channel -- id )
-    256 random-bits dup >r remote-channels set-at r> ;
+    256 random-bits dup [ remote-channels set-at ] dip ;
 
 : get-channel ( id -- channel )
     remote-channels at ;
 
 : unpublish ( id -- )
     remote-channels delete-at ;
-    
+
 <PRIVATE
 
 MATCH-VARS: ?from ?tag ?id ?value ;
 
 SYMBOL: no-channel
+TUPLE: to-message id value ;
+TUPLE: from-message id ;
 
-: channel-process ( -- )
+: channel-thread ( -- )
     [
         {
-            { { to ?id ?value  }
-            [ ?value ?id get-channel dup [ to f ] [ 2drop no-channel ] if ] }
-            { { from ?id }
+            { T{ to-message f ?id ?value  }
+            [ ?value ?id get-channel [ to f ] [ drop no-channel ] if* ] }
+            { T{ from-message f ?id }
             [ ?id get-channel [ from ] [ no-channel ] if* ] }
         } match-cond
     ] handle-synchronous ;
 
-PRIVATE>
-
 : start-channel-node ( -- )
-    "remote-channels" get-process [
-        "remote-channels" 
-        [ channel-process t ] "Remote channels" spawn-server
-        register-process 
+    "remote-channels" get-remote-thread [
+        [ channel-thread t ] "Remote channels" spawn-server
+        "remote-channels" register-remote-thread
     ] unless ;
 
+PRIVATE>
+
 TUPLE: remote-channel node id ;
 
-C: <remote-channel> remote-channel 
+C: <remote-channel> remote-channel
 
-M: remote-channel to ( value remote-channel -- )
-    [ [ \ to , id>> , , ] { } make ] keep
-    node>> "remote-channels" <remote-process> 
-    send-synchronous no-channel = [ no-channel throw ] when ;
+<PRIVATE
 
-M: remote-channel from ( remote-channel -- value )
-    [ [ \ from , id>> , ] { } make ] keep
-    node>> "remote-channels" <remote-process> 
+: send-message ( message remote-channel -- value )
+    node>> "remote-channels" <remote-thread>
     send-synchronous dup no-channel = [ no-channel throw ] when* ;
 
+PRIVATE>
+
+M: remote-channel to
+    [ id>> swap to-message boa ] keep send-message drop ;
+
+M: remote-channel from
+    [ id>> from-message boa ] keep send-message ;
+
 [
     H{ } clone \ remote-channels set-global
     start-channel-node
-] "channel-registry" add-init-hook
+] "channel-registry" add-startup-hook