Skip to content

Instantly share code, notes, and snippets.

@al3x
Created November 24, 2009 02:21
Show Gist options
  • Save al3x/241575 to your computer and use it in GitHub Desktop.
Save al3x/241575 to your computer and use it in GitHub Desktop.
import java.io.{BufferedReader, File, InputStream, InputStreamReader, IOException, PrintWriter, Writer}
import java.net.{InetAddress, ServerSocket, Socket, SocketException}
import java.util.concurrent.Executors
import net.lag.logging.Logger
import scala.actors.Actor
import scala.actors.Actor._
import scala.tools.nsc._
class RemoteDebugServer(port: Int) {
private val log = Logger.get
try {
val listener = new ServerSocket(port)
log.info("remote debugger listening on port " + port)
val jars = findJarFileNames(".")
loop { new RemoteDebuggingClient(listener.accept(), jars).start() }
listener.close()
} catch {
case e: IOException => {
log.error("remote debugger could not listen on port: " + port )
System.exit(-1)
}
}
private def findJarFileNames(inDirPath: String): List[String] = {
new File(inDirPath).listFiles
.filter { f => f.isDirectory == false }
.filter { f => f.getName.endsWith(".jar") }
.map(_.getName.toString)
.toList
}
}
class RemoteDebuggingClient(socket: Socket, jars: List[String]) extends Actor {
private val log = Logger.get
val remoteHost = socket.getInetAddress().getHostName()
def act {
try {
log.info("remote debug client connected from " + remoteHost)
val in = new BufferedReader(new InputStreamReader(socket.getInputStream()))
val out = new PrintWriter(socket.getOutputStream(), true)
val repl = new InterpreterLoop(in, out)
val settings = new Settings(null)
val fakeClasspath = List(settings.classpath.value) ::: jars
settings.classpath.value = fakeClasspath.mkString(File.pathSeparator)
repl.main(settings)
var line = in.readLine()
while (line != null) {
line = in.readLine()
out.flush()
}
} catch {
case e => log.error("error in remote debugger: " + e); e.printStackTrace()
} finally {
socket.close()
log.info("remote debug client at %s quit".format(remoteHost))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment