Author: psmith Date: Sun Feb 11 18:53:09 2007 New Revision: 85
Modified: branches/home/psmith/restructure/src/io/async-fd.lisp branches/home/psmith/restructure/src/io/async-socket.lisp branches/home/psmith/restructure/src/io/nio-server.lisp branches/home/psmith/restructure/src/io/nio.asd branches/home/psmith/restructure/src/io/nodes.lisp branches/home/psmith/restructure/src/nio-logger/nio-logger.asd branches/home/psmith/restructure/src/nio-logger/run-logging-client.lisp branches/home/psmith/restructure/src/nio-logger/run-logging-server.lisp branches/home/psmith/restructure/src/utils/nio-utils-package.lisp Log: Reconnect working
Modified: branches/home/psmith/restructure/src/io/async-fd.lisp ============================================================================== --- branches/home/psmith/restructure/src/io/async-fd.lisp (original) +++ branches/home/psmith/restructure/src/io/async-fd.lisp Sun Feb 11 18:53:09 2007 @@ -45,14 +45,16 @@ :documentation "Have we been notified as write ready and not received EAGAIN from %write?") (close-pending :initform nil :accessor close-pending) +;TODO this is either an inet-socket if we are client side or a node is we are server side... (socket :initarg :socket - :accessor socket))) + :accessor socket + :documentation "The remote node we are talking to")))
(defmethod print-object ((async-fd async-fd) stream) - (with-slots (socket read-fd write-fd) async-fd - (format stream "#<ASYNC-FD :socket ~D :read-fd ~D :write-fd ~D.>" - socket read-fd write-fd))) + (with-slots (read-fd write-fd) async-fd + (format stream "#<ASYNC-FD :read-fd ~D :write-fd ~D.>" + read-fd write-fd)))
;;Implement this in concrete SM for read (defgeneric process-read (async-fd)) @@ -120,6 +122,8 @@
(define-condition read-error (error) ()) +(define-condition write-error (error) + ((error-number :initarg :error)))
(defun write-more (async-fd) "Write data from ASYNC-FD's write bytebuffer" @@ -144,7 +148,7 @@ (unless (eql err 11) ;; eagain - failed to write whole buffer need to wait for next notify (perror) (let ((err-cond (make-instance 'write-error :error err))) - (close err-cond) + (close-fd (write-fd async-fd)) (error err-cond)))) ;;update buffers (if (eql (remaining foreign-write-buffer) 0) @@ -158,17 +162,6 @@
(defconstant +MAX-BUFFER-SIZE-BYTES+ (* 1024 1024))
- - -;(let ((buffer (foreign-read-buffer async-fd))) -; (if (>= (length buffer) size) -; t -; (let ((new-buffer (byte-buffer size))) -; (copy-buffer buffer new-buffer) -; (free-buffer buffer) -; (setf (foreign-read-buffer async-fd) new-buffer))))) - - (defmacro realloc-buffer(async-fd accessor size) `(let ((buffer (,accessor ,async-fd))) (if (>= (buffer-capacity buffer) size)
Modified: branches/home/psmith/restructure/src/io/async-socket.lisp ============================================================================== --- branches/home/psmith/restructure/src/io/async-socket.lisp (original) +++ branches/home/psmith/restructure/src/io/async-socket.lisp Sun Feb 11 18:53:09 2007 @@ -132,10 +132,10 @@ t nil)))
-(defun connect-inet-socket (socket-fd addr port) +(defun connect-inet-socket (socket-fd node) + (format-log t "async-socket:connect-inet-socket ccalled with ~A, and ~A~%" socket-fd node) (with-foreign-object (sa 'sockaddr-in) - (init-inet-socket sa port addr) - + (init-inet-socket sa (remote-port node) (remote-host node)) (let ((res (%connect socket-fd sa +sockaddr-in-len+))) (format-log t "async-socket:connect-inet-socket library connect call returned ~A, and errno ~A~%" res (get-errno)) (if (= res -1) @@ -171,13 +171,6 @@
;;;; SOCKET I/O
-(defclass async-socket-fd () - ((family :initform :unknown :initarg :family) - (remote-host :initform nil :initarg :remote-host) - (remote-port :initform nil :initarg :remote-port))) - - - (defun socket-accept (socket-fd connection-type) "Accept connection from SOCKET-FD. Allocates and returns socket structure denoting the connection."
@@ -200,16 +193,18 @@ (let ((len (foreign-alloc :unsigned-long :initial-element +sockaddr-in6-len+)))
;; accept connection +#+nio-debug (format-log t "async-socket::socket-accept - calling %accept~%") (let* ((res (%accept socket-fd addr len)) -;; (async-socket-fd (make-instance 'async-socket-fd :read-fd res :write-fd res))) - (async-socket-fd (create-state-machine connection-type res res (make-instance 'async-socket-fd)))) + (async-fd (create-state-machine connection-type res res (node nil nil)))) + +#+nio-debug (format-log t "async-socket::socket-accept - create async-fd ~A~%" async-fd)
(unless (< res 0) (let ((len-value (mem-ref len :unsigned-int)))
;; parse sockaddr struct for remote client info
- (with-slots (family remote-host remote-port) (socket async-socket-fd) + (with-slots (family remote-host remote-port) (socket async-fd) (cond ((= len-value +sockaddr-in6-len+) @@ -224,12 +219,5 @@
(foreign-free len) - (if (>= res 0) async-socket-fd nil) + (if (>= res 0) async-fd nil) ))))))) - - -(defun remote-info (async-socket-fd) - "Return FAMILY, REMOTE-HOST and REMOTE-PORT in list." - (with-slots (family remote-host remote-port) async-socket-fd - (list family remote-host remote-port))) -
Modified: branches/home/psmith/restructure/src/io/nio-server.lisp ============================================================================== --- branches/home/psmith/restructure/src/io/nio-server.lisp (original) +++ branches/home/psmith/restructure/src/io/nio-server.lisp Sun Feb 11 18:53:09 2007 @@ -41,7 +41,7 @@ (defun process-async-fds (client-hash) (let ((removals nil)) (maphash #'(lambda (k async-fd) -#+nio-debug2 (format-log t "Dealing with ~a => ~a~%" k async-fd) +#+nio-debug (format-log t "Dealing with ~a => ~a~%" k async-fd) ;process reads (when (read-ready async-fd) (read-more async-fd)) @@ -49,20 +49,22 @@ (process-read async-fd))
;process-writes - (process-write async-fd) - (when (and (write-ready async-fd) - (> (buffer-position (foreign-write-buffer async-fd)) 0)) - (write-more async-fd)) - - (when (close-pending async-fd) - (write-more async-fd) - (push async-fd removals))) + (handler-case + (progn + (process-write async-fd) + (when (and (write-ready async-fd) + (> (buffer-position (foreign-write-buffer async-fd)) 0)) + (write-more async-fd)) + + (when (close-pending async-fd) + (write-more async-fd) + (push async-fd removals))) + (write-error (we) (push async-fd removals)))) client-hash) (dolist (async-fd removals) + (format-log t "nio-server:process-async-fds processing remove for ~a~%" async-fd) + (setf (active-conn (socket async-fd)) nil) (remhash (async-fd-read-fd async-fd) client-hash)))) -; (format t "client-hash list ~A~%"client-hash ) - -
(defun start-server (connection-type @@ -121,9 +123,7 @@ (format t "Error setting socket non-blocking: ") (perror))) (setf (gethash (async-fd-read-fd async-fd) client-hash) async-fd) - (add-async-fd event-queue async-fd :read-write) -; (add-async-fd event-queue async-fd :write) - ) + (add-async-fd event-queue async-fd :read-write))
;; no accept, close (t @@ -149,14 +149,16 @@ (when (read-event-p event) (setf (read-ready async-fd) t)) (when (write-event-p event) (setf (write-ready async-fd) t)))))))))
- ;add outgoing sockets to event queue +;add outgoing sockets to event queue #+nio-debug2 (format-log t "nio-server:start-server - Processing new connections queue ~A~%" +connected-sockets-queue+) (loop for node = (nio-compat:take +connected-sockets-queue+ :blocking-call nil) until (null node) do #+nio-debug (format-log t "nio-server:start-server - adding node to nodes-list ~A~%" node) (push node *nodes-list*)) + (with-connect-ready-nodes (a-node) #+nio-debug (format-log t "nio-server:start-server - attempting connection to node ~A~%" a-node) - (let ((new-fd (connect (host a-node) (port a-node) connection-type))) + (let ((new-fd (connect a-node connection-type))) +#+nio-debug (format-log t "nio-server:start-server - connect returned async-fd ~A~%" new-fd) (update-last-connect-attempt a-node) (when new-fd #+nio-debug (format-log t "nio-server:start-server - adding connection to nio thread ~A~%" new-fd) @@ -171,19 +173,17 @@ (close-fd sock))))
-(defun connect(host port connection-type +(defun connect(node connection-type &key (protocol :inet)) - (format-log t "nio-server:connect - Called with: ~A:~A:~A ~%" protocol host port) + (format-log t "nio-server:connect - Called with: ~A ~A~%" protocol node) (let ((sock nil)) (setq sock (ecase protocol (:inet (make-inet-socket)) (:inet6 (make-inet6-socket))))
- (if (connect-inet-socket sock host port) - (let ((sm (create-state-machine connection-type sock sock sock))) -; (nio-compat:add +connected-sockets-queue+ sm) -; (format-log t "nio-server:connect - Socket enqueued: ~A~%" +connected-sockets-queue+) + (if (connect-inet-socket sock node) + (let ((sm (create-state-machine connection-type sock sock node))) (return-from connect sm)) (format t "Connect failed!!~A ~%" (get-errno)))))
Modified: branches/home/psmith/restructure/src/io/nio.asd ============================================================================== --- branches/home/psmith/restructure/src/io/nio.asd (original) +++ branches/home/psmith/restructure/src/io/nio.asd Sun Feb 11 18:53:09 2007 @@ -8,8 +8,8 @@ (:file "fd-helper" :depends-on ("nio-package")) (:file "packet" :depends-on ("nio-package")) (:file "async-fd" :depends-on ("fd-helper")) - (:file "async-socket" :depends-on ("async-fd")) (:file "nodes" :depends-on ("nio-package")) + (:file "async-socket" :depends-on ("async-fd" "nodes")) (:file "nio-server" :depends-on ("async-socket" "nodes")) (:file "ip-authorisation" :depends-on ("nio-package")) )
Modified: branches/home/psmith/restructure/src/io/nodes.lisp ============================================================================== --- branches/home/psmith/restructure/src/io/nodes.lisp (original) +++ branches/home/psmith/restructure/src/io/nodes.lisp Sun Feb 11 18:53:09 2007 @@ -28,24 +28,28 @@
(declaim (optimize (debug 3) (speed 3) (space 0)))
-;;concept of a remote socket + +;;concept of a remote socket with properties e.g. stats, connection attempts etc (defclass node() - ((host :initarg :host - :reader host) - (port :initarg :port - :reader port) + ((family :initform :unknown :initarg :family) + (remote-host :initarg :remote-host + :initform nil + :accessor remote-host) + (remote-port :initarg :remote-port + :initform nil + :accessor remote-port) (last-connect-attempt :initform nil :accessor last-connect-attempt :documentation "Time we last attempted a connection") - (retry-delay :initform 600 + (retry-delay :initform 60 :accessor retry-delay - :documentation "The delay to wait after the last-connection-attempt before trying to connect again") + :documentation "The delay to wait (in secs) after the last-connection-attempt before trying to connect again (10 mins)") (active-conn :initform nil :accessor active-conn :documentation "If we are connected to this remote socket this is set to the SM")))
(defun node(host port) - (make-instance 'node :host host :port port)) + (make-instance 'node :remote-host host :remote-port port))
;(node-from-socket-repn "192.168.1.1:1234") (defun node-from-socket-repn(socket) @@ -56,8 +60,9 @@
(defmethod print-object ((a-node node) stream) - (with-slots (host port last-connect-attempt retry-delay active-conn) a-node - (format stream "#<NODE :HOST ~A :port ~A :last-connect-attempt ~A :retry-delay ~A :active-conn ~A>" host port last-connect-attempt retry-delay active-conn))) + (with-slots (remote-host remote-port last-connect-attempt retry-delay active-conn) a-node + (format stream "#<NODE :remote-host ~A :remote-port ~A :last-connect-attempt ~A :retry-delay ~A :active-conn ~A>" + remote-host remote-port last-connect-attempt retry-delay active-conn)))
(defparameter *nodes-list* nil
Modified: branches/home/psmith/restructure/src/nio-logger/nio-logger.asd ============================================================================== --- branches/home/psmith/restructure/src/nio-logger/nio-logger.asd (original) +++ branches/home/psmith/restructure/src/nio-logger/nio-logger.asd Sun Feb 11 18:53:09 2007 @@ -8,5 +8,5 @@ (:file "nio-logger" :depends-on ("nio-logger-package")) )
- :depends-on (:nio-yarpc :nio-utils :cl-base64)) + :depends-on (:nio-yarpc :nio-utils :cl-base64 :sb-posix))
Modified: branches/home/psmith/restructure/src/nio-logger/run-logging-client.lisp ============================================================================== --- branches/home/psmith/restructure/src/nio-logger/run-logging-client.lisp (original) +++ branches/home/psmith/restructure/src/nio-logger/run-logging-client.lisp Sun Feb 11 18:53:09 2007 @@ -25,7 +25,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |#
-;(push :nio-debug *features*) +(push :nio-debug *features*) (require :asdf) (require :nio-logger)
@@ -33,7 +33,20 @@ (let ((log-file (second sb-ext:*posix-argv*)) (ip (third sb-ext:*posix-argv*))) (format t "Starting logging client with ~A ~A~%" log-file ip) - (sb-thread:make-thread #'(lambda()(nio-logger:tail-log log-file ip)) :name "nio-server") + (sb-thread:make-thread #'(lambda()(nio:start-server 'nio-yarpc:yarpc-client-state-machine)) :name "nio-server") +; (nio:add-connection (nio:node ip 16323)) +; (sleep 60)
- ;;shouldn't be listenting on the client hence nil for accept SM to start-server - (nio:start-server 'nio-yarpc:yarpc-client-state-machine)) + (setf sb-ext:*invoke-debugger-hook* + (lambda (condition hook) + (declare (ignore hook)) + (with-open-file (out (format nil "error-from-pid~A-thread-~A" (sb-posix:getpid) (sb-thread:thread-name sb-thread:*current-thread*)) + :direction :output :external-format :utf-8 :if-exists :append :if-does-not-exist :create) + (format out "Toplevel catch (~A):~%" (nio-utils:get-readable-time)) + (format out "~A - ~A~%" (type-of condition) condition) + (sb-debug:backtrace 20 out)) + (quit))) + + + (nio-logger:tail-log log-file ip) +)
Modified: branches/home/psmith/restructure/src/nio-logger/run-logging-server.lisp ============================================================================== --- branches/home/psmith/restructure/src/nio-logger/run-logging-server.lisp (original) +++ branches/home/psmith/restructure/src/nio-logger/run-logging-server.lisp Sun Feb 11 18:53:09 2007 @@ -25,7 +25,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |#
-;(push :nio-debug *features*) +(push :nio-debug *features*) (require :asdf) (require :nio-logger)
Modified: branches/home/psmith/restructure/src/utils/nio-utils-package.lisp ============================================================================== --- branches/home/psmith/restructure/src/utils/nio-utils-package.lisp (original) +++ branches/home/psmith/restructure/src/utils/nio-utils-package.lisp Sun Feb 11 18:53:09 2007 @@ -29,5 +29,5 @@ (:export
;;utils - format-log get-universal-high-res + format-log get-universal-high-res get-readable-time ))