comparison c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/server/CompilationServer.java @ 2296:34354e2e40a3

cleanups and client/server fixes: * explicit init of CompilerImpl * CompilationServer terminates without EOFException * moved C1XOptions initialization code into separate class (static initializer changed to static method) * added ConnectionObserver to CompilationServer
author Lukas Stadler <lukas.stadler@jku.at>
date Wed, 13 Apr 2011 14:40:13 +0200
parents 8c426c2891c8
children
comparison
equal deleted inserted replaced
2295:160aacf936ad 2296:34354e2e40a3
20 */ 20 */
21 package com.sun.hotspot.c1x.server; 21 package com.sun.hotspot.c1x.server;
22 22
23 import java.io.*; 23 import java.io.*;
24 import java.net.*; 24 import java.net.*;
25 import java.util.*;
25 26
26 import javax.net.*; 27 import javax.net.*;
27 28
28 import com.sun.hotspot.c1x.*; 29 import com.sun.hotspot.c1x.*;
29 import com.sun.hotspot.c1x.Compiler; 30 import com.sun.hotspot.c1x.Compiler;
30 import com.sun.hotspot.c1x.logging.*; 31 import com.sun.hotspot.c1x.logging.*;
31 32
32 /** 33 /**
33 * Server side of the client/server compilation model. 34 * Server side of the client/server compilation model. The server listens for connections on the hardcoded port 1199.
34 * 35 *
35 * @author Lukas Stadler 36 * @author Lukas Stadler
36 */ 37 */
37 public class CompilationServer { 38 public class CompilationServer implements Runnable {
38 39
39 public static void main(String[] args) throws Exception { 40 public static void main(String[] args) throws Exception {
40 new CompilationServer().run(); 41 new CompilationServer(false).run();
41 } 42 }
42 43
43 private void run() throws IOException, ClassNotFoundException { 44 public static interface ConnectionObserver {
44 ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(1199); 45
46 public void connectionStarted(Compiler compiler);
47
48 public void connectionFinished(Compiler compiler);
49 }
50
51 private final boolean multiple;
52 private final ArrayList<ConnectionObserver> observers = new ArrayList<ConnectionObserver>();
53
54 /**
55 * Creates a new Compilation server. The server is activated by calling {@link #run()} directly or via a new
56 * {@link Thread}.
57 *
58 * @param multiple true if the server should server should serve an infinite amount of consecutive connections,
59 * false if it should terminate after the first connection ends.
60 */
61 public CompilationServer(boolean multiple) {
62 this.multiple = multiple;
63 HotSpotOptions.setDefaultOptions();
64 }
65
66 public void addConnectionObserver(ConnectionObserver observer) {
67 observers.add(observer);
68 }
69
70 public void removeConnectionObserver(ConnectionObserver observer) {
71 observers.remove(observer);
72 }
73
74 public void run() {
75 final ServerSocket serverSocket;
76 try {
77 serverSocket = ServerSocketFactory.getDefault().createServerSocket(1199);
78 } catch (IOException e) {
79 throw new RuntimeException("Couldn't create compilation server", e);
80 }
45 do { 81 do {
46 Socket socket = null; 82 Socket socket = null;
47 try { 83 try {
48 Logger.log("Compilation server ready, waiting for client to connect..."); 84 Logger.log("Compilation server ready, waiting for client to connect...");
49 socket = serverSocket.accept(); 85 socket = serverSocket.accept();
50 Logger.log("Connected to " + socket.getRemoteSocketAddress()); 86 Logger.log("Connected to " + socket.getRemoteSocketAddress());
51 87
52 ReplacingStreams streams = new ReplacingStreams(socket.getOutputStream(), socket.getInputStream()); 88 ReplacingStreams streams = new ReplacingStreams(socket.getOutputStream(), socket.getInputStream());
53 89
54 VMEntries entries = (VMEntries) streams.getInvocation().waitForResult(); 90 // get the VMEntries proxy from the client
91 VMEntries entries = (VMEntries) streams.getInvocation().waitForResult(false);
92
93 // return the initialized compiler to the client
55 Compiler compiler = CompilerImpl.initializeServer(entries); 94 Compiler compiler = CompilerImpl.initializeServer(entries);
56 95 compiler.getCompiler();
57 streams.getInvocation().sendResult(compiler); 96 streams.getInvocation().sendResult(compiler);
58 97
59 streams.getInvocation().waitForResult(); 98 for (ConnectionObserver observer : observers) {
99 observer.connectionStarted(compiler);
100 }
101
102 streams.getInvocation().waitForResult(true);
103
104 for (ConnectionObserver observer : observers) {
105 observer.connectionFinished(compiler);
106 }
60 } catch (IOException e) { 107 } catch (IOException e) {
61 e.printStackTrace(); 108 e.printStackTrace();
109 } catch (ClassNotFoundException e) {
110 throw new RuntimeException(e);
111 } finally {
62 if (socket != null) { 112 if (socket != null) {
63 socket.close(); 113 try {
114 socket.close();
115 } catch (IOException e) {
116 }
64 } 117 }
65 } 118 }
66 } while (false); 119 } while (multiple);
67 } 120 }
68 } 121 }