Code example for DatagramChannel

Methods: configureBlocking, connect, register, socket

1
            socketChannel.socket().setReuseAddress(getSoReuseAddress());
            socketChannel.socket().setOOBInline(getOoBInline());
            socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerTime());
            socketChannel.socket().setTrafficClass(getSoTrafficClass());
        } else if (dataChannel!=null) {
            dataChannel.socket().setSendBufferSize(getUdpTxBufSize());
            dataChannel.socket().setReceiveBufferSize(getUdpRxBufSize());
            dataChannel.socket().setSoTimeout((int)getTimeout());
            dataChannel.socket().setReuseAddress(getSoReuseAddress());
            dataChannel.socket().setTrafficClass(getSoTrafficClass());
        } 
    } 
 
 
 
    protected boolean read(SelectionKey key) throws IOException {
        //if there is no message here, we are done 
        if ( current == null ) return true;
        int read = isUdpBased()?dataChannel.read(readbuf) : socketChannel.read(readbuf);
        //end of stream 
        if ( read == -1 ) throw new IOException("Unable to receive an ack message. EOF on socket channel has been reached.");
        //no data read 
        else if ( read == 0 ) return false;
        readbuf.flip();
        ackbuf.append(readbuf,read);
        readbuf.clear();
        if (ackbuf.doesPackageExist() ) {
            byte[] ackcmd = ackbuf.extractDataPackage(true).getBytes();
            boolean ack = Arrays.equals(ackcmd,org.apache.catalina.tribes.transport.Constants.ACK_DATA);
            boolean fack = Arrays.equals(ackcmd,org.apache.catalina.tribes.transport.Constants.FAIL_ACK_DATA);
            if ( fack && getThrowOnFailedAck() ) throw new RemoteProcessException("Received a failed ack:org.apache.catalina.tribes.transport.Constants.FAIL_ACK_DATA");
            return ack || fack;
        } else { 
            return false; 
        } 
    } 
 
 
    protected boolean write(SelectionKey key) throws IOException {
        if ( (!isConnected()) || (this.socketChannel==null && this.dataChannel==null)) {
            throw new IOException("NioSender is not connected, this should not occur.");
        } 
        if ( current != null ) {
            if ( remaining > 0 ) {
                //we have written everything, or we are starting a new package 
                //protect against buffer overwrite 
                int byteswritten = isUdpBased()?dataChannel.write(writebuf) : socketChannel.write(writebuf);
                if (byteswritten == -1 ) throw new EOFException();
                remaining -= byteswritten;
                //if the entire message was written from the buffer 
                //reset the position counter 
                if ( remaining < 0 ) {
                    remaining = 0;
                } 
            } 
            return (remaining==0);
        } 
        //no message to send, we can consider that complete 
        return true; 
    } 
 
    /** 
     * connect - blocking in this operation 
     * 
     * @throws IOException 
     * TODO Implement this org.apache.catalina.tribes.transport.IDataSender method 
     */ 
    @Override 
    public synchronized void connect() throws IOException { 
        if ( connecting || isConnected()) return;
        connecting = true;
        if ( isConnected() ) throw new IOException("NioSender is already in connected state.");
        if ( readbuf == null ) {
            readbuf = getReadBuffer();
        } else { 
            readbuf.clear();
        } 
        if ( writebuf == null ) {
            writebuf = getWriteBuffer();
        } else { 
            writebuf.clear();
        } 
 
        if (isUdpBased()) { 
            InetSocketAddress daddr = new InetSocketAddress(getAddress(),getUdpPort());
            if ( dataChannel != null ) throw new IOException("Datagram channel has already been established. Connection might be in progress.");
            dataChannel = DatagramChannel.open();
            dataChannel.configureBlocking(false);
            dataChannel.connect(daddr);
            completeConnect(); 
            dataChannel.register(getSelector(),SelectionKey.OP_WRITE, this);
             
        } else { 
            InetSocketAddress addr = new InetSocketAddress(getAddress(),getPort());
            if ( socketChannel != null ) throw new IOException("Socket channel has already been established. Connection might be in progress.");
            socketChannel = SocketChannel.open();