blob: 486fe0e2b5078e28603efa1620d62d61580ea3ad (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
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}
/** The abstract class <code>SocketServer</code> implements the server
* communication for the fast Scala compiler.
*
* @author Martin Odersky
* @version 1.0
*/
abstract class SocketServer {
def shutDown: Boolean
def session()
var out: PrintWriter = _
var in: BufferedReader = _
def fatal(msg: String): Nothing = {
System.err.println(msg)
exit(1)
}
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() {
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.")
}
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)
scala.Console.withOut(bufout) {
session()
}
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()
}
}
|