• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WindhoverLabs / phoebus / #92

26 Sep 2023 03:43PM UTC coverage: 16.596% (+0.009%) from 16.587%
#92

push

lorenzo-gomez-windhover
-Connection status improvements.
-CommandHistory support. WIP.

56 of 56 new or added lines in 3 files covered. (100.0%)

17830 of 107434 relevant lines covered (16.6%)

0.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.59
/core/commander-core/src/main/java/com/windhoverlabs/yamcs/core/YamcsServer.java
1
package com.windhoverlabs.yamcs.core;
2

3
import com.windhoverlabs.pv.yamcs.YamcsAware;
4
import com.windhoverlabs.pv.yamcs.YamcsAware.YamcsAwareMethod;
5
import java.util.ArrayList;
6
import java.util.List;
7
import java.util.concurrent.ExecutionException;
8
import java.util.logging.Logger;
9
import javafx.application.Platform;
10
import javafx.beans.property.SimpleStringProperty;
11
import javafx.beans.property.StringProperty;
12
import javafx.scene.control.Alert;
13
import javafx.scene.control.Alert.AlertType;
14
import org.yamcs.client.ClientException;
15
import org.yamcs.client.ConnectionListener;
16
import org.yamcs.client.YamcsClient;
17
import org.yamcs.protobuf.YamcsInstance;
18

