Skip to content

Commit

Permalink
feat(connection): Replace Tuple with IpDetails which is optimized for…
Browse files Browse the repository at this point in the history
… this specific usecase
  • Loading branch information
cb0s committed Nov 16, 2021
1 parent eb4b258 commit 39f3748
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.wuespace.telestion.services.connection;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.wuespace.telestion.api.message.JsonMessage;

public record IpDetails(@JsonProperty
String ip,
@JsonProperty
int port) implements JsonMessage {
public IpDetails() {
this("0.0.0.0", 0);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import de.wuespace.telestion.api.config.Config;
import de.wuespace.telestion.api.message.JsonMessage;
import de.wuespace.telestion.services.connection.ConnectionData;
import de.wuespace.telestion.services.connection.Tuple;
import de.wuespace.telestion.services.connection.IpDetails;
import io.reactivex.annotations.NonNull;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.CompositeFuture;
Expand Down Expand Up @@ -81,7 +81,7 @@ public TcpClient(Configuration config) {

private void handleDispatchedMsg(TcpData tcpData) {
var details = tcpData.details();
var key = new Tuple<>(details.ip(), details.port());
var key = new IpDetails(details.ip(), details.port());

// Checks if socket already exists.
// If not, connects to Server and if successful sends data asynchronously.
Expand Down Expand Up @@ -110,7 +110,7 @@ private void handleDispatchedMsg(TcpData tcpData) {

logger.debug("Sending data to {}:{}", details.ip(), details.port());
activeClients
.get(new Tuple<>(details.ip(), details.port()))
.get(new IpDetails(details.ip(), details.port()))
.write(Buffer.buffer(tcpData.data()));
});
}
Expand All @@ -123,7 +123,7 @@ private void handleDispatchedMsg(TcpData tcpData) {
private void onConnected(NetSocket socket) {
var ip = socket.remoteAddress().host();
var port = socket.remoteAddress().port();
var key = new Tuple<>(ip, port);
var key = new IpDetails(ip, port);

activeClients.put(key, socket);
logger.info("Connection established ({}:{})", ip, port);
Expand Down Expand Up @@ -161,7 +161,7 @@ private void handleMsg(ConnectionData data) {
}

private Configuration config;
private Map<Tuple<String, Integer>, NetSocket> activeClients;
private Map<IpDetails, NetSocket> activeClients;
private NetClient currentClient;

private static final Logger logger = LoggerFactory.getLogger(TcpClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import de.wuespace.telestion.api.config.Config;
import de.wuespace.telestion.api.message.JsonMessage;
import de.wuespace.telestion.services.connection.ConnectionData;
import de.wuespace.telestion.services.connection.IpDetails;
import de.wuespace.telestion.services.connection.SenderData;
import de.wuespace.telestion.services.connection.Tuple;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;

Expand Down Expand Up @@ -59,7 +59,7 @@ public TcpDispatcher(Configuration config, TcpServer... servers) {

private void handle(byte[] bytes, TcpDetails details) {
for (var server : servers) {
if (server.isActiveCon(new Tuple<>(details.ip(), details.port()))) {
if (server.isActiveCon(new IpDetails(details.ip(), details.port()))) {
vertx.eventBus().publish(server.getConfig().inAddress(),
new TcpData(bytes, details).json());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import de.wuespace.telestion.api.config.Config;
import de.wuespace.telestion.api.message.JsonMessage;
import de.wuespace.telestion.services.connection.ConnectionData;
import de.wuespace.telestion.services.connection.Tuple;
import de.wuespace.telestion.services.connection.IpDetails;
import io.reactivex.annotations.NonNull;
import io.vertx.core.*;
import io.vertx.core.buffer.Buffer;
Expand Down Expand Up @@ -132,14 +132,14 @@ public Configuration getConfig() {
}

// Public for own Dispatcher implementations
public boolean isActiveCon(Tuple<String, Integer> key) {
public boolean isActiveCon(IpDetails key) {
return activeCons.containsKey(key);
}

private void onConnected(NetSocket netSocket) {
var ip = netSocket.remoteAddress().hostAddress();
var port = netSocket.remoteAddress().port();
var key = new Tuple<>(ip, port);
var key = new IpDetails(ip, port);

logger.info("Connection established with {}:{}", ip, port);
activeCons.put(key, netSocket);
Expand Down Expand Up @@ -171,7 +171,7 @@ private void onConnected(NetSocket netSocket) {
private void handleDispatchedMsg(TcpData data) {
var details = data.details();

var element = activeCons.get(new Tuple<>(details.ip(), details.port()));
var element = activeCons.get(new IpDetails(details.ip(), details.port()));

// Might be useful to send this to the TCP-Client however the config must be varied for this (future update?)
if (element == null) {
Expand Down Expand Up @@ -203,7 +203,7 @@ private void handleMsg(ConnectionData data) {

private Configuration config;
private NetServer server;
private Map<Tuple<String, Integer>, NetSocket> activeCons;
private Map<IpDetails, NetSocket> activeCons;

private static final Logger logger = LoggerFactory.getLogger(TcpServer.class);
}

0 comments on commit 39f3748

Please sign in to comment.