Skip to content

Commit

Permalink
hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
cemerick committed Feb 9, 2012
1 parent 414b118 commit 0f5d5ea
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/main/clojure/clojure/tools/nrepl.clj
Expand Up @@ -188,7 +188,8 @@
responses."
[& {:keys [port host]}]
{:pre [port]}
(transport/bencode (java.net.Socket. (or host "localhost") port)))
(let [^String host (or host "localhost")]
(transport/bencode (java.net.Socket. host (int port)))))

(def connect-defaults
{"nrepl" {:transport-fn transport/bencode
Expand Down
4 changes: 2 additions & 2 deletions src/main/clojure/clojure/tools/nrepl/bencode.clj
Expand Up @@ -149,7 +149,7 @@
;;
;; So we have to do some translation here. Luckily `.byteValue`
;; does that for us.
(Byte/valueOf (.byteValue c))))
(Byte/valueOf (.byteValue (int c)))))

(defn #^{:private true :tag "[B"} read-bytes
[#^InputStream input n]
Expand Down Expand Up @@ -411,7 +411,7 @@
(loop [i 0]
(if (== i len)
(- alen blen)
(let [x (- (aget a i) (aget b i))]
(let [x (- (int (aget a i)) (int (aget b i)))]
(if (zero? x)
(recur (inc i))
x))))))
Expand Down
7 changes: 4 additions & 3 deletions src/main/clojure/clojure/tools/nrepl/server.clj
Expand Up @@ -30,7 +30,7 @@
(defn stop-server
"Stops a server started via `start-server`."
[server]
(send-off server #(returning % (.close (:ss %)))))
(send-off server #(returning % (.close ^ServerSocket (:ss %)))))

(defn start-server
"Starts a socket-based nREPL server. Configuration includes:
Expand All @@ -48,7 +48,8 @@
Returns a handle to the server that is started, which may be stopped
either via `stop-server`, (.close server), or automatically via `with-open`."
[& {:keys [port transport-fn handler ack-port greeting-fn] :or {port 0}}]
(let [smap {:ss (ServerSocket. port)
(let [ss (ServerSocket. port)
smap {:ss ss
:transport (or transport-fn transport/bencode)
:greeting greeting-fn
:handler (or handler
Expand All @@ -57,5 +58,5 @@
(close [] (stop-server this)))]
(send-off server accept-connection)
(when ack-port
(ack/send-ack (.getLocalPort (:ss @server)) ack-port))
(ack/send-ack (.getLocalPort ss) ack-port))
server))
34 changes: 8 additions & 26 deletions src/main/clojure/clojure/tools/nrepl/transport.clj
Expand Up @@ -19,40 +19,24 @@
ms or if the underlying channel has been closed.")
(send [this msg] "Sends msg."))

(defprotocol CloseableTransport
(close [this])
(closed? [this]))

(defn- closeable?
[fn]
(when-not fn (throw (UnsupportedOperationException. "Transport is not closeable"))))

(deftype FnTransport [recv-fn send-fn close closed?]
(deftype FnTransport [recv-fn send-fn close]
Transport
(send [this msg] (-> msg clojure.walk/stringify-keys send-fn) this)
(recv [this] (.recv this Long/MAX_VALUE))
(recv [this timeout] (clojure.walk/keywordize-keys (recv-fn timeout)))
java.io.Closeable
(close [this] (closeable? close) (close)))

(extend-type FnTransport
CloseableTransport
(close [this] (.close this))
(closed? [this]
(closeable? (.closed? this))
((.closed? this))))
(close [this] (close)))

(defn fn-transport
([read write] (fn-transport read write nil nil))
([read write close closed?]
([read write close]
(let [read-queue (SynchronousQueue.)]
(future (while true
(.put read-queue (read))))
(FnTransport.
#(.poll read-queue % TimeUnit/MILLISECONDS)
write
close
closed?))))
close))))

(defn bencode
([^Socket s] (bencode s s s))
Expand All @@ -65,8 +49,7 @@
(doto out
(be/write-bencode %)
.flush))
(when s #(.close s))
(when s #(.isClosed s))))))
(when s #(.close s))))))

(defn tty
([^Socket s] (tty s s s))
Expand All @@ -85,9 +68,9 @@
write (fn [{:strs [out err value status ns new-session id] :as msg}]
(when new-session (reset! session-id new-session))
(when ns (reset! cns ns))
(doseq [x [out err value] :when x]
(doseq [^String x [out err value] :when x]
(.write w x))
(when (and (= status #{:done}) id (.startsWith id "eval"))
(when (and (= status #{:done}) id (.startsWith ^String id "eval"))
(prompt true))
(.flush w))
read #(let [head (promise)]
Expand All @@ -98,8 +81,7 @@
(fn-transport read write
(when s
(swap! read-seq (partial cons {:session @session-id :op "close"}))
#(.close s))
(when s #(.isClosed s))))))
#(.close s))))))

(defn tty-greeting
[transport]
Expand Down

0 comments on commit 0f5d5ea

Please sign in to comment.