| 
									
										
										
										
											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 ;
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | <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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:39:25 -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  } | 
					
						
							| 
									
										
										
										
											2015-07-21 01:24:30 -04:00
										 |  |  |             [ ?value ?id get-channel [ to f ] [ drop 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 ( -- )
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:39:25 -04:00
										 |  |  |     "remote-channels" get-remote-thread [ | 
					
						
							|  |  |  |         [ channel-thread t ] "Remote channels" spawn-server | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  |         "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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  | C: <remote-channel> remote-channel | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  |     node>> "remote-channels" <remote-thread> | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     send-synchronous dup no-channel = [ no-channel throw ] when* ;
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2009-10-19 22:17:02 -04:00
										 |  |  | ] "channel-registry" add-startup-hook |