private void initClientServer() { this.clientServer = new ClientServer(port, GatewayServer.defaultAddress(), pythonPort, GatewayServer.defaultAddress(), GatewayServer.DEFAULT_CONNECT_TIMEOUT, GatewayServer.DEFAULT_READ_TIMEOUT, ServerSocketFactory.getDefault(), SocketFactory.getDefault(), null); }
private PythonEntryPoint initPythonEntryPoint() { if (this.clientServer == null) { initClientServer(); } return (PythonEntryPoint) clientServer.getPythonServerEntryPoint(new Class[]{PythonEntryPoint.class}); }
public void exit() { if (pep != null) { pep.shutdownKernel(); pep = null; } if (clientServer != null) { clientServer.shutdown(); clientServer = null; } if (pythonProcess != null) { pythonProcess.destroy(); pythonProcess = null; } }
//AdditionApplication.java import py4j.GatewayServer; public class AdditionApplication { public static void main(String[] args) { AdditionApplication app = new AdditionApplication(); // app is now the gateway.entry_point GatewayServer server = new GatewayServer(app); server.start(); } }
/** * Updates the python callback port in the {@link GatewayServer}. */ public static void setGatewayCallbackPort(GatewayServer gatewayServer, int port) { gatewayServer.resetCallbackClient(gatewayServer.getCallbackClient().getAddress(), port); }
@Override protected Py4JServerConnection createConnection(Gateway gateway, Socket socket) throws IOException { ClientServerConnection connection = new ClientServerConnection(gateway, socket, getCustomCommands(), (Py4JPythonClientPerThread) getCallbackClient(), this, getReadTimeout(), authToken); connection.startServerConnection(); return connection; } }
private String createJVMView(BufferedReader reader) throws IOException { String name = StringUtil.unescape(reader.readLine()); reader.readLine(); JVMView newView = new JVMView(name, null); ReturnObject rObject = gateway.getReturnObject(newView); newView.setId(rObject.getName()); return Protocol.getOutputCommand(rObject); }
private String removeImport(BufferedReader reader) throws IOException { String jvmId = reader.readLine(); String importString = StringUtil.unescape(reader.readLine()); reader.readLine(); JVMView view = (JVMView) Protocol.getObject(jvmId, gateway); boolean removed = false; if (importString.endsWith("*")) { removed = view.removeStarImport(importString); } else { removed = view.removeSingleImport(importString); } return Protocol.getOutputCommand(ReturnObject.getPrimitiveReturnObject(removed)); }
/** * <p> * Gets a reference to the entry point on the Python side. This is often * necessary if Java is driving the communication because Java cannot call * static methods, initialize Python objects or load Python modules yet. * </p> * * @param interfacesToImplement * @return */ public Object getPythonServerEntryPoint(@SuppressWarnings("rawtypes") Class[] interfacesToImplement) { return getCallbackClient().getPythonServerEntryPoint(gateway, interfacesToImplement); }
public static ReturnObject getErrorReturnObject(Throwable throwable) { ReturnObject rObject = new ReturnObject(); rObject.isError = true; StringBuilder builder = new StringBuilder(); builder.append(Protocol.ERROR); builder.append(Protocol.STRING_TYPE); builder.append(StringUtil.escape(Protocol.getThrowableAsString(throwable))); rObject.commandPart = builder.toString(); return rObject; }
public static ReturnObject getSetReturnObject(String name, int size) { ReturnObject rObject = new ReturnObject(); rObject.name = name; rObject.size = size; rObject.isSet = true; rObject.commandPart = Protocol.SET_TYPE + name; return rObject; }
public KernelConfigurationFile(final String[] args) { if (args.length > 2) { logger.error("Invalid parameters passed to the Kernel. Expected one or two parameter, found " + args.length); for (String string : args) { logger.error(string); } System.exit(1); } this.config = getConfig(args[0]); if (args.length == 2) { String contextAsJson = null; try { contextAsJson = new String(Base64.decode(args[1]), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } this.context = Optional.of(contextAsJson); } }
@Override public void start() throws IOException { if (authToken != null) { try { // TODO should we receive an AuthException instead of an IOException? NetworkUtil.authToServer(reader, writer, authToken); } catch (IOException ioe) { shutdown(true); throw ioe; } } }
@Override protected boolean shouldRetrySendCommand(Py4JClientConnection cc, Py4JNetworkException pne) { boolean shouldRetry = super.shouldRetrySendCommand(cc, pne); if (shouldRetry && cc instanceof ClientServerConnection) { ClientServerConnection csc = (ClientServerConnection) cc; shouldRetry = csc.isInitiatedFromClient(); } return shouldRetry; }
public void run() { pythonClient.setPerThreadConnection(this); waitForCommands(); }
public ClientServerBuilder authToken(String authToken) { this.authToken = StringUtil.escape(authToken); return this; } }
/** * <p> * Transform the byte array into Base64 characters. * </p> * * @param bytes * @return */ public static String encodeBytes(byte[] bytes) { return Base64.encodeToString(bytes, false); }
/** * <p> * Releases all objects that were referenced by this Gateway and shuts * down the CallbackClient. * <p> */ public void shutdown() { this.shutdown(true); }
/** * Updates the python callback port in the {@link GatewayServer}. */ public static void setGatewayCallbackPort(GatewayServer gatewayServer, int port) { gatewayServer.resetCallbackClient(gatewayServer.getCallbackClient().getAddress(), port); }
@RequestMapping(value="/postLogout.html", method=RequestMethod.GET) @ResponseBody public MyreturnObject getPostLogout(){ return new ReturnObject(true,true,false); }