-
Notifications
You must be signed in to change notification settings - Fork 32
/
connection.clj
52 lines (45 loc) · 1.51 KB
/
connection.clj
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
(ns puppetlabs.pcp.broker.connection
(:require [clojure.string :as str]
[puppetlabs.kitchensink.core :as ks]
[puppetlabs.pcp.broker.websocket :refer [Websocket ws->remote-address ws->common-name]]
[puppetlabs.pcp.protocol :as p]
[schema.core :as s]
[slingshot.slingshot :refer [throw+ try+]])
(:import (clojure.lang IFn)))
(def Codec
"Message massaging functions"
{:decode IFn
:encode IFn})
(defprotocol ConnectionInterface
"Operations on the Connection type"
(summarize [connection]
"Returns a ConnectionLog suitable for logging."))
(declare -summarize)
(s/defrecord Connection
[websocket :- Websocket
codec :- Codec
uri :- p/Uri
expired :- s/Bool]
ConnectionInterface
(summarize [c] (-summarize c)))
(def ConnectionLog
"summarize a connection for logging"
{:commonname (s/maybe s/Str)
:remoteaddress s/Str})
(s/defn make-connection :- Connection
"Return the initial state for a websocket"
[websocket :- Websocket
codec :- Codec
uri :- p/Uri
expired :- s/Bool]
;; NOTE(ale): the 'map->...' constructor comes from schema.core's defrecord
(map->Connection
{:websocket websocket
:codec codec
:uri uri
:expired expired}))
(s/defn -summarize :- ConnectionLog
[connection :- Connection]
(let [websocket (:websocket connection)]
{:commonname (ws->common-name websocket)
:remoteaddress (ws->remote-address websocket)}))