[cells-devel] Re: chat-server using cells?

Lars Rune Nøstdal larsnostdal at gmail.com
Tue Jun 13 01:04:38 UTC 2006


ok, after some mailing with Kenny - this works:


(defpackage CellsChat
  (:use :cl :cells))
(in-package :CellsChat)


(defparameter *newline* (princ-to-string #\Newline))



(defmodel Participant ()
  ((chat :cell nil :accessor chat-of :initarg :chat
         :initform (error "Participants need something for its `chat'-slot."))

   (username :cell nil :accessor username-of :initarg :username
             :initform (error "CellsChat needs a `username'."))

   (speech :cell :ephemeral :accessor speech-of :initarg :speech
           :initform (c-in nil))))



(defmethod initialize-instance :after ((participant Participant) &key)
  (push participant (participants-of (chat-of participant))))



(defobserver speech ((participant Participant))
  ;; `new-value' always refers to the slot `speech'
  ;; since that is what we're observing
  (when new-value
    (dolist (participant (participants-of (chat-of participant)))
      (format t "Update interface for '~A', appending: ~A~%"
              (username-of participant) new-value))))



(defmethod say ((participant Participant) (what string))
  (setf (speech-of participant)
        (concatenate 'string (username-of participant) ": " what *newline*)))



(defmodel Chat ()
  ((text-box :accessor text-box-of
             :initform (c? (concatenate 'string
                                        ;; conversation till now..
                                        (or .cache "")
                                        ;; well, ok then - this is neat O_o
                                        (some 'speech-of
(participants-of self)))))

   (participants  :accessor participants-of
                  :initform (c-in nil))))



(defun testChat ()
  (let* ((chat (make-instance 'Chat))
         (user1 (make-instance 'Participant :username "user1" :chat chat))
         (user2 (make-instance 'Participant :username "user2" :chat chat)))
    (say user1 "Hello, anyone here?")
    (say user2 "Well hello there - I'm here :)")
    (say user1 "Cool .. what's up?")
    (say user2 "Just doing some Lisp-hacking -- you?")
    (say user1 "Naaaw .. nothing; I'm kind of tired, so I'm just
sitting in the sun here listening to some music")))


..pretty darn cool :)

-- 
Mvh,
Lars Rune Nøstdal
http://lars.nostdal.org/


More information about the cells-devel mailing list