Skip to content

Commit

Permalink
clip refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Mar 11, 2024
1 parent e716e82 commit 472d870
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 91 deletions.
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
ring/ring-anti-forgery {:mvn/version "1.3.0"}
org.pinkgorilla/modular {:mvn/version "0.0.39"} ; encoding
org.pinkgorilla/permission {:mvn/version "0.0.15"}
org.pinkgorilla/webserver {:mvn/version "0.0.10"}}
org.pinkgorilla/webserver {:mvn/version "0.0.10"} ; middleware
}

:aliases
{:outdated {:extra-deps {com.github.liquidz/antq {:mvn/version "RELEASE"}}
Expand Down
2 changes: 1 addition & 1 deletion resources/ext/ws.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
:lazy false
:cljs-namespace [modular.ws.core]
:cljs-ns-bindings {'modular.ws.core {'send! modular.ws.core/send!
'init-ws! modular.ws.core/init-ws!
'start-websocket-client! modular.ws.core/start-websocket-client!
}}}
58 changes: 26 additions & 32 deletions src/modular/ws/core.clj
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
(ns modular.ws.core
(:require
[taoensso.timbre :as log :refer [error info warn]]
[modular.ws.adapter :refer [ws-init! start-router!]]
[modular.ws.handler :refer [add-ws-handler]]
[modular.ws.ws :as ws]))
[modular.ws.ws :as ws]
[modular.ws.service :as service]))

(def c (atom nil))
(defonce state-a (atom nil))

(def watcher-cbs (atom []))
(defn start-websocket-server [server-type sente-debug?]
(let [state (service/start-websocket-server server-type sente-debug?)]
(reset! state-a state)
state))

(defn- watch-conn-start [conn]
(let [{:keys [connected-uids]} conn]
(add-watch connected-uids :connected-uids
(fn [_ _ old new]
(when (not= old new)
(doall (for [cb @watcher-cbs]
(cb old new))))))))
(defn stop-websocket-server [state]
(service/stop-websocket-server state)
(reset! state-a nil)
nil)

(defn watch-conn [cb]
(swap! watcher-cbs conj cb))

(defn- log-conn-chg [old new]
(info "conn chg: old:" old "new: " new))

(defn init-ws! [server-type]
(let [conn (ws-init! server-type)]
(reset! c conn)
(watch-conn log-conn-chg)
(watch-conn-start conn)
(start-router! conn)
(add-ws-handler conn)))
(defn watch-conn [cb]
(swap! (:watch @state-a) conj cb))

(defn send! [uid data]
(if @c
(ws/send! @c uid data)
(if-let [conn (:conn @state-a)]
(ws/send! conn uid data)
(error "ws/send - not setup. data: " data)))

(defn send-all! [data]
(if @c
(ws/send-all! @c data)
(if-let [conn (:conn @state-a)]
(ws/send-all! conn data)
(error "ws/send-all - not setup. data: " data)))

(defn connected-uids []
(let [conn (:conn @state-a)
{:keys [connected-uids]} conn
uids (:any @connected-uids)]
uids))



(defn send-response [{:as ev-msg :keys [id ?data ring-req ?reply-fn uid send-fn]}
msg-type response]
;(let [session (:session ring-req)
Expand All @@ -59,10 +56,7 @@
:else (error "Cannot send ws-response: neither ?reply-fn nor uid was set!"))
(error "Can not send ws-response - msg-type and response have to be set, msg-type:" msg-type "response: " response)))

(defn connected-uids []
(let [{:keys [connected-uids]} @c
uids (:any @connected-uids)]
uids))


(comment
;(println "clients: " @connected-uids)
Expand Down
22 changes: 14 additions & 8 deletions src/modular/ws/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@
(:require
[taoensso.timbre :refer-macros [debug info error]]
[re-frame.core :as rf]
[modular.ws.adapter :refer [ws-init! start-router!]]
[modular.ws.service :as service]
[modular.ws.ws :as ws]))

(defonce c (atom nil))
(defonce state (atom nil))

(defn start-websocket-client! [path port]
(let [s (service/start-websocket-client! path port)]
(reset! state s)
s))

(defn stop-websocket-client! [state]
(service/stop-websocket-client! state)
(reset! state nil)
nil)

(defn init-ws! [path port]
(let [conn (ws-init! path port)]
(reset! c conn)
(start-router! conn)))

(defn send!
([data]
(when data
(info "sending (no cb): " data)
(try
(ws/send @c data)
(ws/send (:conn @state) data)
(catch :default e
(error "exception sending to ws: " e)))))
([data cb timeout]
(when data
(info "sending (cb): " data)
(try
(ws/send @c data cb timeout)
(ws/send (:conn @state) data cb timeout)
(catch :default e
(error "exception sending to ws: " e))))))

Expand Down
4 changes: 2 additions & 2 deletions src/modular/ws/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[taoensso.timbre :refer-macros [debug info warn error]]
[cemerick.url :as url]
[re-frame.core :as rf]
[modular.ws.core :refer [init-ws!]]))
[modular.ws.core :refer [start-websocket-client!]]))

