[iolib-devel] event handlers not working

Stelian Ionescu sionescu at common-lisp.net
Fri Jun 1 13:21:16 UTC 2007


On Fri, Jun 01, 2007 at 01:40:55PM +0300, Aycan iRiCAN wrote:
>I'm trying to write a simple example. But I cannot make the event
>handler work. Could you please check this code (mostly stolen from
>philip-jose).
you forgot to run the event loop :)
I've attached a slightly modified version of the code

-- 
(sign :name "Stelian Ionescu" :aka "fe[nl]ix"
      :quote "Quidquid latine dictum sit, altum videtur.")
-------------- next part --------------
;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-

(in-package :common-lisp-user)

(defpackage :iolib-test
  (:use #:common-lisp #:net.sockets #:iomux #:bordeaux-threads))

(in-package :iolib-test)

(defvar *server*)
(defvar *event-base* (make-instance 'iomux:event-base))
(defvar *event-loop-thread*)
(defvar *server-event*)

(defun handle-connection (sock handler)
  (unwind-protect
       (progn
         (funcall handler sock)
         (finish-output sock))
    (close sock)))

(defun make-server (port)
  (make-socket :address-family :internet :type :stream
               :connect :passive :reuse-address t
               :local-host +ipv4-unspecified+ :local-port port
               :ipv6 nil))

(defun make-connection-worker (socket handler)
  #'(lambda ()
      (handle-connection socket handler)))

(defun connection-spawner (listener-socket handler)
  #'(lambda (fd evtype)
      (if (eql evtype :read)
          (let ((connection (accept-connection listener-socket)))
            (make-thread (make-connection-worker connection handler)
                         :name "connection worker"))
          (format t "Error on FD ~A" fd))))

(defun add-multi-threaded-server (event-base listener-socket handler)
  (add-fd event-base (socket-fd listener-socket)
          :read (connection-spawner listener-socket handler)
          :persistent t))

(defun start-server (port)
  (prog1 (setf *server* (make-server port))
    (make-thread #'(lambda () (event-dispatch *event-base*))
                 :name "event loop")
    (setf *server-event* (add-multi-threaded-server *event-base* *server*
                                                    #'test-handler))))

(defun stop-server ()
  (remove-event *event-base* *server-event*)
  (close *server*)
  (setf *server* nil)
  (setf *server-event* nil))

(defun test-handler (sock)
  (write "<HTML><HEAD><TITLE>Test</TITLE></HEAD><BODY><P>Hello, World!</P></BODY></HTML>"
         :stream sock)
  (terpri sock))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/iolib-devel/attachments/20070601/b9cfd6f2/attachment.sig>


More information about the iolib-devel mailing list