Client Programs
This write-up is a concise introduction to the creation of world programs that communicate with a server. It is not a guide to the design of client-server systems.
Registration
"dictionary.ccs.neu.edu"
10000, 10001, 10002, 10003, 10004, 10005, 10006, 10007, 10008, 10009, 10010, 10011
last-name1.first-name1-initial:0123+last-name2.first-name2-initial:4567
last-name.first-name-initial:0123
partner 1
partner 2
credentials
name
id
name
id
Matt Mac
4444
Alan Misery
3344
"mac.m:4444+misery.a:3344"
Leena Bot
1221
Nadia Comm
9999
"bot.l:221+comm.n:9999"
Al Sam-To
6789
Becca Fell
5642
"sam-to.a:6789+fell.b:5642"
Ma Fell
1234
"fell.m:5642"
Receiving Messages
A world program that wishes to receive message must specify the message handler with a on-receive clause. The handler consumes two arguments: the current state of the world and the message that the server sent.
Sending Messages
To send a message, a handler must produce a Package, which consists of two parts: the next state of the world and a message to be sent to the server on behalf of the world program. Any handler can return packages.
(define (mouse-handler w x y me) (make-package w "hello world"))
A complete example
; PURPOSE of PROGRAM ; This world program connects to a server that sends out one word from a ; dictionary per second. When the client receives a string, it uses it as its ; own state and displays it in the world canvas. ; If this client sends a string message to the server, the latter appends it ; to the strings it sends from now on (to this client). The words that a client ; sends out accumulates at the server. (define the-server "dictionary.ccs.neu.edu") (define the-port 123456) ; Client is String ; interpretation The state of the client is a word. ; It changes every time when a message (string) arrives from the server. ; String -> String ; just shows the last word received from the (define (client s) (big-bang s [name "matthias:1234"] [register the-server] [port the-port] [on-tick send-word 3] [to-draw render-word] [on-receive receive-word])) ; Client -> Package (of String and String) ; send the word "dd" to the server (check-expect (send-word "hello") (make-package "hello" "dd")) (check-expect (send-word "world") (make-package "world" "dd")) (define (send-word s) (make-package s "dd")) ; Client -> Image ; render the state of the client as a text image with a 22 font in red (check-expect (render-word "bye") (text "bye" 22 "red")) (define (render-word s) (text s 22 "red")) ; Client String -> Client ; turn the received word into the state of the client (check-expect (receive-word "bye" "good") "good") (define (receive-word s m) m)
Warning
Do not attempt to run your main function until your handlers pass a solid suite of test cases. Otherwise you will experience nothing but frustration.