<commit_before>(ns server
  (:require [zeromq.zmq           :as zmq]
            [org.zproto.hydra-msg :as msg])
  (:import [org.zproto HydraMsg]))

(alter-var-root #'*out* (constantly *out*))

(declare get-latest-post)
(declare get-all-tags)
(declare get-single-tag)


(defn get-latest-post
  [& more]
  ;;
  ;; Fill in the blanks
  ;;
  (println more))
(defn get-all-tags
  [& more]
  ;;
  ;; Fill in the blanks
  ;;
  (println more))
(defn get-single-tag
  [& more]
  ;;
  ;; Fill in the blanks
  ;;
  (println more))


(defprotocol HydraServer
  (hello    [this])
  (get-tags [this])
  (get-tag  [this tag])
  (get-post [this post-id])
  (goodbye  [this])
  (invalid  [this]))

(defn next-state
  [atom expected-state next-state]
  (swap! atom
         (fn [st]
           (if (= st expected-state)
             next-state
             (throw (RuntimeException. (str "Expected to have "
                                            expected-state
                                            " state, but had "
                                            next-state)))))))

(defn ensure-state
  [atom expected-state]
  (when (not (= @atom expected-state))
    (throw (RuntimeException.
            (str "Expected to have "
                 expected-state
                 " state, but had "
                 next-state)))))

(defrecord Server [socket state]
  HydraServer
  (hello [this]
    ;; (next-state state :start :connected)
    (let [response (HydraMsg. HydraMsg/HELLO_OK)]
      (get-latest-post response)
      (.send response socket)
      (.destroy response)
      ))

  (get-tags [this]
    (ensure-state state :connected)
    (let [response (HydraMsg. HydraMsg/GET_TAGS_OK)]
      (get-all-tags response)
      (.send response socket)
      (.destroy response)
      ))

  (get-tag [this tag-id]
    (ensure-state state :connected)
    (let [response (HydraMsg. HydraMsg/GET_TAG_OK)]
      (get-single-tag response tag-id)
      (.send response socket)
      (.destroy response)
      ))

  (invalid [this]
    (.send (HydraMsg. HydraMsg/INVALID) socket))
  )





(defn match-msg
  [server ^HydraMsg msg]
  (let [id (.id msg)]
    (println (= id HydraMsg/HELLO))
    (cond
     (= id HydraMsg/HELLO)    (hello    server)
     (= id HydraMsg/GET_TAGS) (get-tags server)
     (= id HydraMsg/GET_TAG)  (get-tag  server (.tag msg))

     :default                 (invalid  server)

     ;; HydraMsg/GET_POST
     ;; HydraMsg/GET_POST_OK<commit_msg>use routing-id capability for hello/hello-ok.<commit_after>(ns server
  (:require [zeromq.zmq           :as zmq]
            [org.zproto.hydra-msg :as msg])
  (:import [org.zproto HydraMsg]))

(alter-var-root #'*out* (constantly *out*))

(declare get-latest-post)
(declare get-all-tags)
(declare get-single-tag)


(defn get-latest-post
  [& more]
  ;;
  ;; Fill in the blanks
  ;;
  (println more)
  "dummy-id")

(defn get-all-tags
  [& more]
  ;;
  ;; Fill in the blanks
  ;;
  (println more))
(defn get-single-tag
  [& more]
  ;;
  ;; Fill in the blanks
  ;;
  (println more))


(defprotocol HydraServer
  (hello    [this routing-id])
  (get-tags [this])
  (get-tag  [this tag])
  (get-post [this post-id])
  (goodbye  [this])
  (invalid  [this]))

(defn next-state
  [atom expected-state next-state]
  (swap! atom
         (fn [st]
           (if (= st expected-state)
             next-state
             (throw (RuntimeException. (str "Expected to have "
                                            expected-state
                                            " state, but had "
                                            next-state)))))))

(defn ensure-state
  [atom expected-state]
  (when (not (= @atom expected-state))
    (throw (RuntimeException.
            (str "Expected to have "
                 expected-state
                 " state, but had "
                 next-state)))))

(defrecord Server [socket state]
  HydraServer
  (hello [this routing-id]
    ;; (next-state state :start :connected)
    (let [post-id (get-latest-post)]
      (msg/hello-ok socket routing-id post-id)))

  (get-tags [this]
    (ensure-state state :connected)
    (let [response (HydraMsg. HydraMsg/GET_TAGS_OK)]
      (get-all-tags response)
      (.send response socket)
      (.destroy response)
      ))

  (get-tag [this tag-id]
    (ensure-state state :connected)
    (let [response (HydraMsg. HydraMsg/GET_TAG_OK)]
      (get-single-tag response tag-id)
      (.send response socket)
      (.destroy response)
      ))

  (invalid [this]
    (.send (HydraMsg. HydraMsg/INVALID) socket))
  )





(defn match-msg
  [server ^HydraMsg msg]
  (let [id (.id msg)
        routing-id (.routingId msg)]
    (println (= id HydraMsg/HELLO))
    (cond
     (= id HydraMsg/HELLO)    (hello    server routing-id)
     (= id HydraMsg/GET_TAGS) (get-tags server)
     (= id HydraMsg/GET_TAG)  (get-tag  server (.tag msg))

     :default                 (invalid  server)

     ;; HydraMsg/GET_POST
     ;; HydraMsg/GET_POST_OK
{'fraction_non_alphanumeric': 0.13768844221105528, 'fraction_numerical': 0.0, 'mean_word_length': 1.9672033392963626, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3001259', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1652, 'n_words': 410}