Skip to content

Commit db1cc1c

Browse files
authored
Merge pull request #26 from ryukinix/lisp-chat-client-compatible-with-slime
fix: make lisp-chat/chat compatible with slime
2 parents 9cf049f + 4c796e5 commit db1cc1c

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/client.lisp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
"Erase the last line by using ANSI Escape codes"
2424
(format t "~C[1A~C[2K" #\Esc #\Esc))
2525

26+
(defun exit (&optional (code 0))
27+
#-swank
28+
(uiop:quit code))
2629

2730
(defun get-user-input (username)
2831
"Get the user input by using readline"
32+
(declare (ignore username))
33+
#+swank (read-line)
34+
#-swank
2935
(prog1 (cl-readline:readline :prompt (format nil "[~A]: " username)
3036
:erase-empty-line t
3137
:add-history t)
@@ -46,6 +52,7 @@
4652
;; before printing messages from server, so I'm cleaning all the stuff
4753
;; before print a new message, and restore again. Maybe there is a
4854
;; better way for doing that.
55+
#-swank
4956
(defun receive-message (message)
5057
"Receive a message and print in the terminal carefully with IO race conditions"
5158
(with-lock-held (*io-lock*)
@@ -64,14 +71,21 @@
6471
(cl-readline:set-prompt prompt)
6572
(cl-readline:redisplay)))))
6673

74+
#+swank
75+
(defun receive-message (message)
76+
"Receive a message and print in the terminal carefully with IO race conditions"
77+
(unless (system-pongp message)
78+
(format t "~a~%" message)))
79+
80+
6781
(defun client-sender (socket username)
6882
"Routine to check new messages being typed by the user"
6983
(loop for message = (get-user-input username)
7084
when (or (equal message "/quit")
7185
(equal message nil))
7286
return nil
7387
do (send-message message socket))
74-
(uiop:quit))
88+
(exit))
7589

7690

7791
(defun server-listener (socket)
@@ -85,7 +99,7 @@
8599
(handler-case (server-listener socket)
86100
(end-of-file (e)
87101
(format t "~%End of connection: ~a~%" e)
88-
(uiop:quit 1))
102+
(exit 1))
89103
(simple-error ()
90104
(server-broadcast socket))))
91105

@@ -112,15 +126,14 @@ The systematic pong is consumed and the @server response is not shown in the ter
112126
(let* ((socket (socket-connect host port))
113127
(username (login socket)))
114128
(format t "Connected as ~a\@~a\:~a ~%" username *host* *port*)
115-
(let ((sender (make-thread (lambda () (client-sender socket username))
116-
:name "client sender"))
117-
(broadcast (make-thread (lambda () (server-broadcast socket))
129+
(let ((broadcast (make-thread (lambda () (server-broadcast socket))
118130
:name "server broadcast"))
119131
(background-ping (make-thread (lambda () (client-background-ping socket))
120132
:name "background ping")))
121-
(join-thread background-ping)
122-
(join-thread sender)
123-
(join-thread broadcast))))
133+
(client-sender socket username)
134+
(destroy-thread background-ping)
135+
(destroy-thread broadcast)
136+
(socket-close socket))))
124137

125138
(defun main (&key (host *host*) (port *port*))
126139
"Main function of client"
@@ -130,7 +143,7 @@ The systematic pong is consumed and the @server response is not shown in the ter
130143
#+clisp system::simple-interrupt-condition
131144
#+ecl ext:interactive-interrupt
132145
#+allegro excl:interrupt-signal ()
133-
(uiop:quit 0))
146+
(exit 0))
134147
(usocket:connection-refused-error ()
135148
(progn (write-line "Server seems offline. Run first the server.lisp.")
136-
(uiop:quit 1)))))
149+
(exit 1)))))

0 commit comments

Comments
 (0)