//request for Connection came in ClientServerConnection csc = new ClientServerConnection(this, "and everything you need, at least client IP for connecting the socket"); csc.run(); //running in its own thread, of cause ClientServerConnection should extend Thread connectionList.add(csc); //a list of the connections the Server holds
/** * <p> * Override this method to initialize custom commands. * </p> * * @param gateway * @param commandsClazz */ 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); } } }
@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; }
@Override protected Py4JClientConnection getConnection() throws IOException { ClientServerConnection connection = null; connection = getPerThreadConnection(); if (connection != null) { try { lock.lock(); connections.remove(connection); } finally { lock.unlock(); } } if (connection == null || connection.getSocket() == null) { Socket socket = startClientSocket(); connection = new ClientServerConnection(gateway, socket, customCommands, this, javaServer, readTimeout, authToken); connection.setInitiatedFromClient(true); connection.start(); setPerThreadConnection(connection); } return connection; }
@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; } }
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; } }
} finally { if (error != null && executing && writer != null) { quietSendFatalError(writer, error); shutdown(reset);
while (true) { if (blocking) { returnCommand = this.readBlockingResponse(this.reader); } else { returnCommand = this.readNonBlockingResponse(this.socket, this.reader);
@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); socket = null; writer = null; reader = null; if (!initiatedFromClient) { // Only fires this event when the connection is created by the JavaServer to respect the protocol. fireConnectionStopped(); } }