Browse code

Less verbosity, tweaks

Jaidyn Lev authored on 2018-12-04 05:30:12
Showing 4 changed files
... ...
@@ -21,7 +21,7 @@
21 21
 (defun input-handle-ex (socket client-id input-string)
22 22
   "Example input-handler. Reverses user input and sends it back at them!"
23 23
 
24
-  (if input-string
24
+  (if (< 0 (length input-string))
25 25
     (let* ((reversed-input
26 26
 	     (nih:char-string (reverse (nih:char-list input-string))))
27 27
 
... ...
@@ -43,122 +43,129 @@
43 43
 
44 44
   (socket-write-bytes
45 45
     socket
46
-    (tu8:string-to-utf-8-bytes
46
+    (babel:string-to-octets
47 47
       (if line-break
48 48
 	(format nil "~A~%" string)
49
-	string))))
49
+	string)
50
+      :encoding :utf-8)))
50 51
 
51 52
 
52 53
 ;; STRING [BOOLEAN] [SOCKET] --> NIL
53 54
 (defun socket-broadcast (string &optional (line-break nil) (exception nil))
54
-  "Writes a `string` to all client sockets (aside from an `exception`--
55
-						  w/o line-break, by default."
55
+  "Writes a `string` to all client sockets (aside from an `exception`)."
56 56
 
57
-						  (client-broadcast string line-break
58
-								    (ignore-errors (socket-to-client exception))))
57
+  (client-broadcast string line-break
58
+		    (socket-to-client exception)))
59 59
 
60 60
 
