#+BEGIN_SRC scheme
(define-class <my-irc-bot> (<irc-bot>))
- (define-method (handle-line (irc-bot <my-irc-bot>) speaker channel
- line emote?)
+ (define-method (handle-line (irc-bot <my-irc-bot>) message
+ speaker channel line emote?)
(if emote?
(format #t "~a emoted ~s in channel ~a\n"
speaker line channel)
(8sync uses GOOPS to define actors.)
We extended the handle-line generic method, so this is the code that
will be called whenever the IRC bot "hears" anything.
+This method is itself an action handler, hence the second argument
+for =message=, which we can ignore for now.
+Pleasantly, the message's argument body is passed in as the rest of
+the arguments.
+
For now the code is pretty basic: it just outputs whatever it "hears"
from a user in a channel to the current output port.
Pretty boring!
Change handle-line to this:
#+BEGIN_SRC scheme
- (define-method (handle-line (irc-bot <my-irc-bot>) speaker channel
- line emote?)
+ (define-method (handle-line (irc-bot <my-irc-bot>) message
+ speaker channel line emote?)
(<- (actor-id irc-bot) 'send-line channel
(format #f "Bawwwwk! ~a says: ~a" speaker line)))
#+END_SRC
like so:
#+BEGIN_SRC scheme
- (define-method (handle-line (irc-bot <my-irc-bot>) speaker channel
- line emote?)
+ (define-method (handle-line (irc-bot <my-irc-bot>) message
+ speaker channel line emote?)
(irc-bot-send-line irc-bot channel
(format #f "Bawwwwk! ~a says: ~a" speaker line)))
#+END_SRC
To implement it, we're going to pull out Guile's pattern matcher.
#+BEGIN_SRC scheme
- (define-method (handle-line (irc-bot <my-irc-bot>) speaker channel
- line emote?)
+ (define-method (handle-line (irc-bot <my-irc-bot>) message
+ speaker channel line emote?)
(define my-name (irc-bot-username irc-bot))
(define (looks-like-me? str)
(or (equal? str my-name)
you're right:
#+BEGIN_SRC scheme
- (define-method (handle-line (irc-bot <my-irc-bot>) speaker channel
- line emote?)
+ (define-method (handle-line (irc-bot <my-irc-bot>) message
+ speaker channel line emote?)
(define my-name (irc-bot-username irc-bot))
(define (looks-like-me? str)
(or (equal? str my-name)
everywhere as we are in this program... that's just to make the
examples more illustrative.)
+"<-reply" is what actually returns the information to the actor
+waiting on the reply.
+It takes as an argument the actor sending the message, the message
+it is in reply to, and the rest of the arguments are the "body" of
+the message.
+(If an actor handles a message that is being "waited on" but does not
+explicitly reply to it, an auto-reply with an empty body will be
+triggered so that the waiting actor is not left waiting around.)
+
+The last thing to note is the call to "self-destruct".
+This does what you might expect: it removes the actor from the hive.
+No new messages will be sent to it.
+Ka-poof!
+
Running it is the same as before:
#+BEGIN_SRC scheme
worker> Whew! Free at last.
#+END_SRC
-"<-reply" is what actually returns the information to the actor
-waiting on the reply.
-It takes as an argument the actor sending the message, the message
-it is in reply to, and the rest of the arguments are the "body" of
-the message.
-(If an actor handles a message that is being "waited on" but does not
-explicitly reply to it, an auto-reply with an empty body will be
-triggered so that the waiting actor is not left waiting around.)
-
-The last thing to note is the call to "self-destruct".
-This does what you might expect: it removes the actor from the hive.
-No new messages will be sent to it.
-Ka-poof!
-
** Writing our own network-enabled actor
So, you want to write a networked actor!
things to:
#+BEGIN_SRC scheme
- (define-method (handle-line (irc-bot <my-irc-bot>) speaker channel
- line emote?)
+ (define-method (handle-line (irc-bot <my-irc-bot>) message
+ speaker channel line emote?)
;; [... snip ...]
(define (respond respond-line)
(<- (actor-id irc-bot) 'send-line (pk 'channel channel)
(We can remove the pk now that we know what's going on.)
#+BEGIN_SRC scheme
- (define-method (handle-line (irc-bot <my-irc-bot>) speaker channel
- line emote?)
+ (define-method (handle-line (irc-bot <my-irc-bot>) message
+ speaker channel line emote?)
;; [... snip ...]
(define (respond respond-line)
(<- (actor-id irc-bot) 'send-line