@Override public void write(byte[] b, int off, int len) throws IOException { if (interrupted) { throw new ClosedByInterruptException(); } writing = true; try { SubstrateServerMessage message = new SubstrateServerMessage(command, b, off, len); SubstrateServerMessage.send(message, original); } finally { writing = false; } }
DataInputStream is = new DataInputStream(svmClient.getInputStream())) { SubstrateServerMessage.send(new SubstrateServerMessage(command, payload), os); if (ServerCommand.GET_VERSION.equals(command)) { SubstrateServerMessage response = SubstrateServerMessage.receive(is); if (response != null) { outln.accept(new String(response.payload)); while ((serverCommand = SubstrateServerMessage.receive(is)) != null) { Consumer<byte[]> selectedConsumer; switch (serverCommand.command) {
case GET_VERSION: log("Received 'version' request. Responding with " + System.getProperty(GRAALVM_VERSION_PROPERTY) + ".\n"); SubstrateServerMessage.send(new SubstrateServerMessage(serverCommand.command, System.getProperty(GRAALVM_VERSION_PROPERTY).getBytes()), output); return Instant.now().isBefore(lastKeepAliveAction.plus(Duration.ofMinutes(TIMEOUT_MINUTES))); case BUILD_IMAGE: sendExitStatus(output, -1); } else { log("Starting compilation for request:\n%s\n", serverCommand.payloadString()); final ArrayList<String> arguments = new ArrayList<>(Arrays.asList(serverCommand.payloadString().split("\n")));
static SubstrateServerMessage receive(DataInputStream is) throws IOException { try { ServerCommand command = ServerCommand.values()[is.readInt()]; int length = is.readInt(); byte[] payload = new byte[length]; is.readFully(payload); return new SubstrateServerMessage(command, payload); } catch (EOFException ex) { return null; } }
private boolean processRequest(Socket socket) { try { DataOutputStream output = new DataOutputStream(socket.getOutputStream()); DataInputStream input = new DataInputStream(socket.getInputStream()); try { return processCommand(socket, SubstrateServerMessage.receive(input)); } catch (Throwable t) { log("Execution failed: " + t + "\n"); t.printStackTrace(logOutput); sendExitStatus(output, 1); } } catch (IOException ioe) { log("Failed fetching the output stream."); } finally { closeConnection(socket); log("Connection with the client closed.\n"); // Remove the application class loader and save a GC on the next compilation System.gc(); System.runFinalization(); System.gc(); log("Available Memory: " + Runtime.getRuntime().freeMemory() + "\n"); } return true; }
private static void sendError(DataOutputStream output, String message) { try { SubstrateServerMessage.send(new SubstrateServerMessage(ServerCommand.WRITE_ERR, message.getBytes()), output); } catch (IOException e) { throw VMError.shouldNotReachHere(e); } }
private static void sendExitStatus(DataOutputStream output, int exitStatus) { try { SubstrateServerMessage.send(new SubstrateServerMessage(ServerCommand.SEND_STATUS, ByteBuffer.allocate(4).putInt(exitStatus).array()), output); } catch (IOException e) { throw VMError.shouldNotReachHere(e); } }