protected boolean write() 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; }
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; }
protected boolean write() throws IOException { if ( (!isConnected()) || (this.socketChannel==null && this.dataChannel==null)) { throw new IOException(sm.getString("nioSender.not.connected")); } 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; }
protected boolean read() 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 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; } }
public synchronized void setMessage(byte[] data,int offset, int length) throws IOException { if ( data != null ) { current = data; remaining = length; ackbuf.clear(); if ( writebuf != null ) writebuf.clear(); else writebuf = getBuffer(length); if ( writebuf.capacity() < length ) writebuf = getBuffer(length); //TODO use ByteBuffer.wrap to avoid copying the data. writebuf.put(data,offset,length); //writebuf.rewind(); //set the limit so that we don't write non wanted data //writebuf.limit(length); writebuf.flip(); if (isConnected()) { if (isUdpBased()) dataChannel.register(getSelector(), SelectionKey.OP_WRITE, this); else socketChannel.register(getSelector(), SelectionKey.OP_WRITE, this); } } }
public void setMessage(byte[] data,int offset, int length) throws IOException { if (data != null) { synchronized (this) { current = data; remaining = length; ackbuf.clear(); if (writebuf != null) { writebuf.clear(); } else { writebuf = getBuffer(length); } if (writebuf.capacity() < length) { writebuf = getBuffer(length); } // TODO use ByteBuffer.wrap to avoid copying the data. writebuf.put(data,offset,length); writebuf.flip(); if (isConnected()) { if (isUdpBased()) dataChannel.register(getSelector(), SelectionKey.OP_WRITE, this); else socketChannel.register(getSelector(), SelectionKey.OP_WRITE, this); } } } }
public void setMessage(byte[] data,int offset, int length) throws IOException { if (data != null) { synchronized (this) { current = data; remaining = length; ackbuf.clear(); if (writebuf != null) { writebuf.clear(); } else { writebuf = getBuffer(length); } if (writebuf.capacity() < length) { writebuf = getBuffer(length); } // TODO use ByteBuffer.wrap to avoid copying the data. writebuf.put(data,offset,length); writebuf.flip(); if (isConnected()) { if (isUdpBased()) dataChannel.register(getSelector(), SelectionKey.OP_WRITE, this); else socketChannel.register(getSelector(), SelectionKey.OP_WRITE, this); } } } }
protected boolean read() 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(sm.getString("nioSender.unable.receive.ack")); //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(sm.getString("nioSender.receive.failedAck")); return ack || fack; } else { return false; } }
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.");
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.");
if (isUdpBased()) { InetSocketAddress daddr = new InetSocketAddress(getAddress(),getUdpPort()); if ( dataChannel != null ) throw new IOException(sm.getString("nioSender.datagram.already.established"));