Package org.jgroups.blocks.cs
Class NioConnection
- java.lang.Object
-
- org.jgroups.blocks.cs.Connection
-
- org.jgroups.blocks.cs.NioConnection
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class NioConnection extends Connection
An NIO based impl ofConnection
- Since:
- 3.6.5
- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.channels.SocketChannel
channel
protected boolean
copy_on_partial_write
protected java.nio.channels.SelectionKey
key
protected java.nio.ByteBuffer
length_buf
protected int
partial_writes
protected Buffers
recv_buf
protected Buffers
send_buf
protected java.util.concurrent.locks.Lock
send_lock
-
Fields inherited from class org.jgroups.blocks.cs.Connection
cookie, last_access, peer_addr, server
-
-
Constructor Summary
Constructors Constructor Description NioConnection(java.nio.channels.SocketChannel channel, NioBaseServer server)
NioConnection(Address peer_addr, NioBaseServer server)
Creates a connection stub and binds it, useconnect(Address)
to connect
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
_read()
void
clearSelectionKey(int interest_ops)
void
close()
void
connect(Address dest)
protected void
connect(Address dest, boolean send_local_addr)
boolean
copyOnPartialWrite()
NioConnection
copyOnPartialWrite(boolean b)
void
flush()
protected static byte[]
getBuffer(java.nio.ByteBuffer buf)
boolean
isClosed()
boolean
isConnected()
boolean
isConnectionPending()
java.nio.channels.SelectionKey
key()
NioConnection
key(java.nio.channels.SelectionKey k)
Address
localAddress()
protected java.nio.ByteBuffer
makeLengthBuffer(int length)
int
numPartialWrites()
void
read()
Read the length first, then the actual data.protected Address
readPeerAddress()
void
registerSelectionKey(int interest_ops)
void
send()
void
send(byte[] buf, int offset, int length)
void
send(java.nio.ByteBuffer buf)
Sends a message.protected void
send(java.nio.ByteBuffer buf, boolean send_length)
protected void
sendLocalAddress(Address local_addr)
protected void
setSocketParameters(java.net.Socket client_sock)
void
start()
java.lang.String
status()
java.lang.String
toString()
-
Methods inherited from class org.jgroups.blocks.cs.Connection
getTimestamp, isExpired, peerAddress, updateLastAccessed
-
-
-
-
Field Detail
-
channel
protected java.nio.channels.SocketChannel channel
-
key
protected java.nio.channels.SelectionKey key
-
send_buf
protected final Buffers send_buf
-
length_buf
protected final java.nio.ByteBuffer length_buf
-
copy_on_partial_write
protected boolean copy_on_partial_write
-
partial_writes
protected int partial_writes
-
send_lock
protected final java.util.concurrent.locks.Lock send_lock
-
recv_buf
protected Buffers recv_buf
-
-
Constructor Detail
-
NioConnection
public NioConnection(Address peer_addr, NioBaseServer server) throws java.lang.Exception
Creates a connection stub and binds it, useconnect(Address)
to connect- Throws:
java.lang.Exception
-
NioConnection
public NioConnection(java.nio.channels.SocketChannel channel, NioBaseServer server) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-
Method Detail
-
isConnected
public boolean isConnected()
- Specified by:
isConnected
in classConnection
-
isConnectionPending
public boolean isConnectionPending()
- Specified by:
isConnectionPending
in classConnection
-
isClosed
public boolean isClosed()
- Specified by:
isClosed
in classConnection
-
localAddress
public Address localAddress()
- Specified by:
localAddress
in classConnection
-
key
public java.nio.channels.SelectionKey key()
-
key
public NioConnection key(java.nio.channels.SelectionKey k)
-
copyOnPartialWrite
public NioConnection copyOnPartialWrite(boolean b)
-
copyOnPartialWrite
public boolean copyOnPartialWrite()
-
numPartialWrites
public int numPartialWrites()
-
registerSelectionKey
public void registerSelectionKey(int interest_ops)
-
clearSelectionKey
public void clearSelectionKey(int interest_ops)
-
connect
public void connect(Address dest) throws java.lang.Exception
- Specified by:
connect
in classConnection
- Throws:
java.lang.Exception
-
connect
protected void connect(Address dest, boolean send_local_addr) throws java.lang.Exception
- Throws:
java.lang.Exception
-
start
public void start() throws java.lang.Exception
- Specified by:
start
in classConnection
- Throws:
java.lang.Exception
-
send
public void send(byte[] buf, int offset, int length) throws java.lang.Exception
- Specified by:
send
in classConnection
- Throws:
java.lang.Exception
-
send
public void send(java.nio.ByteBuffer buf) throws java.lang.Exception
Sends a message. If the previous write didn't complete, tries to complete it. If this still doesn't complete, the message is dropped (needs to be retransmitted, e.g. by UNICAST3 or NAKACK2).- Specified by:
send
in classConnection
- Parameters:
buf
-- Throws:
java.lang.Exception
-
send
protected void send(java.nio.ByteBuffer buf, boolean send_length) throws java.lang.Exception
- Throws:
java.lang.Exception
-
send
public void send() throws java.lang.Exception
- Throws:
java.lang.Exception
-
read
public void read() throws java.lang.Exception
Read the length first, then the actual data. This method is not reentrant and access must be synchronized- Throws:
java.lang.Exception
-
_read
protected boolean _read() throws java.lang.Exception
- Throws:
java.lang.Exception
-
close
public void close() throws java.io.IOException
- Throws:
java.io.IOException
-
flush
public void flush()
- Specified by:
flush
in classConnection
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
status
public java.lang.String status()
- Specified by:
status
in classConnection
-
setSocketParameters
protected void setSocketParameters(java.net.Socket client_sock) throws java.net.SocketException
- Throws:
java.net.SocketException
-
sendLocalAddress
protected void sendLocalAddress(Address local_addr) throws java.lang.Exception
- Throws:
java.lang.Exception
-
readPeerAddress
protected Address readPeerAddress() throws java.lang.Exception
- Throws:
java.lang.Exception
-
getBuffer
protected static byte[] getBuffer(java.nio.ByteBuffer buf)
-
makeLengthBuffer
protected java.nio.ByteBuffer makeLengthBuffer(int length)
-
-