From 32c8c6319ea9aed17154353ccfc334e44cebb976 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Fri, 6 Jan 2017 16:19:42 -0600 Subject: [PATCH] doc: Have telcmd tutorial example send itself a message for line handling. * doc/8sync-new-manual.org: Have telcmd tutorial example send itself a message for line handling. --- doc/8sync-new-manual.org | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/doc/8sync-new-manual.org b/doc/8sync-new-manual.org index 2545b16..269dafb 100644 --- a/doc/8sync-new-manual.org +++ b/doc/8sync-new-manual.org @@ -660,7 +660,8 @@ Well, luckily that's pretty easy, especially with all you know so far. (define-actor () ((*init* telcmd-init) (*cleanup* telcmd-cleanup) - (new-client telcmd-new-client)) + (new-client telcmd-new-client) + (handle-line telcmd-handle-line)) (socket #:accessor telcmd-socket #:init-value #f)) #+END_SRC @@ -740,12 +741,12 @@ In our case, we politely close the socket when == dies. (cond ((eof-object? line) (close client)) (else - (telcmd-handle-line telcmd client - (string-trim-right line #\return)) + (<- (actor-id telcmd) 'handle-line + client (string-trim-right line #\return)) (when (actor-alive? telcmd) (loop))))))) - (define (telcmd-handle-line telcmd client line) + (define (telcmd-handle-line telcmd message client line) (match (string-split line #\space) (("") #f) ; ignore empty lines (("time" _ ...) @@ -770,7 +771,7 @@ agenda.[fn:setvbuf] The actual method called whenever we have a "line" of input is pretty straightforward... in fact it looks an awful lot like the IRC bot handle-line procedure we used earlier. -No surprises there! +No surprises there![fn:why-send-a-message-to-handle-line] Now let's run it: @@ -824,6 +825,20 @@ Happy hacking! finishes handling one message. Our loop couldn't look quite like this though! +[fn:why-send-a-message-to-handle-line] + Well, there may be one surprise to the astute observer. + Why are we sending a message to ourselves? + Couldn't we have just dropped the argument of "message" to + telcmd-handle-line and just called it like any other procedure? + Indeed, we /could/ do that, but sending a message to ourself has + an added advantage: if we accidentally "break" the + telcmd-handle-line procedure in some way (say we add a fun new + command we're playing with it), raising an exception won't break + and disconnect the client's main loop, it'll just break the + message handler for that one line, and our telcmd will happily + chug along accepting another command from the user while we try + to figure out what happened to the last one. + ** An intermission: about live hacking This section is optional, but highly recommended. -- 2.31.1