-
Notifications
You must be signed in to change notification settings - Fork 1
/
facilservil.lisp
311 lines (230 loc) · 10.9 KB
/
facilservil.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
;; facilservil.lisp
;; Based on a server by Traut,
;; https://gist.github.com/traut/6bf71d0da54493e6f22eb3d00671f2a9
;; which is in turn inspired by
;; https://gist.github.com/shortsightedsid/71cf34282dfae0dd2528
;; https://gist.github.com/shortsightedsid/a760e0d83a9557aaffcc
;; http://mihai.bazon.net/blog/howto-multi-threaded-tcp-server-in-common-lisp
(defpackage :facilservil
(:use :cl)
(:export :server :ex-server
:send :recieve
:dig :bury
:close-it
:get-ip
:socket→con
:logger))
(in-package :facilservil)
;; —————————————————————————————————————
;; CLASSES
(defclass connection ()
((socket :accessor con→socket :initarg :socket)
(data :initform (make-hash-table :test #'equal) :initarg :data)))
;; —————————————————————————————————————
;; MACROS
;; LIST-OF-CONNECTIONS CONNECTION FUNCTION FUNCTION → NIL
(defmacro old-activity (all-connections con on-input on-disconnect)
"Macro for #'server, for handling client activity."
`(bordeaux-threads:make-thread
(lambda ()
(handler-case
(process-con-activity ,con ,all-connections ,on-input)
(t (e)
(logger "Error during processing ~a" e)
(setf ,all-connections (delete ,con ,all-connections))
(funcall ,on-disconnect ,con ,all-connections)
(close-it ,con))))))
;; LIST-OF-CONNECTIONS CONNECTION FUNCTION
(defmacro new-connection (all-connections master-con on-connect)
"Macro for #'server, for handling new connections."
`(let* ((new-socket
(usocket:socket-accept (con→socket ,master-con) :element-type 'character))
(new-con
(make-instance 'connection :socket new-socket)))
(logger "New connection from ~A" (get-ip new-con))
(push new-con ,all-connections)
(funcall on-connect new-con ,all-connections)))
;; —————————————————————————————————————
;; SERVER
;; STRING NUMBER [:FUNCTION :FUNCTION :FUNCTION] → NIL
(defun server (host port &key (on-connect #'blank) (on-input #'blank)
(on-disconnect #'blank) (on-loop #'blank))
"Starts server on given host at given port; and executes the given functions
(with connection/connections/input as arguments) according to their triggers.
This is the function you want to use.
Look at #'ex-*, the example server, for example of use."
(let* ((master-socket (usocket:socket-listen host port :backlog 256))
(master-con (make-instance 'connection :socket master-socket))
(all-connections `(,master-con)))
(loop
(loop for con in (wait-for-input all-connections)
do (if (eq con master-con)
(new-connection all-connections master-con on-connect)
(old-activity all-connections con on-input on-disconnect)))
(funcall on-loop all-connections))))
;; STRING NUMBER → THREAD
(defun server-in-thread (host port)
"Run the TCP server in a seperate thread."
(let ((thread-name (format nil "facilservil")))
(logger "Starting server in a separate thread:'~a'" thread-name)
(bordeaux-threads:make-thread
(lambda () (server host port))
:name thread-name)))
;; —————————————————————————————————————
;; CONNECTION I/O
(defgeneric send (target message &rest args)
(:documentation "Send a given message to a target user."))
;; CONNECTION VARYING → NIL
(defmethod send ((con connection) message &rest args)
(apply 'send (append (list (con→socket con) message) args)))
;; STREAM-USOCKET VARYING → NIL
(defmethod send ((socket usocket::stream-usocket) message &rest args)
(let ((sstream (usocket:socket-stream socket)))
(apply 'format (append (list sstream message) args))
;; (format sstream (format nil (format nil "~A" message)))
(force-output sstream)))
;; STREAM-SERVER-USOCKET VARYING → NIL
(defmethod send ((s usocket::stream-server-usocket) a &rest d) nil)
;; LIST-OF-SOCKETS/CONNECTIONS VARYING → NIL
(defmethod send ((sockets list) message &rest args)
(mapcar (lambda (socket)
(apply 'send (append (list socket message) args))) sockets))
;; —————————————————
(defgeneric recieve (target)
(:documentation "Recieve a string from a given target."))
;; CONNECTION → STRING
(defmethod recieve ((con connection))
(recieve (con→socket con)))
;; STREAM-USOCKET → STRING
(defmethod recieve ((socket usocket::stream-usocket))
(string-sanitize (read-line (usocket:socket-stream socket))))
;; STREAM-SERVER-USOCKET → NIL
(defmethod recieve ((socket usocket::stream-server-usocket)) nil)
;; —————————————————————————————————————
;; CONNECTION STORAGE
;; CONNECTION STRING → VARYING
(defun dig (connection variable)
"Get the value of a variable from the connection's hashtable."
(gethash variable (slot-value connection 'data)))
;; CONNECTION STRING VARYING → VARYING
(defun bury (connection variable value)
"Set the value of a variable in the connection's hashtable."
(setf (gethash variable (slot-value connection 'data)) value))
;; —————————————————————————————————————
;; CONNECTION MANAGEMENT
;; SOCKET → NIL
(defun process-con-activity (con connection-list on-input)
"Process client socket that got some activity"
(let ((message (recieve con)))
(logger "~A: ~A" (get-ip con) message)
(funcall on-input con message connection-list)))
;; —————————————————
(defgeneric close-it (target &optional con-list on-disconnect)
(:documentation "Shut down a target's connection, forcefully.
Run the disconnect function as well."))
;; CONNECTION LIST-OF-CONNECTIONS FUNCTION
(defmethod close-it ((con connection) &optional connection-list on-disconnect)
(close-it (con→socket con) connection-list on-disconnect))
;; STREAM-USOCKET LIST-OF-CONNECTIONS FUNCTION
(defmethod close-it ((socket usocket:stream-usocket) &optional connection-list on-disconnect)
(when connection-list (funcall on-disconnect socket connection-list))
(handler-case
(usocket:socket-close socket)
(error (e)
(logger "Ignoring the error from closing connection: ~a" e)))
(logger "Connection closed: ~A" socket))
;; —————————————————
;; SOCKET LIST-OF-CONNECTIONS → CONNECTION
(defun socket→con (socket connections)
"Return the connection— from a list of them— that matches the given socket."
(loop :for con :in connections
:if (eq (con→socket con) socket)
:return con))
;; —————————————————
;; LIST-OF-CONNECTIONS → LIST-OF-READY-CONNECTIONS
(defun wait-for-input (connections)
"Basically a wrapper around #'usocket:wait-for-input, but for connections
rather than stream-usocket objects."
(let ((sockets (mapcar #'con→socket connections)))
(mapcar (lambda (socket) (socket→con socket connections))
(usocket:wait-for-input sockets :timeout 10 :ready-only 'T))))
;; —————————————————————————————————————
;; LOGGING, ETC
;; STRING … ARG → NIL
(defun logger (text &rest args)
"Simple wrapper around format func to simplify logging."
(apply 'format (append (list t (concatenate 'string text "~%")) args)))
;; —————————————————————————————————————
;; CONNECTION METADATA
(defgeneric server-p (target)
(:documentation "Return if a given item's the server's connection/socket."))
;; USOCKET → BOOL
(defmethod server-p ((socket usocket::usocket))
(eq (type-of socket) 'usocket:stream-server-usocket))
;; CONNECTION → BOOL
(defmethod server-p ((con connection))
(server-p (con→socket con)))
;; —————————————————
(defgeneric get-ip (target)
(:documentation "Return the IP address of a given socket/connection."))
;; CONNECTION → IP
(defmethod get-ip ((con connection))
(get-ip (con→socket con)))
;; STREAM-USOCKET → IP
(defmethod get-ip ((socket usocket::stream-usocket))
(usocket:get-peer-address socket))
;; —————————————————————————————————————
;; MISC
;; STRING → STRING
(defun string-remove-octets (string &rest restricted-octs)
"Remove characters from a string matching any passed 'restricted' octet."
(let ((octets (flexi-streams:string-to-octets string :external-format :utf-8)))
(mapcar (lambda (octet) (setq octets (remove octet octets))) restricted-octs)
(flexi-streams:octets-to-string octets :external-format :utf-8)))
;; NUMBER NUMBER → LIST
(defun range (start end)
"Return whole numbers between start and end, inclusive."
(loop :for i :from start :to end :collect i))
(defun string-sanitize (string)
(string-remove-octets string 12 13 14 15))
;; VARYING … → NIL
(defun blank (&rest ignored)
"Literal nothing. Used as a default for #'server, so that one can ommit
any given trigger, if they want."
nil)
;; —————————————————————————————————————
;; EXAMPLE SERVER
;; This is a general outline of any server using facilservil.
;; Four functions for each type of trigger (connection, disconnection, input,
;; loop), passed to the #'facilservil:server function.
;; If you can't tell, it's a simple chat server!
;; STRING NUMBER
(defun ex-server (host port)
"Wrapping up the example-server for convenience."
(server host port
:on-connect #'ex-connect :on-disconnect #'ex-disconnect
:on-input #'ex-input :on-loop #'ex-loop))
;; CONNECTION LIST-OF-CONNECTIONS → NIL
(defun ex-connect (con con-list)
"Executed whenever a client connects."
(bury con "id-number" (random 9999))
(send con "Welcome to facila example! ♥~%")
(send con "Users online now—~%")
(mapcar (lambda (acon) (send con "~A, " (dig acon "id-number"))) con-list)
(send con "~%~%")
(send con-list
(format nil "~A just joined as ~A!~%"
(get-ip con) (dig con "id-number"))))
;; CONNECTION LIST-OF-CONNECTIONS → NIL
(defun ex-disconnect (con con-list)
"Executed whenever a client disconnects."
(send con-list (format nil "~A just died~%" (dig con "id-number"))))
;; CONNECTION STRING LIST-OF-CONNECTIONS → NIL
(defun ex-input (con input con-list)
"Executed on a connection + it's input."
(send (remove con con-list) "~A: ~A~%" (dig con "id-number") input))
;; LIST-OF-CONNECTIONS → NIL
(defun ex-loop (con-list)
"Executed after input taken, or after #'wait-until-input timeout
(so maximum, every 10 seconds)."
nil)