Ignore:
Timestamp:
02/11/07 23:53:09 (18 years ago)
Author:
psmith
Message:

Reconnect working

Location:
branches/home/psmith/restructure/src/io
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified branches/home/psmith/restructure/src/io/async-fd.lisp

    r84 r85  
    4646   (close-pending :initform nil
    4747                  :accessor close-pending)
     48;TODO this is either an inet-socket if we are client side or a node is we are server side...
    4849   (socket :initarg :socket
    49            :accessor socket)))
     50           :accessor socket
     51           :documentation "The remote node we are talking to")))
    5052
    5153
    5254(defmethod print-object ((async-fd async-fd) stream)
    53   (with-slots (socket read-fd write-fd) async-fd
    54     (format stream "#<ASYNC-FD :socket ~D :read-fd ~D :write-fd ~D.>"
    55             socket read-fd write-fd)))
     55  (with-slots (read-fd write-fd) async-fd
     56    (format stream "#<ASYNC-FD :read-fd ~D :write-fd ~D.>"
     57            read-fd write-fd)))
    5658
    5759;;Implement this in concrete SM for read
     
    121123
    122124(define-condition read-error (error) ())
     125(define-condition write-error (error)
     126  ((error-number :initarg :error)))
    123127
    124128(defun write-more (async-fd)
     
    145149            (perror)
    146150            (let ((err-cond (make-instance 'write-error :error err)))
    147               (close err-cond)
     151              (close-fd (write-fd async-fd))
    148152              (error err-cond))))
    149153        ;;update buffers
     
    158162
    159163(defconstant +MAX-BUFFER-SIZE-BYTES+ (* 1024 1024))
    160 
    161 
    162 
    163 ;(let ((buffer (foreign-read-buffer async-fd)))
    164 ;          (if (>= (length buffer) size)
    165 ;              t
    166 ;              (let ((new-buffer (byte-buffer size)))
    167 ;                (copy-buffer buffer new-buffer)
    168 ;                (free-buffer buffer)
    169 ;                (setf (foreign-read-buffer async-fd) new-buffer)))))
    170 
    171164
    172165(defmacro realloc-buffer(async-fd accessor size)
  • TabularUnified branches/home/psmith/restructure/src/io/async-socket.lisp

    r50 r85  
    133133        nil)))
    134134
    135 (defun connect-inet-socket (socket-fd addr port)
     135(defun connect-inet-socket (socket-fd node)
     136      (format-log t "async-socket:connect-inet-socket ccalled with ~A, and ~A~%" socket-fd node)
    136137  (with-foreign-object (sa 'sockaddr-in)
    137     (init-inet-socket sa port addr)
    138 
     138    (init-inet-socket sa (remote-port node) (remote-host node))
    139139    (let ((res (%connect socket-fd sa +sockaddr-in-len+)))
    140140      (format-log t "async-socket:connect-inet-socket library connect call returned ~A, and errno ~A~%" res (get-errno))
     
    171171
    172172;;;; SOCKET I/O
    173 
    174 (defclass async-socket-fd ()
    175   ((family :initform :unknown :initarg :family)
    176    (remote-host :initform nil :initarg :remote-host)
    177    (remote-port :initform nil :initarg :remote-port)))
    178 
    179 
    180173
    181174(defun socket-accept (socket-fd connection-type)
     
    201194
    202195        ;; accept connection
     196#+nio-debug (format-log t "async-socket::socket-accept - calling %accept~%")
    203197      (let* ((res (%accept socket-fd addr len))
    204 ;;           (async-socket-fd (make-instance 'async-socket-fd :read-fd res :write-fd res)))
    205              (async-socket-fd (create-state-machine connection-type res res (make-instance 'async-socket-fd))))
     198             (async-fd (create-state-machine connection-type res res (node nil nil))))
     199
     200#+nio-debug (format-log t "async-socket::socket-accept - create async-fd ~A~%" async-fd)
    206201
    207202        (unless (< res 0)
     
    210205            ;; parse sockaddr struct for remote client info
    211206
    212             (with-slots (family remote-host remote-port) (socket async-socket-fd)
     207            (with-slots (family remote-host remote-port) (socket async-fd)
    213208             
    214209              (cond
     
    225220
    226221            (foreign-free len)
    227             (if (>= res 0) async-socket-fd nil)
     222            (if (>= res 0) async-fd nil)
    228223            )))))))
    229 
    230 
    231 (defun remote-info (async-socket-fd)
    232   "Return FAMILY, REMOTE-HOST and REMOTE-PORT in list."
    233   (with-slots (family remote-host remote-port) async-socket-fd
    234     (list family remote-host remote-port)))
    235 
  • TabularUnified branches/home/psmith/restructure/src/io/nio-server.lisp

    r84 r85  
    4242  (let ((removals nil))
    4343    (maphash #'(lambda (k async-fd)
    44 #+nio-debug2    (format-log t "Dealing with ~a => ~a~%" k async-fd)
     44#+nio-debug     (format-log t "Dealing with ~a => ~a~%" k async-fd)
    4545               
    4646               ;process reads
     
    5050
    5151               ;process-writes
    52                (process-write async-fd)
    53                (when (and (write-ready async-fd)
    54                               (> (buffer-position (foreign-write-buffer async-fd)) 0))
    55                  (write-more async-fd))
    56 
    57                (when (close-pending async-fd)
    58                  (write-more async-fd)
    59                  (push async-fd removals)))
     52               (handler-case
     53                   (progn
     54                     (process-write async-fd)
     55                     (when (and (write-ready async-fd)
     56                                (> (buffer-position (foreign-write-buffer async-fd)) 0))
     57                       (write-more async-fd))
     58                     
     59                     (when (close-pending async-fd)
     60                       (write-more async-fd)
     61                       (push async-fd removals)))
     62                 (write-error (we) (push async-fd removals))))
    6063           client-hash)
    6164    (dolist (async-fd removals)
     65      (format-log t "nio-server:process-async-fds processing remove for ~a~%" async-fd)
     66      (setf (active-conn (socket async-fd)) nil)
    6267      (remhash (async-fd-read-fd async-fd) client-hash))))
    63 ;    (format t "client-hash list ~A~%"client-hash )
    64 
    65 
    6668                             
    6769
     
    122124                              (perror)))
    123125                          (setf (gethash (async-fd-read-fd async-fd) client-hash) async-fd)
    124                           (add-async-fd event-queue async-fd :read-write)
    125 ;                         (add-async-fd event-queue async-fd :write)
    126                           )
     126                          (add-async-fd event-queue async-fd :read-write))
    127127
    128128                         ;; no accept, close
     
    150150                             (when (write-event-p event) (setf (write-ready async-fd) t)))))))))
    151151
    152                                         ;add outgoing sockets to event queue
     152;add outgoing sockets to event queue
    153153#+nio-debug2     (format-log t "nio-server:start-server - Processing new connections queue ~A~%" +connected-sockets-queue+)
    154154             (loop for node = (nio-compat:take +connected-sockets-queue+ :blocking-call nil) until (null node) do
    155155#+nio-debug       (format-log t "nio-server:start-server - adding node to nodes-list ~A~%" node)
    156156                  (push node *nodes-list*))
     157
    157158             (with-connect-ready-nodes (a-node)
    158159#+nio-debug       (format-log t "nio-server:start-server - attempting connection to node ~A~%" a-node)
    159                (let ((new-fd (connect (host a-node) (port a-node) connection-type)))
     160               (let ((new-fd (connect a-node connection-type)))
     161#+nio-debug       (format-log t "nio-server:start-server - connect returned async-fd ~A~%" new-fd)
    160162                 (update-last-connect-attempt a-node)
    161163                 (when new-fd
     
    172174
    173175
    174 (defun connect(host port connection-type
     176(defun connect(node connection-type
    175177               &key
    176178               (protocol :inet))
    177   (format-log t "nio-server:connect - Called with: ~A:~A:~A ~%" protocol host port)
     179  (format-log t "nio-server:connect - Called with: ~A ~A~%" protocol node)
    178180  (let ((sock nil))
    179181    (setq sock (ecase protocol
     
    181183                 (:inet6 (make-inet6-socket))))
    182184   
    183     (if (connect-inet-socket sock host port)
    184         (let ((sm (create-state-machine connection-type sock sock sock)))
    185 ;         (nio-compat:add +connected-sockets-queue+ sm)
    186 ;         (format-log t "nio-server:connect - Socket enqueued: ~A~%" +connected-sockets-queue+)
     185    (if (connect-inet-socket sock node)
     186        (let ((sm (create-state-machine connection-type sock sock node)))
    187187          (return-from connect sm))
    188188        (format t "Connect failed!!~A ~%" (get-errno)))))
  • TabularUnified branches/home/psmith/restructure/src/io/nio.asd

    r81 r85  
    99                 (:file "packet" :depends-on ("nio-package"))
    1010                 (:file "async-fd" :depends-on ("fd-helper"))
    11                  (:file "async-socket" :depends-on ("async-fd"))
    1211                 (:file "nodes" :depends-on ("nio-package"))
     12                 (:file "async-socket" :depends-on ("async-fd" "nodes"))
    1313                 (:file "nio-server" :depends-on ("async-socket" "nodes"))
    1414                 (:file "ip-authorisation" :depends-on ("nio-package"))
  • TabularUnified branches/home/psmith/restructure/src/io/nodes.lisp

    r81 r85  
    2929(declaim (optimize (debug 3) (speed 3) (space 0)))
    3030
    31 ;;concept of a remote socket
     31
     32;;concept of a remote socket with properties e.g. stats, connection attempts etc
    3233(defclass node()
    33   ((host :initarg :host
    34          :reader host)
    35    (port :initarg :port
    36          :reader port)
     34  ((family :initform :unknown :initarg :family)
     35   (remote-host :initarg :remote-host
     36                :initform nil
     37                :accessor remote-host)
     38   (remote-port :initarg :remote-port
     39                :initform nil
     40                :accessor remote-port)
    3741   (last-connect-attempt :initform nil
    3842                         :accessor last-connect-attempt
    3943                         :documentation "Time we last attempted a connection")
    40    (retry-delay :initform 600
     44   (retry-delay :initform 60
    4145                :accessor retry-delay
    42                 :documentation "The delay to wait after the last-connection-attempt before trying to connect again")
     46                :documentation "The delay to wait (in secs) after the last-connection-attempt before trying to connect again (10 mins)")
    4347   (active-conn :initform nil
    4448                :accessor active-conn
     
    4650
    4751(defun node(host port)
    48   (make-instance 'node :host host :port port))
     52  (make-instance 'node :remote-host host :remote-port port))
    4953
    5054;(node-from-socket-repn "192.168.1.1:1234")
     
    5761
    5862(defmethod print-object ((a-node node) stream)
    59   (with-slots (host port last-connect-attempt retry-delay active-conn) a-node
    60     (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)))
     63  (with-slots (remote-host remote-port last-connect-attempt retry-delay active-conn) a-node
     64    (format stream "#<NODE :remote-host ~A :remote-port ~A :last-connect-attempt ~A :retry-delay ~A :active-conn ~A>"
     65            remote-host remote-port last-connect-attempt retry-delay active-conn)))
    6166
    6267
Note: See TracChangeset for help on using the changeset viewer.