Skip to content

Commit

Permalink
feat(connection): Add Broadcaster verticle and improve java docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cb0s committed Nov 16, 2021
1 parent 39f3748 commit ad3cad3
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 4 deletions.
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<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* <p>
* Classes from the old connection api.<br>
* The old connection api contains the following network interfaces:
* <ul>
* <li>Serial ({@link de.wuespace.telestion.services.connection.legacy.SerialConn})</li>
* <li>Tcp ({@link de.wuespace.telestion.services.connection.legacy.TcpConn})</li>
* </ul>
* </p>
* <ul>
* <li>Serial ({@link de.wuespace.telestion.services.connection.legacy.SerialConn})</li>
* <li>Tcp ({@link de.wuespace.telestion.services.connection.legacy.TcpConn})</li>
* </ul>
* <p>
* <em>Disclaimer:<br>
* Classes within this package are all marked as deprecated and will be removed soon.
Expand Down
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;

0 comments on commit ad3cad3

Please sign in to comment.