Client Programs
This web page is deprecated.
Please see the main page for Fundamentals I.
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"
10001, 10002, 10003, 10004, 10005
husky.username:0123
partner 1
credentials
name
id
Matt Mac
4444
"mac.m:4444"
Leena Bot
1221
"bot.l:1221"
Al Sam-To
6789
"sam-to.a:6789"
Ma Fell
1234
"fell.m:1234"
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
; A HandlerResponse is one of ; - a WorldState ; - a (make-package WorldState Message)
A Package 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) ; Replace this with the correct port number ; 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 "fell.m: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.