use of org.yamcs.YamcsException in project yamcs-studio by yamcs.
the class YamcsClient method doConnect.
private FutureTask<YamcsConnectionProperties> doConnect() {
if (connected) {
disconnect();
}
restClient = new RestClient(yprops);
restClient.setAutoclose(false);
wsclient = new WebSocketClient(yprops, this);
wsclient.setUserAgent(application);
wsclient.enableReconnection(true);
// Provides compatiblity with old Yamcs instances
wsclient.enableLegacyURLFallback(true);
wsclient.setMaxFramePayloadLength(MAX_FRAME_PAYLOAD_LENGTH);
FutureTask<YamcsConnectionProperties> future = new FutureTask<>(new Runnable() {
@Override
public void run() {
log.info("Connecting to " + yprops);
int maxAttempts = 10;
try {
if (reconnecting && !retry) {
log.warning("Retries are disabled, cancelling reconnection");
reconnecting = false;
return;
}
connecting = true;
connecting();
for (int i = 0; i < maxAttempts; i++) {
try {
log.fine(String.format("Connecting to %s attempt %d", yprops, i));
instances = restClient.blockingGetYamcsInstances();
if (instances == null || instances.isEmpty()) {
log.warning("No configured yamcs instance");
return;
}
String defaultInstanceName = instances.get(0).getName();
String instanceName = defaultInstanceName;
if (yprops.getInstance() != null) {
// check if the instance saved in properties exists,
// otherwise use the default one
instanceName = instances.stream().map(yi -> yi.getName()).filter(s -> s.equals(yprops.getInstance())).findFirst().orElse(defaultInstanceName);
}
yprops.setInstance(instanceName);
ChannelFuture future = wsclient.connect();
future.get(5000, TimeUnit.MILLISECONDS);
return;
} catch (Exception e) {
// For anything other than a security exception, re-try
if (log.isLoggable(Level.FINEST)) {
log.log(Level.FINEST, String.format("Connection to %s failed (attempt %d of %d)", yprops, i + 1, maxAttempts), e);
} else {
log.warning(String.format("Connection to %s failed (attempt %d of %d)", yprops, i + 1, maxAttempts));
}
Thread.sleep(5000);
}
}
connecting = false;
for (ConnectionListener cl : connectionListeners) {
cl.connectionFailed(null, new YamcsException(maxAttempts + " connection attempts failed, giving up."));
}
log.warning(maxAttempts + " connection attempts failed, giving up.");
} catch (InterruptedException e) {
log.info("Connection cancelled by user");
connecting = false;
for (ConnectionListener cl : connectionListeners) {
cl.connectionFailed(null, new YamcsException("Thread interrupted", e));
}
}
}
}, yprops);
executor.submit(future);
// Add Progress indicator in status bar
String jobName = "Connecting to " + yprops;
scheduleAsJob(jobName, future, Job.SHORT);
return future;
}
Aggregations