<commit_before>(ns block-chain.net
  (:require [aleph.tcp :as tcp]
            [manifold.stream :as s]
            [clojure.core.async :as async]))


(defn echo-handler [s info]
  (s/connect s s))

(defn server [port]
  (tcp/start-server echo-handler {:port 10001}))

(defn client [host port]
  @(tcp/client {:host host :port port}))

(defn listen [client handler]
  (async/go
    (loop [resp @(s/take! client)]
      (println "got resp: " (apply handler resp))
      (recur @(s/take! client)))
    (println "stopping listen loop")))<commit_msg>first crude pass at some echo handling<commit_after>(ns block-chain.net
  (:require #_[aleph.tcp :as tcp]
            #_[manifold.stream :as s]
            [clojure.java.io :as io]
            [clojure.core.async :as async])
  (:import  [java.net ServerSocket]))


;; (defn echo-handler [s info]
;;   (s/connect s s))

;; (defn server [port]
;;   (tcp/start-server echo-handler {:port 10001}))

;; (defn client [host port]
;;   @(tcp/client {:host host :port port}))

;; (defn listen [client handler]
;;   (async/go
;;     (loop [resp @(s/take! client)]
;;       (println "got resp: " (apply handler resp))
;;       (recur @(s/take! client)))
;;     (println "stopping listen loop")))


(defn gets
  "Read a line of textual data from the given socket"
  [reader]
  (.readLine reader))

(defn write-response
  "Send the given string message out over the given socket"
  [socket msg]
  (let [writer (io/writer socket)]
      (.write writer msg)
      (.flush writer)))

(defn serve [port handler]
  (with-open [server-sock (ServerSocket. port)
              socket (.accept server-sock)
              stream (io/reader socket)]
    (loop [lines []
           next-line (gets stream)]
      (if (empty? next-line)
        (write-response socket (handler lines))
        (recur (conj lines next-line)
               (gets stream))))))


(defn try-it []
  (serve 8334 (fn [lines]
                (println "handling lines: " lines)
                (first lines))))

;;echo handler:
#_(serve 9000 (partial apply str))
#_(let [msg-in (receive sock)
          msg-out (handler msg-in)]
      (write-response sock msg-out))

{'fraction_non_alphanumeric': 0.15423807318202873, 'fraction_numerical': 0.008337193144974525, 'mean_word_length': 2.6486486486486487, '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': '3479804', 'n_tokens_mistral': 740, 'n_tokens_neox': 700, 'n_words': 224}