| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Copyright (C) 2005 Chris Double. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2018-01-08 18:53:04 -05:00
										 |  |  | ! Copyright (C) 2018 Alexander Ilin. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2018-01-09 23:21:54 -05:00
										 |  |  | USING: accessors arrays assocs concurrency.messaging | 
					
						
							|  |  |  | continuations destructors fry init io io.encodings.binary | 
					
						
							|  |  |  | io.servers io.sockets io.streams.duplex kernel namespaces | 
					
						
							|  |  |  | sequences serialize threads ;
 | 
					
						
							|  |  |  | FROM: concurrency.messaging => send ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: concurrency.distributed | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:02:07 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:39:25 -04:00
										 |  |  | : registered-remote-threads ( -- hash )
 | 
					
						
							|  |  |  |    \ registered-remote-threads get-global ;
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:02:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-20 07:28:51 -05:00
										 |  |  | : thread-connections ( -- hash )
 | 
					
						
							|  |  |  |     \ thread-connections get-global ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:02:07 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:39:25 -04:00
										 |  |  | : register-remote-thread ( thread name -- )
 | 
					
						
							|  |  |  |     registered-remote-threads set-at ;
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:02:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:39:25 -04:00
										 |  |  | : unregister-remote-thread ( name -- )
 | 
					
						
							|  |  |  |     registered-remote-threads delete-at ;
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:02:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:39:25 -04:00
										 |  |  | : get-remote-thread ( name -- thread )
 | 
					
						
							| 
									
										
										
										
											2010-03-30 01:17:39 -04:00
										 |  |  |     dup registered-remote-threads at [ ] [ threads at ] ?if ;
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:02:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 04:23:41 -05:00
										 |  |  | SYMBOL: local-node | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : handle-node-client ( -- )
 | 
					
						
							| 
									
										
										
										
											2018-01-09 23:21:54 -05:00
										 |  |  |     deserialize [ | 
					
						
							|  |  |  |         first2 get-remote-thread send handle-node-client | 
					
						
							|  |  |  |     ] [ stop-this-server ] if* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-25 17:58:19 -04:00
										 |  |  | : <node-server> ( addrspec -- threaded-server )
 | 
					
						
							| 
									
										
										
										
											2009-05-30 20:15:53 -04:00
										 |  |  |     binary <threaded-server> | 
					
						
							| 
									
										
										
										
											2008-06-25 17:58:19 -04:00
										 |  |  |         swap >>insecure | 
					
						
							|  |  |  |         "concurrency.distributed" >>name | 
					
						
							|  |  |  |         [ handle-node-client ] >>handler ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 04:23:41 -05:00
										 |  |  | : start-node ( addrspec -- )
 | 
					
						
							|  |  |  |     <node-server> start-server local-node set-global ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-20 07:28:51 -05:00
										 |  |  | TUPLE: remote-thread node id ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-20 07:28:51 -05:00
										 |  |  | C: <remote-thread> remote-thread | 
					
						
							| 
									
										
										
										
											2018-01-09 23:21:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: connection remote stream local ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <connection> connection | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : connect ( remote-thread -- )
 | 
					
						
							| 
									
										
										
										
											2018-01-20 11:16:25 -05:00
										 |  |  |     [ node>> dup binary <client> <connection> ] | 
					
						
							|  |  |  |     [ thread-connections set-at ] bi ;
 | 
					
						
							| 
									
										
										
										
											2018-01-09 23:21:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : disconnect ( remote-thread -- )
 | 
					
						
							| 
									
										
										
										
											2018-01-20 11:18:36 -05:00
										 |  |  |     thread-connections delete-at*
 | 
					
						
							|  |  |  |     [ stream>> dispose ] [ drop ] if ;
 | 
					
						
							| 
									
										
										
										
											2018-01-09 23:21:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : with-connection ( remote-thread quot -- )
 | 
					
						
							|  |  |  |     '[ connect @ ] over [ disconnect ] curry [ ] cleanup ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-11 23:42:32 -04:00
										 |  |  | : send-remote-message ( message node -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  |     binary [ serialize ] with-client ;
 | 
					
						
							| 
									
										
										
										
											2008-03-11 23:42:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-09 23:21:54 -05:00
										 |  |  | : send-to-connection ( message connection -- )
 | 
					
						
							|  |  |  |     stream>> [ serialize flush ] with-stream* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:39:25 -04:00
										 |  |  | M: remote-thread send ( message thread -- )
 | 
					
						
							| 
									
										
										
										
											2018-01-20 11:16:25 -05:00
										 |  |  |     [ id>> 2array ] [ node>> ] [ thread-connections at ] tri
 | 
					
						
							| 
									
										
										
										
											2018-01-09 23:21:54 -05:00
										 |  |  |     [ nip send-to-connection ] [ send-remote-message ] if* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-18 06:07:40 -05:00
										 |  |  | M: thread (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2016-11-10 04:23:41 -05:00
										 |  |  |     id>> [ local-node get insecure>> ] dip <remote-thread> (serialize) ;
 | 
					
						
							| 
									
										
										
										
											2008-03-11 23:42:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 04:23:41 -05:00
										 |  |  | : stop-node ( -- )
 | 
					
						
							| 
									
										
										
										
											2018-01-08 18:52:21 -05:00
										 |  |  |     f local-node get insecure>> send-remote-message ;
 | 
					
						
							| 
									
										
										
										
											2009-10-29 01:02:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | [ | 
					
						
							| 
									
										
										
										
											2009-10-29 01:39:25 -04:00
										 |  |  |     H{ } clone \ registered-remote-threads set-global
 | 
					
						
							| 
									
										
										
										
											2018-01-20 07:28:51 -05:00
										 |  |  |     H{ } clone \ thread-connections set-global
 | 
					
						
							| 
									
										
										
										
											2009-11-30 18:36:03 -05:00
										 |  |  | ] "remote-thread-registry" add-startup-hook |