(in-package :cs325-user) (eval-when (:compile-toplevel :load-toplevel :execute) (require :s-xml-rpc #.(get-code-file "s-xml-rpc")) (require :xml-rpc-utils #.(get-code-file "xml-rpc-utils")) (use-package :s-xml-rpc) ) ;;; Change Log ;;; 11-06-2009 CKR changed REQUIRE to use GET-CODE-FILE ;;; 11-06-2009 CKR changed default port so client-server can run on same machine ;;; 11-04-2009 CKR fixed output stream for madlibs.showStory ;;; 11-04-2009 CKR changed REGISTER API ;;; 11-04-2009 CKR fixed REQUIRE calls to compile in Allegro and Lispworks ;;; When the Madlibs server is running somewhere ;;; Set *madlibs-host* to the IP ;;; (optionally) set *madlibs-port* if not 80 ;;; ;;; Execute (REGISTER host &key local-port remote-port) ;;; to register your machine with the host as a player. ;;; Both ports default to 80. ;;; ;;; REGISTER sends your IP and local-port to the host ;;; and starts an XML-RPC service on your machine. ;;; ;;; When the Madlibs server runs a game, it will call your ;;; machine with madlibs.getWord. That function is defined ;;; below to randomly pick words from *my-words*. Put your ;;; own words into *my-words*. ;;; GLOBAL PARAMETERS ;;; The words to send back to the Madlibs Server ;;; Silly words work best -- nothing offensive please. (defparameter *my-words* '((noun widget oven dog car pancake wrench horse) (adjective rancid smokey tiny loud acrid formidable sweaty) (name "Bienen" "Harrison Ford" "Tom Hanks") (plural-noun riots dweebs centaurs semis pucks headbangers))) ;;; When you call REGISTER, the following parameters are updated: ;;; the Madlibs host you've registered with (defvar *madlibs-host* nil) ;;; the host port (defvar *madlibs-remote-port* nil) ;;; the output stream for printing the results ;;; (reset to the value of *standard-output* by REGISTER) (defvar *madlibs-output* *terminal-io*) ;;; the client server that's started (defvar *madlibs-client-server* nil) ;;; SERVER-SIDE XML-RPC SERVICES ;;; Respond to acknowledge request from server, prior to play. (defun s-xml-rpc-exports::|madlibs.acknowledge| () t) ;;; Respond to request to ask user for some value (defun s-xml-rpc-exports::|madlibs.getWord| (type) (random-pick (get-words-of-type (intern-strings type :cs325-user)))) ;;; Respond to request to show user generated story (defun s-xml-rpc-exports::|madlibs.showStory| (story) (format *madlibs-output* "~%~%~A~%" story) t) (defun get-words-of-type (type) (cdr (assoc type *my-words*))) (defun random-pick (l) (nth (random (length l)) l)) ;;; CLIENT SIDE ;;; - a client registers to play, quits ;;; Register to play. Starts a local server on the given ;;; port and registers with the Madlibs host. ;;; If successful, returns the IP address and port registered. (defun register (host &key (local-port 8001) (remote-port 8000)) (setq *madlibs-host* host) (setq *madlibs-remote-port* remote-port) ;; capture the output stream for the window where this is called (setq *madlibs-output* *standard-output*) (unless *madlibs-client-server* (setq *madlibs-client-server* (s-xml-rpc:start-xml-rpc-server :port local-port))) (with-timeout (3 :timeout) (xml-rpc-call (encode-xml-rpc-call "madlibs.register" local-port) :host host :port remote-port))) (defun unregister () (when *madlibs-client-server* (stop-server *madlibs-client-server*) (setq *madlibs-client-server* nil)) (with-timeout (3 :timeout) (xml-rpc-call (encode-xml-rpc-call "madlibs.quit") :host *madlibs-host* :port *madlibs-remote-port*)))