From db8c41b53586961f10c2ba3a48ad14fa622d507f Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Mon, 24 Aug 2009 01:07:45 +0000 Subject: Taking a little more advantage of some recent a... Taking a little more advantage of some recent abstractions. --- src/compiler/scala/tools/util/SocketServer.scala | 77 +++++++++++++----------- 1 file changed, 42 insertions(+), 35 deletions(-) (limited to 'src/compiler/scala/tools/util/SocketServer.scala') 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 SocketServer 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() -- cgit v1.2.3