2016-11-10 04:23:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								USING:  help.markup  help.syntax  concurrency.messaging  io.servers  threads  ;
  
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								IN:  concurrency.distributed  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-11-10 04:23:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  local-node  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $var-description "A variable containing the "  { $link threaded-server } " the current node is running on."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  start-node  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "addrspec"  "an addrspec to listen on"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Starts a "  { $link threaded-server } " for receiving messages from remote Factor instances."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2009-10-28 23:15:26 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								ARTICLE: "concurrency.distributed.example"  "Distributed Concurrency Example" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"In this example the Factor instance associated with port 9000 will run "  
						 
					
						
							
								
									
										
										
										
											2017-01-05 12:06:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"a thread that receives and prints messages in the listener. "  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"The code to run the server is:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $code
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "USING: io.servers ;" 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-04 17:00:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  "9000 local-server start-node" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-28 23:15:26 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2017-01-05 12:06:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"The code to start the thread is:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $code
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "USING: concurrency.messaging threads ;" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ": log-message ( -- ) receive . flush log-message ;" 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-04 17:00:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    "[ log-message ] \"logger\" [ spawn ] keep register-remote-thread" 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-05 12:06:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"This spawns a thread that waits for the messages and prints them. It registers "  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"the thread as remotely accessible with "  { $link register-remote-thread } "."  
						 
					
						
							
								
									
										
										
										
											2009-10-28 23:15:26 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
									
										
										
										
											2017-12-19 18:06:21 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"The second Factor instance can send "  
						 
					
						
							
								
									
										
										
										
											2009-10-29 01:39:25 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"messages to the 'logger' thread by name:"  
						 
					
						
							
								
									
										
										
										
											2017-01-05 12:06:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $code
							 
						 
					
						
							
								
									
										
										
										
											2017-12-19 17:44:01 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    "USING: io.servers concurrency.distributed ; FROM: concurrency.messaging => send ;" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "\"hello\" 9000 local-server \"logger\" <remote-thread> send" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-28 23:15:26 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2017-01-05 12:06:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"The "  { $link send } " word is used to send messages to threads. If an "  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"instance of "  { $link remote-thread } " is provided, then "  
						 
					
						
							
								
									
										
										
										
											2009-10-29 01:39:25 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"the message is marshalled to the named thread on the given machine using the "  
						 
					
						
							
								
									
										
										
										
											2009-10-28 23:15:26 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $vocab-link "serialize"  } " vocabulary." 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Running this code should show the message \"hello\" in the first Factor "  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"instance."  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"It is also possible to use "  { $link send-synchronous } " to receive a "  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"response to a distributed message. When an instance of "  { $link thread } " "  
						 
					
						
							
								
									
										
										
										
											2017-01-05 12:06:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"is marshalled, it is converted into an instance of "  { $link remote-thread } 
						 
					
						
							
								
									
										
										
										
											2009-10-28 23:15:26 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								". The receiver of this can use it as the target of a "  { $link send } 
						 
					
						
							
								
									
										
										
										
											2018-01-06 14:35:25 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								", "  { $link send-synchronous } " or "  { $link reply-synchronous } " call."  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Note: "  { $link send-synchronous } " can only work if "  { $link local-node } " is assigned (use "  { $link start-node } "), because there must be a server for the remote instance to send its reply to."  ;
  
						 
					
						
							
								
									
										
										
										
											2009-10-28 23:15:26 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-18 17:20:18 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								ARTICLE: "concurrency.distributed"  "Distributed message passing" 
							 
						 
					
						
							
								
									
										
										
										
											2010-09-20 15:17:27 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"The "  { $vocab-link "concurrency.distributed"  } " implements transparent distributed message passing, inspired by Erlang and Termite."  $nl 
						 
					
						
							
								
									
										
										
										
											2016-11-10 04:23:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections local-node start-node }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Instances of "  { $link thread } " can be sent to remote nodes, at which point they are converted to objects holding the thread ID and the current node's addrspec:"  
						 
					
						
							
								
									
										
										
										
											2009-10-29 01:39:25 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections remote-thread }
							 
						 
					
						
							
								
									
										
										
										
											2011-02-09 11:51:13 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"The "  { $vocab-link "serialize"  } " vocabulary is used to convert Factor objects to byte arrays for transfer over a socket."  
						 
					
						
							
								
									
										
										
										
											2009-10-28 23:15:26 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections "concurrency.distributed.example"  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-18 17:20:18 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								ABOUT: "concurrency.distributed"