summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/util/SocketServer.scala
diff options
context:
space:
mode:
authorAntonio Cunei <antonio.cunei@epfl.ch>2008-10-22 09:46:19 +0000
committerAntonio Cunei <antonio.cunei@epfl.ch>2008-10-22 09:46:19 +0000
commitc06b1d3f61f8d1bdf4d8ad58c97fec2c51e1e9f2 (patch)
treeab95adaea8cb03ee33b0e9a97a5a37cb186ca31d /src/compiler/scala/tools/util/SocketServer.scala
parentf9924c9efdeb247b0ed330e70391aec0966555e9 (diff)
downloadscala-c06b1d3f61f8d1bdf4d8ad58c97fec2c51e1e9f2.tar.gz
scala-c06b1d3f61f8d1bdf4d8ad58c97fec2c51e1e9f2.tar.bz2
scala-c06b1d3f61f8d1bdf4d8ad58c97fec2c51e1e9f2.zip
fsc watchdog removal.
In addition, fixed a potential initialization problem in the fsc server, which would open a socket, get a port number, and close the socket, only to try to reallocate the same port number a bit later, even though the port might have become unavailable in the meantime. Now the socket is initialized only once, and the port is not released.
Diffstat (limited to 'src/compiler/scala/tools/util/SocketServer.scala')
-rw-r--r--src/compiler/scala/tools/util/SocketServer.scala72
1 files changed, 43 insertions, 29 deletions
diff --git a/src/compiler/scala/tools/util/SocketServer.scala b/src/compiler/scala/tools/util/SocketServer.scala
index 3c311f1c53..486fe0e2b5 100644
--- a/src/compiler/scala/tools/util/SocketServer.scala
+++ b/src/compiler/scala/tools/util/SocketServer.scala
@@ -15,7 +15,7 @@ import java.io.PrintWriter
import java.io.BufferedOutputStream
import java.io.{BufferedReader, InputStreamReader}
import java.io.IOException
-import java.net.ServerSocket
+import java.net.{ServerSocket, SocketException, SocketTimeoutException}
/** The abstract class <code>SocketServer</code> implements the server
* communication for the fast Scala compiler.
@@ -36,46 +36,60 @@ abstract class SocketServer {
exit(1)
}
- val port: Int = try {
- val s = new ServerSocket(0) // a port of 0 creates a socket on any free port.
- val p = s.getLocalPort()
- s.close()
- p
+ private def warn(msg: String) {
+ System.err.println(msg)
+ }
+
+ // called after a timeout is detected,
+ // for SocketServer subclasses to perform
+ // some cleanup, if any
+ def timeout() {}
+
+ val serverSocket = try {
+ new ServerSocket(0)
} catch {
case e: IOException =>
fatal("Could not listen on any port; exiting.")
}
+ val port: Int = serverSocket.getLocalPort()
def run() {
- val serverSocket = try {
- new ServerSocket(port)
+ try {
+ // After 30 idle minutes, politely exit.
+ // Should the port file disappear, and the clients
+ // therefore unable to contact this server instance,
+ // the process will just eventually terminate by itself.
+ serverSocket.setSoTimeout(1800000)
} catch {
- case e: IOException =>
- fatal("Could not listen on port: " + port + "; exiting.")
+ case e: SocketException =>
+ fatal("Could not set timeout on port: " + port + "; exiting.")
}
- while (!shutDown) {
- val clientSocket = try {
- serverSocket.accept()
- } catch {
- case e: IOException =>
- fatal("Accept on port " + port + " failed; exiting.")
- }
+ try {
+ while (!shutDown) {
+ val clientSocket = try {
+ serverSocket.accept()
+ } catch {
+ case e: IOException =>
+ fatal("Accept on port " + port + " failed; exiting.")
+ }
- out = new PrintWriter(clientSocket.getOutputStream(), true)
- in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))
- val bufout = new BufferedOutputStream(clientSocket.getOutputStream, 10240)
+ out = new PrintWriter(clientSocket.getOutputStream(), true)
+ in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))
+ val bufout = new BufferedOutputStream(clientSocket.getOutputStream, 10240)
- scala.Console.withOut(bufout) {
- session()
+ scala.Console.withOut(bufout) {
+ session()
+ }
+ bufout.close()
+ out.close()
+ in.close()
+ clientSocket.close()
}
-
- bufout.close()
- out.close()
- in.close()
- clientSocket.close()
+ } catch {
+ case e: SocketTimeoutException =>
+ warn("Timeout elapsed with no requests from clients on port " + port + "; exiting")
+ timeout()
}
serverSocket.close()
}
-
}
-