Skip to content

Commit

Permalink
update reconnect loops
Browse files Browse the repository at this point in the history
  • Loading branch information
icklesteve committed Jun 27, 2020
1 parent 29ec411 commit 47f9e8c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
34 changes: 17 additions & 17 deletions java/src/jmri/jmrix/AbstractNetworkPortController.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ public DataOutputStream getOutputStream() {

@Override
public void dispose() {
allowConnectionRecovery = false;
super.dispose();
}

//private boolean allowConnectionRecovery = false;
/**
* Close the client side socket connection, reset the open flag and attempt
* a reconnection. Called when a connection is initially lost.
Expand All @@ -303,7 +303,7 @@ public void recover() {
public void reconnect() {

// If the connection is already open, then we shouldn't try a re-connect.
if (opened && !allowConnectionRecovery) {
if (opened || !allowConnectionRecovery) {
return;
}
ReconnectWait thread = new ReconnectWait();
Expand All @@ -314,12 +314,11 @@ public void reconnect() {
} catch (InterruptedException e) {
log.error("Unable to join to the reconnection thread");
}

if (!opened) {
log.error("Failed to re-establish connectivity");

} else {
resetupConnection();
log.info("Reconnected to {}", getHostName());
log.info(Bundle.getMessage("ReconnectedTo", getHostName()));
}
}

Expand Down Expand Up @@ -348,11 +347,12 @@ public ReconnectWait() {
public void run() {
boolean reply = true;
int count = 0;
int secondCount = 0;
while (reply) {
safeSleep(reconnectinterval, "Waiting");
int interval = reconnectinterval;
int totalsleep = 0;
while (reply && allowConnectionRecovery) {
safeSleep(interval*1000L, "Waiting");
count++;

totalsleep += interval;
try {
// if the device allows autoConfiguration,
// we need to run the autoConfigure() call
Expand All @@ -365,17 +365,17 @@ public void run() {
log.trace("restart failed", ex); // main warning to log.error done within connect();
// if returned on exception stops thread and connection attempts
}

reply = !opened;
if (count >= retryAttempts) {
log.error("Unable to reconnect after {} Attempts, increasing duration of retries", count);
if (opened){
return;
}
if (count % 10==0) {
//retrying but with twice the retry interval.
reconnectinterval = reconnectinterval * 2;
count = 0;
secondCount++;
interval = Math.min(interval * 2, reconnectMaxInterval);
log.error(Bundle.getMessage("ReconnectFailRetry", totalsleep, count,interval));
}
if (secondCount >= 10) {
log.error("Giving up on reconnecting after 100 attempts");
if ((reconnectMaxAttempts > -1) && (count >= reconnectMaxAttempts)) {
log.error(Bundle.getMessage("ReconnectFailAbort",totalsleep,count));
reply = false;
}
}
Expand Down
52 changes: 26 additions & 26 deletions java/src/jmri/jmrix/AbstractSerialPortController.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ public Vector<String> getPortNames() {
*/
@Override
public void dispose() {
allowConnectionRecovery = false;
super.dispose();
}

Expand Down Expand Up @@ -498,26 +499,22 @@ protected void resetupConnection() {
}

/**
* Attempts to reconnect to a failed Server
* Attempts to reconnect to a failed port.
*/
public void reconnect() {
// If the connection is already open, then we shouldn't try a re-connect.
if (opened && !allowConnectionRecovery) {
if (opened || !allowConnectionRecovery) {
return;
}
closeConnection();
ReconnectWait thread = new ReconnectWait();
thread.setName("Connection Recovery " + getCurrentPortName() );
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
log.error("Unable to join to the reconnection thread {}", e.getMessage());
}
if (!opened) {
log.error("Failed to re-establish connectivity");
} else {
log.info("Reconnected to {}", getCurrentPortName());
resetupConnection();
}
}

class ReconnectWait extends Thread {
Expand All @@ -538,44 +535,47 @@ public ReconnectWait() {
public void run() {
boolean reply = true;
int count = 0;
int secondCount = 0;
while (reply) {
safeSleep(reconnectinterval, "Waiting");
int interval = reconnectinterval;
int totalsleep = 0;
while (reply && allowConnectionRecovery) {
safeSleep(interval*1000L, "Waiting");
count++;
totalsleep += interval;
try {
log.error("Retrying Connection attempt {}-{}", secondCount, count);
log.info("Retrying Connection attempt {} for {}", count,mPort);
Enumeration<CommPortIdentifier> portIDs = CommPortIdentifier.getPortIdentifiers();
while (portIDs.hasMoreElements()) {
CommPortIdentifier id = portIDs.nextElement();
// filter out line printers
if (id.getPortType() != CommPortIdentifier.PORT_PARALLEL) // accumulate the names in a vector
{
if (id.getName().equals(mPort)) {
log.info("{} port has reappeared as being valid, trying to reconnect", mPort);
log.info(Bundle.getMessage("ReconnectPortReAppear", mPort));
openPort(mPort, "jmri");
}
}
}
} catch (RuntimeException e) {
log.warn("failed to reconnect to port {}", (mPort == null ? "null" : mPort));
log.warn(Bundle.getMessage("ReconnectFail",(mPort == null ? "null" : mPort)));
}
reply = !opened;
if (count >= retryAttempts) {
log.error("Unable to reconnect after {} attempts, increasing duration of retries", count);
// retrying but with twice the retry interval.
reconnectinterval = reconnectinterval * 2;
count = 0;
secondCount++;
}
if (secondCount >= 10) {
log.error("Giving up on reconnecting after 100 attempts to reconnect");
reply = false;
if (!opened) {
if (count % 10==0 ) {
//retrying but with twice the retry interval.
interval = Math.min(interval * 2, reconnectMaxInterval);
log.error(Bundle.getMessage("ReconnectFailRetry", totalsleep, count,interval));
log.info(Bundle.getMessage("ReconnectSerialTip"));
}
if ((reconnectMaxAttempts > -1) && (count >= reconnectMaxAttempts)) {
log.error(Bundle.getMessage("ReconnectFailAbort",totalsleep,count));
reply = false;
}
}
}
if (!opened) {
log.error("Failed to re-establish connectivity");
log.error(Bundle.getMessage("ReconnectFailAbort"));
} else {
log.error("Reconnected to {}", getCurrentPortName());
log.info(Bundle.getMessage("ReconnectedTo",getCurrentPortName()));
resetupConnection();
}
}
Expand Down
9 changes: 9 additions & 0 deletions java/src/jmri/jmrix/JmrixBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ ConnectionNameDialog = Connection Name {0} is already assigned
TipBaudRateFixed = The baud rate is fixed for this protocol
TipBaudRateMatch = Must match the baud rate setting of your hardware

# port controller reconnect
ReconnectAttempt = Retrying Connection attempt {0} for {1}
ReconnectFail = Failed to reconnect to port {0}
ReconnectFailRetry = Unable to reconnect after {0}s and {1} attempts. Duration between retries {2}s.
ReconnectFailAbort = Giving up on reconnection after {0}s and {1} attempts. Failed to re-establish connectivity.
ReconnectSerialTip = If your device is connected, you may need to reset the physical connection.
ReconnectPortReAppear = Port {0} has reappeared as being valid, trying to reconnect.
ReconnectedTo = Reconnected to {0}

# serverframe shared items
#Server status label
StatusLabel = Server Status: {0} - Client Count: {1}
Expand Down

0 comments on commit 47f9e8c

Please sign in to comment.