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

WindhoverLabs / phoebus / #71

28 Aug 2023 03:02PM UTC coverage: 16.563% (+0.05%) from 16.512%
#71

push

lorenzo-gomez-windhover
-Add links viewer. Functional.
-TODO:Need to handle edge case when count drops.

6 of 6 new or added lines in 1 file covered. (100.0%)

17760 of 107229 relevant lines covered (16.56%)

0.17 hits per line

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

48.55
/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

103
              try {
104
                List<YamcsInstance> instances = yamcsClient.listInstances().get();
1✔
105
                for (YamcsInstance instance : instances) {
1✔
106
                  createAndAddChild(instance.getName());
1✔
107

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

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

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

134
            @Override
135
            public void disconnected() {
136

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

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

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

163
    serverStateStrProperty.set(this.toString());
1✔
164

165
    return true;
1✔
166
  }
167

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

174
    yamcsClient = YamcsClient.newBuilder(connection.getUrl(), connection.getPort()).build();
×
175

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

186
    try {
187
      yamcsClient.addConnectionListener(
×
188
          new ConnectionListener() {
×
189

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

195
            @Override
196
            public void connected() {
197

198
              try {
199
                List<YamcsInstance> instances = yamcsClient.listInstances().get();
×
200
                for (YamcsInstance instance : instances) {
×
201
                  createAndAddChild(instance.getName());
×
202

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

212
                  getItems().get(getItems().size() - 1).activate(yamcsClient, getName());
×
213

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

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

244
            @Override
245
            public void connectionFailed(Throwable cause) {
246
              log.warning("Failed to connect to " + getName());
×
247
            }
×
248
          });
249
      yamcsClient.connectWebSocket();
×
250

251
    } catch (ClientException e) {
×
252
      // TODO Auto-generated catch block
253
      e.printStackTrace();
×
254
      errorDisconnectedDialog();
×
255
      return false;
×
256
    }
×
257

258
    return true;
×
259
  }
260

261
  private void unInit() {
262
    for (CMDR_YamcsInstance instance : this.getItems()) {
1✔
263
      instance.deActivate(yamcsClient, this.getName());
1✔
264
    }
1✔
265
    this.getItems().clear();
1✔
266
  }
1✔
267

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

277
  public void disconnect() {
278
    for (YamcsAware l : listeners) {
1✔
279
      l.onYamcsDisconnected();
1✔
280
    }
1✔
281

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

295
  public YamcsServerConnection getConnection() {
296
    return connection;
1✔
297
  }
298

299
  public CMDR_YamcsInstance getInstance(String instanceName) {
300
    CMDR_YamcsInstance resultInstance = null;
1✔
301

302
    if (instanceName != null) {
1✔
303
      for (CMDR_YamcsInstance instance : getItems()) {
1✔
304
        if (instanceName.equals(instance.getName())) {
1✔
305
          resultInstance = instance;
1✔
306
          break;
1✔
307
        }
308
      }
×
309
    }
310

311
    return resultInstance;
1✔
312
  }
313

314
  void setDefaultInstance(String instanceName) {
315
    defaultInstance = getInstance(instanceName);
1✔
316
  }
1✔
317

318
  public CMDR_YamcsInstance getDefaultInstance() {
319
    return defaultInstance;
1✔
320
  }
321

322
  public ConnectionState getServerState() {
323
    return serverState;
1✔
324
  }
325

326
  /**
327
   * Attempt to connect.
328
   *
329
   * @param newConnection
330
   * @return true if the connection attempt is successful. Otherwise, this function returns false.
331
   */
332
  public static boolean testConnection(YamcsServerConnection newConnection) {
333

334
    YamcsClient yamcsClient = null;
1✔
335
    try {
336
      yamcsClient =
1✔
337
          YamcsClient.newBuilder(newConnection.getUrl(), newConnection.getPort())
1✔
338
              .withTls(false)
1✔
339
              .build();
1✔
340

341
      if (newConnection.getPassword() != null && newConnection.getUser() != null) {
1✔
342
        yamcsClient.login(newConnection.getUser(), newConnection.getPassword().toCharArray());
×
343
      }
344

345
      yamcsClient.connectWebSocket();
1✔
346

347
    } catch (Exception e) {
1✔
348
      // TODO Auto-generated catch block
349
      return false;
1✔
350
    }
1✔
351

352
    yamcsClient.close();
1✔
353

354
    return true;
1✔
355
  }
356

357
  public final String toString() {
358
    return getName() + " | " + getServerState();
1✔
359
  }
360

361
  private YamcsServer getObj() {
362
    return this;
×
363
  }
364

365
  private void errorDisconnectedDialog() {
366
    Platform.runLater(
×
367
        () -> {
368
          Alert dialog = new Alert(AlertType.ERROR);
×
369
          dialog.setContentText(
×
370
              "Connection was ended prematurely.\n"
371
                  + "This is most likely a bug or an incomtaible YAMCS version."
372
                  + "To report a bug or get in touch with the dev team:"
373
                  + "https://github.com/WindhoverLabs/phoebus");
374
          dialog.showAndWait();
×
375
        });
×
376
  }
×
377
}
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