(defn application-url
"gets the current url, as a map"
Expand Down Expand Up @@ -34,7 +34,7 @@
(fn [db [_]]
(let [port (changed-port db)]
(warn "ws connect to port (nil=unchanged): " port)
(init-ws! "/api/chsk" port)
(start-websocket-client! "/api/chsk" port)
db)))

#_[{:type :auto
Expand Down
22 changes: 22 additions & 0 deletions src/modular/ws/service.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(ns modular.ws.service
(:require
[taoensso.sente :as sente]
[modular.ws.service.adapter :as adapter]
[modular.ws.service.handler :as handler]
[modular.ws.service.router :as router]
[modular.ws.service.watch :as watch]))

(defn start-websocket-server [server-type sente-debug?]
(let [conn (adapter/ws-init! server-type)
bidi-routes (handler/create-bidi-routes conn)
router (router/start-router! conn)
watch (watch/watch-conn-start conn)]
(when sente-debug?
(reset! sente/debug-mode?_ true))
{:conn conn
:bidi-routes bidi-routes
:router router
:watch watch}))

(defn stop-websocket-server [{:keys [conn bidi-routes router watch] :as state}]
(router/stop-router! router))
21 changes: 14 additions & 7 deletions src/modular/ws/adapter.cljs → src/modular/ws/service.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns modular.ws.adapter
(ns modular.ws.service
(:require
[taoensso.timbre :as timbre :refer-macros [tracef debug debugf infof warn warnf errorf trace]]
[taoensso.sente :as sente :refer [cb-success?]]
Expand Down Expand Up @@ -31,9 +31,6 @@

; router

(defonce router_ (atom nil))

(defn stop-router! [] (when-let [stop-f @router_] (stop-f)))

(defn sente-csrf-warning []
(if ?csrf-token
Expand All @@ -42,10 +39,20 @@

(defn start-router! [conn]
(let [{:keys [ch-chsk]} conn]
(stop-router!)
(sente-csrf-warning)
(reset! router_
(sente/start-client-chsk-router! ch-chsk event-msg-handler))))
(sente/start-client-chsk-router! ch-chsk event-msg-handler)))

(defn stop-router! [stop-f]
(when stop-f
(stop-f)))


(defn start-websocket-client! [path port]
(let [conn (ws-init! path port)
router (start-router! conn)]
{:conn conn
:router router}))

(defn stop-websocket-client! [{:keys [conn router] :as state}]
(stop-router! router))

26 changes: 5 additions & 21 deletions src/modular/ws/adapter.clj → src/modular/ws/service/adapter.clj
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
(ns modular.ws.adapter
(ns modular.ws.service.adapter
(:require
[taoensso.timbre :as log :refer [debug info infof]]
[taoensso.sente.packers.transit :as sente-transit]
[taoensso.sente :as sente]
[modular.encoding.transit :as e]
[modular.ws.id :refer [get-sente-session-uid]]
[modular.ws.msg-handler :refer [event-msg-handler]]
[modular.ws.service.id :refer [get-sente-session-uid]]
[modular.ws.adapter.jetty :as jetty]))

(defn get-adapter [server-type]
(case server-type
:undertow (let [get-sch-adapter (requiring-resolve 'modular.ws.adapter.undertow/get-sch-adapter)]
:undertow (let [get-sch-adapter (requiring-resolve 'modular.ws.service.adapter.undertow/get-sch-adapter)]
(get-sch-adapter))
:jetty (jetty/get-sch-adapter)
:httpkit (let [get-sch-adapter (requiring-resolve 'modular.ws.adapter.httpkit/get-sch-adapter)]
:httpkit (let [get-sch-adapter (requiring-resolve 'modular.ws.service.adapter.httpkit/get-sch-adapter)]
(get-sch-adapter))
;
))

(reset! sente/debug-mode?_ true) ; Uncomment for extra debug info

(defn ws-init! [server-type]
(let [get-sch-adapter (get-adapter server-type)
packer (sente-transit/get-transit-packer :json e/encode e/decode)
Expand All @@ -34,17 +31,4 @@
:ch-chsk ch-recv
:connected-uids connected-uids
:ring-ajax-get-or-ws-handshake ajax-get-or-ws-handshake-fn
:ring-ajax-post ajax-post-fn}))

; router

(defonce router_ (atom nil))

(defn stop-router! []
(when-let [stop-fn @router_] (stop-fn)))

(defn start-router! [conn]
(let [{:keys [ch-chsk]} conn]
(stop-router!)
(reset! router_
(sente/start-server-chsk-router! ch-chsk event-msg-handler))))
:ring-ajax-post ajax-post-fn}))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns modular.ws.adapter.httpkit
(ns modular.ws.service.adapter.httpkit
(:require
[taoensso.timbre :as log :refer [debug info infof]]
[taoensso.sente.server-adapters.http-kit]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns modular.ws.adapter.jetty
(ns modular.ws.service.adapter.jetty
(:require
[taoensso.timbre :as log :refer [debug info infof]]
[taoensso.sente.server-adapters.jetty9]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns modular.ws.adapter.undertow
(ns modular.ws.service.adapter.undertow
(:require
[taoensso.timbre :as log :refer [debug info infof]]
[taoensso.sente.server-adapters.undertow]))
Expand Down
21 changes: 7 additions & 14 deletions src/modular/ws/handler.clj → src/modular/ws/service/handler.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
(ns modular.ws.handler
(ns modular.ws.service.handler
(:require
[taoensso.timbre :as log :refer [debug debugf info infof]]
[cheshire.core :as json]
[ring.middleware.anti-forgery :refer [*anti-forgery-token*]]
[modular.webserver.handler.registry :refer [add-ring-handler]]
[modular.ws.id :refer [get-sente-session-uid]]
[modular.ws.middleware :refer [wrap-ws]]))
[modular.ws.service.id :refer [get-sente-session-uid]]
[modular.ws.service.middleware :refer [wrap-ws]]))

; CSRF TOKEN

Expand Down Expand Up @@ -42,13 +41,7 @@
;(info "ws csrf: " (get-in req [:session :ring.middleware.anti-forgery/anti-forgery-token]))
res))

(def ws-token-handler-raw-wrapped (wrap-ws ws-token-handler-raw))
;(def ws-handshake-handler-wrapped (wrap-ws ws-handshake-handler))
;(def ws-ajax-post-handler-wrapped (wrap-ws ws-ajax-post-handler))


(defn add-ws-handler [conn]
(add-ring-handler :ws/token (wrap-ws ws-token-handler-raw))
(add-ring-handler :ws/chsk-get (wrap-ws (partial ws-handshake-handler conn)))
(add-ring-handler :ws/chsk-post (wrap-ws (partial ws-ajax-post-handler conn))))

(defn create-bidi-routes [conn]
{"token" (wrap-ws ws-token-handler-raw)
"chsk" {:get (wrap-ws (partial ws-handshake-handler conn))
:post (wrap-ws (partial ws-ajax-post-handler conn))}})
2 changes: 1 addition & 1 deletion src/modular/ws/id.clj → src/modular/ws/service/id.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns modular.ws.id)
(ns modular.ws.service.id)

(defn unique-id
"Get a unique id for a session."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns modular.ws.middleware
(ns modular.ws.service.middleware
"a middleware takes a handler, and wraps a middleware around it.
It is handler transformation, not routing related."
(:require
Expand Down
14 changes: 14 additions & 0 deletions src/modular/ws/service/router.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns modular.ws.service.router
(:require
[taoensso.sente :as sente]
[modular.ws.msg-handler :refer [event-msg-handler]]))

; router

(defn stop-router! [stop-fn]
(when stop-fn
(stop-fn)))

(defn start-router! [conn]
(let [{:keys [ch-chsk]} conn]
(sente/start-server-chsk-router! ch-chsk event-msg-handler)))
14 changes: 14 additions & 0 deletions src/modular/ws/service/watch.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns modular.ws.service.watch)


(defn watch-conn-start [conn]
(let [watcher-cbs (atom [])
{:keys [connected-uids]} conn]
(add-watch connected-uids :connected-uids
(fn [_ _ old new]
(when (not= old new)
(doall (for [cb @watcher-cbs]
(cb old new))))))
watcher-cbs
))

0 comments on commit 472d870

Please sign in to comment.