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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
NioConnection.Reader
protected static class
NioConnection.State
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.channels.SocketChannel
channel
protected boolean
connected
protected boolean
copy_on_partial_write
protected java.nio.channels.SelectionKey
key
protected int
partial_writes
protected NioConnection.Reader
reader
protected long
reader_idle_time
protected Buffers
recv_buf
protected Buffers
send_buf
protected java.util.concurrent.locks.Lock
send_lock
protected NioBaseServer
server
protected boolean
write_interest_set
-
Fields inherited from class org.jgroups.blocks.cs.Connection
cookie, last_access, peer_addr
-
-
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
_receive(boolean update)
void
clearSelectionKey(int interest_ops)
void
close()
void
connect(Address dest)
protected void
connect(Address dest, boolean send_local_addr)
NioConnection
connected(boolean c)
boolean
copyOnPartialWrite()
NioConnection
copyOnPartialWrite(boolean b)
protected static byte[]
getBuffer(java.nio.ByteBuffer buf)
protected long
getTimestamp()
boolean
isConnected()
boolean
isConnectionPending()
boolean
isExpired(long now)
boolean
isOpen()
java.nio.channels.SelectionKey
key()
NioConnection
key(java.nio.channels.SelectionKey k)
Address
localAddress()
protected static java.nio.ByteBuffer
makeLengthBuffer(java.nio.ByteBuffer buf)
int
numPartialWrites()
Address
peerAddress()
long
readerIdleTime()
NioConnection
readerIdleTime(long t)
boolean
readerRunning()
protected Address
readPeerAddress()
void
receive()
Read the length first, then the actual data.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()
protected void
updateLastAccessed()
protected void
writeInterest(boolean register)
-
-
-
Field Detail
-
channel
protected java.nio.channels.SocketChannel channel
-
key
protected java.nio.channels.SelectionKey key
-
server
protected final NioBaseServer server
-
send_buf
protected final Buffers send_buf
-
write_interest_set
protected boolean write_interest_set
-
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
-
reader
protected NioConnection.Reader reader
-
reader_idle_time
protected long reader_idle_time
-
connected
protected boolean connected
-
-
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
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in classConnection
-
isConnected
public boolean isConnected()
- Specified by:
isConnected
in classConnection
-
isConnectionPending
public boolean isConnectionPending()
- Specified by:
isConnectionPending
in classConnection
-
isExpired
public boolean isExpired(long now)
- Specified by:
isExpired
in classConnection
-
updateLastAccessed
protected void updateLastAccessed()
-
localAddress
public Address localAddress()
- Specified by:
localAddress
in classConnection
-
peerAddress
public Address peerAddress()
- Specified by:
peerAddress
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()
-
readerIdleTime
public long readerIdleTime()
-
readerIdleTime
public NioConnection readerIdleTime(long t)
-
readerRunning
public boolean readerRunning()
-
connected
public NioConnection connected(boolean c)
-
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
public void send() throws java.lang.Exception
- Throws:
java.lang.Exception
-
receive
public void receive() 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
-
send
protected void send(java.nio.ByteBuffer buf, boolean send_length) throws java.lang.Exception
- Throws:
java.lang.Exception
-
_receive
protected boolean _receive(boolean update) throws java.lang.Exception
- Throws:
java.lang.Exception
-
close
public void close() throws java.io.IOException
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
status
public java.lang.String status()
- Specified by:
status
in classConnection
-
getTimestamp
protected long getTimestamp()
-
writeInterest
protected void writeInterest(boolean register)
-
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 static java.nio.ByteBuffer makeLengthBuffer(java.nio.ByteBuffer buf)
-
-