hw10 - Week 10 solutions HOMEWORK = 1. Send message to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Week 10 solutions HOMEWORK ======== 1. Send message to multiple clients. We're supposed to do this all in the client program. Here's the code that sends messages now: (define (im who message) ;;;Send message to who. (if (not (send-request (make-request whoiam who 'send-msg message) port-to-server)) (close-connection))) We want to change this so that WHO can be a list instead of just one name. The minimal solution is as follows: (define (im who message) ;;;Send message to who. (IF (LIST? WHO) (FOR-EACH (LAMBDA (USER) (IM USER MESSAGE)) WHO) (if (not (send-request (make-request whoiam who 'send-msg message) port-to- server)) (close-connection)))) If WHO is a list of names, instead of a single name, just call IM recursively for each name in the list. This isn't perfect, because if something goes wrong, we keep trying to send even after closing the connection. There are many ways to fix that; maybe the easiest is to make sure we have a server before trying to send: (define (im who message) ;;;Send message to who. (if (list? who) (for-each (lambda (user) (IF TO-SERVER (im user message))) who) (if (not (send-request (make-request whoiam who 'send-msg message) port-to- server)) (close-connection)))) 2. Broadcast to all clients. We're supposed to do the broadcasting in the server, so first we have to be able to send a BROADCAST message from the client to the server: (define (broadcast message) ;; This is in im-client.scm (if (not (send-request (make-request whoiam 'server 'broadcast message) port-to- server)) (close-connection))) [Note that there is a procedure named SERVER-BROADCAST in im-server.scm that has
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Then we have to modify the message-handling procedure in the server to understand and process the BROADCAST message: (define (setup-client-request-handler name port-from-client) ;;;Handle messages from the client. ; ;Only handles "send-msg" "BROADCAST" and "logout" messages. *** FIXED COMMENT! ; (define (client-request-handler) (let* ((port-from-client (socket-input client-sock)) (port-to-client (socket-output client-sock)) (req (get-request port-from-client))) (if (not req) (remove-client name) (begin (format logging "Received request: ~S~%" req) (cond ((equal? 'send-msg (request-action req)) (let ((port-to-dst (find-port-to-client (request-dst req)))) (if port-to-dst (begin (format logging "Delivering message from ~A to ~A.~%" (request-src req) (request-dst req)) (if (not (send-request (make-request (request-src req) (request-dst req) 'receive-msg (request-data req)) port-to-dst)) (remove-client (request-dst req)))) (begin (format logging "User not found: ~A. Letting sender know.~%" (request-dst req)) (if (not (send-request (make-request 'server name 'receive-msg (format #f "User not found: ~A" (request-dst req))) port-to-client)) (remove-client name)) ))) ) ((EQUAL? 'BROADCAST (REQUEST-ACTION REQ)) (FOR-EACH (LAMBDA (CLIENT) (IF (NOT (SEND-REQUEST (MAKE-REQUEST (REQUEST-SRC REQ) CLIENT 'RECEIVE-REQ (REQUEST-DATA REQ))
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

hw10 - Week 10 solutions HOMEWORK = 1. Send message to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online