<commit_before>(ns hellhound.connection
  (:require [hellhound.system :refer [get-system]]
            [hellhound.logger.core :as logger]))



(defmulti router
  "Multimethod to handle Sente `event-msg`s"
  :id ; Dispatch on event-id
  )

(defmethod router
   :default ; Default/fallback case (no other matching handler)
  [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}]
  (let [session (:session ring-req)
        uid     (:uid     session)]
    (logger/warn (str "Unhandled event: " event))
    (when ?reply-fn
      (?reply-fn {:umatched-event-as-echoed-from-from-server event}))))

(defmethod router :chsk/ws-ping
  [{:as ev-msg :keys [?data ?reply-fn event]}]
  (logger/warn "TODO: Handle ws-ping event"))


(defmethod router :hellhound/event
  [{:as ev-msg :keys [?data ?reply-fn event event-router send-fn]}]
  (logger/warn "TODO: Handle hellhound event")

  (let [event-name    (:event-name ?data)
        event-handler (get event-router event-name)]
    (if (nil? event-handler)
      ;; TODO: Should we send any error code or something similar?
      (logger/warn "Can't find an event handler for '%s' event" event-name)
      (event-handler (:data ?data) ev-msg))))

(defn event-router [{:as ev-msg :keys [id ?data event]}]
  (router ev-msg))

(defn router-builder
  [router-map]
  (fn [{:as ev-msg :keys [id ?data event]}]
    (let [event-map (assoc ev-msg :event-router router-map)]
      (router event-map))))

(defn send-to-all
  "Send the given event to all the connected users."
  [event]
  (let [func (:chsk-send! (:websocket (get-system)))]
    (func :sente/all-users-without-uid event)))
<commit_msg>A brief description added to message dispatcher on connection ns<commit_after>(ns hellhound.connection
  "This namespace is responsible for dispatching reveived data
  from the client to the correct message handler in the server side
  application. In order to define a message router checkout the
  `hellhound.messaging.core` namespace. This namespace is an internal
  namespace which used by the `websocket` component."
  (:require [hellhound.system :refer [get-system]]
            [hellhound.logger.core :as logger]))



(defmulti router
  "Multimethod to handle Sente `event-msg`s. Dispatch messages by `:id`"
  :id)

(defmethod router
   :default ; Default/fallback case (no other matching handler)
  [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}]
  (let [session (:session ring-req)
        uid     (:uid     session)]
    (logger/warn (str "Unhandled event: " event))
    (when ?reply-fn
      (?reply-fn {:umatched-event-as-echoed-from-from-server event}))))

(defmethod router :chsk/ws-ping
  [{:as ev-msg :keys [?data ?reply-fn event]}]
  (logger/warn "TODO: Handle ws-ping event"))


(defmethod router :hellhound/event
  [{:as ev-msg :keys [?data ?reply-fn event message-router uid
                      client-id send-fn]}]
  (logger/warn "TODO: Handle hellhound event")

  (let [message-name    (:message-name ?data)
        message-handler (get event-router event-name)]
    (if (nil? event-handler)
      ;; TODO: Should we send any error code or something similar?
      (logger/warn "Can't find an event handler for '%s' event" event-name)
      ;; Calls the message handler function provided by the message router
      ;; with the following arguments:
      ;; First argument is the `data` which sent by the client, second argument
      ;; is a `function` to send data back to the same client and finally the
      ;; third argument is the `ev-msg` itself which contains complete
      ;; on the received message information.
      (message-handler (:data ?data) #(send-fn uid %) ev-msg))))

(defn event-router [{:as ev-msg :keys [id ?data event]}]
  (router ev-msg))

(defn router-builder
  [router-map]
  (fn [{:as ev-msg :keys [id ?data event]}]
    (let [event-map (assoc ev-msg :event-router router-map)]
      (router event-map))))

(defn send-to-all
  "Send the given event to all the connected users."
  [event]
  (let [func (:chsk-send! (:websocket (get-system)))]
    (func :sente/all-users-without-uid event)))

{'fraction_non_alphanumeric': 0.13591996095656417, 'fraction_numerical': 0.0, 'mean_word_length': 3.3560042507970245, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3930415', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1303, 'n_words': 448}