/** * <p> * Creates a server connection from a Python call to the Java side. * </p> * * @param gateway * @param socket * @return * @throws IOException */ protected Py4JServerConnection createConnection(Gateway gateway, Socket socket) throws IOException { GatewayConnection connection = new GatewayConnection(gateway, socket, authToken, customCommands, listeners); connection.startConnection(); return connection; }
@Override public void shutdown() { shutdown(false); }
public GatewayConnection(Gateway gateway, Socket socket, String authToken, List<Class<? extends Command>> customCommands, List<GatewayServerListener> listeners) throws IOException { super(); this.socket = socket; this.authToken = authToken; if (authToken != null) { this.authCommand = new AuthCommand(authToken); } else { this.authCommand = null; } this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charset.forName("UTF-8"))); this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), Charset.forName("UTF-8"))); this.commands = new HashMap<String, Command>(); initCommands(gateway, baseCommands); if (customCommands != null) { initCommands(gateway, customCommands); } if (authCommand != null) { initCommand(gateway, authCommand); } this.listeners = listeners; }
} finally { if (error != null && executing && writer != null) { quietSendFatalError(writer, error); shutdown(reset);
/** * <p> * Override this method to initialize custom commands. * </p> * * @param gateway */ protected void initCommands(Gateway gateway, List<Class<? extends Command>> commandsClazz) { for (Class<? extends Command> clazz : commandsClazz) { try { Command cmd = clazz.newInstance(); initCommand(gateway, cmd); } catch (Exception e) { String name = "null"; if (clazz != null) { name = clazz.getName(); } logger.log(Level.SEVERE, "Could not initialize command " + name, e); } } }
public ClientServerConnection(Gateway gateway, Socket socket, List<Class<? extends Command>> customCommands, Py4JPythonClientPerThread pythonClient, Py4JJavaServer javaServer, int readTimeout, String authToken) throws IOException { super(); this.socket = socket; this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charset.forName("UTF-8"))); this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), Charset.forName("UTF-8"))); this.commands = new HashMap<String, Command>(); initCommands(gateway, GatewayConnection.getBaseCommands()); if (customCommands != null) { initCommands(gateway, customCommands); } this.javaServer = javaServer; this.pythonClient = pythonClient; this.blockingReadTimeout = readTimeout; if (readTimeout > 0) { this.nonBlockingReadTimeout = readTimeout; } else { this.nonBlockingReadTimeout = CallbackConnection.DEFAULT_NONBLOCKING_SO_TIMEOUT; } this.authToken = authToken; if (authToken != null) { this.authCommand = new AuthCommand(authToken); initCommand(gateway, authCommand); } else { this.authCommand = null; } }
/** * <p> * Shuts down the connection by closing the socket, the writer, and the reader. * </p> * <p> * Internal: emits a connection stopped signal so GatewayServer can remove the connection from the connections list. * In rare occasions, the shutdown method may be called twice (when the server shuts down at the same time as the * connection fails and shuts down). * </p> */ @Override public void shutdown(boolean reset) { if (reset) { NetworkUtil.quietlySetLinger(socket); } // XXX Close socket first, otherwise, reader.close() will block if stuck on readLine. NetworkUtil.quietlyClose(socket); NetworkUtil.quietlyClose(reader); NetworkUtil.quietlyClose(writer); fireConnectionStopped(); } }