-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connection): Add Broadcaster verticle and improve java docs
- Loading branch information
Showing
3 changed files
with
179 additions
and
4 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...lestion-services/src/main/java/de/wuespace/telestion/services/connection/Broadcaster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package de.wuespace.telestion.services.connection; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import de.wuespace.telestion.api.config.Config; | ||
import de.wuespace.telestion.api.message.JsonMessage; | ||
import io.vertx.core.AbstractVerticle; | ||
import io.vertx.core.Promise; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class Broadcaster extends AbstractVerticle { | ||
|
||
public final static int DEFAULT_ID = 0; | ||
|
||
public final record Configuration(@JsonProperty | ||
String inAddress, | ||
@JsonProperty | ||
int id) implements JsonMessage { | ||
public Configuration() { | ||
this(null, DEFAULT_ID); | ||
} | ||
} | ||
|
||
@Override | ||
public void start(Promise<Void> startPromise) { | ||
this.config = Config.get(this.config, new Configuration(), this.config(), Configuration.class); | ||
|
||
if (broadcasterMap.containsKey(this.config.id())) { | ||
startPromise.fail("The broadcasters id #%d is already taken!".formatted(this.config.id())); | ||
return; | ||
} | ||
|
||
startPromise.complete(); | ||
} | ||
|
||
@Override | ||
public void stop(Promise<Void> stopPromise) { | ||
stopPromise.complete(); | ||
} | ||
|
||
public static boolean register(int broadcasterId, String address) { | ||
if (!broadcasterMap.containsKey(broadcasterId)) { | ||
return false; | ||
} | ||
|
||
var broadcaster = broadcasterMap.get(broadcasterId); | ||
broadcaster.addressList.add(address); | ||
|
||
broadcaster.getVertx().eventBus() | ||
.consumer(broadcaster.config.inAddress(), raw -> { | ||
if (!JsonMessage.on(RawMessage.class, raw, broadcaster::send)) { | ||
if (!JsonMessage.on(SenderData.class, raw, broadcaster::send)) { | ||
JsonMessage.on(ConnectionData.class, raw, broadcaster::send); | ||
} | ||
} | ||
}); | ||
return true; | ||
} | ||
|
||
public String[] getAddresses() { | ||
return addressList.toArray(String[]::new); | ||
} | ||
|
||
public Broadcaster() { | ||
this(null); | ||
} | ||
|
||
public Broadcaster(Configuration config) { | ||
this.config = config; | ||
this.addressList = new HashSet<>(); | ||
} | ||
|
||
private void send(JsonMessage msg) { | ||
addressList.forEach(addr -> vertx.eventBus().publish(addr, msg.json())); | ||
} | ||
|
||
private Configuration config; | ||
private final Set<String> addressList; | ||
|
||
private static final Map<Integer, Broadcaster> broadcasterMap = new HashMap<>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...estion-services/src/main/java/de/wuespace/telestion/services/connection/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* <h2>Connection API</h2> | ||
* | ||
* <h3>Short description:</h3> | ||
* <p> | ||
* This module contains the connection api.<br> | ||
* It provides a simplified interface for the most commonly used network interfaces in ground-stations and is | ||
* optimized for the VertX framework. | ||
* </p> | ||
* | ||
* <h3><a id="Supported network interfaces"/>Supported network interfaces:</h3> | ||
* <ul> | ||
* <li>TCP</li> | ||
* <li>UDP</li> | ||
* <li>Serial connection (e.g. UART)</li> | ||
* </ul> | ||
* | ||
* <h3><a id="General Concepts"/>General Concepts:</h3> | ||
* <p> | ||
* The general concept of the connection api is to allow verticles to implement networking code without knowing the | ||
* specifics of the interface itself. This is achieved by the following concepts: | ||
* </p> | ||
* <ul> | ||
* <li>Sender-Api</li> | ||
* <li>Receiver-Api</li> | ||
* <li>Static-Sending</li> | ||
* <li>Special Concepts</li> | ||
* <li>Manual Mode</li> | ||
* </ul> | ||
* | ||
* <h4><a id="Sender-Api"/>{@link de.wuespace.telestion.services.connection.Sender Sender-Api}:</h4> | ||
* <p> | ||
* {@link de.wuespace.telestion.services.connection.ConnectionData} or | ||
* {@link de.wuespace.telestion.services.connection.SenderData} sent to this class will be rerouted to the | ||
* designated network interface or their dispatchers. If a connection is not, yet, established the default | ||
* behaviour is to try to create a new connection to the target. | ||
* </p> | ||
* <p> | ||
* <em> | ||
* Note:<br> | ||
* This can fail and the package can be dropped if the targeted network interface controller does not support | ||
* informing about failed packet delivery! | ||
* </em> | ||
* </p> | ||
* | ||
* <h4><a id="Receiver-Api"/>{@link de.wuespace.telestion.services.connection.Receiver Receiver-Api}:</h4> | ||
* <p> | ||
* If this interface is used all incoming {@link de.wuespace.telestion.services.connection.ConnectionData packages} | ||
* will be routed to its output address.<br> | ||
* This allows parsers to listen on multiple network connections at once. Answers then can be sent to the | ||
* <a href="#Sender-Api">Sender-Api</a> again. | ||
* </p> | ||
* | ||
* <h4><a id="Static-Sending"/>{@link de.wuespace.telestion.services.connection.StaticSender Static-Sending}:</h4> | ||
* <p> | ||
* In cases where the receiver is already known at start-up, the | ||
* {@link de.wuespace.telestion.services.connection.StaticSender} which can be fed with | ||
* {@link de.wuespace.telestion.services.connection.RawMessage RawMessages} which do not require network interface | ||
* information.<br> | ||
* The static sender will automatically send the packages to the connection specified in its config. | ||
* </p> | ||
* | ||
* <h4><a id="Special Concepts"/>Special Concepts:</h4> | ||
* <p> | ||
* There are more complex concepts than the previously described ones. This allows for more advanced or network | ||
* specific structures. | ||
* </p> | ||
* <h5>Connection established:</h5> | ||
* <p> | ||
* There are cases when one needs to know when a new connection has been established. In this case the XXXX will | ||
* yield the {@link de.wuespace.telestion.services.connection.ConnectionDetails} specifying the connection. | ||
* </p> | ||
* | ||
* <h5>{@link de.wuespace.telestion.services.connection.Broadcaster Broadcasting}:</h5> | ||
* <p> | ||
* To send information to all active network interfaces (which support broadcasting), a | ||
* {@link de.wuespace.telestion.services.connection.Broadcaster} verticle needs to be added. | ||
* </p> | ||
* | ||
* <h4><a id="Manual Mode"/>Manual Mode:</h4> | ||
* <p> | ||
* In rare cases where the <a href="#General Concepts">previously described abstractions</a> cannot be applied, | ||
* there is still the possibility to use the <a href="#Supported network interfaces">network interfaces</a> | ||
* directly. For this refer to the package descriptions of the designated packages. | ||
* </p> | ||
* | ||
* @since 0.2.0 | ||
* @version 1.0 | ||
* @author Cedric Boes (cb0s) | ||
*/ | ||
package de.wuespace.telestion.services.connection; |