Browse code

Abstracted server to :facilservil

Jaidyn Lev authored on 2018-12-02 07:32:14
Showing 2 changed files
... ...
@@ -2,7 +2,7 @@
2 2
            :version "0.2"
3 3
            :author "Jaidyn Ann <jadedctrl@teknik.io>"
4 4
            :license "AGPLv3"
5
-           :depends-on ("nih" "usocket")
5
+           :depends-on ("nih" "facilservil")
6 6
            :components ((:module "src"
7 7
                                  :components
8 8
                                  ((:file "package")
... ...
@@ -1,53 +1,32 @@
1 1
 (in-package :qotdd)
2 2
 
3
-;; [HOST] [PORT]
3
+(defvar *qotd-path*)
4
+
5
+;; [HOST] [PORT] [PATH] --> NIL
4 6
 (defun server (&key
5
-                (host "127.0.0.1")
7
+                (host "0.0.0.0")
6 8
                 (port 1117)
7 9
                 (path "/usr/share/games/qotdd/qotd2018"))
8 10
   "Start the QOTD server."
9 11
 
10
-  (let ((socket (usocket:socket-listen host port)))
11
-
12
-    (unwind-protect
13
-      (loop
14
-        :do
15
-        (let ((connection (connection-get socket)))
16
-          (main connection path)
17
-          (connection-kill connection)))
18
-
19
-      (progn
20
-        (format t "Dying...")
21
-        (usocket:socket-close socket)))))
22
-
23
-
24
-
25
-;; SOCKET --> CONNECTION_ON_SOCKET
26
-(defun connection-get (socket)
27
-  "Return a Connection from a Socket; until Connection recieved,
28
-  wait patiently."
29
-
30
-  (usocket:socket-accept socket :element-type 'character))
31
-
32
-
33
-
34
-;; CONNECTION
35
-(defun connection-kill (connection)
36
-  "Close a connection."
37
-
38
-  (usocket:socket-close connection))
12
+  (setq *qotd-path* path)
13
+  ;; (server host port connect-function disconnect-function input-handler
14
+  ;;         :halting halt-function)
15
+  ;; Since we don't take input, and only connect-then-disconnect, everything's
16
+  ;; blank but the connect-function.
17
+  (facilservil:server host port 'main 'blank 'blank :halting 'blank))
39 18
 
40 19
 
41 20
 
42
-(defun main (connection qotd-path)
21
+;; SOCKET NUMBER --> NIL
22
+(defun main (socket client-id)
43 23
   "Main function on a connection-- send the QOTD to them."
44 24
 
45
-  (connection-msg connection
46
-                  (get-quote qotd-path (get-universal-time))))
25
+  (facilservil:client-write socket
26
+                  (get-quote *qotd-path* (get-universal-time)) 'T)
47 27
 
28
+  (facilservil:client-slaughter socket))
48 29
 
49
-;; CONNECTION
50
-(defun connection-msg (connection message)
51
-  "Send a message to a connection."
52 30
 
53
-  (format (usocket:socket-stream connection) "~A" message))
31
+;; SOCKET NUMBER [STRING] --> NIL
32
+(defun blank (socket client-id &optional (input-string nil)))