2020-11-13 11:11:58 -08:00
|
|
|
;;;; backplane-server.lisp
|
|
|
|
|
|
|
|
(in-package #:backplane-dns)
|
|
|
|
|
|
|
|
;; request
|
|
|
|
|
|
|
|
(defclass request ()
|
2020-11-13 17:27:44 -08:00
|
|
|
((sender :initarg :sender)
|
|
|
|
(msg-id :initarg :msg-id
|
|
|
|
:reader msg-id)))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
|
|
|
(defclass unknown-request (request)
|
|
|
|
((request :initarg :request
|
|
|
|
:reader request)))
|
|
|
|
|
|
|
|
(defclass result ()
|
2020-11-13 17:27:44 -08:00
|
|
|
((message :initarg :message)
|
|
|
|
(msg-id :initarg :msg-id
|
|
|
|
:reader msg-id)))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
|
|
|
;; result
|
|
|
|
|
|
|
|
(defclass result/success (result) ())
|
|
|
|
(defclass result/error (result) ())
|
|
|
|
|
2020-11-13 15:22:37 -08:00
|
|
|
(defun error-p (obj) (typep obj (find-class 'result/error)))
|
|
|
|
(defun success-p (obj) (typep obj (find-class 'result/success)))
|
|
|
|
|
2020-11-13 17:27:44 -08:00
|
|
|
(defun make-success (&key msg msg-id)
|
|
|
|
(make-instance 'result/success
|
|
|
|
:message msg
|
|
|
|
:msg-id msg-id))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
2020-11-13 17:27:44 -08:00
|
|
|
(defun make-error (&key msg msg-id)
|
|
|
|
(make-instance 'result/error
|
|
|
|
:message msg
|
|
|
|
:msg-id msg-id))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
|
|
|
(defgeneric render-result (result))
|
|
|
|
|
|
|
|
(defmethod render-result ((res result/success))
|
2020-11-13 17:27:44 -08:00
|
|
|
(with-slots (message msg-id) res
|
|
|
|
(let ((msg (if message (format nil ":~A" message) ""))
|
|
|
|
(msgid (if msg-id (format nil " (~A)" msg-id) "")))
|
|
|
|
(format nil "OK~A~A" msgid msg))))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
|
|
|
(defmethod render-result ((res result/error))
|
2020-11-13 17:27:44 -08:00
|
|
|
(with-slots (message msg-id) res
|
|
|
|
(let ((msg (if message (format nil ":~A" message) ""))
|
|
|
|
(msgid (if msg-id (format nil " (~A)" msg-id) "")))
|
|
|
|
(format nil "ERROR~A~A" msgid msg))))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
2020-11-13 17:27:44 -08:00
|
|
|
(defgeneric parse-message (sender service api-version message msg-id)
|
2020-11-13 11:11:58 -08:00
|
|
|
(:documentation "Given an incoming message, turn it into the appropriate request."))
|
|
|
|
|
2020-11-13 17:27:44 -08:00
|
|
|
(defmethod parse-message (sender service api-version message msg-id)
|
|
|
|
(make-error :msg (format nil "unsupported service: ~A" service)
|
|
|
|
:msg-id msg-id))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
|
|
|
(defun decode-message (message-str)
|
2020-11-13 15:22:37 -08:00
|
|
|
(handler-case
|
|
|
|
(cl-json:decode-json-from-string message-str)
|
|
|
|
(json:json-syntax-error (err)
|
|
|
|
(declare (ignorable err))
|
2020-11-13 17:27:44 -08:00
|
|
|
(make-error :msg (format nil "invalid json string: ~A" message-str)))))
|
2020-11-13 15:22:37 -08:00
|
|
|
|
|
|
|
(defun symbolize (str) (-> str string-upcase (intern :KEYWORD)))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
|
|
|
(defun dispatch-parse-message (message sender)
|
2020-11-13 15:22:37 -08:00
|
|
|
(if-let ((api-version (cdr (assoc :VERSION message)))
|
2020-11-13 17:27:44 -08:00
|
|
|
(service (symbolize (cdr (assoc :SERVICE message))))
|
|
|
|
(msg-id (cdr (assoc :MSGID message))))
|
|
|
|
(parse-message sender service api-version (cdr (assoc :PAYLOAD message)) msg-id)
|
|
|
|
(make-error :msg (format nil "missing api_version, msgid, or service name in request in message")
|
|
|
|
:msg-id msg-id)))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
|
|
|
(defgeneric handle-message (message)
|
|
|
|
(:documentation "Perform necessary actions to handle a backplane message, and return a result."))
|
|
|
|
|
|
|
|
(defmethod handle-message ((message unknown-request))
|
2020-11-13 17:27:44 -08:00
|
|
|
(make-error :msg (format nil "unknown request: ~A" (request message))
|
|
|
|
:msg-id (msg-id message)))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
2020-11-13 15:22:37 -08:00
|
|
|
(defmacro success-> (init &rest forms)
|
|
|
|
(let ((blocksym (gensym)))
|
|
|
|
(flet ((maybe-call (f arg args)
|
|
|
|
`(let ((result ,arg))
|
|
|
|
(if (error-p result)
|
|
|
|
(return-from ,blocksym result)
|
|
|
|
(funcall (function ,f) result ,@args)))))
|
|
|
|
`(block ,blocksym
|
|
|
|
,(reduce (lambda (acc next)
|
|
|
|
(if (listp next)
|
|
|
|
(maybe-call (car next) acc (cdr next))
|
|
|
|
(maybe-call next acc '())))
|
|
|
|
forms
|
|
|
|
:initial-value init)))))
|
|
|
|
|
2020-11-13 11:11:58 -08:00
|
|
|
(defmethod xmpp:handle ((conn xmpp:connection) (message xmpp:message))
|
|
|
|
(let ((sender (xmpp:from message)))
|
2020-11-13 15:22:37 -08:00
|
|
|
(format *standard-output* "message received from ~A" sender)
|
2020-11-13 11:11:58 -08:00
|
|
|
(xmpp:message conn
|
|
|
|
(xmpp:from message)
|
2020-11-13 15:22:37 -08:00
|
|
|
(render-result (success-> message
|
|
|
|
(xmpp:body)
|
|
|
|
(decode-message)
|
|
|
|
(dispatch-parse-message sender)
|
|
|
|
(handle-message))))))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
|
|
|
(let ((backplane nil))
|
|
|
|
(defun backplane-connect (xmpp-host xmpp-username xmpp-password)
|
|
|
|
(if backplane
|
|
|
|
backplane
|
|
|
|
(progn (setf backplane (xmpp:connect-tls :hostname xmpp-host))
|
|
|
|
(xmpp:auth backplane
|
|
|
|
xmpp-username
|
|
|
|
xmpp-password
|
|
|
|
(format nil "backplane-~A" (machine-instance))
|
|
|
|
:mechanism :sasl-plain)
|
|
|
|
backplane))))
|