 (ns pfrt.pf
   (:require [clojure.java.io :as io]
+            [pfrt.core :refer [IComponentLifecycle]]
             [pfrt.util :as util]
             [pfrt.executor :as executor])
   (:gen-class))

                       (alter pfspeed assoc-in [host :out] (:out data))))))]
    (executor/run-interval calcfn 1000)))

(defrecord PacketFilter [data speed lastpk]
  Lifecycle

  (init [_ system] system
    (let [t-packet-reader     (thread (s-packet-reader data lastpk))
          t-speed-calculator  (thread (s-speed-calculator data speed))]
      (.setDaemon t-packet-reader false)
      (.setDaemon t-speed-calculator false)
      (-> system
          (assoc ::pf-reader-thread t-packet-reader)
          (assoc ::pf-speedc-thread t-speed-calculator))))

  (start [_ system]
    (.start (::pf-reader-thread system))
    (.start (::pf-speedc-thread system))
    (-> system
        (assoc ::pf-data data)
        (assoc ::pf-data-last lastpk)))

  (stop [_ system]
    (.interrupt (::pf-reader-thread system))
    (.interrupt (::pf-speedc-thread system))
    system))

(defn packet-filter []
  (->PacketFilter (ref {}) (ref {}) (agent {})))


(defn s-speed-calculator
  "Runnable that calculate speed over time of captured packets."
  [config pfdata pfspeed]
  (let [calcfn (fn []
                 (doseq [[host data] (seq (deref pfdata))]
                   (dosync