19
public class YamcsServer extends YamcsObject<CMDR_YamcsInstance> {
20

21
  public static final Logger log = Logger.getLogger(YamcsServer.class.getPackageName());
1✔
22

23
  public static String OBJECT_TYPE = "server";
1✔
24
  private YamcsClient yamcsClient;
25
  private CMDR_YamcsInstance defaultInstance = null;
1✔
26
  // TODO: It's starting to look like this class and YamcsObjectManager should be the same class...
27

28
  private ConnectionState serverState = ConnectionState.DISCONNECTED;
1✔
29
  private ArrayList<YamcsAware> listeners = new ArrayList<YamcsAware>();
1✔
30
  private StringProperty serverStateStrProperty = new SimpleStringProperty();
1✔
31

32
  public StringProperty getServerStateStrProperty() {
33
    return serverStateStrProperty;
×
34
  }
35

36
  public YamcsClient getYamcsClient() {
37
    return yamcsClient;
×
38
  }
39

40
  private YamcsServerConnection connection;
41

42
  public void setConnection(YamcsServerConnection connection) {
43
    if (serverState == ConnectionState.DISCONNECTED) {
1✔
44
      this.connection = connection;
1✔
45
      this.setName(connection.getName());
1✔
46
      serverStateStrProperty.set(this.toString());
1✔
47
    }
48
  }
1✔
49

50
  public YamcsServer(String name) {
51
    super(name);
1✔
52
    serverStateStrProperty.set(this.toString());
1✔
53
  }
1✔
54

55
  @Override
56
  public void createAndAddChild(String name) {
57
    getItems().add(new CMDR_YamcsInstance(name));
1✔
58
  }
1✔
59

60
  @Override
61
  public String getObjectType() {
62
    return OBJECT_TYPE;
1✔
63
  }
64

65
  public void addListener(YamcsAware newListener) {
66
    if (serverState == ConnectionState.CONNECTED) {
1✔
67
      newListener.onInstancesReady(this);
1✔
68
    }
69
    listeners.add(newListener);
1✔
70
  }
1✔
71

72
  public boolean connect(YamcsServerConnection newConnection) {
73
    connection = newConnection;
1✔
74

75
    // TODO:Not sure if this is necessary given our non-global model of instances
76
    if (yamcsClient != null) {
1✔
77
      yamcsClient.close();
×
78
    }
79
    yamcsClient = YamcsClient.newBuilder(connection.getUrl(), connection.getPort()).build();
1✔
80

81
    if (newConnection.getPassword() != null && newConnection.getUser() != null) {
1✔
82
      try {
83
        yamcsClient.login(newConnection.getUser(), newConnection.getPassword().toCharArray());
×
84
      } catch (ClientException e) {
×
85
        // TODO Auto-generated catch block
86
        e.printStackTrace();
×
87
        return false;
×
88
      }
×
89
    }
90

91
    try {
92
      yamcsClient.addConnectionListener(
1✔
93
          new ConnectionListener() {
1✔
94

95
            @Override
96
            public void connecting() {
97
              // TODO Call of the subscribers
98
            }
1✔
99

100
            @Override
101
            public void connected() {
102
              com.windhoverlabs.yamcs.core.YamcsObjectManager.triggerYamcsListeners(
1✔
103
                  YamcsAwareMethod.onYamcsConnected);
104
              try {
105
                List<YamcsInstance> instances = yamcsClient.listInstances().get();
1✔
106
                for (YamcsInstance instance : instances) {
1✔
107
                  createAndAddChild(instance.getName());
1✔
108

109
                  // TODO:Don't really like doing this here...We should either make
110
                  // YamcsObjectManager
111
                  // package-private or move all of the code from YamcsObjectManager to this class.
112
                  if (YamcsObjectManager.getDefaultInstanceName() != null
1✔
113
                      && YamcsObjectManager.getDefaultInstanceName().equals(instance.getName())
×
114
                      && YamcsObjectManager.getDefaultServerName().equals(getName())) {
×
115
                    YamcsObjectManager.setDefaultInstance(getName(), instance.getName());
×
116
                  }
117

118
                  getItems().get(getItems().size() - 1).activate(yamcsClient, getName());
1✔
119

120
                  for (YamcsAware l : listeners) {
1✔
121
                    l.onInstancesReady(getObj());
×
122
                    ;
123
                  }
×
124
                }
1✔
125
              } catch (InterruptedException e1) {
×
126
                // TODO Auto-generated catch block
127
                e1.printStackTrace();
×
128
              } catch (ExecutionException e1) {
×
129
                // TODO Auto-generated catch block
130
                e1.printStackTrace();
×
131
              }
1✔
132
              // TODO:This works as long as this code is called under the JFX thread...
133
              init();
1✔
134
            }
1✔
135

136
            @Override
137
            public void disconnected() {
138

139
              switch (serverState) {
1✔
140
                case DISCONNECTED:
141
                  break;
×
142
                case CONNECTED:
143
                  // This means we disconnected prematurely
144
                  log.warning("Error on disconnect");
1✔
145
                  disconnect();
×
146
                  errorDisconnectedDialog();
×
147
                  break;
148
              }
149
            }
×
150

151
            @Override
152
            public void connectionFailed(Throwable cause) {
153
              log.warning("Failed to connect to " + getName());
×
154
            }
×
155
          });
156

157
      yamcsClient.connectWebSocket();
1✔
158
      serverState = ConnectionState.CONNECTED;
1✔
159
    } catch (ClientException e) {
×
160
      // TODO Auto-generated catch block
161
      e.printStackTrace();
×
162
      return false;
×
163
    }
1✔
164

165
    serverStateStrProperty.set(this.toString());
1✔
166

167
    return true;
1✔
168
  }
169

170
  public boolean connect() {
171
    // TODO:Not sure if this is necessary given our non-global model of instances
172
    if (yamcsClient != null) {
×
173
      yamcsClient.close();
×
174
    }
175

176
    yamcsClient = YamcsClient.newBuilder(connection.getUrl(), connection.getPort()).build();
×
177

178
    if (connection.getPassword() != null && connection.getUser() != null) {
×
179
      try {
180
        yamcsClient.login(connection.getUser(), connection.getPassword().toCharArray());
×
181
      } catch (ClientException e) {
×
182
        // TODO Auto-generated catch block
183
        e.printStackTrace();
×
184
        return false;
×
185
      }
×
186
    }
187

188
    try {
189
      yamcsClient.addConnectionListener(
×
190
          new ConnectionListener() {
×
191

192
            @Override
193
            public void connecting() {
194
              // TODO Call of the subscribers
195
            }
×
196

197
            @Override
198
            public void connected() {
199

200
              try {
201
                List<YamcsInstance> instances = yamcsClient.listInstances().get();
×
202
                com.windhoverlabs.yamcs.core.YamcsObjectManager.triggerYamcsListeners(
×
203
                    YamcsAwareMethod.onYamcsConnected);
204
                for (YamcsInstance instance : instances) {
×
205
                  createAndAddChild(instance.getName());
×
206

207
                  // TODO:Don't really like doing this here...We should either make
208
                  // YamcsObjectManager
209
                  // package-private or move all of the code from YamcsObjectManager to this class.
210
                  if (YamcsObjectManager.getDefaultInstanceName() != null
×
211
                      && YamcsObjectManager.getDefaultInstanceName().equals(instance.getName())
×
212
                      && YamcsObjectManager.getDefaultServerName().equals(getName())) {
×
213
                    YamcsObjectManager.setDefaultInstance(getName(), instance.getName());
×
214
                  }
215

216
                  getItems().get(getItems().size() - 1).activate(yamcsClient, getName());
×
217

218
                  for (YamcsAware l : listeners) {
×
219
                    l.onInstancesReady(getObj());
×
220
                  }
×
221
                }
×
222
              } catch (InterruptedException e1) {
×
223
                // TODO Auto-generated catch block
224
                e1.printStackTrace();
×
225
                return;
×
226
              } catch (ExecutionException e1) {
×
227
                // TODO Auto-generated catch block
228
                e1.printStackTrace();
×
229
                return;
×
230
              }
×
231
              init();
×
232
            }
×
233

234
            @Override
235
            public void disconnected() {
236
              switch (serverState) {
×
237
                case DISCONNECTED:
238
                  break;
×
239
                case CONNECTED:
240
                  // This means we disconnected prematurely
241
                  log.warning("Error on disconnect");
×
242
                  disconnect();
×
243
                  errorDisconnectedDialog();
×
244
                  break;
245
              }
246
            }
×
247

248
            @Override
249
            public void connectionFailed(Throwable cause) {
250
              log.warning("Failed to connect to " + getName());
×
251
            }
×
252
          });
253
      yamcsClient.connectWebSocket();
×
254

255
    } catch (ClientException e) {
×
256
      // TODO Auto-generated catch block
257
      e.printStackTrace();
×
258
      errorDisconnectedDialog();
×
259
      return false;
×
260
    }
×
261

262
    return true;
×
263
  }
264

265
  private void unInit() {
266
    for (CMDR_YamcsInstance instance : this.getItems()) {
1✔
267
      instance.deActivate(yamcsClient, this.getName());
1✔
268
    }
1✔
269
    this.getItems().clear();
1✔
270
  }
1✔
271

272
  private void init() {
273
    // TODO: Have to think about this one for a minute
274
    //    for (CMDR_YamcsInstance instance : this.getItems()) {
275
    //      instance.activate(yamcsClient, this.getName());
276
    //    }
277
    serverState = ConnectionState.CONNECTED;
1✔
278
    serverStateStrProperty.set(this.toString());
1✔
279
  }
1✔
280

281
  public void disconnect() {
282
    unInit();
1✔
283
    serverState = ConnectionState.DISCONNECTED;
1✔
284
    Platform.runLater(
×
285
        () -> {
286
          serverStateStrProperty.set(this.toString());
×
287
        });
×
288
    // TODO: unInit resources such as event subscriptions, parameter subscriptions, etc
289
    if (yamcsClient != null) {
×
290
      yamcsClient.close();
×
291
    }
292

293
    //    The following lines should really be inside one function
294
    for (YamcsAware l : listeners) {
×
295
      l.onYamcsDisconnected();
×
296
    }
×
297
    YamcsObjectManager.triggerYamcsListeners(YamcsAwareMethod.onYamcsDisconnected);
×
298
  }
×
299

300
  public YamcsServerConnection getConnection() {
301
    return connection;
1✔
302
  }
303

304
  public CMDR_YamcsInstance getInstance(String instanceName) {
305
    CMDR_YamcsInstance resultInstance = null;
1✔
306

307
    if (instanceName != null) {
1✔
308
      for (CMDR_YamcsInstance instance : getItems()) {
1✔
309
        if (instanceName.equals(instance.getName())) {
1✔
310
          resultInstance = instance;
1✔
311
          break;
1✔
312
        }
313
      }
×
314
    }
315

316
    return resultInstance;
1✔
317
  }
318

319
  void setDefaultInstance(String instanceName) {
320
    defaultInstance = getInstance(instanceName);
1✔
321
    YamcsObjectManager.triggerYamcsListeners(YamcsAwareMethod.changeDefaultInstance);
1✔
322
  }
1✔
323

324
  public CMDR_YamcsInstance getDefaultInstance() {
325
    return defaultInstance;
1✔
326
  }
327

328
  public ConnectionState getServerState() {
329
    return serverState;
1✔
330
  }
331

332
  /**
333
   * Attempt to connect.
334
   *
335
   * @param newConnection
336
   * @return true if the connection attempt is successful. Otherwise, this function returns false.
337
   */
338
  public static boolean testConnection(YamcsServerConnection newConnection) {
339

340
    YamcsClient yamcsClient = null;
1✔
341
    try {
342
      yamcsClient =
1✔
343
          YamcsClient.newBuilder(newConnection.getUrl(), newConnection.getPort())
1✔
344
              .withTls(false)
1✔
345
              .build();
1✔
346

347
      if (newConnection.getPassword() != null && newConnection.getUser() != null) {
1✔
348
        yamcsClient.login(newConnection.getUser(), newConnection.getPassword().toCharArray());
×
349
      }
350

351
      yamcsClient.connectWebSocket();
1✔
352

353
    } catch (Exception e) {
1✔
354
      // TODO Auto-generated catch block
355
      return false;
1✔
356
    }
1✔
357

358
    yamcsClient.close();
1✔
359

360
    return true;
1✔
361
  }
362

363
  public final String toString() {
364
    return getName() + " | " + getServerState();
1✔
365
  }
366

367
  private YamcsServer getObj() {
368
    return this;
×
369
  }
370

371
  private void errorDisconnectedDialog() {
372
    Platform.runLater(
×
373
        () -> {
374
          Alert dialog = new Alert(AlertType.ERROR);
×
375
          dialog.setContentText(
×
376
              "Connection was ended prematurely.\n"
377
                  + "This is most likely a bug or an incomtaible YAMCS version."
378
                  + "To report a bug or get in touch with the dev team:"
379
                  + "https://github.com/WindhoverLabs/phoebus");
380
          dialog.showAndWait();
×
381
        });
×
382
  }
×
383
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc