Skip to content

Instantly share code, notes, and snippets.

@francoisdevlin
Created November 13, 2009 01:09
Show Gist options
  • Save francoisdevlin/233484 to your computer and use it in GitHub Desktop.
Save francoisdevlin/233484 to your computer and use it in GitHub Desktop.
(ns crr.launch-once
(:require
[clojure.contrib [duck-streams :as duck]])
(:use
lib.sfd.swing.messages
lib.sfd.thread-utils
))
;(def my-agent (agent 0))
(def *local-host* "127.0.0.1")
;This integer was chosen randomly.
(def *launch-port* 32456)
(def *mod-code-string* "Mod code tool by Sean Devlin.")
(defn socket
([host port] (java.net.Socket. host port))
([host port timeout]
(let [s (java.net.Socket. )]
(try
(do
(.connect s (java.net.InetSocketAddress. host port) timeout)
s)
(catch java.net.SocketTimeoutException e nil)))))
(defn server-socket
[port]
(java.net.ServerSocket. port))
(defn input-stream
[inputable]
(.getInputStream inputable))
(defn launch-server []
(let [ss (server-socket *launch-port*)
s (.accept ss)]
(do
(duck/spit (.getOutputStream s) *mod-code-string*)
(.close s)
(.close ss)
;(send my-agent inc)
)))
(defn is-it-running? []
;arbitrary 50 ms timout
(let [launch-socket (socket *local-host* *launch-port* 50)]
(if launch-socket
(if (= (duck/slurp* (input-stream launch-socket))
*mod-code-string*)
true))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment