The following code never manages to tail a file. It simply hangs waiting for reader input. Has anyone tried anything similar?
(def output (ref [] ))
(import 'ch.ethz.ssh2.Connection)
(import 'ch.ethz.ssh2.Session)
(import 'ch.ethz.ssh2.StreamGobbler)
(import 'java.lang.StringBuilder)
(import 'java.io.InputStream)
(import 'java.io.BufferedReader)
(import 'java.io.InputStreamReader)
(let [connection (new Connection "hostname")]
(. connection connect)
(let [ok (. connection authenticateWithPassword "username" "password" )
session (. connection openSession )]
(. session execCommand "tail -f filename.txt")
(let [sb (StringBuilder.)
stdout (StreamGobbler. (. session getStdout))
br (BufferedReader. (InputStreamReader. stdout))
]
(future (loop [line2 (. br readLine)]
(if (= line2 nil)
nil
(do
(dosync (ref-set output (conj @output line2)))
(recur (. br readLine))))
)
)
)
)
)
I agree with Arthur. Its not clear to me how this would work in practice since the remote command will never return / finish. Try the following as an example:
I think a possibly better approach would to take the threading out your client while providing a means to get access to the tail of the file asynchronously. For example, you might consider using netcat to send the output of tail -f to a socket and then periodically read from that socket in your client to get the file output. Something like this on the remote side:
Then in your clojure code:
Something like that. Hope this helps.