Skip to content

Commit

Permalink
Merge pull request #10 from RoboTeamTwente/feature/headless-no-lib
Browse files Browse the repository at this point in the history
Feature: real headless mode for environment without graphical server
  • Loading branch information
tollsimy authored Nov 3, 2023
2 parents 5644e52 + 2946f6b commit 3b33632
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 31 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ Finally, you can use Gradle to run the application:
### Program Arguments
The program accepts a few program arguments:
```
--world-ip=... the IP on which the application tries to connect to the RoboTeam World Observer to [default = 127.0.0.1]
--world-port=... the port on which the application tries to connect to the RoboTeam World Observer to [default = 5558]
--gc-ip=... the IP on which the application tries to connect to the Game Controller [default = 127.0.0.1]
--gc-port=... the port on which the application tries to connect to the Game Controller [default = 10007]
--active if the application should start in active mode instead of passive [default = passive]
--cli run in headless mode
-active if the application should start in active mode instead of passive [default = passive]
-cli run in headless mode
-gpip=<value>,--gc-ip=<value> the IP on which the application tries to connect to the Game Controller [default = 127.0.0.1]
-gcp=<value>,--gc-port=<value> the port on which the application tries to connect to the Game Controller [default = 10007]
-wip=<value>,--world-ip=<value> the IP on which the application tries to connect to the RoboTeam World Observer to [default = 127.0.0.1]
-wp=<value>,--world-port=<value> the port on which the application tries to connect to the RoboTeam World Observer to [default = 5558]
```

When running using gradle, these arguments can be specified in the following way:
```bash
./gradlew run --args="--active"
./gradlew run --args="-active"
```

## Rules
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
group 'nl.roboteamtwente.autoref'
version '1.0-SNAPSHOT'

mainClassName = "nl.roboteamtwente.autoref.ui.AutoRefUi"
mainClassName = "nl.roboteamtwente.autoref.ui.AutoRef"

