#| Copyright (c) 2007 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |# (in-package :nio) (declaim (optimize (debug 3) (speed 3) (space 0))) ;;concept of a remote socket with properties e.g. stats, connection attempts etc (defclass node() ((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 60 :accessor retry-delay :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 :remote-host host :remote-port port)) ;(node-from-socket-repn "192.168.1.1:1234") (defun node-from-socket-repn(socket) (let ((colon-idx (search ":" socket))) (if colon-idx (node (subseq socket 0 colon-idx) (parse-integer (subseq socket (+ colon-idx 1)))) (error 'parse-error)))) (defmethod print-object ((a-node node) stream) (with-slots (remote-host remote-port last-connect-attempt retry-delay active-conn) a-node (format stream "#" remote-host remote-port last-connect-attempt retry-delay active-conn))) (defparameter *nodes-list* nil "List of nodes to connect to") (defun load-nodes (filename) (with-open-file (stream filename) (loop for line = (read-line stream nil nil) do (push (node-from-socket-repn line) *nodes-list*)))) ;;returns floating point (high-res) next allowed connect time (defun get-next-allowed-connect-time(node) (if (null (last-connect-attempt node)) (get-universal-high-res) (+ (last-connect-attempt node) (retry-delay node)))) (defun allowed-to-connect(node) (if (null (last-connect-attempt node)) t (and (not (active-conn node)) (< (+ (last-connect-attempt node) (retry-delay node)) (get-universal-high-res))))) (defun update-last-connect-attempt(node) (setf (last-connect-attempt node) (get-universal-high-res))) ;;iterates over the nodes list looking for nodes that are ready to be connected to ;;i.e. the SM is null and the next-allowed-connect time has expired (defmacro with-connect-ready-nodes ((node) &rest body) `(dolist (,node *nodes-list*) (when (allowed-to-connect ,node) ,@body))) (defmacro with-connected-nodes ((node) &rest body) `(dolist (,node *nodes-list*) (when (active-conn ,node) ,@body))) (defun connected-nodes-count() (let ((count 0)) (with-connected-nodes (node) (incf count)) count))