forked from AeroNotix/ryeboy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ryeboy-conn.lisp
67 lines (57 loc) · 2.38 KB
/
ryeboy-conn.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(in-package :ryeboy)
(define-condition riemann-error-response ()
((error-type :initarg :name :reader error-type)
(error-msg :initarg :msg :reader error-msg))
(:report
(lambda (condition stream)
(format stream
"Error ~:(~A~) ~A"
(error-type condition)
(error-msg condition)))))
(defun make-connection (&key (connection-type :tcp) (host "localhost") (port 5555))
(usocket:socket-stream
(usocket:socket-connect host port
:element-type '(unsigned-byte 8)
:protocol (if (eq :tcp connection-type)
:stream :datagram))))
(defun make-msg (&rest events)
(let* ((msg (make-instance 'com.aphyr.riemann:msg))
(events (make-array (list (length events))
:element-type 'com.aphyr.riemann:event
:initial-contents events)))
(setf (com.aphyr.riemann:events msg) events)
msg))
(defun handle-response (response request-type)
(if (eq :events request-type)
T
(com.aphyr.riemann:events response)))
(defun read-reply (conn request-type)
(let* ((wire-size (read-int conn))
(buf (make-array wire-size :element-type '(unsigned-byte 8))))
(read-sequence buf conn)
(let ((response (bytes->msg buf)))
(when (com.aphyr.riemann:has-ok response)
(if (com.aphyr.riemann:ok response)
(handle-response response request-type)
(error (make-instance 'riemann-error-response
:msg (protocol-buffer:string-value
(com.aphyr.riemann:error response))
:name request-type)))))))
(defun send-msg (conn msg req-type)
(let ((encoded (thing->bytes msg)))
(write-int (length encoded) conn)
(write-sequence encoded conn)
(finish-output conn)
(read-reply conn req-type)))
(defun send-event (conn event)
;; TODO: UDP connections aren't length prefixed, namsay
(let ((msg (make-msg event)))
(send-msg conn msg :events)))
(defun send-events (conn &rest events)
(let ((msg (apply #'make-msg events)))
(send-msg conn msg :events)))
(defun query (conn query-string)
(let* ((msg (make-instance 'com.aphyr.riemann:msg))
(query (make-query query-string)))
(setf (com.aphyr.riemann:query msg) query)
(send-msg conn msg :query)))