Skip to content

Commit 263cd25

Browse files
author
kasemir
committed
PVA: TCP thread keeps trying to connect
1 parent 74480bb commit 263cd25

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

core/pva/src/main/java/org/epics/pva/client/ClientTCPHandler.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,19 @@ public ClientTCPHandler(final PVAClient client, final InetSocketAddress address,
131131
}
132132

133133
@Override
134-
protected void initializeSocket() throws Exception
134+
protected boolean initializeSocket()
135135
{
136-
socket = SecureSockets.createClientSocket(server_address, tls);
137-
socket.setTcpNoDelay(true);
138-
socket.setKeepAlive(true);
136+
try
137+
{
138+
socket = SecureSockets.createClientSocket(server_address, tls);
139+
socket.setTcpNoDelay(true);
140+
socket.setKeepAlive(true);
141+
}
142+
catch (Exception ex)
143+
{
144+
logger.log(Level.WARNING, "PVA client cannot connect to " + server_address, ex);
145+
return false;
146+
}
139147

140148
// For TLS, check if the socket has a name that's used to authenticate
141149
x509_name = tls ? SecureSockets.getPrincipalCN(((SSLSocket) socket).getSession().getLocalPrincipal()) : null;
@@ -145,6 +153,8 @@ protected void initializeSocket() throws Exception
145153
last_life_sign = last_message_sent = System.currentTimeMillis();
146154
final long period = Math.max(1, PVASettings.EPICS_PVA_CONN_TMO * 1000L / 30 * 3);
147155
alive_check = timer.scheduleWithFixedDelay(this::checkResponsiveness, period, period, TimeUnit.MILLISECONDS);
156+
157+
return true;
148158
}
149159

150160
/** @return Client context */

core/pva/src/main/java/org/epics/pva/common/TCPHandler.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ public TCPHandler(final boolean client_mode)
138138
/** Initialize the {@link #socket}. Called by receiver.
139139
*
140140
* Server received socket from `accept` during construction and this may be a NOP.
141-
* Client will have to create socket and connect to server's address in here
141+
* Client will have to create socket and connect to server's address in here.
142+
*
143+
* @return Success?
142144
*/
143-
abstract protected void initializeSocket() throws Exception;
145+
abstract protected boolean initializeSocket();
144146

145147
/** Start receiving data
146148
* To be called by Client/ServerTCPHandler when fully constructed
@@ -267,10 +269,20 @@ protected void send(final ByteBuffer buffer) throws Exception
267269
/** Receiver */
268270
private Void receiver()
269271
{
272+
// Establish connection
273+
Thread.currentThread().setName("TCP receiver");
274+
while (! initializeSocket())
275+
try
276+
{ // Delay for (another) connection timeout, at least 1 sec
277+
Thread.sleep(Math.max(1, PVASettings.EPICS_PVA_TCP_SOCKET_TMO) * 1000);
278+
}
279+
catch (Exception ignore)
280+
{
281+
// NOP
282+
}
283+
// Listen on the connection
270284
try
271285
{
272-
Thread.currentThread().setName("TCP receiver");
273-
initializeSocket();
274286
Thread.currentThread().setName("TCP receiver " + socket.getLocalSocketAddress());
275287
logger.log(Level.FINER, () -> Thread.currentThread().getName() + " started for " + socket.getRemoteSocketAddress());
276288
logger.log(Level.FINER, "Native byte order " + receive_buffer.order());

core/pva/src/main/java/org/epics/pva/server/ServerTCPHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ public ServerTCPHandler(final PVAServer server, final Socket client, final TLSHa
116116
}
117117

118118
@Override
119-
protected void initializeSocket() throws Exception
119+
protected boolean initializeSocket()
120120
{
121121
// Nothing to do, received client socket on construction
122+
return true;
122123
}
123124

124125
PVAServer getServer()

0 commit comments

Comments
 (0)