Search in sources :

Example 1 with ConnectionFailureException

use of com.volosyukivan.HttpConnection.ConnectionFailureException in project wifikeyboard by IvanVolosyuk.

the class HttpServer method run.

@Override
public void run() {
    //    Debug.d("HttpServer started listening");
    try {
        ch.configureBlocking(false);
        SelectionKey serverkey = ch.register(selector, SelectionKey.OP_ACCEPT);
        final ArrayList<Update> newUpdates = new ArrayList<Update>();
        Action checkUpdates = new Action() {

            @Override
            public Object run() {
                for (Update u : pendingUpdates) {
                    newUpdates.add(u);
                }
                pendingUpdates.clear();
                return null;
            }
        };
        while (true) {
            newUpdates.clear();
            runAction(checkUpdates);
            for (Update u : newUpdates) {
                u.run();
            }
            //        Debug.d("waiting for event");
            selector.select();
            //        Debug.d("got an event");
            Set<SelectionKey> keys = selector.selectedKeys();
            for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext(); ) {
                SelectionKey key = i.next();
                i.remove();
                if (key == serverkey) {
                    if (key.isAcceptable()) {
                        SocketChannel client = ch.accept();
                        //              Debug.d("NEW CONNECTION from " + client.socket().getRemoteSocketAddress());
                        client.configureBlocking(false);
                        SelectionKey clientkey = client.register(selector, SelectionKey.OP_READ);
                        HttpConnection con = newConnection(client);
                        clientkey.attach(con);
                        con.setKey(clientkey);
                    }
                } else {
                    HttpConnection conn = (HttpConnection) key.attachment();
                    try {
                        HttpConnection.ConnectionState prevState, newState;
                        if (key.isReadable()) {
                            prevState = HttpConnection.ConnectionState.SELECTOR_WAIT_FOR_NEW_INPUT;
                            //                Debug.d("processing read event");
                            //                long start = System.currentTimeMillis();
                            newState = conn.newInput();
                        //                long end = System.currentTimeMillis();
                        //                Debug.d("delay = " + (end - start));
                        //                int size = android.os.Debug.getThreadAllocSize();
                        //                android.os.Debug.stopAllocCounting();
                        //                Log.d("wifikeyboard", "finished read event, allocs = " + size);
                        //                android.os.Debug.startAllocCounting();
                        } else if (key.isWritable()) {
                            prevState = HttpConnection.ConnectionState.SELECTOR_WAIT_FOR_OUTPUT_BUFFER;
                            //                Debug.d("processing write event");
                            newState = conn.newOutputBuffer();
                        //                Log.d("wifikeyboard", "finished write event");
                        } else {
                            continue;
                        }
                        if (newState == prevState)
                            continue;
                        key.interestOps((newState == HttpConnection.ConnectionState.SELECTOR_WAIT_FOR_NEW_INPUT ? SelectionKey.OP_READ : 0) | (newState == HttpConnection.ConnectionState.SELECTOR_WAIT_FOR_OUTPUT_BUFFER ? SelectionKey.OP_WRITE : 0));
                    } catch (IOException io) {
                        //                  conn.getClient().socket().getRemoteSocketAddress());
                        if (key != null)
                            key.cancel();
                        conn.getClient().close();
                    } catch (ConnectionFailureException e) {
                        if (key != null)
                            key.cancel();
                        conn.getClient().close();
                    } catch (NumberFormatException e) {
                        // FIXME: move to ConnectionFailureException
                        if (key != null)
                            key.cancel();
                        conn.getClient().close();
                    }
                }
            }
        }
    } catch (IOException e) {
        Debug.e("network loop terminated", e);
    } catch (NetworkThreadStopException e) {
        Debug.e("network thread stop requested", e);
    }
    try {
        selector.close();
    } catch (Throwable t) {
    }
    try {
        ch.close();
    } catch (Throwable t) {
    }
    onExit();
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ConnectionFailureException(com.volosyukivan.HttpConnection.ConnectionFailureException)

Aggregations

ConnectionFailureException (com.volosyukivan.HttpConnection.ConnectionFailureException)1 IOException (java.io.IOException)1 SelectionKey (java.nio.channels.SelectionKey)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 ArrayList (java.util.ArrayList)1