|
|
Here, I'll walk you through the Common class, containing code used by every component during startup. Additional startup procedures are explained in the documentation for the [`start_component.sh`](TODO) and the [`run_demo.sh`](TODO) scripts.
|
|
|
|
|
|
The discussed file can be found [here](https://gitlab.hrz.tu-chemnitz.de/s6869070--tu-dresden.de/vws-spielwiese/-/blob/b1802bdd98650bdb666f03483f1e13c7f99bf4b8/basyx.lichterkette/src/main/java/de/olipar/basyx/lichterkette/Common.java).
|
|
|
|
|
|
We'll skip the license header, imports and comments, as they are either pretty much self explanatory or not the product of conscious development effort.
|
|
|
|
|
|
In a nutshell, this class offers four methods to get commonly used parameters of the system. Specifically the host address, the port to host the started component on, the path to the AAS registry and server.
|
|
|
|
|
|
```java
|
|
|
public class Common {
|
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Common.class);
|
|
|
```
|
|
|
At the start of the `Common`-class, we aquire a logger object, to which all console output will be passed. This is not only used internally by BaSyx too, but allows us to easily utilize different loglevels, such as `INFO`, `WARN` and `ERROR`, indicating severity.
|
|
|
|
|
|
```java
|
|
|
public static String getHostAddress() {
|
|
|
```
|
|
|
This method returns the host address, ie. the locally resolvable address of the machine this code is executed on.
|
|
|
|
|
|
```java
|
|
|
try {
|
|
|
return InetAddress.getLocalHost().getHostName() + ".local";
|
|
|
```
|
|
|
This demo utilizes [mDNS](https://en.wikipedia.org/wiki/Multicast_DNS) to dynamically resolve the IP addresses of other components in the system. To get the host address, we take the hostname and append `.local`. Note that you might have to change this, if you configured your mDNS suffix to a different string.
|
|
|
If the machine executing has the hostname `registry`, the locally resolvable address returned will be `registry.local`.
|
|
|
|
|
|
```java
|
|
|
} catch (UnknownHostException e) {
|
|
|
logger.warn("Local hostname could not be found. Using loopback address (127.0.0.1) as fallback.");
|
|
|
e.printStackTrace();
|
|
|
return "127.0.0.1";
|
|
|
}
|
|
|
```
|
|
|
Should we be unable to aquire the local hostname, we'll give a warning and use a loopback address going forward. This will however render the endpoints in the registry only accessible from the local machine.
|
|
|
|
|
|
```java
|
|
|
public static int getHostPort() {
|
|
|
```
|
|
|
This method returns the port to host the started component, given as an environment variable.
|
|
|
|
|
|
```java
|
|
|
String uncheckedPort = System.getenv("PORT");
|
|
|
```
|
|
|
Here, we get the content of the environment variable `PORT`, used to set the port to host the service on.
|
|
|
|
|
|
```java
|
|
|
if (uncheckedPort == null || uncheckedPort.isEmpty()) {
|
|
|
logger.warn("No valid PORT set.");
|
|
|
logger.warn("You might want to explicitly set a PORT environment variable, eg:");
|
|
|
logger.warn("$ export PORT=4000");
|
|
|
logger.warn("A port 4000 will be assumed going forward.");
|
|
|
return 4000;
|
|
|
```
|
|
|
If there is no or an empty `PORT` environment variable set, we'll go forward assuming a port 4000 to be used. This fallback will only work once per machine, as any single port can only be used once.
|
|
|
|
|
|
```java
|
|
|
} else {
|
|
|
try {
|
|
|
return Integer.parseUnsignedInt(uncheckedPort);
|
|
|
```
|
|
|
If a non-empty `PORT` environment variable is set, we'll try to convert it to an Integer and return the resulting value.
|
|
|
|
|
|
```java
|
|
|
} catch (NumberFormatException e) {
|
|
|
logger.warn("No valid PORT set.");
|
|
|
logger.warn("You might want to explicitly set a PORT environment variable, eg:");
|
|
|
logger.warn("$ export PORT=4000");
|
|
|
logger.warn("A port 4000 will be assumed going forward.");
|
|
|
return 4000;
|
|
|
}
|
|
|
```
|
|
|
If a non-empty `PORT` environment variable is set, but isn't a valid unsigned integer, we'll fallback like before.
|
|
|
|
|
|
```java
|
|
|
public static String getRegistryPath() {
|
|
|
String uncheckedRegistryPath = System.getenv("REGISTRYPATH");
|
|
|
```
|
|
|
This method returns the value of the environment variable `REGISTRYPATH` or a fallback, handling the acquisition of the value similar to the method above.
|
|
|
|
|
|
```java
|
|
|
if (uncheckedRegistryPath == null || uncheckedRegistryPath.isEmpty()) {
|
|
|
logger.warn("No valid REGISTRYPATH set.");
|
|
|
logger.warn("You might want to explicitly set a REGISTRYPATH environment variable, eg:");
|
|
|
logger.warn("$ export REGISTRYPATH=\"http://registry.local:4000/registry\"");
|
|
|
logger.warn("A registry endpoint at http://127.0.0.1:4000/registry will be assumed going forward.");
|
|
|
return "http://127.0.0.1:4000/registry";
|
|
|
```
|
|
|
If there is no or an empty `REGISTRYPATH` environment variable set, we'll go forward assuming a registry endpoint at `http://127.0.0.1:4000/registry` to be used.
|
|
|
|
|
|
```java
|
|
|
} else {
|
|
|
return uncheckedRegistryPath;
|
|
|
}
|
|
|
```
|
|
|
If there is a non-empty `REGISTRYPATH` environment variable set, the content is passed back to the caller.
|
|
|
This might of course be invalid in all sorts of ways, causing the component to crash later on, but without knowing the correct registry endpoint, moving on past then would be useless and finding the correct endpoint automatically is pretty much impossible.
|
|
|
|
|
|
```java
|
|
|
public static String getAASServerPath() {
|
|
|
String uncheckedAASServerPath = System.getenv("AASSERVERPATH");
|
|
|
if (uncheckedAASServerPath == null || uncheckedAASServerPath.isEmpty()) {
|
|
|
logger.warn("No valid AASSERVERPATH set.");
|
|
|
logger.warn("You might want to explicitly set a AASSERVERPATH environment variable, eg:");
|
|
|
logger.warn("$ export AASSERVERPATH=\"http://aasserver.local:4001/aasServer\"");
|
|
|
logger.warn("An AAS server endpoint at http://127.0.0.1:4001/aasServer will be assumed going forward.");
|
|
|
return "http://localhost:4001/aasServer";
|
|
|
} else {
|
|
|
return uncheckedAASServerPath;
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
This method returns the value of the environment variable `AASSERVERPATH` or a fallback, handling the acquisition of the value very similar to the method above. |
|
|
\ No newline at end of file |