             [clj-wamp.server :as wamp]
             [clojure.string :refer (split)]
             [cheshire.core :as json]
-            [taoensso.timbre :as timbre])
+            [taoensso.timbre :as timbre]
+            [clojure.core.cache :as cache])
   (:import [java.net InetAddress URI]
            [java.util UUID]))
 (timbre/refer-timbre)

(defn- send-summary
  "Sends a summary of the published messages, built by the optional
  summary function given at source creation."
  [{:keys [summary-fn summaries] :as source} topic]
  (debug "Requested to send summary of" topic "to" wamp/*call-sess-id*)
  (if summary-fn
    (let [summary (get @summaries topic)]
      (debug "Sending summary of" topic "to" wamp/*call-sess-id*)
      (trace "Summary is" summary)

  (info "Starting websocket source with directory writer" directory-writer "and config" config)
  (let [uri (or uri (URI. (str "ws://" (. (InetAddress/getLocalHost) getHostName) ":" port)))
        stop-fn (atom nil)
        config (assoc config :cache-size cache-size :uri uri)
        summaries (when summary-fn
                    (if (< 0 summary-ttl)
                      (cache/ttl-cache-factory {} :ttl summary-ttl)
                      {}))
        source (WebsocketSource. directory-writer stop-fn (atom true) (ref {}) config
                                 (atom summaries))
        app (make-app source)
        wrapped-app (if wrap-fn (wrap-fn app) app)]
    (reset! stop-fn (http/run-server wrapped-app {:port port}))
