mirror of
https://github.com/nasa/trick.git
synced 2025-03-24 13:05:36 +00:00
refactored connect()
This commit is contained in:
parent
f9bf78c8a8
commit
3368bbb460
@ -329,8 +329,54 @@ public class SimControlApplication extends TrickApplication implements PropertyC
|
|||||||
*/
|
*/
|
||||||
@Action
|
@Action
|
||||||
public void connect() {
|
public void connect() {
|
||||||
// get host and port for selected sim
|
parseHostPortFromInput(); // get host and port from running sim list
|
||||||
if (runningSimList != null && runningSimList.getSelectedItem() != null) {
|
getInitializationPacket(); // init variable server connection
|
||||||
|
|
||||||
|
if (commandSimcom == null) {
|
||||||
|
String errMsg = "Sorry, can't connect. Please make sure the availability of both server and port!";
|
||||||
|
printErrorMessage(errMsg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setEnabledAllActions(true); // enable all actions
|
||||||
|
scheduleGetSimState(); // set up the sim status variables
|
||||||
|
startStatusMonitors(); // start monitors for sim and health status
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method for starting monitors for sim status as well as health status.
|
||||||
|
*/
|
||||||
|
private void startStatusMonitors() {
|
||||||
|
MonitorSimStatusTask monitorSimStatusTask = new MonitorSimStatusTask(this);
|
||||||
|
monitorSimStatusTask.addPropertyChangeListener(this);
|
||||||
|
getContext().getTaskService().execute(monitorSimStatusTask);
|
||||||
|
|
||||||
|
// For receiving hs messages.
|
||||||
|
getContext().getTaskService().execute(new MonitorHealthStatusTask(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets all actions as either enabled or disabled
|
||||||
|
* @param isEnabled the state to set each action as
|
||||||
|
*/
|
||||||
|
private void setEnabledAllActions(bool isEnabled) {
|
||||||
|
Object[] keys = actionMap.allKeys();
|
||||||
|
// If there is a server connection established, enable all actions.
|
||||||
|
for (int i = 0; i < keys.length; i++) {
|
||||||
|
String theKey = (String)keys[i];
|
||||||
|
getAction(theKey).setEnabled(isEnabled);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the host and port from the runningSimList object
|
||||||
|
*/
|
||||||
|
protected void parseHostPortFromInput() {
|
||||||
|
String hostname = "";
|
||||||
|
int portNum = -1;
|
||||||
|
|
||||||
|
if (runningSimList != null && runningSimList.getSelectedItem() != null) {
|
||||||
String selectedStr = runningSimList.getSelectedItem().toString();
|
String selectedStr = runningSimList.getSelectedItem().toString();
|
||||||
// remove the run info if it is shown
|
// remove the run info if it is shown
|
||||||
int leftPre = selectedStr.indexOf("(");
|
int leftPre = selectedStr.indexOf("(");
|
||||||
@ -347,51 +393,19 @@ public class SimControlApplication extends TrickApplication implements PropertyC
|
|||||||
if (elements == null || elements.length < 2) {
|
if (elements == null || elements.length < 2) {
|
||||||
String errMsg = "Can't connect! Please provide valid host name and port number separated by : or whitespace!";
|
String errMsg = "Can't connect! Please provide valid host name and port number separated by : or whitespace!";
|
||||||
printErrorMessage(errMsg);
|
printErrorMessage(errMsg);
|
||||||
return;
|
} else {
|
||||||
}
|
hostname = elements[0].trim();
|
||||||
host = elements[0].trim();
|
try { portNum = Integer.parseInt(elements[1].trim()); }
|
||||||
try {
|
catch (NumberFormatException nfe) {
|
||||||
port = Integer.parseInt(elements[1].trim());
|
String errMsg = elements[1] + " is not a valid port number!";
|
||||||
} catch (NumberFormatException nfe) {
|
printErrorMessage(errMsg);
|
||||||
String errMsg = elements[1] + " is not a valid port number!";
|
}
|
||||||
printErrorMessage(errMsg);
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getInitializationPacket();
|
|
||||||
|
|
||||||
if (commandSimcom == null) {
|
|
||||||
String errMsg = "Sorry, can't connect. Please make sure the availability of both server and port!";
|
|
||||||
printErrorMessage(errMsg);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
Object[] keys = actionMap.allKeys();
|
|
||||||
// If there is a server connection established, enable all actions.
|
|
||||||
for (int i = 0; i < keys.length; i++) {
|
|
||||||
String theKey = (String)keys[i];
|
|
||||||
getAction(theKey).setEnabled(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduleGetSimState();
|
setHostPort(hostname, portNum);
|
||||||
|
|
||||||
startStatusMonitors();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper method for starting monitors for sim status as well as health status.
|
|
||||||
*/
|
|
||||||
private void startStatusMonitors() {
|
|
||||||
MonitorSimStatusTask monitorSimStatusTask = new MonitorSimStatusTask(this);
|
|
||||||
monitorSimStatusTask.addPropertyChangeListener(this);
|
|
||||||
getContext().getTaskService().execute(monitorSimStatusTask);
|
|
||||||
|
|
||||||
// For receiving hs messages.
|
|
||||||
getContext().getTaskService().execute(new MonitorHealthStatusTask(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//========================================
|
//========================================
|
||||||
// Set/Get methods
|
// Set/Get methods
|
||||||
//========================================
|
//========================================
|
||||||
@ -529,6 +543,11 @@ public class SimControlApplication extends TrickApplication implements PropertyC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void setHostPort(String hostname, int portNum) {
|
||||||
|
host = hostname;
|
||||||
|
port = portNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//========================================
|
//========================================
|
||||||
// Methods
|
// Methods
|
||||||
|
Loading…
x
Reference in New Issue
Block a user