summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/util/SocketServer.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler/scala/tools/util/SocketServer.scala')
-rw-r--r--src/compiler/scala/tools/util/SocketServer.scala77
1 files changed, 42 insertions, 35 deletions
diff --git a/src/compiler/scala/tools/util/SocketServer.scala b/src/compiler/scala/tools/util/SocketServer.scala
index 1a1815f02a..00180f7659 100644
--- a/src/compiler/scala/tools/util/SocketServer.scala
+++ b/src/compiler/scala/tools/util/SocketServer.scala
@@ -10,12 +10,22 @@
package scala.tools.util
-import java.lang.System
-import java.io.PrintWriter
-import java.io.BufferedOutputStream
-import java.io.{BufferedReader, InputStreamReader}
-import java.io.IOException
-import java.net.{ServerSocket, SocketException, SocketTimeoutException}
+import java.io.{ PrintWriter, BufferedOutputStream, BufferedReader, InputStreamReader, IOException }
+import java.net.{ Socket, ServerSocket, SocketException, SocketTimeoutException }
+
+object SocketServer
+{
+ // 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.
+ val IdleTimeout = 1800000
+ val BufferSize = 10240
+
+ def bufferedReader(s: Socket) = new BufferedReader(new InputStreamReader(s.getInputStream()))
+ def bufferedOutput(s: Socket) = new BufferedOutputStream(s.getOutputStream, BufferSize)
+}
+import SocketServer._
/** The abstract class <code>SocketServer</code> implements the server
* communication for the fast Scala compiler.
@@ -23,8 +33,8 @@ import java.net.{ServerSocket, SocketException, SocketTimeoutException}
* @author Martin Odersky
* @version 1.0
*/
-abstract class SocketServer {
-
+abstract class SocketServer
+{
def shutDown: Boolean
def session()
@@ -51,41 +61,38 @@ abstract class SocketServer {
val port: Int = serverSocket.getLocalPort()
+ // @todo: this is going to be a prime candidate for ARM
+ def doSession(clientSocket: Socket) = {
+ out = new PrintWriter(clientSocket.getOutputStream(), true)
+ in = bufferedReader(clientSocket)
+ val bufout = bufferedOutput(clientSocket)
+
+ scala.Console.withOut(bufout) { session() }
+
+ bufout.close()
+ out.close()
+ in.close()
+ }
+
def run() {
- 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: SocketException =>
- fatal("Could not set timeout on port: " + port + "; exiting.")
+ def fail(s: String) = fatal(s format port)
+
+ try serverSocket setSoTimeout IdleTimeout catch {
+ case e: SocketException => fail("Could not set timeout on port: %d; exiting.")
}
+
try {
while (!shutDown) {
- val clientSocket = try {
- serverSocket.accept()
- } catch {
- case e: IOException =>
- fatal("Accept on port " + port + " failed; exiting.")
+ val clientSocket = try serverSocket.accept() catch {
+ case e: IOException => fail("Accept on port %d failed; exiting.")
}
-
- 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()
- }
- bufout.close()
- out.close()
- in.close()
+ doSession(clientSocket)
clientSocket.close()
}
- } catch {
+ }
+ catch {
case e: SocketTimeoutException =>
- warn("Timeout elapsed with no requests from clients on port " + port + "; exiting")
+ warn("Timeout elapsed with no requests from clients on port %d; exiting" format port)
timeout()
}
serverSocket.close()