comparison c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/server/CompilationServer.java @ 2285:762de4b26788

turn Compiler and HotSpotTypeResolved into interfaces
author Lukas Stadler <lukas.stadler@jku.at>
date Fri, 08 Apr 2011 13:43:05 +0200
parents 569d3fe7d65c
children 8c426c2891c8
comparison
equal deleted inserted replaced
2284:569d3fe7d65c 2285:762de4b26788
25 import java.net.*; 25 import java.net.*;
26 import java.rmi.registry.*; 26 import java.rmi.registry.*;
27 27
28 import javax.net.*; 28 import javax.net.*;
29 29
30 import com.sun.cri.ci.*;
31 import com.sun.hotspot.c1x.*; 30 import com.sun.hotspot.c1x.*;
32 import com.sun.hotspot.c1x.Compiler; 31 import com.sun.hotspot.c1x.Compiler;
33 import com.sun.hotspot.c1x.InvocationSocket.DelegateCallback; 32 import com.sun.hotspot.c1x.InvocationSocket.DelegateCallback;
34 import com.sun.hotspot.c1x.logging.*; 33 import com.sun.hotspot.c1x.logging.*;
34 import com.sun.hotspot.c1x.server.ReplacingStreams.ReplacingInputStream;
35 import com.sun.hotspot.c1x.server.ReplacingStreams.ReplacingOutputStream;
35 36
36 /** 37 /**
37 * Server side of the client/server compilation model. 38 * Server side of the client/server compilation model.
38 * 39 *
39 * @author Lukas Stadler 40 * @author Lukas Stadler
40 */ 41 */
41 public class CompilationServer { 42 public class CompilationServer {
42 43
43 private Registry registry;
44 private ServerSocket serverSocket;
45 private Socket socket;
46
47 private ReplacingOutputStream output; 44 private ReplacingOutputStream output;
48 private ReplacingInputStream input; 45 private ReplacingInputStream input;
49
50 46
51 public static void main(String[] args) throws Exception { 47 public static void main(String[] args) throws Exception {
52 new CompilationServer().run(); 48 new CompilationServer().run();
53 } 49 }
54 50
55 public static class Container implements Serializable {
56
57 public final Class<?> clazz;
58 public final Object[] values;
59
60 public Container(Class<?> clazz, Object... values) {
61 this.clazz = clazz;
62 this.values = values;
63 }
64 }
65
66 /**
67 * Replaces certain cir objects that cannot easily be made Serializable.
68 */
69 public static class ReplacingOutputStream extends ObjectOutputStream {
70
71 public ReplacingOutputStream(OutputStream out) throws IOException {
72 super(out);
73 enableReplaceObject(true);
74 }
75
76 @Override
77 protected Object replaceObject(Object obj) throws IOException {
78 Class<? extends Object> clazz = obj.getClass();
79 if (clazz == CiConstant.class) {
80 CiConstant o = (CiConstant) obj;
81 return new Container(clazz, o.kind, o.boxedValue());
82 } else if (obj == CiValue.IllegalValue) {
83 return new Container(CiValue.class);
84 } else if (obj instanceof Compiler) {
85 return new Container(Compiler.class);
86 }
87 return obj;
88 }
89 }
90
91 /**
92 * Replaces certain cir objects that cannot easily be made Serializable.
93 */
94 public static class ReplacingInputStream extends ObjectInputStream {
95 private Compiler compiler;
96
97 public ReplacingInputStream(InputStream in) throws IOException {
98 super(in);
99 enableResolveObject(true);
100 }
101
102 public void setCompiler(Compiler compiler) {
103 this.compiler = compiler;
104 }
105
106 @Override
107 protected Object resolveObject(Object obj) throws IOException {
108 if (obj instanceof Container) {
109 Container c = (Container) obj;
110 if (c.clazz == CiConstant.class) {
111 return CiConstant.forBoxed((CiKind) c.values[0], c.values[1]);
112 } else if (c.clazz == CiValue.class) {
113 return CiValue.IllegalValue;
114 } else if (c.clazz == Compiler.class) {
115 assert compiler != null;
116 return compiler;
117 }
118 throw new RuntimeException("unexpected container class: " + c.clazz);
119 }
120 return obj;
121 }
122 }
123
124 private void run() throws IOException, ClassNotFoundException { 51 private void run() throws IOException, ClassNotFoundException {
125 serverSocket = ServerSocketFactory.getDefault().createServerSocket(1199); 52 ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(1199);
126 do { 53 do {
54 Socket socket = null;
55 ReplacingStreams streams = new ReplacingStreams();
127 try { 56 try {
128 Logger.log("Compilation server ready, waiting for client to connect..."); 57 Logger.log("Compilation server ready, waiting for client to connect...");
129 socket = serverSocket.accept(); 58 socket = serverSocket.accept();
130 Logger.log("Connected to " + socket.getRemoteSocketAddress()); 59 Logger.log("Connected to " + socket.getRemoteSocketAddress());
131 60
132 output = new ReplacingOutputStream(new BufferedOutputStream(socket.getOutputStream())); 61 output = streams.new ReplacingOutputStream(new BufferedOutputStream(socket.getOutputStream()));
133 // required, because creating an ObjectOutputStream writes a header, but doesn't flush the stream 62 // required, because creating an ObjectOutputStream writes a header, but doesn't flush the stream
134 output.flush(); 63 output.flush();
135 input = new ReplacingInputStream(new BufferedInputStream(socket.getInputStream())); 64 input = streams.new ReplacingInputStream(new BufferedInputStream(socket.getInputStream()));
136 65
137 final InvocationSocket invocation = new InvocationSocket(output, input); 66 final InvocationSocket invocation = new InvocationSocket(output, input);
138 invocation.setDelegateCallback( new DelegateCallback() { 67 invocation.setDelegateCallback(new DelegateCallback() {
139 public Object getDelegate() { 68 public Object getDelegate() {
140 VMEntries entries = (VMEntries) Proxy.newProxyInstance(VMEntries.class.getClassLoader(), new Class<?>[] {VMEntries.class}, invocation); 69 VMEntries entries = (VMEntries) Proxy.newProxyInstance(VMEntries.class.getClassLoader(), new Class<?>[] { VMEntries.class}, invocation);
141 Compiler compiler = Compiler.initializeServer(entries); 70 Compiler compiler = CompilerImpl.initializeServer(entries);
142 input.setCompiler(compiler); 71 input.setCompiler(compiler);
143 return compiler.getVMExits(); 72 return compiler.getVMExits();
144 } 73 }
145 }); 74 });
146 75
147 invocation.waitForResult(); 76 invocation.waitForResult();
148 } catch (IOException e) { 77 } catch (IOException e) {
149 e.printStackTrace(); 78 e.printStackTrace();
150 socket.close(); 79 if (socket != null) {
80 socket.close();
81 }
151 } 82 }
152 } while (false); 83 } while (false);
153 } 84 }
154 } 85 }