Browse code
Reformat, make cleanup work
Ed Langley authored on 07/03/2019 18:14:43
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -15,6 +15,11 @@ |
15 | 15 |
(%latest-id :accessor latest-id :initform 0) |
16 | 16 |
(%queue-pair :accessor queue-pair :initarg :queue-pair :initform (make-instance 'queue-pair)))) |
17 | 17 |
|
18 |
+(defgeneric start-module (client module) |
|
19 |
+ (:documentation "start a module")) |
|
20 |
+(defgeneric stop-module (client module) |
|
21 |
+ (:documentation "stop a module")) |
|
22 |
+ |
|
18 | 23 |
(defmethod result-queue ((obj event-pump)) |
19 | 24 |
(result-queue (queue-pair obj))) |
20 | 25 |
|
... | ... |
@@ -23,3 +28,82 @@ |
23 | 28 |
|
24 | 29 |
(defun clear-waiting-pings (event-pump) |
25 | 30 |
(setf (waiting-pings event-pump) 0)) |
31 |
+ |
|
32 |
+(defmethod fwoar.event-loop:prepare-loop ((event-pump event-pump)) |
|
33 |
+ (declare (optimize (debug 3))) |
|
34 |
+ (let ((client (funcall (client-factory event-pump) event-pump))) |
|
35 |
+ #+nil (websocket-driver:start-connection client))) |
|
36 |
+ |
|
37 |
+(defmethod fwoar.event-loop:cleanup ((event-pump event-pump)) |
|
38 |
+ (setf (running event-pump) nil) |
|
39 |
+ (do-hash-table (_ v (modules event-pump)) |
|
40 |
+ (declare (ignore _)) |
|
41 |
+ (stop-module event-pump v)) |
|
42 |
+ (when (ws-client event-pump) |
|
43 |
+ (wsd:close-connection (ws-client event-pump)))) |
|
44 |
+ |
|
45 |
+(let ((last-ping nil)) |
|
46 |
+ (defun maybe-ping (event-pump) |
|
47 |
+ (let ((current-time (get-universal-time))) |
|
48 |
+ (when (or (null last-ping) |
|
49 |
+ (< 15 (- current-time last-ping))) |
|
50 |
+ (setf last-ping current-time) |
|
51 |
+ (websocket-driver:send-ping (ws-client event-pump)))))) |
|
52 |
+ |
|
53 |
+ |
|
54 |
+(defun handle-work-queue (event-pump) |
|
55 |
+ (multiple-value-bind (message message-p) |
|
56 |
+ (chanl:recv (work-queue event-pump) |
|
57 |
+ :blockp nil) |
|
58 |
+ (when message-p |
|
59 |
+ (funcall message |
|
60 |
+ event-pump)))) |
|
61 |
+ |
|
62 |
+(defmethod fwoar.event-loop:tick ((task event-pump)) |
|
63 |
+ (handle-work-queue task) |
|
64 |
+ (maybe-ping task) |
|
65 |
+ (sleep (slot-value task '%tick-pause))) |
|
66 |
+ |
|
67 |
+(defmethod attach-module ((event-pump event-pump) module &rest args &key) |
|
68 |
+ (setf (gethash (make-keyword module) |
|
69 |
+ (modules event-pump)) |
|
70 |
+ (apply #'make-instance |
|
71 |
+ module |
|
72 |
+ args))) |
|
73 |
+ |
|
74 |
+(defgeneric get-module (module event-pump) |
|
75 |
+ (:documentation "Get one of the activated modules") |
|
76 |
+ (:method (module (event-pump event-pump)) |
|
77 |
+ (gethash (make-keyword module) |
|
78 |
+ (modules event-pump)))) |
|
79 |
+ |
|
80 |
+(defun stop-slacker (event-pump) |
|
81 |
+ (funcall (finish-cb event-pump))) |
|
82 |
+ |
|
83 |
+(defun throttle-continue (num) |
|
84 |
+ (let ((continue-count 0) |
|
85 |
+ (now (local-time:now))) |
|
86 |
+ (lambda (&optional condition) |
|
87 |
+ (format t "~&~s ~s~%" now continue-count) |
|
88 |
+ (when (< continue-count num) |
|
89 |
+ (let ((new-now (local-time:now))) |
|
90 |
+ (if (< (* 1000 (local-time:timestamp-difference new-now now)) |
|
91 |
+ 5) |
|
92 |
+ (incf continue-count) |
|
93 |
+ (setf now new-now |
|
94 |
+ continue-count 0)) |
|
95 |
+ (continue condition)))))) |
|
96 |
+ |
|
97 |
+(defun test-event-pump () |
|
98 |
+ (let ((the-event-pump (make-instance 'event-pump :client-factory 'identity))) |
|
99 |
+ (chanl:send (work-queue the-event-pump) |
|
100 |
+ (lambda (a) |
|
101 |
+ (error "test"))) |
|
102 |
+ (chanl:send (work-queue the-event-pump) |
|
103 |
+ (lambda (a) |
|
104 |
+ (format t "This should happen ~s~%" a))) |
|
105 |
+ (bt:make-thread (lambda () |
|
106 |
+ (sleep 5) |
|
107 |
+ (funcall (finish-cb the-event-pump)))) |
|
108 |
+ (handler-bind ((serious-condition #'continue)) |
|
109 |
+ (fwoar.event-loop:run-loop the-event-pump)))) |
... | ... |
@@ -202,22 +202,22 @@ Return a string with the generated JSON output." |
202 | 202 |
&rest args) |
203 | 203 |
(format *xxx* "~&~a: ~{~a~^ ~}~%" target args) |
204 | 204 |
(dbind* (&optional target-name target-id) (find-channel target) |
205 |
- (if target-name |
|
206 |
- (progn |
|
207 |
- (with (message (string-join args #\space)) |
|
208 |
- (when-let* ((start-link (position #\< message)) |
|
209 |
- (stop-link (position #\> message :start start-link)) |
|
210 |
- (_ (> stop-link (+ 4 start-link)))) |
|
211 |
- (setf message (concat (subseq message 0 start-link) |
|
212 |
- (subseq message (1+ start-link) stop-link) |
|
213 |
- (subseq message (1+ stop-link))))) |
|
214 |
- (queue-message event-pump target-id |
|
215 |
- message)) |
|
216 |
- (queue-message event-pump target-id |
|
217 |
- (format nil "Notifying channel ~a" target-name) |
|
218 |
- :thread (ensure-thread message))) |
|
219 |
- (queue-message event-pump target-id (format nil "Can't find channel `~a`" target) |
|
220 |
- :thread (ensure-thread message))))) |
|
205 |
+ (if target-name |
|
206 |
+ (progn |
|
207 |
+ (with (message (string-join args #\space)) |
|
208 |
+ (when-let* ((start-link (position #\< message)) |
|
209 |
+ (stop-link (position #\> message :start start-link)) |
|
210 |
+ (_ (> stop-link (+ 4 start-link)))) |
|
211 |
+ (setf message (concat (subseq message 0 start-link) |
|
212 |
+ (subseq message (1+ start-link) stop-link) |
|
213 |
+ (subseq message (1+ stop-link))))) |
|
214 |
+ (queue-message event-pump target-id |
|
215 |
+ message)) |
|
216 |
+ (queue-message event-pump target-id |
|
217 |
+ (format nil "Notifying channel ~a" target-name) |
|
218 |
+ :thread (ensure-thread message))) |
|
219 |
+ (queue-message event-pump target-id (format nil "Can't find channel `~a`" target) |
|
220 |
+ :thread (ensure-thread message))))) |
|
221 | 221 |
|
222 | 222 |
(defparameter *reaction-store* (make-hash-table :test 'equalp :synchronized t)) |
223 | 223 |
|
... | ... |
@@ -1,18 +1,5 @@ |
1 | 1 |
(in-package :slacker) |
2 | 2 |
|
3 |
-(defmethod attach-module ((event-pump event-pump) module &rest args &key) |
|
4 |
- (setf (gethash (make-keyword module) |
|
5 |
- (modules event-pump)) |
|
6 |
- (apply #'make-instance |
|
7 |
- module |
|
8 |
- args))) |
|
9 |
- |
|
10 |
-(defgeneric get-module (module event-pump) |
|
11 |
- (:documentation "Get one of the activated modules") |
|
12 |
- (:method (module (event-pump event-pump)) |
|
13 |
- (gethash (make-keyword module) |
|
14 |
- (modules event-pump)))) |
|
15 |
- |
|
16 | 3 |
(defvar *api-token*) |
17 | 4 |
|
18 | 5 |
(defun make-client (event-pump) |
... | ... |
@@ -33,10 +20,14 @@ |
33 | 20 |
message))) |
34 | 21 |
(wsd:on :close client |
35 | 22 |
(lambda (&key code reason) |
36 |
- (format t "~&Closed with code: ~a for reason ~a. Restarting in 2 seconds~%" code reason) |
|
37 |
- (sleep 2) |
|
38 |
- (format t "~& ... restarting~%") |
|
39 |
- (make-client event-pump))) |
|
23 |
+ (if (running event-pump) |
|
24 |
+ (progn |
|
25 |
+ (format t "~&Closed with code: ~a for reason ~a. Restarting in 2 seconds~%" |
|
26 |
+ code reason) |
|
27 |
+ (sleep 2) |
|
28 |
+ (format t "~& ... restarting~%") |
|
29 |
+ (make-client event-pump)) |
|
30 |
+ (format t "~&exiting~%")))) |
|
40 | 31 |
(wsd:start-connection client))))) |
41 | 32 |
|
42 | 33 |
(defgeneric send-message (client type &key) |
... | ... |
@@ -75,11 +66,6 @@ |
75 | 66 |
:test 'equal))) |
76 | 67 |
|
77 | 68 |
|
78 |
-(defgeneric start-module (client module) |
|
79 |
- (:documentation "start a module")) |
|
80 |
-(defgeneric stop-module (client module) |
|
81 |
- (:documentation "stop a module")) |
|
82 |
- |
|
83 | 69 |
(define-condition connection-lost (error) |
84 | 70 |
()) |
85 | 71 |
|
... | ... |
@@ -109,29 +95,6 @@ |
109 | 95 |
(send-message event-pump :ping) |
110 | 96 |
(error 'connection-lost))) |
111 | 97 |
|
112 |
-(defmethod fwoar.event-loop:prepare-loop ((event-pump event-pump)) |
|
113 |
- (declare (optimize (debug 3))) |
|
114 |
- (let ((client (funcall (client-factory event-pump) event-pump))) |
|
115 |
- (websocket-driver:start-connection client))) |
|
116 |
- |
|
117 |
-(defmethod fwoar.event-loop:cleanup ((event-pump event-pump)) |
|
118 |
- (setf (running event-pump) nil) |
|
119 |
- (when (ws-client event-pump) |
|
120 |
- (wsd:close-connection (ws-client event-pump)))) |
|
121 |
- |
|
122 |
-(let ((last-ping nil)) |
|
123 |
- (defun maybe-ping (event-pump) |
|
124 |
- (let ((current-time (get-universal-time))) |
|
125 |
- (when (or (null last-ping) |
|
126 |
- (< 15 (- current-time last-ping))) |
|
127 |
- (setf last-ping current-time) |
|
128 |
- (websocket-driver:send-ping (ws-client event-pump)))))) |
|
129 |
- |
|
130 |
-(defmethod fwoar.event-loop:tick ((task event-pump)) |
|
131 |
- (handle-work-queue task) |
|
132 |
- (maybe-ping task) |
|
133 |
- (sleep (slot-value task '%tick-pause))) |
|
134 |
- |
|
135 | 98 |
(defun network-loop (event-pump modules) |
136 | 99 |
(declare (optimize (debug 3))) |
137 | 100 |
(loop for (module . args) in modules |