blob: 06b732c6cbf94e8fb6b25354b95a7eb523d4966d (
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
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2002-2006, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $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
/** 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(): unit
var out: PrintWriter = _
var in: BufferedReader = _
def fatal(msg: String) = {
System.err.println(msg)
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
} catch {
case e: IOException =>
fatal("Could not listen on any port; exiting.")
}
def run(): unit =
while (!shutDown) {
val serverSocket = try {
new ServerSocket(port)
} catch {
case e: IOException =>
fatal("Could not listen on port: " + port + "; exiting.")
}
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()
serverSocket.close()
}
}
|