Package org.jgroups.blocks.cs
Class TcpConnection
- java.lang.Object
-
- org.jgroups.blocks.cs.Connection
-
- org.jgroups.blocks.cs.TcpConnection
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
TcpConnectionNonBlocking
public class TcpConnection extends Connection
Blocking IO (BIO) connection. Starts 1 reader thread for the peer socket and blocks until data is available. CallsBaseServer.receive(Address,byte[],int,int)when data has been received.- Since:
- 3.6.5
- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classTcpConnection.Receiver
-
Field Summary
Fields Modifier and Type Field Description protected booleanconnectedprotected java.io.DataInputStreaminprotected byte[]length_bufprotected java.io.OutputStreamoutprotected TcpConnection.Receiverreceiverprotected java.net.Socketsockprotected booleanuse_lock_to_sendprotected java.util.concurrent.atomic.AtomicIntegerwriters-
Fields inherited from class org.jgroups.blocks.cs.Connection
cookie, last_access, peer_addr, send_lock, server
-
-
Constructor Summary
Constructors Constructor Description TcpConnection(java.net.Socket s, TcpServer server)Called byTcpServer.Acceptor.handleAccept(Socket)TcpConnection(Address peer_addr, TcpBaseServer server)Creates a connection to a remote peer, useconnect(Address)to connect
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidconnect(Address dest)protected voidconnect(Address dest, boolean send_local_addr)protected java.io.DataInputStreamcreateDataInputStream(java.io.InputStream in)protected java.io.OutputStreamcreateDataOutputStream(java.io.OutputStream out)protected voiddoSend(byte[] data, int offset, int length, boolean flush)voidflush()protected java.lang.StringgetSockAddress()booleanisClosed()booleanisConnected()booleanisConnectionPending()AddresslocalAddress()voidlocklessSend(byte[] data, int offset, int length)protected AddressreadPeerAddress(java.net.Socket client_sock)Reads the peer's address.voidsend(byte[] data, int offset, int length)voidsend(java.nio.ByteBuffer buf)protected voidsendLocalAddress(Address local_addr)Send the cookie first, then our port number.protected voidsetSocketParameters(java.net.Socket client_sock)voidstart()java.lang.Stringstatus()java.lang.StringtoString()booleanuseLockToSend()<T extends Connection>
TuseLockToSend(boolean u)-
Methods inherited from class org.jgroups.blocks.cs.Connection
getTimestamp, isExpired, peerAddress, updateLastAccessed
-
-
-
-
Field Detail
-
sock
protected final java.net.Socket sock
-
out
protected java.io.OutputStream out
-
in
protected java.io.DataInputStream in
-
receiver
protected volatile TcpConnection.Receiver receiver
-
writers
protected final java.util.concurrent.atomic.AtomicInteger writers
-
connected
protected volatile boolean connected
-
length_buf
protected final byte[] length_buf
-
use_lock_to_send
protected boolean use_lock_to_send
-
-
Constructor Detail
-
TcpConnection
public TcpConnection(Address peer_addr, TcpBaseServer server) throws java.lang.Exception
Creates a connection to a remote peer, useconnect(Address)to connect- Throws:
java.lang.Exception
-
TcpConnection
public TcpConnection(java.net.Socket s, TcpServer server) throws java.lang.ExceptionCalled byTcpServer.Acceptor.handleAccept(Socket)- Throws:
java.lang.Exception
-
-
Method Detail
-
useLockToSend
public boolean useLockToSend()
-
useLockToSend
public <T extends Connection> T useLockToSend(boolean u)
-
localAddress
public Address localAddress()
- Specified by:
localAddressin classConnection
-
getSockAddress
protected java.lang.String getSockAddress()
-
connect
public void connect(Address dest) throws java.lang.Exception
- Specified by:
connectin 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()
- Specified by:
startin classConnection
-
send
public void send(byte[] data, int offset, int length) throws java.lang.Exception- Specified by:
sendin classConnection- Throws:
java.lang.Exception
-
locklessSend
public void locklessSend(byte[] data, int offset, int length) throws java.lang.Exception- Throws:
java.lang.Exception
-
send
public void send(java.nio.ByteBuffer buf) throws java.lang.Exception- Specified by:
sendin classConnection- Throws:
java.lang.Exception
-
doSend
protected void doSend(byte[] data, int offset, int length, boolean flush) throws java.lang.Exception- Throws:
java.lang.Exception
-
flush
public void flush()
- Specified by:
flushin classConnection
-
createDataOutputStream
protected java.io.OutputStream createDataOutputStream(java.io.OutputStream out)
-
createDataInputStream
protected java.io.DataInputStream createDataInputStream(java.io.InputStream in)
-
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
Send the cookie first, then our port number. If the cookie doesn't match the receiver's cookie, the receiver will reject the connection and close it.- Throws:
java.lang.Exception
-
readPeerAddress
protected Address readPeerAddress(java.net.Socket client_sock) throws java.lang.Exception
Reads the peer's address. First a cookie has to be sent which has to match my own cookie, otherwise the connection will be refused- Throws:
java.lang.Exception
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
status
public java.lang.String status()
- Specified by:
statusin classConnection
-
isConnected
public boolean isConnected()
- Specified by:
isConnectedin classConnection
-
isConnectionPending
public boolean isConnectionPending()
- Specified by:
isConnectionPendingin classConnection
-
isClosed
public boolean isClosed()
- Specified by:
isClosedin classConnection
-
close
public void close() throws java.io.IOException- Throws:
java.io.IOException
-
-