/** * ( begin auto-generated from Server_available.xml ) * * <p>Returns the next client in line with a new message. * * <p>( end auto-generated ) * * @brief Returns the next client in line with a new message. * @webref server * @usage application */ public Client available() { synchronized (clients) { int index = lastAvailable + 1; if (index >= clientCount) index = 0; for (int i = 0; i < clientCount; i++) { int which = (index + i) % clientCount; Client client = clients[which]; // Check for valid client if (!client.active()) { removeIndex(which); // Remove dead client i--; // Don't skip the next client // If the client has data make sure lastAvailable // doesn't end up skipping the next client which--; // fall through to allow data from dead clients // to be retreived. } if (client.available() > 0) { lastAvailable = which; return client; } } } return null; }
/** * ( begin auto-generated from Server_disconnect.xml ) * * <p>Disconnect a particular client. * * <p>( end auto-generated ) * * @brief Disconnect a particular client. * @webref server:server * @param client the client to disconnect */ public void disconnect(Client client) { client.stop(); int index = clientIndex(client); if (index != -1) { removeIndex(index); } }
public void run() { while (Thread.currentThread() == thread) { try { Socket socket = server.accept(); Client client = new Client(parent, socket); if (clientValidationMethod != null) { try { clientValidationMethod.invoke(parent, new Object[] {this, client}); } catch (Exception e) { // System.err.println("Disabling serverEvent() for port " + port); e.printStackTrace(); } } if (client.active()) { synchronized (clients) { addClient(client); if (serverEventMethod != null) { try { serverEventMethod.invoke(parent, new Object[] {this, client}); } catch (Exception e) { // System.err.println("Disabling serverEvent() for port " + port); e.printStackTrace(); } } } } } catch (SocketException e) { // thrown when server.close() is called and server is waiting on accept System.err.println("Server SocketException: " + e.getMessage()); thread = null; } catch (IOException e) { // errorMessage("run", e); e.printStackTrace(); thread = null; } try { Thread.sleep(8); } catch (InterruptedException ex) { } } }