Skip to content

Commit 87fcf04

Browse files
committed
feat(connection): Add Broadcaster verticle and improve java docs
1 parent 39f3748 commit 87fcf04

File tree

3 files changed

+179
-4
lines changed

3 files changed

+179
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package de.wuespace.telestion.services.connection;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import de.wuespace.telestion.api.config.Config;
5+
import de.wuespace.telestion.api.message.JsonMessage;
6+
import io.vertx.core.AbstractVerticle;
7+
import io.vertx.core.Promise;
8+
9+
import java.util.HashMap;
10+
import java.util.HashSet;
11+
import java.util.Map;
12+
import java.util.Set;
13+
14+
public class Broadcaster extends AbstractVerticle {
15+
16+
public final static int DEFAULT_ID = 0;
17+
18+
public final record Configuration(@JsonProperty
19+
String inAddress,
20+
@JsonProperty
21+
int id) implements JsonMessage {
22+
public Configuration() {
23+
this(null, DEFAULT_ID);
24+
}
25+
}
26+
27+
@Override
28+
public void start(Promise<Void> startPromise) {
29+
this.config = Config.get(this.config, new Configuration(), this.config(), Configuration.class);
30+
31+
if (broadcasterMap.containsKey(this.config.id())) {
32+
startPromise.fail("The broadcasters id #%d is already taken!".formatted(this.config.id()));
33+
return;
34+
}
35+
36+
startPromise.complete();
37+
}
38+
39+
@Override
40+
public void stop(Promise<Void> stopPromise) {
41+
stopPromise.complete();
42+
}
43+
44+
public static boolean register(int broadcasterId, String address) {
45+
if (!broadcasterMap.containsKey(broadcasterId)) {
46+
return false;
47+
}
48+
49+
var broadcaster = broadcasterMap.get(broadcasterId);
50+
broadcaster.addressList.add(address);
51+
52+
broadcaster.getVertx().eventBus()
53+
.consumer(broadcaster.config.inAddress(), raw -> {
54+
if (!JsonMessage.on(RawMessage.class, raw, broadcaster::send)) {
55+
if (!JsonMessage.on(SenderData.class, raw, broadcaster::send)) {
56+
JsonMessage.on(ConnectionData.class, raw, broadcaster::send);
57+
}
58+
}
59+
});
60+
return true;
61+
}
62+
63+
public String[] getAddresses() {
64+
return addressList.toArray(String[]::new);
65+
}
66+
67+
public Broadcaster() {
68+
this(null);
69+
}
70+
71+
public Broadcaster(Configuration config) {
72+
this.config = config;
73+
this.addressList = new HashSet<>();
74+
}
75+
76+
private void send(JsonMessage msg) {
77+
addressList.forEach(addr -> vertx.eventBus().publish(addr, msg.json()));
78+
}
79+
80+
private Configuration config;
81+
private final Set<String> addressList;
82+
83+
private static final Map<Integer, Broadcaster> broadcasterMap = new HashMap<>();
84+
}