61
-  ;; SOCKET --> NIL
62
-  (defun socket-input-flush (socket)
63
-    "Clean all input from a socket."
61
+;; SOCKET --> NIL
62
+(defun socket-input-flush (socket)
63
+  "Clean all input from a socket."
64 64
 
65
-    (let ((client-id   (socket-to-client socket)))
66
-      (client-data-set client-id "input" '())))
65
+  (let ((client-id   (socket-to-client socket)))
66
+    (client-data-set client-id "input" '())))
67 67
 
68 68
 
69
-  ;; SOCKET --> STRING
70
-  (defun socket-input-string (socket)
71
-    "Get input from a socket as a string."
69
+;; SOCKET --> STRING
70
+(defun socket-input-string (socket)
71
+  "Get input from a socket as a string."
72 72
 
73
-    (client-input-string (socket-to-client socket)))
73
+  (client-input-string (socket-to-client socket)))
74 74
 
75 75
 
76 76
 
77
-  ;; -------------------------------------
78
-  ;; CLIENT I/O
77
+;; -------------------------------------
78
+;; CLIENT I/O
79 79
 
80 80
 
81 81
 
82
-  ;; NUMBER --> NIL
83
-  (defun client-read (client)
84
-    "Read new input from a client to their stack of input bytes."
82
+;; NUMBER --> NIL
83
+(defun client-read (client)
84
+  "Read new input from a client to their stack of input bytes."
85 85
 
86
-    (socket-read (client-to-socket client)))
86
+  (socket-read (client-to-socket client)))
87 87
 
88 88
 
89
-  ;; NUMBER LIST --> NIL
90
-  (defun client-write-bytes (client bytes)
91
-    "Write bytes to a client's socket."
92
-    (format t "CLIENT-TO-SOCKET: " (client-to-socket client))
89
+;; NUMBER LIST --> NIL
90
+(defun client-write-bytes (client bytes)
91
+  "Write bytes to a client's socket."
92
+  (format t "CLIENT-TO-SOCKET: " (client-to-socket client))
93 93
 
94
-    (socket-write-bytes (client-to-socket client) bytes))
94
+  (socket-write-bytes (client-to-socket client) bytes))
95 95
 
96 96
 
97
-  ;; NUMBER STRING [BOOLEAN] --> NIL
98
-  (defun client-write (client string &optional (line-break nil))
99
-    "Writes a string to a client's socket-- w/o line-break, default."
97
+;; NUMBER STRING [BOOLEAN] --> NIL
98
+(defun client-write (client string &optional (line-break nil))
99
+  "Writes a string to a client's socket-- w/o line-break, default."
100 100
 
101
-    (socket-write (client-to-socket client) string line-break))
101
+  (socket-write (client-to-socket client) string line-break))
102 102
 
103 103
 
104
-  ;; NUMBER --> NIL
105
-  (defun client-input-flush (client)
106
-    "Clean up input from a client."
104
+;; NUMBER --> NIL
105
+(defun client-input-flush (client)
106
+  "Clean up input from a client."
107 107
 
108
-    (socket-input-flush (client-to-socket client)))
108
+  (socket-input-flush (client-to-socket client)))
109 109
 
110 110
 
111
-  ;; STRING [BOOLEAN] [SOCKET] --> NIL
112
-  (defun client-broadcast (string &optional (line-break nil) (exception nil))
113
-    "Writes a `string` to all client sockets (aside from an `exception`--
114
-						    w/o line-break, by default."
111
+;; STRING [BOOLEAN] [SOCKET] --> NIL
112
+(defun client-broadcast (string &optional (line-break nil) (exception nil))
113
+  "Writes a `string` to all client sockets (aside from an `exception`-- w/o line-break, by default."
115 114
 
116
-						    (mapcar
117
-						      (lambda (client)
118
-							(if (not (eq client exception))
119
-							  (client-write client string line-break)))
120
-						      *client-list*))
115
+  (mapcar
116
+    (lambda (client)
117
+      (if (not (eq client exception))
118
+	(client-write client string line-break)))
119
+    *client-list*))
121 120
 
122 121
 
123
-    ;; NUMBER --> STRING
124
-    (defun client-input-string (client)
125
-      "Get input from a client as a string."
122
+;; NUMBER --> STRING
123
+(defun client-input-string (client)
124
+  "Get input from a client as a string."
126 125
 
127
-      (let* ((input-bytes (client-data-get client "input"))
128
-	     (sanitized-bytes (remove-newline-bytes input-bytes)))
126
+  (let* ((input-bytes (client-data-get client "input"))
127
+	 (sanitized-bytes (remove-newline-bytes input-bytes))
128
+	 (byte-vector (list-to-byte-vector sanitized-bytes)))
129 129
 
130
-	(ignore-errors (trivial-utf-8:utf-8-bytes-to-string sanitized-bytes))))
130
+    (if sanitized-bytes
131
+      (ignore-errors (babel:octets-to-string byte-vector :encoding :utf-8))
132
+      "")))
131 133
 
132 134
 
133 135
 
134
-    ;; -------------------------------------
135
-    ;; MISC.
136
+(defun list-to-byte-vector (list)
137
+  (make-array (list (length list))
138
+	      :initial-contents list
139
+	      :element-type '(unsigned-byte 8)))
140
+
141
+;; -------------------------------------
142
+;; MISC.
136 143
 
137 144
 
138 145
 
139
-    ;; LIST --> LIST
140
-    (defun remove-newline-bytes (bytes)
141
-      "Remove undesired bytes-- null, LF, CR, etc, from a list of bytes."
146
+;; LIST --> LIST
147
+(defun remove-newline-bytes (bytes)
148
+  "Remove undesired bytes-- null, LF, CR, etc, from a list of bytes."
142 149
 
143
-      (remove 0 (remove 10 (remove 13 bytes))))
150
+  (remove 0 (remove 10 (remove 13 bytes))))
144 151
 
145 152
 
146
-    ;; SOCKET
147
-    (defun commandp (byte-list command-byte)
148
-      "Returns whether or not a command is complete, judging on it's bytes."
153
+;; SOCKET
154
+(defun commandp (byte-list command-byte)
155
+  "Returns whether or not a command is complete, judging on it's bytes."
149 156
 
150
-      (let* ((last-byte (car (last byte-list))))
157
+  (let* ((last-byte (car (last byte-list))))
151 158
 
152
-	(eq command-byte last-byte)))
159
+    (eq command-byte last-byte)))
153 160
 
154 161
 
155
-    ;; STRING STRING
156
-    (defun strequal (str1 str2)
157
-      "Returns whether or not strings are equal-- in their UTF bytes."
162
+;; STRING STRING
163
+(defun strequal (str1 str2)
164
+  "Returns whether or not strings are equal-- in their UTF bytes."
158 165
 
159
-      (let ((str1-u
160
-	      (delete 0 (tu8:string-to-utf-8-bytes str1)))
161
-	    (str2-u
162
-	      (delete 0 (tu8:string-to-utf-8-bytes str2))))
166
+  (let ((str1-u
167
+	  (delete 0 (tu8:string-to-utf-8-bytes str1 :encoding :utf-8)))
168
+	(str2-u
169
+	  (delete 0 (tu8:string-to-utf-8-bytes str2 :encoding :utf-8))))
163 170
 
164
-	(equalp str1-u str2-u)))
171
+    (equalp str1-u str2-u)))
... ...
@@ -2,9 +2,13 @@
2 2
 
3 3
 
4 4
 ;; DATA [STRING] --> STRING
5
-(defun journal (data &optional (name "unnamed"))
5
+(defun journal (data &optional (name "unnamed") (second-name "-"))
6 6
   "Print out a piece of data for logging on stdout."
7
-  (format t "~A | ~A~%" (force-string-length name 10)  data))
7
+  (format t "~A | ~A | ~A | ~A~%"
8
+	  (string-date (get-universal-time))
9
+	  (force-string-length name 10)
10
+	  (force-string-length second-name 10)
11
+	  data))
8 12
 
9 13
 
10 14
 (defun standard-journaling ())
... ...
@@ -13,13 +17,29 @@
13 17
 ;; -------------------------------------
14 18
 
15 19
 
20
+(defun string-date (universal-time)
21
+  (multiple-value-bind (second minute hour day month year)
22
+    (decode-universal-time universal-time)
23
+
24
+    (nih:string-combine
25
+      (nih:string-combine year (make-digits month 2) (make-digits day 2)
26
+			  :seperator "-")
27
+      " "
28
+      (nih:string-combine (make-digits hour 2) (make-digits minute 2)
29
+			  (make-digits second 2) :seperator ":"))))
30
+
31
+
32
+(defun make-digits (string number)
33
+  (nih:min-string-length string number :prefix "0"))
34
+
35
+
16 36
 ;; LIST --> STRING
17 37
 (defun print-bytes (bytes)
18 38
   "Print a list of (UTF-8) bytes as a string to stdout."
19 39
 
20 40
   (if bytes
21 41
     (format t "~A"
22
-	    (ignore-errors (tu8:utf-8-bytes-to-string bytes)))))
42
+	    (ignore-errors (babel:octets-to-string bytes :encoding :utf-8)))))
23 43
 
24 44
 
25 45
 ;; STRING NUMBER [STRING} --> STRING
... ...
@@ -48,7 +48,7 @@
48 48
   (let* ((master-socket
49 49
 	   (usocket:socket-listen host port
50 50
 				  :reuse-address 'T
51
-				  :element-type 'unsigned-byte )))
51
+				  :element-type '(unsigned-byte 8))))
52 52
     (reset-globals)
53 53
     (setq *socket-list* (list master-socket))
54 54
 
... ...
@@ -79,7 +79,6 @@
79 79
 		    (let* ((client-id (socket-to-client socket))
80 80
 			   (client-bytes (client-data-get client-id "input"))
81 81
 			   (client-input (client-input-string client-id)))
82
-		      (journal client-input "Client Input")
83 82
 
84 83
 		      ;; if reached *command-byte*, handle and flush input
85 84
 		      (if (commandp client-bytes command-byte)