2005-08-04 18:46:50 -04:00
|
|
|
! Copyright (C) 2005 Chris Double. All Rights Reserved.
|
|
|
|
!
|
|
|
|
! Redistribution and use in source and binary forms, with or without
|
|
|
|
! modification, are permitted provided that the following conditions are met:
|
|
|
|
!
|
|
|
|
! 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer.
|
|
|
|
!
|
|
|
|
! 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer in the documentation
|
|
|
|
! and/or other materials provided with the distribution.
|
|
|
|
!
|
|
|
|
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
!
|
|
|
|
! Examples of using the concurrency library.
|
|
|
|
IN: concurrency-examples
|
2006-06-15 01:36:23 -04:00
|
|
|
USING: concurrency dlists errors gadgets gadgets-labels
|
|
|
|
gadgets-panes gadgets-theme io kernel math namespaces opengl
|
|
|
|
prettyprint sequences threads ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
|
|
|
: (logger) ( mailbox -- )
|
|
|
|
#! Using the given mailbox, start a thread which
|
|
|
|
#! logs messages put into the box.
|
|
|
|
dup mailbox-get print (logger) ;
|
|
|
|
|
|
|
|
: logger ( -- mailbox )
|
|
|
|
#! Start a logging thread, which will log messages to the
|
|
|
|
#! console that are put in the returned mailbox.
|
2006-06-15 01:36:23 -04:00
|
|
|
make-mailbox dup [ (logger) ] curry in-thread ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
: (pong-server0) ( -- )
|
2006-06-15 01:36:23 -04:00
|
|
|
receive second "ping" = [
|
2005-08-06 22:10:32 -04:00
|
|
|
"pong" swap send (pong-server0)
|
|
|
|
] [
|
|
|
|
"Pong server shutting down" swap send
|
2005-09-25 02:03:36 -04:00
|
|
|
] if ;
|
2005-08-06 22:10:32 -04:00
|
|
|
|
2006-01-19 19:30:18 -05:00
|
|
|
: pong-server0 ( -- process )
|
2005-08-06 22:10:32 -04:00
|
|
|
[ (pong-server0) ] spawn ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
TUPLE: ping-message from ;
|
|
|
|
TUPLE: shutdown-message from ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
GENERIC: handle-message
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
M: ping-message handle-message ( message -- bool )
|
|
|
|
ping-message-from "pong" swap send t ;
|
|
|
|
|
|
|
|
M: shutdown-message handle-message ( message -- bool )
|
|
|
|
shutdown-message-from "Pong server shutdown commenced" swap send f ;
|
|
|
|
|
|
|
|
: (pong-server1) ( -- )
|
|
|
|
"pong-server1 waiting for message..." print
|
|
|
|
receive handle-message [ (pong-server1) ] when ;
|
|
|
|
|
|
|
|
: pong-server1 ( -- process )
|
|
|
|
[
|
|
|
|
(pong-server1)
|
|
|
|
"pong-server1 exiting..." print
|
2005-08-04 18:46:50 -04:00
|
|
|
] spawn ;
|
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
TUPLE: echo-message from text ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
M: echo-message handle-message ( message -- bool )
|
|
|
|
dup echo-message-text swap echo-message-from send t ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
GENERIC: handle-message2
|
|
|
|
PREDICATE: tagged-message ping-message2 ( obj -- ? ) tagged-message-data "ping" = ;
|
|
|
|
PREDICATE: tagged-message shutdown-message2 ( obj -- ? ) tagged-message-data "shutdown" = ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
M: ping-message2 handle-message2 ( message -- bool )
|
|
|
|
"pong" reply t ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
M: shutdown-message2 handle-message2 ( message -- bool )
|
|
|
|
"Pong server shutdown commenced" reply f ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
: (pong-server2) ( -- )
|
|
|
|
"pong-server2 waiting for message..." print
|
|
|
|
receive handle-message2 [ (pong-server2) ] when ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
: pong-server2 ( -- process )
|
|
|
|
[
|
|
|
|
(pong-server2)
|
|
|
|
"pong-server2 exiting..." print
|
|
|
|
] spawn ;
|
|
|
|
|
|
|
|
: pong-server3 ( -- process )
|
|
|
|
[ handle-message2 ] spawn-server ;
|
|
|
|
|
|
|
|
GENERIC: handle-rpc-message
|
|
|
|
GENERIC: run-rpc-command
|
|
|
|
|
|
|
|
TUPLE: rpc-command op args ;
|
|
|
|
PREDICATE: rpc-command add-command ( msg -- bool )
|
|
|
|
rpc-command-op "add" = ;
|
|
|
|
PREDICATE: rpc-command product-command ( msg -- bool )
|
|
|
|
rpc-command-op "product" = ;
|
|
|
|
PREDICATE: rpc-command shutdown-command ( msg -- bool )
|
|
|
|
rpc-command-op "shutdown" = ;
|
|
|
|
PREDICATE: rpc-command crash-command ( msg -- bool )
|
|
|
|
rpc-command-op "crash" = ;
|
|
|
|
|
|
|
|
M: tagged-message handle-rpc-message ( message -- bool )
|
|
|
|
dup tagged-message-data run-rpc-command -rot reply not ;
|
|
|
|
|
|
|
|
M: add-command run-rpc-command ( command -- shutdown? result )
|
|
|
|
rpc-command-args sum f ;
|
|
|
|
|
|
|
|
M: product-command run-rpc-command ( command -- shutdown? result )
|
|
|
|
rpc-command-args product f ;
|
|
|
|
|
|
|
|
M: shutdown-command run-rpc-command ( command -- shutdown? result )
|
|
|
|
drop t t ;
|
|
|
|
|
|
|
|
M: crash-command run-rpc-command ( command -- shutdown? result )
|
|
|
|
drop 1 0 / f ;
|
2005-08-04 18:46:50 -04:00
|
|
|
|
2005-08-06 22:10:32 -04:00
|
|
|
: fragile-rpc-server ( -- process )
|
|
|
|
[ handle-rpc-message ] spawn-server ;
|
|
|
|
|
|
|
|
: (robust-rpc-server) ( worker -- )
|
2005-08-04 18:46:50 -04:00
|
|
|
[
|
2005-08-06 22:10:32 -04:00
|
|
|
receive over send
|
2005-09-20 22:15:29 -04:00
|
|
|
]
|
|
|
|
catch
|
|
|
|
[
|
|
|
|
"Worker died, Starting a new worker" print
|
|
|
|
drop [ handle-rpc-message ] spawn-linked-server
|
|
|
|
] when
|
2005-08-06 22:10:32 -04:00
|
|
|
(robust-rpc-server) ;
|
|
|
|
|
|
|
|
: robust-rpc-server ( -- process )
|
2005-08-04 18:46:50 -04:00
|
|
|
[
|
2005-08-06 22:10:32 -04:00
|
|
|
[ handle-rpc-message ] spawn-linked-server
|
|
|
|
(robust-rpc-server)
|
|
|
|
] spawn ;
|
|
|
|
|
|
|
|
: test-add ( process -- )
|
|
|
|
[
|
|
|
|
"add" [ 1 2 3 ] <rpc-command> swap send-synchronous .
|
2006-06-15 01:36:23 -04:00
|
|
|
] curry spawn drop ;
|
2005-08-06 22:10:32 -04:00
|
|
|
|
|
|
|
: test-crash ( process -- )
|
|
|
|
[
|
|
|
|
"crash" f <rpc-command> swap send-synchronous .
|
2006-06-15 01:36:23 -04:00
|
|
|
] curry spawn drop ;
|
2005-08-06 22:10:32 -04:00
|
|
|
|
|
|
|
! ******************************
|
|
|
|
! Experimental code below
|
|
|
|
! ******************************
|
|
|
|
|
2005-10-31 19:54:31 -05:00
|
|
|
TUPLE: promised-label promise font color ;
|
2005-08-06 22:10:32 -04:00
|
|
|
|
|
|
|
C: promised-label ( promise -- promised-label )
|
2005-10-31 19:54:31 -05:00
|
|
|
dup delegate>gadget dup label-theme
|
|
|
|
[ set-promised-label-promise ] keep
|
2006-06-15 01:36:23 -04:00
|
|
|
[ [ dup promised-label-promise ?promise drop relayout ] curry spawn drop ] keep ;
|
2005-08-06 22:10:32 -04:00
|
|
|
|
|
|
|
: promised-label-text ( promised-label -- text )
|
|
|
|
promised-label-promise dup promise-fulfilled? [
|
|
|
|
?promise
|
|
|
|
] [
|
|
|
|
drop "Unfulfilled Promise"
|
2005-09-25 02:03:36 -04:00
|
|
|
] if ;
|
2005-08-06 22:10:32 -04:00
|
|
|
|
2006-01-26 23:01:14 -05:00
|
|
|
M: promised-label pref-dim* ( promised-label - dim )
|
2005-10-31 19:54:31 -05:00
|
|
|
label-size ;
|
2005-08-06 22:10:32 -04:00
|
|
|
|
|
|
|
M: promised-label draw-gadget* ( promised-label -- )
|
2005-10-31 19:54:31 -05:00
|
|
|
draw-label ;
|
|
|
|
|
|
|
|
M: promised-label label-text promised-label-text ;
|
|
|
|
|
|
|
|
M: promised-label label-color promised-label-color ;
|
|
|
|
|
|
|
|
M: promised-label label-font promised-label-font ;
|
|
|
|
|
|
|
|
M: promised-label set-label-color set-promised-label-color ;
|
|
|
|
|
|
|
|
M: promised-label set-label-font set-promised-label-font ;
|
2005-08-06 22:10:32 -04:00
|
|
|
|
|
|
|
: fib ( n -- n )
|
2006-02-22 21:15:32 -05:00
|
|
|
1 sleep dup 2 < [ drop 1 ] [ dup 1 - fib swap 2 - fib + ] if ;
|
2005-08-06 22:10:32 -04:00
|
|
|
|
|
|
|
: test-promise-ui ( -- )
|
2006-06-15 01:36:23 -04:00
|
|
|
<promise> dup <promised-label> gadget. [ 15 fib unparse swap fulfill ] curry spawn drop ;
|