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
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 class
TcpConnection.Receiver
-
Field Summary
Fields Modifier and Type Field Description protected boolean
connected
protected java.io.DataInputStream
in
protected java.io.DataOutputStream
out
protected TcpConnection.Receiver
receiver
protected java.util.concurrent.locks.ReentrantLock
send_lock
protected TcpBaseServer
server
protected java.net.Socket
sock
protected java.util.concurrent.atomic.AtomicInteger
writers
-
Fields inherited from class org.jgroups.blocks.cs.Connection
cookie, last_access, peer_addr
-
-
Constructor Summary
Constructors Constructor Description TcpConnection(java.net.Socket s, TcpServer server)
TcpConnection(Address peer_addr, TcpBaseServer server)
Creates a connection stub and binds it, useconnect(Address)
to connect
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
connect(Address dest)
protected void
connect(Address dest, boolean send_local_addr)
protected java.io.BufferedInputStream
createBufferedInputStream(java.io.InputStream in)
protected java.io.BufferedOutputStream
createBufferedOutputStream(java.io.OutputStream out)
protected void
doSend(byte[] data, int offset, int length)
protected void
flush()
protected java.lang.String
getSockAddress()
protected long
getTimestamp()
boolean
isConnected()
boolean
isConnectionPending()
boolean
isExpired(long now)
boolean
isOpen()
Address
localAddress()
Address
peerAddress()
protected Address
readPeerAddress(java.net.Socket client_sock)
Reads the peer's address.void
send(byte[] data, int offset, int length)
void
send(java.nio.ByteBuffer buf)
protected void
sendLocalAddress(Address local_addr)
Send the cookie first, then the our port number.protected void
setSocketParameters(java.net.Socket client_sock)
void
start()
java.lang.String
status()
java.lang.String
toString()
protected void
updateLastAccessed()
-
-
-
Field Detail
-
sock
protected final java.net.Socket sock
-
send_lock
protected final java.util.concurrent.locks.ReentrantLock send_lock
-
out
protected java.io.DataOutputStream out
-
in
protected java.io.DataInputStream in
-
receiver
protected volatile TcpConnection.Receiver receiver
-
server
protected final TcpBaseServer server
-
writers
protected final java.util.concurrent.atomic.AtomicInteger writers
-
connected
protected boolean connected
-
-
Constructor Detail
-
TcpConnection
public TcpConnection(Address peer_addr, TcpBaseServer server) throws java.lang.Exception
Creates a connection stub and binds it, useconnect(Address)
to connect- Throws:
java.lang.Exception
-
TcpConnection
public TcpConnection(java.net.Socket s, TcpServer server) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-
Method Detail
-
localAddress
public Address localAddress()
- Specified by:
localAddress
in classConnection
-
peerAddress
public Address peerAddress()
- Specified by:
peerAddress
in classConnection
-
getTimestamp
protected long getTimestamp()
-
getSockAddress
protected java.lang.String getSockAddress()
-
updateLastAccessed
protected void updateLastAccessed()
-
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()
- Specified by:
start
in classConnection
-
send
public void send(byte[] data, int offset, int length) throws java.lang.Exception
- Specified by:
send
in classConnection
- Parameters:
data
- Guaranteed to be non nulloffset
-length
-- Throws:
java.lang.Exception
-
send
public void send(java.nio.ByteBuffer buf) throws java.lang.Exception
- Specified by:
send
in classConnection
- Throws:
java.lang.Exception
-
doSend
protected void doSend(byte[] data, int offset, int length) throws java.lang.Exception
- Throws:
java.lang.Exception
-
flush
protected void flush()
-
createBufferedOutputStream
protected java.io.BufferedOutputStream createBufferedOutputStream(java.io.OutputStream out)
-
createBufferedInputStream
protected java.io.BufferedInputStream createBufferedInputStream(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 the 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:
toString
in classjava.lang.Object
-
status
public java.lang.String status()
- Specified by:
status
in classConnection
-
isExpired
public boolean isExpired(long now)
- Specified by:
isExpired
in classConnection
-
isConnected
public boolean isConnected()
- Specified by:
isConnected
in classConnection
-
isConnectionPending
public boolean isConnectionPending()
- Specified by:
isConnectionPending
in classConnection
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in classConnection
-
close
public void close() throws java.io.IOException
- Throws:
java.io.IOException
-
-