|
6 | 6 | import scala.tools.nsc.interpreter.Completion.Candidates; |
7 | 7 |
|
8 | 8 | import scalive.Log; |
| 9 | +import scalive.Net; |
9 | 10 |
|
10 | 11 | import java.io.BufferedReader; |
11 | 12 | import java.io.IOException; |
|
18 | 19 | * @see scalive.client.Completer |
19 | 20 | */ |
20 | 21 | class Completer { |
21 | | - static void run(Socket socket, ILoopWithCompletion iloop) throws IOException { |
| 22 | + static void run( |
| 23 | + Socket socket, ILoopWithCompletion iloop, Runnable socketCleaner |
| 24 | + ) throws IOException, InterruptedException { |
22 | 25 | InputStream in = socket.getInputStream(); |
23 | 26 | OutputStream out = socket.getOutputStream(); |
24 | 27 |
|
25 | 28 | BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); |
26 | 29 |
|
27 | | - while (true) { |
28 | | - String line = reader.readLine(); |
29 | | - if (line == null) break; |
| 30 | + Net.throwSocketTimeoutExceptionForLongInactivity(socket); |
| 31 | + try { |
| 32 | + while (true) { |
| 33 | + // See throwSocketTimeoutExceptionForLongInactivity above |
| 34 | + String line = reader.readLine(); |
30 | 35 |
|
31 | | - int idx = line.indexOf(" "); |
32 | | - String cursorString = line.substring(0, idx); |
33 | | - int cursor = Integer.parseInt(cursorString); |
34 | | - String buffer = line.substring(idx + 1); |
| 36 | + // Socket closed |
| 37 | + if (line == null) break; |
35 | 38 |
|
36 | | - Completion completion = getCompletion(iloop); |
37 | | - Candidates candidates = completion.completer().complete(buffer, cursor); |
| 39 | + int idx = line.indexOf(" "); |
| 40 | + String cursorString = line.substring(0, idx); |
| 41 | + int cursor = Integer.parseInt(cursorString); |
| 42 | + String buffer = line.substring(idx + 1); |
38 | 43 |
|
39 | | - out.write(("" + candidates.cursor()).getBytes("UTF-8")); |
| 44 | + Completion completion = getCompletion(iloop); |
| 45 | + Candidates candidates = completion.completer().complete(buffer, cursor); |
40 | 46 |
|
41 | | - List<String> list = candidates.candidates(); |
42 | | - Iterator<String> it = list.iterator(); |
43 | | - while (it.hasNext()) { |
44 | | - String candidate = it.next(); |
45 | | - out.write(' '); |
46 | | - out.write(candidate.getBytes("UTF-8")); |
47 | | - } |
| 47 | + out.write(("" + candidates.cursor()).getBytes("UTF-8")); |
| 48 | + |
| 49 | + List<String> list = candidates.candidates(); |
| 50 | + Iterator<String> it = list.iterator(); |
| 51 | + while (it.hasNext()) { |
| 52 | + String candidate = it.next(); |
| 53 | + out.write(' '); |
| 54 | + out.write(candidate.getBytes("UTF-8")); |
| 55 | + } |
48 | 56 |
|
49 | | - out.write('\n'); |
50 | | - out.flush(); |
| 57 | + out.write('\n'); |
| 58 | + out.flush(); |
| 59 | + } |
| 60 | + } catch (IOException e) { |
| 61 | + // Socket closed |
51 | 62 | } |
52 | 63 |
|
| 64 | + socketCleaner.run(); |
| 65 | + |
| 66 | + // Before logging this out, wait a litte for System.out to be restored back to the remote process |
| 67 | + Thread.sleep(1000); |
53 | 68 | Log.log("Completer closed"); |
54 | 69 | } |
55 | 70 |
|
|
0 commit comments