[iolib-devel] event handlers not working

Aycan iRiCAN aycan.irican at core.gen.tr
Fri Jun 1 10:40:55 UTC 2007


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).

(in-package :asdf)

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

(in-package :iolib-test)

(defparameter *server* nil)
(defparameter *event-base* (make-instance 'iomux:event-base))
(defparameter *server-event* nil)

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

(defun make-server (port)
  (let ((sock (make-socket :address-family
                           :internet
                           :type
                           :stream
                           :connect
                           :passive
                           :ipv6 nil)))
    (bind-address sock +ipv4-unspecified+ :port port)
    (socket-listen sock)
    sock))

(defun make-connection-worker (socket handler)
  #'(lambda ()
      (break)
      (apply #'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)
  (iomux::add-fd event-base (sockets::socket-fd listener-socket)
                 :read (connection-spawner listener-socket handler)
                 :persistent t))

(defun start-server (port)
  (setf *server* (make-server port))
  (setf *server-event* (add-multi-threaded-server *event-base* *server*
#'test-handler)))

(defun stop-server ()
  (close *server*)
  (remove-event *event-base* *server-event*)
  (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))

Best Regards,

-- 
Aycan iRiCAN
http://people.core.gen.tr/~aycan.irican/




More information about the iolib-devel mailing list