Search in sources :

Example 11 with ObjectReader

use of org.apache.catalina.tribes.io.ObjectReader in project tomcat70 by apache.

the class NioReplicationTask method cancelKey.

private void cancelKey(final SelectionKey key) {
    if (log.isTraceEnabled())
        log.trace("Adding key for cancel event:" + key);
    ObjectReader reader = (ObjectReader) key.attachment();
    if (reader != null) {
        reader.setCancelled(true);
        reader.finish();
    }
    Runnable cx = new Runnable() {

        @Override
        public void run() {
            if (log.isTraceEnabled())
                log.trace("Cancelling key:" + key);
            NioReceiver.cancelledKey(key);
        }
    };
    receiver.addEvent(cx);
}
Also used : ObjectReader(org.apache.catalina.tribes.io.ObjectReader)

Example 12 with ObjectReader

use of org.apache.catalina.tribes.io.ObjectReader in project tomcat70 by apache.

the class NioReplicationTask method serviceChannel.

/**
 * Called to initiate a unit of work by this worker thread
 * on the provided SelectionKey object.  This method is
 * synchronized, as is the run() method, so only one key
 * can be serviced at a given time.
 * Before waking the worker thread, and before returning
 * to the main selection loop, this key's interest set is
 * updated to remove OP_READ.  This will cause the selector
 * to ignore read-readiness for this channel while the
 * worker thread is servicing it.
 */
public synchronized void serviceChannel(SelectionKey key) {
    if (log.isTraceEnabled())
        log.trace("About to service key:" + key);
    ObjectReader reader = (ObjectReader) key.attachment();
    if (reader != null)
        reader.setLastAccess(System.currentTimeMillis());
    this.key = key;
    key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
    key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE));
}
Also used : ObjectReader(org.apache.catalina.tribes.io.ObjectReader)

Example 13 with ObjectReader

use of org.apache.catalina.tribes.io.ObjectReader in project tomcat by apache.

the class NioReceiver method listen.

/**
 * Get data from channel and store in byte array
 * send it to cluster
 * @throws IOException IO error
 */
protected void listen() throws Exception {
    if (doListen()) {
        log.warn(sm.getString("nioReceiver.alreadyStarted"));
        return;
    }
    setListen(true);
    // Avoid NPEs if selector is set to null on stop.
    Selector selector = this.selector.get();
    if (selector != null && datagramChannel != null) {
        // max size for a datagram packet
        ObjectReader oreader = new ObjectReader(MAX_UDP_SIZE);
        registerChannel(selector, datagramChannel, SelectionKey.OP_READ, oreader);
    }
    while (doListen() && selector != null) {
        // selected set contains keys of the ready channels
        try {
            events();
            socketTimeouts();
            int n = selector.select(getSelectorTimeout());
            if (n == 0) {
                // nothing to do
                continue;
            }
            // get an iterator over the set of selected keys
            Iterator<SelectionKey> it = selector.selectedKeys().iterator();
            // look at each key in the selected set
            while (it != null && it.hasNext()) {
                SelectionKey key = it.next();
                // Is a new connection coming in?
                if (key.isAcceptable()) {
                    ServerSocketChannel server = (ServerSocketChannel) key.channel();
                    SocketChannel channel = server.accept();
                    channel.socket().setReceiveBufferSize(getRxBufSize());
                    channel.socket().setSendBufferSize(getTxBufSize());
                    channel.socket().setTcpNoDelay(getTcpNoDelay());
                    channel.socket().setKeepAlive(getSoKeepAlive());
                    channel.socket().setOOBInline(getOoBInline());
                    channel.socket().setReuseAddress(getSoReuseAddress());
                    channel.socket().setSoLinger(getSoLingerOn(), getSoLingerTime());
                    channel.socket().setSoTimeout(getTimeout());
                    Object attach = new ObjectReader(channel);
                    registerChannel(selector, channel, SelectionKey.OP_READ, attach);
                }
                // is there data to read on this channel?
                if (key.isReadable()) {
                    readDataFromSocket(key);
                } else {
                    key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE));
                }
                // remove key from selected set, it's been handled
                it.remove();
            }
        } catch (java.nio.channels.ClosedSelectorException cse) {
        // ignore is normal at shutdown or stop listen socket
        } catch (java.nio.channels.CancelledKeyException nx) {
            log.warn(sm.getString("nioReceiver.clientDisconnect"));
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            log.error(sm.getString("nioReceiver.requestError"), t);
        }
    }
    serverChannel.close();
    if (datagramChannel != null) {
        try {
            datagramChannel.close();
        } catch (Exception iox) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to close datagram channel.", iox);
            }
        }
        datagramChannel = null;
    }
    closeSelector();
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) CancelledKeyException(java.nio.channels.CancelledKeyException) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) ObjectReader(org.apache.catalina.tribes.io.ObjectReader) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 14 with ObjectReader

use of org.apache.catalina.tribes.io.ObjectReader in project tomcat by apache.

the class NioReplicationTask method serviceChannel.

/**
 * Called to initiate a unit of work by this worker thread
 * on the provided SelectionKey object.  This method is
 * synchronized, as is the run() method, so only one key
 * can be serviced at a given time.
 * Before waking the worker thread, and before returning
 * to the main selection loop, this key's interest set is
 * updated to remove OP_READ.  This will cause the selector
 * to ignore read-readiness for this channel while the
 * worker thread is servicing it.
 * @param key The key to process
 */
public synchronized void serviceChannel(SelectionKey key) {
    if (log.isTraceEnabled()) {
        log.trace("About to service key:" + key);
    }
    ObjectReader reader = (ObjectReader) key.attachment();
    if (reader != null) {
        reader.setLastAccess(System.currentTimeMillis());
    }
    this.key = key;
    key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
    key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE));
}
Also used : ObjectReader(org.apache.catalina.tribes.io.ObjectReader)

Aggregations

ObjectReader (org.apache.catalina.tribes.io.ObjectReader)14 IOException (java.io.IOException)8 CancelledKeyException (java.nio.channels.CancelledKeyException)8 ClosedSelectorException (java.nio.channels.ClosedSelectorException)4 DatagramChannel (java.nio.channels.DatagramChannel)4 SelectionKey (java.nio.channels.SelectionKey)4 Selector (java.nio.channels.Selector)4 ServerSocketChannel (java.nio.channels.ServerSocketChannel)4 SocketChannel (java.nio.channels.SocketChannel)4 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2 RemoteProcessException (org.apache.catalina.tribes.RemoteProcessException)2