Less verbosity, tweaks

This commit is contained in:
Jaidyn Lev 2018-12-03 23:30:12 -06:00
parent a7f5301e87
commit 9696ecb825
4 changed files with 100 additions and 74 deletions

View File

@ -21,7 +21,7 @@
(defun input-handle-ex (socket client-id input-string) (defun input-handle-ex (socket client-id input-string)
"Example input-handler. Reverses user input and sends it back at them!" "Example input-handler. Reverses user input and sends it back at them!"
(if input-string (if (< 0 (length input-string))
(let* ((reversed-input (let* ((reversed-input
(nih:char-string (reverse (nih:char-list input-string)))) (nih:char-string (reverse (nih:char-list input-string))))

View File

@ -43,19 +43,19 @@
(socket-write-bytes (socket-write-bytes
socket socket
(tu8:string-to-utf-8-bytes (babel:string-to-octets
(if line-break (if line-break
(format nil "~A~%" string) (format nil "~A~%" string)
string)))) string)
:encoding :utf-8)))
;; STRING [BOOLEAN] [SOCKET] --> NIL ;; STRING [BOOLEAN] [SOCKET] --> NIL
(defun socket-broadcast (string &optional (line-break nil) (exception nil)) (defun socket-broadcast (string &optional (line-break nil) (exception nil))
"Writes a `string` to all client sockets (aside from an `exception`-- "Writes a `string` to all client sockets (aside from an `exception`)."
w/o line-break, by default."
(client-broadcast string line-break (client-broadcast string line-break
(ignore-errors (socket-to-client exception)))) (socket-to-client exception)))
;; SOCKET --> NIL ;; SOCKET --> NIL
@ -110,8 +110,7 @@
;; STRING [BOOLEAN] [SOCKET] --> NIL ;; STRING [BOOLEAN] [SOCKET] --> NIL
(defun client-broadcast (string &optional (line-break nil) (exception nil)) (defun client-broadcast (string &optional (line-break nil) (exception nil))
"Writes a `string` to all client sockets (aside from an `exception`-- "Writes a `string` to all client sockets (aside from an `exception`-- w/o line-break, by default."
w/o line-break, by default."
(mapcar (mapcar
(lambda (client) (lambda (client)
@ -125,12 +124,20 @@
"Get input from a client as a string." "Get input from a client as a string."
(let* ((input-bytes (client-data-get client "input")) (let* ((input-bytes (client-data-get client "input"))
(sanitized-bytes (remove-newline-bytes input-bytes))) (sanitized-bytes (remove-newline-bytes input-bytes))
(byte-vector (list-to-byte-vector sanitized-bytes)))
(ignore-errors (trivial-utf-8:utf-8-bytes-to-string sanitized-bytes)))) (if sanitized-bytes
(ignore-errors (babel:octets-to-string byte-vector :encoding :utf-8))
"")))
(defun list-to-byte-vector (list)
(make-array (list (length list))
:initial-contents list
:element-type '(unsigned-byte 8)))
;; ------------------------------------- ;; -------------------------------------
;; MISC. ;; MISC.
@ -157,8 +164,8 @@
"Returns whether or not strings are equal-- in their UTF bytes." "Returns whether or not strings are equal-- in their UTF bytes."
(let ((str1-u (let ((str1-u
(delete 0 (tu8:string-to-utf-8-bytes str1))) (delete 0 (tu8:string-to-utf-8-bytes str1 :encoding :utf-8)))
(str2-u (str2-u
(delete 0 (tu8:string-to-utf-8-bytes str2)))) (delete 0 (tu8:string-to-utf-8-bytes str2 :encoding :utf-8))))
(equalp str1-u str2-u))) (equalp str1-u str2-u)))

View File

@ -2,9 +2,13 @@
;; DATA [STRING] --> STRING ;; DATA [STRING] --> STRING
(defun journal (data &optional (name "unnamed")) (defun journal (data &optional (name "unnamed") (second-name "-"))
"Print out a piece of data for logging on stdout." "Print out a piece of data for logging on stdout."
(format t "~A | ~A~%" (force-string-length name 10) data)) (format t "~A | ~A | ~A | ~A~%"
(string-date (get-universal-time))
(force-string-length name 10)
(force-string-length second-name 10)
data))
(defun standard-journaling ()) (defun standard-journaling ())
@ -13,13 +17,29 @@
;; ------------------------------------- ;; -------------------------------------
(defun string-date (universal-time)
(multiple-value-bind (second minute hour day month year)
(decode-universal-time universal-time)
(nih:string-combine
(nih:string-combine year (make-digits month 2) (make-digits day 2)
:seperator "-")
" "
(nih:string-combine (make-digits hour 2) (make-digits minute 2)
(make-digits second 2) :seperator ":"))))
(defun make-digits (string number)
(nih:min-string-length string number :prefix "0"))
;; LIST --> STRING ;; LIST --> STRING
(defun print-bytes (bytes) (defun print-bytes (bytes)
"Print a list of (UTF-8) bytes as a string to stdout." "Print a list of (UTF-8) bytes as a string to stdout."
(if bytes (if bytes
(format t "~A" (format t "~A"
(ignore-errors (tu8:utf-8-bytes-to-string bytes))))) (ignore-errors (babel:octets-to-string bytes :encoding :utf-8)))))
;; STRING NUMBER [STRING} --> STRING ;; STRING NUMBER [STRING} --> STRING

View File

@ -48,7 +48,7 @@
(let* ((master-socket (let* ((master-socket
(usocket:socket-listen host port (usocket:socket-listen host port
:reuse-address 'T :reuse-address 'T
:element-type 'unsigned-byte ))) :element-type '(unsigned-byte 8))))
(reset-globals) (reset-globals)
(setq *socket-list* (list master-socket)) (setq *socket-list* (list master-socket))
@ -79,7 +79,6 @@
(let* ((client-id (socket-to-client socket)) (let* ((client-id (socket-to-client socket))
(client-bytes (client-data-get client-id "input")) (client-bytes (client-data-get client-id "input"))
(client-input (client-input-string client-id))) (client-input (client-input-string client-id)))
(journal client-input "Client Input")
;; if reached *command-byte*, handle and flush input ;; if reached *command-byte*, handle and flush input
(if (commandp client-bytes command-byte) (if (commandp client-bytes command-byte)