7 combinators continuations
8 namespaces generic threads sequences arrays vars ;
12 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
16 : <user> ( client -- user )
19 dup [ "name: " write flush readln ] with-stream* over set-user-name ;
21 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
25 : init-users ( -- ) V{ } clone >users ;
27 : show-users ( -- ) users> [ user-name print ] each flush ;
29 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
33 : init-conversation ( -- ) V{ } clone >conversation ;
35 : show-conversation ( -- ) conversation> [ print ] each flush ;
37 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
41 : ((send-input)) ( other -- ) [ input> print flush ] with-stream* ;
43 : (send-input) ( other -- )
44 [ ((send-input)) ] catch [ print dup dispose users> delete ] when ;
46 : send-input ( other -- )
47 dup duplex-stream-closed? [ users> delete ] [ (send-input) ] if ;
49 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
51 : tag-input ( -- ) user> user-name ": " input> 3append >input ;
53 : log-input ( -- ) input> conversation> push ;
55 ! : send-message ( -- ) tag-input users> >array [ send-input ] each ;
57 : send-message ( -- ) tag-input log-input users> >array [ send-input ] each ;
59 : handle-user-loop ( -- )
61 { { [ input> f eq? ] [ user> users> delete ] }
62 { [ input> "/log" = ] [ show-conversation handle-user-loop ] }
63 { [ input> "/users" = ] [ show-users handle-user-loop ] }
64 { [ t ] [ send-message handle-user-loop ] } }
67 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
69 ! : handle-client ( client -- ) <user> dup users> push
70 ! dup [ >user [ handle-user-loop ] with-stream* ] with-scope ;
72 : handle-client ( client -- )
73 <user> dup users> push
74 dup [ >user [ handle-user-loop ] with-stream ] with-scope ;
76 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
78 : accept-client-loop ( server -- )
79 [ accept [ handle-client ] curry in-thread ] keep
82 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
84 ! : start-cabal ( -- )
87 ! 8000 <server> accept-client-loop ;
92 8000 internet-server [ inet4? ] find nip <server> accept-client-loop ;