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,
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
On Fri, Jun 01, 2007 at 03:21:16PM +0200, Stelian Ionescu wrote:
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
I forgot to say that you'll need the live sources of iolib-posix and iolib, although I'll soon make a new release see http://common-lisp.net/project/iolib/download.shtml