Example usage for java.net Socket wait

List of usage examples for java.net Socket wait

Introduction

In this page you can find the example usage for java.net Socket wait.

Prototype

public final void wait() throws InterruptedException 

Source Link

Document

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Usage

From source file:org.rifidi.emulator.io.comm.ip.tcpserver.TCPServerCommunicationIncomingConnectionHandler.java

/**
 * The main logic of this monitor, which creates a new server socket bound
 * to the current local IP / port combination and listens for clients to
 * connect until explictly unbound.//from   ww  w .  ja  va2 s . co  m
 * 
 * @see java.lang.Runnable#run()
 */
public void run() {
    logger.debug("Attempting to create TCPServer...");

    /* Create the ServerSocket and check to see 
     * if the server socket was made successfully */

    hasNoError = bindServerSocket();

    if (hasNoError) {
        logger.debug("No error creating server, proceeding.");

        /* A string which will be used multiple times in log statements. */
        String serverString = "[" + curServerSocket.getInetAddress().getHostAddress() + ":"
                + curServerSocket.getLocalPort() + "]";

        /* Keep running while the server socket is open. */
        while (!curServerSocket.isClosed() && hasNoError) {

            /* Try to accept a connection */
            Socket curClientSocket = null;
            try {
                logger.debug(serverString + " - Waiting for client...");
                curClientSocket = curServerSocket.accept();
                curClientSocket.setKeepAlive(true);
                //TODO Maybe we should do a disconnect 
            } catch (IOException e) {
                logger.debug(serverString + " - Server accept interrupted.");
                // Retry, because no Socket was created
                continue;
            }
            /* set the new Socket */
            this.hostCommunication.setClientSocket(curClientSocket);

            /* Check to see if a client successfully connected */
            if (curClientSocket != null) {
                final String connectionMessage = serverString + " - Client connected ("
                        + curClientSocket.getInetAddress().getHostAddress() + ":" + curClientSocket.getPort()
                        + ")";

                /* Log the connection */
                logger.info(connectionMessage);

                /* Call connect on the current communication. */
                this.hostCommunication.connect();

                /* Wait until the client socket is disconnected */
                synchronized (curClientSocket) {
                    while (!curClientSocket.isClosed() && curClientSocket.isConnected()) {
                        try {
                            /* Close the ServerSocket so that he couldn't accept 
                             * more than one connections a time (SYN/SYN ACK - Problem)
                             */
                            curServerSocket.close();
                            /* wait until the client connection is closed */
                            curClientSocket.wait();
                            /* bind the ServerSocket again, so that 
                             * new Connections can be made
                             */
                            PowerState powerState = this.hostCommunication.getPowerState();
                            logger.debug("Comm power state is " + powerState);
                            if (powerState != TCPServerOffCommunicationPowerState.getInstance()) {
                                hasNoError = bindServerSocket();
                            }
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            logger.debug("Interrupted Exception happened.");
                        }
                    }

                    /* Done waiting */

                }

            }

        } /* while (!serverSocket.isClosed())... */

        /* Server socket closed */

    } else {
        /* Log the error message. */
        logger.error(errorMessage);
        /* Force a shutdown of the component. */
        this.hostCommunication.turnOff();
    }
    /* All done running. */
}