Abstracted server to :facilservil

This commit is contained in:
Jaidyn Lev 2018-12-02 01:32:14 -06:00
parent 404d4201aa
commit bb6a648045
2 changed files with 19 additions and 40 deletions

View File

@ -2,7 +2,7 @@
:version "0.2" :version "0.2"
:author "Jaidyn Ann <jadedctrl@teknik.io>" :author "Jaidyn Ann <jadedctrl@teknik.io>"
:license "AGPLv3" :license "AGPLv3"
:depends-on ("nih" "usocket") :depends-on ("nih" "facilservil")
:components ((:module "src" :components ((:module "src"
:components :components
((:file "package") ((:file "package")

View File

@ -1,53 +1,32 @@
(in-package :qotdd) (in-package :qotdd)
;; [HOST] [PORT] (defvar *qotd-path*)
;; [HOST] [PORT] [PATH] --> NIL
(defun server (&key (defun server (&key
(host "127.0.0.1") (host "0.0.0.0")
(port 1117) (port 1117)
(path "/usr/share/games/qotdd/qotd2018")) (path "/usr/share/games/qotdd/qotd2018"))
"Start the QOTD server." "Start the QOTD server."
(let ((socket (usocket:socket-listen host port))) (setq *qotd-path* path)
;; (server host port connect-function disconnect-function input-handler
(unwind-protect ;; :halting halt-function)
(loop ;; Since we don't take input, and only connect-then-disconnect, everything's
:do ;; blank but the connect-function.
(let ((connection (connection-get socket))) (facilservil:server host port 'main 'blank 'blank :halting 'blank))
(main connection path)
(connection-kill connection)))
(progn
(format t "Dying...")
(usocket:socket-close socket)))))
;; SOCKET --> CONNECTION_ON_SOCKET ;; SOCKET NUMBER --> NIL
(defun connection-get (socket) (defun main (socket client-id)
"Return a Connection from a Socket; until Connection recieved,
wait patiently."
(usocket:socket-accept socket :element-type 'character))
;; CONNECTION
(defun connection-kill (connection)
"Close a connection."
(usocket:socket-close connection))
(defun main (connection qotd-path)
"Main function on a connection-- send the QOTD to them." "Main function on a connection-- send the QOTD to them."
(connection-msg connection (facilservil:client-write socket
(get-quote qotd-path (get-universal-time)))) (get-quote *qotd-path* (get-universal-time)) 'T)
(facilservil:client-slaughter socket))
;; CONNECTION ;; SOCKET NUMBER [STRING] --> NIL
(defun connection-msg (connection message) (defun blank (socket client-id &optional (input-string nil)))
"Send a message to a connection."
(format (usocket:socket-stream connection) "~A" message))