In this example the Factor instance associated with port 9000 will run a thread that receives and prints messages in the listener. The code to run the server is:
USING: io.servers ;
9000 local-server start-node
The code to start the thread is:
USING: concurrency.messaging threads ;
: log-message ( -- ) receive . flush log-message ;
[ log-message ] "logger" [ spawn ] keep register-remote-thread
This spawns a thread that waits for the messages and prints them. It registers the thread as remotely accessible with
register-remote-thread.
The second Factor instance can send messages to the 'logger' thread by name:
USING: io.servers concurrency.distributed ; FROM: concurrency.messaging => send ;
"hello" 9000 local-server "logger" <remote-thread> send
The
send word is used to send messages to threads. If an instance of
remote-thread is provided, then the message is marshalled to the named thread on the given machine using the
serialize vocabulary.
Running this code should show the message "hello" in the first Factor instance.
It is also possible to use
send-synchronous to receive a response to a distributed message. When an instance of
thread is marshalled, it is converted into an instance of
remote-thread. The receiver of this can use it as the target of a
send,
send-synchronous or
reply-synchronous call.
Note:
send-synchronous can only work if
local-node is assigned (use
start-node ), because there must be a server for the remote instance to send its reply to.