diff --git a/lisp/gateway-tui-main.lisp b/lisp/gateway-tui-main.lisp index 2ab33a4..876e730 100644 --- a/lisp/gateway-tui-main.lisp +++ b/lisp/gateway-tui-main.lisp @@ -197,15 +197,28 @@ (sleep 0.5))))) (defun connect-daemon (&optional (host "127.0.0.1") (port 9105)) - (handler-case - (let ((s (usocket:socket-connect host port :timeout 10))) - (setf (st :stream) (usocket:socket-stream s) (st :connected) t) - (bt:make-thread (lambda () (reader-loop (st :stream))) :name "tui-reader") - (add-msg :system "* Connected *") - t) - (error (c) - (add-msg :system (format nil "* Connection failed: ~a *" c)) - nil))) + (add-msg :system "* Connecting to daemon... *") + (loop for attempt from 1 to 3 + for backoff = 0 then 3 + do (sleep backoff) + (handler-case + (let ((s (usocket:socket-connect host port :timeout 5))) + (setf (st :stream) (usocket:socket-stream s) + (st :connected) t) + (bt:make-thread (lambda () (reader-loop (st :stream))) + :name "tui-reader") + (add-msg :system (format nil "* Connected v~a *" "0.2.0")) + (return-from connect-daemon t)) + (usocket:connection-refused-error (c) + (when (= attempt 3) + (add-msg :system (format nil "* No daemon on port ~a after ~a attempts *" + port attempt)))) + (error (c) + (add-msg :system (format nil "* Connection attempt ~a failed: ~a *" + attempt c)) + (when (= attempt 3) + (add-msg :system "* TIP: run 'passepartout daemon' first *"))))) + nil) (defun disconnect-daemon () (when (st :stream) diff --git a/org/gateway-tui-main.org b/org/gateway-tui-main.org index 8e722be..6065e48 100644 --- a/org/gateway-tui-main.org +++ b/org/gateway-tui-main.org @@ -231,15 +231,28 @@ Event handlers + daemon I/O + main loop. ** Connection #+begin_src lisp (defun connect-daemon (&optional (host "127.0.0.1") (port 9105)) - (handler-case - (let ((s (usocket:socket-connect host port :timeout 10))) - (setf (st :stream) (usocket:socket-stream s) (st :connected) t) - (bt:make-thread (lambda () (reader-loop (st :stream))) :name "tui-reader") - (add-msg :system "* Connected *") - t) - (error (c) - (add-msg :system (format nil "* Connection failed: ~a *" c)) - nil))) + (add-msg :system "* Connecting to daemon... *") + (loop for attempt from 1 to 3 + for backoff = 0 then 3 + do (sleep backoff) + (handler-case + (let ((s (usocket:socket-connect host port :timeout 5))) + (setf (st :stream) (usocket:socket-stream s) + (st :connected) t) + (bt:make-thread (lambda () (reader-loop (st :stream))) + :name "tui-reader") + (add-msg :system (format nil "* Connected v~a *" "0.2.0")) + (return-from connect-daemon t)) + (usocket:connection-refused-error (c) + (when (= attempt 3) + (add-msg :system (format nil "* No daemon on port ~a after ~a attempts *" + port attempt)))) + (error (c) + (add-msg :system (format nil "* Connection attempt ~a failed: ~a *" + attempt c)) + (when (= attempt 3) + (add-msg :system "* TIP: run 'passepartout daemon' first *"))))) + nil) (defun disconnect-daemon () (when (st :stream)