private void connect(NioSender[] senders) throws ChannelException { ChannelException x = null; for (int i=0; i<senders.length; i++ ) { try { if (!senders[i].isConnected()) senders[i].connect(); }catch ( IOException io ) { if ( x==null ) x = new ChannelException(io); x.addFaultyMember(senders[i].getDestination(),io); } } if ( x != null ) throw x; }
public void remove(Member member) { //disconnect senders NioSender sender = (NioSender)nioSenders.remove(member); if ( sender != null ) sender.disconnect(); }
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); 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()) { socketChannel.register(getSelector(), SelectionKey.OP_WRITE, this); } } }
private ByteBuffer getWriteBuffer() { return getBuffer(getTxBufSize()); }
private ByteBuffer getReadBuffer() { return getBuffer(getRxBufSize()); }
NioSender sender = (NioSender) sk.attachment(); try { if (sender.process(sk,waitForAck)) { completed++; sender.setComplete(true); if ( Logs.MESSAGES.isTraceEnabled() ) { Logs.MESSAGES.trace("ParallelNioSender - Sent msg:" + new UniqueId(msg.getUniqueId()) + " at " +new java.sql.Timestamp(System.currentTimeMillis())+ " to "+sender.getDestination().getName()); SenderState.getSenderState(sender.getDestination()).setReady(); SenderState state = SenderState.getSenderState(sender.getDestination()); int attempt = sender.getAttempt()+1; boolean retry = (sender.getAttempt() <= maxAttempts && maxAttempts>0); synchronized (state) { state.setSuspect(); if ( retry ) log.warn("Member send is failing for:" + sender.getDestination().getName() +" ; Setting to suspect and retrying."); else log.warn("Member send is failing for:" + sender.getDestination().getName() +" ; Setting to suspect.", x); log.warn("Not retrying send for:" + sender.getDestination().getName() + "; Sender is disconnected."); ChannelException cx = new ChannelException("Send failed, and sender is disconnected. Not retrying.",x); cx.addFaultyMember(sender.getDestination(),x); throw cx; byte[] data = sender.getMessage(); if ( retry ) { try {
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(); 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(); configureSocket(); dataChannel.connect(daddr); completeConnect(); dataChannel.register(getSelector(),SelectionKey.OP_WRITE, this); 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(); configureSocket(); if ( socketChannel.connect(addr) ) { completeConnect(); socketChannel.register(getSelector(), SelectionKey.OP_WRITE, this); } else {
key.interestOps(key.interestOps() & ~ops); if ((!isConnected()) && (!connecting)) throw new IOException("Sender has been disconnected, can't selection key."); if ( !key.isValid() ) throw new IOException("Key is not valid, it must have been cancelled."); if ( key.isConnectable() ) { if ( socketChannel.finishConnect() ) { completeConnect(); if ( current != null ) key.interestOps(key.interestOps() | SelectionKey.OP_WRITE); return false; boolean writecomplete = write(); if ( writecomplete ) { read();//this causes overhead setRequestCount(getRequestCount()+1); return true; boolean readcomplete = read(); if ( readcomplete ) { setRequestCount(getRequestCount()+1); return true; } else {
if ( connecting ) return; connecting = true; if ( isConnected() ) throw new IOException("NioSender is already in connected state."); if ( readbuf == null ) { readbuf = getReadBuffer(); } else { readbuf.clear(); writebuf = getWriteBuffer(); } else { writebuf.clear(); 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(); socketChannel.configureBlocking(false); if ( socketChannel.connect(addr) ) { completeConnect(); socketChannel.register(getSelector(), SelectionKey.OP_WRITE, this); } else { socketChannel.register(getSelector(), SelectionKey.OP_CONNECT, this);
private NioSender[] setupForSend(Member[] destination) throws ChannelException { ChannelException cx = null; NioSender[] result = new NioSender[destination.length]; for ( int i=0; i<destination.length; i++ ) { NioSender sender = nioSenders.get(destination[i]); try { if (sender == null) { sender = new NioSender(); AbstractSender.transferProperties(this, sender); nioSenders.put(destination[i], sender); } sender.reset(); sender.setDestination(destination[i]); sender.setSelector(selector); sender.setUdpBased(isUdpBased()); result[i] = sender; }catch ( UnknownHostException x ) { if (cx == null) cx = new ChannelException("Unable to setup NioSender.", x); cx.addFaultyMember(destination[i], x); } } if ( cx != null ) throw cx; else return result; }
public boolean keepalive() { boolean result = false; for ( Iterator i = nioSenders.entrySet().iterator(); i.hasNext(); ) { Map.Entry entry = (Map.Entry)i.next(); NioSender sender = (NioSender)entry.getValue(); if ( sender.keepalive() ) { //nioSenders.remove(entry.getKey()); i.remove(); result = true; } else { try { sender.read(null); }catch ( IOException x ) { sender.disconnect(); sender.reset(); //nioSenders.remove(entry.getKey()); i.remove(); result = true; }catch ( Exception x ) { log.warn("Error during keepalive test for sender:"+sender,x); } } } //clean up any cancelled keys if ( result ) try { selector.selectNow(); }catch (Exception ignore){} return result; }
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); } } }
private NioSender[] setupForSend(Member[] destination) throws ChannelException { ChannelException cx = null; NioSender[] result = new NioSender[destination.length]; for ( int i=0; i<destination.length; i++ ) { NioSender sender = (NioSender)nioSenders.get(destination[i]); try { if (sender == null) { sender = new NioSender(); sender.transferProperties(this, sender); nioSenders.put(destination[i], sender); } if (sender != null) { sender.reset(); sender.setDestination(destination[i]); sender.setSelector(selector); result[i] = sender; } }catch ( UnknownHostException x ) { if (cx == null) cx = new ChannelException("Unable to setup NioSender.", x); cx.addFaultyMember(destination[i], x); } } if ( cx != null ) throw cx; else return result; }
private void setData(NioSender[] senders, byte[] data) throws ChannelException { ChannelException x = null; for (int i=0; i<senders.length; i++ ) { try { senders[i].setMessage(data); }catch ( IOException io ) { if ( x==null ) x = new ChannelException(io); x.addFaultyMember(senders[i].getDestination(),io); } } if ( x != null ) throw x; }
if (!senders[i].isComplete()) { cx.addFaultyMember(senders[i].getDestination(),x); if (!senders[i].isComplete()) { cx.addFaultyMember(senders[i].getDestination(),cxtimeout);
private void connect(NioSender[] senders) throws ChannelException { ChannelException x = null; for (int i=0; i<senders.length; i++ ) { try { senders[i].connect(); }catch ( IOException io ) { if ( x==null ) x = new ChannelException(io); x.addFaultyMember(senders[i].getDestination(),io); } } if ( x != null ) throw x; }
private ByteBuffer getBuffer(int size) { return (getDirectBuffer()?ByteBuffer.allocateDirect(size):ByteBuffer.allocate(size)); }
NioSender sender = (NioSender) sk.attachment(); try { if (sender.process(sk,waitForAck)) { completed++; sender.setComplete(true); if ( Logs.MESSAGES.isTraceEnabled() ) { Logs.MESSAGES.trace("ParallelNioSender - Sent msg:" + new UniqueId(msg.getUniqueId()) + " at " +new java.sql.Timestamp(System.currentTimeMillis())+ " to "+sender.getDestination().getName()); SenderState.getSenderState(sender.getDestination()).setReady(); log.trace("Error while processing send to " + sender.getDestination().getName(), x); SenderState state = SenderState.getSenderState(sender.getDestination()); int attempt = sender.getAttempt()+1; boolean retry = (sender.getAttempt() <= maxAttempts && maxAttempts>0); synchronized (state) { state.setSuspect(); if ( retry ) log.warn("Member send is failing for:" + sender.getDestination().getName() +" ; Setting to suspect and retrying."); else log.warn("Member send is failing for:" + sender.getDestination().getName() +" ; Setting to suspect.", x); log.warn("Not retrying send for:" + sender.getDestination().getName() + "; Sender is disconnected."); ChannelException cx = new ChannelException("Send failed, and sender is disconnected. Not retrying.",x); cx.addFaultyMember(sender.getDestination(),x); throw cx; byte[] data = sender.getMessage();
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(); 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(); configureSocket(); dataChannel.connect(daddr); completeConnect(); dataChannel.register(getSelector(),SelectionKey.OP_WRITE, this); 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(); configureSocket(); if ( socketChannel.connect(addr) ) { completeConnect(); socketChannel.register(getSelector(), SelectionKey.OP_WRITE, this); } else {
key.interestOps(key.interestOps() & ~ops); if ((!isConnected()) && (!connecting)) throw new IOException("Sender has been disconnected, can't selection key."); if ( !key.isValid() ) throw new IOException("Key is not valid, it must have been cancelled."); if ( key.isConnectable() ) { if ( socketChannel.finishConnect() ) { completeConnect(); if ( current != null ) key.interestOps(key.interestOps() | SelectionKey.OP_WRITE); return false; boolean writecomplete = write(key); if ( writecomplete ) { read(key);//this causes overhead setRequestCount(getRequestCount()+1); return true; boolean readcomplete = read(key); if ( readcomplete ) { setRequestCount(getRequestCount()+1); return true; } else {