repositories {
mavenCentral()
Expand All @@ -18,6 +18,7 @@ repositories {
dependencies {
implementation 'com.google.protobuf:protobuf-java:3.21.5'
implementation 'org.zeromq:jeromq:0.5.3'
implementation 'commons-cli:commons-cli:1.6.0'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/nl/roboteamtwente/autoref/ui/AutoRef.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package nl.roboteamtwente.autoref.ui;
import java.util.*;

import javafx.application.Application;

import org.apache.commons.cli.*;

public class AutoRef {

private static AutoRefController controller;

public static void main(String[] args) throws Exception {

Options options = new Options();

Option wip_opt = new Option("wip", "world-ip", true, "world ip");
options.addOption(wip_opt);
Option wp_opt = new Option("wp", "world-port", true, "world port");
options.addOption(wp_opt);
Option gcip_opt = new Option("gcip", "gc-ip", true, "game controller ip");
options.addOption(gcip_opt);
Option gcport_opt = new Option("gcp", "gc-port", true, "game controller port");
options.addOption(gcport_opt);
Option active_opt = new Option("active", "active or passive mode");
active_opt.setRequired(false);
options.addOption(active_opt);
Option headless_opt = new Option("cli", "headless mode");
headless_opt.setRequired(false);
options.addOption(headless_opt);

CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd = null;

try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
formatter.printHelp("RTT AutoRef", options);
System.exit(1);
}

String wip = cmd.getOptionValue("wip","127.0.0.1");
String wp = cmd.getOptionValue("wp","5558");
String gcip = cmd.getOptionValue("gcip","127.0.0.1");
String gcport = cmd.getOptionValue("gcp","10007");
boolean active = cmd.hasOption("active");
boolean headless = cmd.hasOption("cli");

if(headless){
System.out.println("Running Headless");
controller = new AutoRefController();
controller.initialize_headless();
controller.start(wip,wp,gcip,gcport,active,headless);
}
else
{
Application.launch(AutoRefUi.class,wip,wp,gcip,gcport,
String.valueOf(active),String.valueOf(headless));
}
}
}
48 changes: 34 additions & 14 deletions src/main/java/nl/roboteamtwente/autoref/ui/AutoRefController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
import nl.roboteamtwente.autoref.SSLAutoRef;

import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;

public class AutoRefController implements Initializable {
private SSLAutoRef sslAutoRef;
private boolean isHeadless;

@FXML
public ComboBox<String> modeBox;
Expand All @@ -42,6 +44,7 @@ public class AutoRefController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
sslAutoRef = new SSLAutoRef();

canvas.setSslAutoRef(sslAutoRef);

sslAutoRef.setOnViolation((violation) -> {
Expand All @@ -51,11 +54,12 @@ public void initialize(URL location, ResourceBundle resources) {

Text timeText = new Text("[" + timeString + "] ");
timeText.setStyle("-fx-font-weight: bold");

Platform.runLater(() -> {
logList.getItems().add(new TextFlow(timeText, new Text(violation.toString())));
logList.scrollTo(logList.getItems().size() - 1);
});
if(!isHeadless){
Platform.runLater(() -> {
logList.getItems().add(new TextFlow(timeText, new Text(violation.toString())));
logList.scrollTo(logList.getItems().size() - 1);
});
}
});

modeBox.getItems().addAll("Passive", "Active");
Expand All @@ -78,19 +82,31 @@ public void handle(long now) {
anim.start();
}

public void start(Application.Parameters parameters) {
try {
String ipWorld = parameters.getNamed().getOrDefault("world-ip", "127.0.0.1");
String ipGameController = parameters.getNamed().getOrDefault("gc-ip", "127.0.0.1");
int portWorld = Integer.parseInt(parameters.getNamed().getOrDefault("world-port", "5558"));
int portGameController = Integer.parseInt(parameters.getNamed().getOrDefault("gc-port", "10007"));
public void initialize_headless() {
sslAutoRef = new SSLAutoRef();

boolean active = parameters.getUnnamed().contains("--active");
sslAutoRef.setOnViolation((violation) -> {
double time = sslAutoRef.getReferee().getGame().getTime();
String timeString = String.format("%d:%05.2f", (int) (time / 60), time % 60);
System.out.println("[" + timeString + "] " + violation);

modeBox.setValue(active ? "Active" : "Passive");
Text timeText = new Text("[" + timeString + "] ");
timeText.setStyle("-fx-font-weight: bold");
});
}

public void start(String ipWorld, String portWorld, String ipGameController,
String portGameController, boolean active, boolean headless) {
try {
setHeadless(headless);
if(!isHeadless){
modeBox.setValue(active ? "Active" : "Passive");
}
sslAutoRef.setActive(active);
sslAutoRef.start(ipWorld, ipGameController, portWorld, portGameController);
sslAutoRef.start(ipWorld, ipGameController,
Integer.valueOf(portWorld),
Integer.valueOf(portGameController));

} catch (NumberFormatException e) {
System.err.println("Failed to parse port program argument.");
System.exit(1);
Expand All @@ -100,4 +116,8 @@ public void start(Application.Parameters parameters) {
public void stop() {
sslAutoRef.stop();
}

private void setHeadless(boolean headless){
isHeadless=headless;
}
}
16 changes: 7 additions & 9 deletions src/main/java/nl/roboteamtwente/autoref/ui/AutoRefUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ public void start(Stage primaryStage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Objects.requireNonNull(getClass().getResource("/auto_ref.fxml")));
Parent root = fxmlLoader.load();
controller = fxmlLoader.getController();

if(!getParameters().getUnnamed().contains("--cli"))
{
primaryStage.setTitle("RoboTeam Twente: AutoRef");
primaryStage.setScene(new Scene(root, 640, 480));
primaryStage.show();
}

controller.start(getParameters());
primaryStage.setTitle("RoboTeam Twente: AutoRef");
primaryStage.setScene(new Scene(root, 640, 480));
primaryStage.show();
controller.start(getParameters().getRaw().get(0),getParameters().getRaw().get(1),
getParameters().getRaw().get(2),getParameters().getRaw().get(3),
Boolean.valueOf(getParameters().getRaw().get(4)),
Boolean.valueOf(getParameters().getRaw().get(5)));
}

@Override
Expand Down

0 comments on commit 3b33632

Please sign in to comment.