modules/telestion-services/src/main/java/de/wuespace/telestion/services/connection/legacy/package-info.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* <p>
44
* Classes from the old connection api.<br>
55
* The old connection api contains the following network interfaces:
6-
* <ul>
7-
* <li>Serial ({@link de.wuespace.telestion.services.connection.legacy.SerialConn})</li>
8-
* <li>Tcp ({@link de.wuespace.telestion.services.connection.legacy.TcpConn})</li>
9-
* </ul>
106
* </p>
7+
* <ul>
8+
* <li>Serial ({@link de.wuespace.telestion.services.connection.legacy.SerialConn})</li>
9+
* <li>Tcp ({@link de.wuespace.telestion.services.connection.legacy.TcpConn})</li>
10+
* </ul>
1111
* <p>
1212
* <em>Disclaimer:<br>
1313
* Classes within this package are all marked as deprecated and will be removed soon.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* <h2>Connection API</h2>
3+
*
4+
* <h3>Short description:</h3>
5+
* <p>
6+
* This module contains the connection api.<br>
7+
* It provides a simplified interface for the most commonly used network interfaces in ground-stations and is
8+
* optimized for the VertX framework.
9+
* </p>
10+
*
11+
* <h3>Supported network interfaces:</h3>
12+
* <ul>
13+
* <li>TCP</li>
14+
* <li>UDP</li>
15+
* <li>Serial connection (e.g. UART)</li>
16+
* </ul>
17+
*
18+
* <h3>General Concepts:</h3>
19+
* <p>
20+
* The general concept of the connection api is to allow verticles to implement networking code without knowing the
21+
* specifics of the interface itself. This is achieved by the following concepts:
22+
* </p>
23+
* <ul>
24+
* <li>Sender-Api</li>
25+
* <li>Receiver-Api</li>
26+
* <li>Static-Sending</li>
27+
* <li>Special Concepts</li>
28+
* <li>Manual Mode</li>
29+
* </ul>
30+
*
31+
* <h4>{@link de.wuespace.telestion.services.connection.Sender Sender-Api}:</h4>
32+
* <p>
33+
* {@link de.wuespace.telestion.services.connection.ConnectionData} or
34+
* {@link de.wuespace.telestion.services.connection.SenderData} sent to this class will be rerouted to the
35+
* designated network interface or their dispatchers. If a connection is not, yet, established the default
36+
* behaviour is to try to create a new connection to the target.
37+
* </p>
38+
* <p>
39+
* <em>
40+
* Note:<br>
41+
* This can fail and the package can be dropped if the targeted network interface controller does not support
42+
* informing about failed packet delivery!
43+
* </em>
44+
* </p>
45+
*
46+
* <h4>{@link de.wuespace.telestion.services.connection.Receiver Receiver-Api}:</h4>
47+
* <p>
48+
* If this interface is used all incoming {@link de.wuespace.telestion.services.connection.ConnectionData packages}
49+
* will be routed to its output address.<br>
50+
* This allows parsers to listen on multiple network connections at once. Answers then can be sent to the
51+
* Sender-Api again.
52+
* </p>
53+
*
54+
* <h4>{@link de.wuespace.telestion.services.connection.StaticSender Static-Sending}:</h4>
55+
* <p>
56+
* In cases where the receiver is already known at start-up, the
57+
* {@link de.wuespace.telestion.services.connection.StaticSender} which can be fed with
58+
* {@link de.wuespace.telestion.services.connection.RawMessage RawMessages} which do not require network interface
59+
* information.<br>
60+
* The static sender will automatically send the packages to the connection specified in its config.
61+
* </p>
62+
*
63+
* <h4>Special Concepts:</h4>
64+
* <p>
65+
* There are more complex concepts than the previously described ones. This allows for more advanced or network
66+
* specific structures.
67+
* </p>
68+
* <h5>Connection established:</h5>
69+
* <p>
70+
* There are cases when one needs to know when a new connection has been established. In this case the XXXX will
71+
* yield the {@link de.wuespace.telestion.services.connection.ConnectionDetails} specifying the connection.
72+
* </p>
73+
*
74+
* <h5>{@link de.wuespace.telestion.services.connection.Broadcaster Broadcasting}:</h5>
75+
* <p>
76+
* To send information to all active network interfaces (which support broadcasting), a
77+
* {@link de.wuespace.telestion.services.connection.Broadcaster} verticle needs to be added.
78+
* </p>
79+
*
80+
* <h4>Manual Mode:</h4>
81+
* <p>
82+
* In rare cases where the previously described abstractions cannot be applied,
83+
* there is still the possibility to use the network interfaces directly. For this refer to the package
84+
* descriptions of the designated packages.
85+
* </p>
86+
*
87+
* @since 0.2.0
88+
* @version 1.0
89+
* @author Cedric Boes (cb0s)
90+
*/
91+
package de.wuespace.telestion.services.connection;

0 commit comments

Comments
 (0)