Skip to content

Commit

Permalink
change WebSocketMessage data type
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Mar 29, 2024
1 parent c173beb commit f6aab9f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
19 changes: 4 additions & 15 deletions src/main/java/telraam/logic/positioner/PositionSender.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
package telraam.logic.positioner;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.NoArgsConstructor;
import telraam.websocket.WebSocketMessage;
import telraam.websocket.WebSocketMessageSingleton;

import java.util.List;
import java.util.logging.Logger;

public class PositionSender {
private static final Logger logger = Logger.getLogger(PositionSender.class.getName());
private final ObjectMapper mapper = new ObjectMapper();
private final WebSocketMessage message = new WebSocketMessage();
private final WebSocketMessage<List<Position>> message = new WebSocketMessage<>();

public PositionSender() {
this.message.setTopic("position");
}

public void send(List<Position> position) {
try {
String json = mapper.writeValueAsString(position);
this.message.setData(json);
WebSocketMessageSingleton.getInstance().sendToAll(this.message);
} catch (JsonProcessingException e) {
logger.severe("Json conversion error for \"%s\"".formatted(position.toString()));
}
public void send(List<Position> positions) {
this.message.setData(positions);
WebSocketMessageSingleton.getInstance().sendToAll(this.message);
}

}
4 changes: 2 additions & 2 deletions src/main/java/telraam/websocket/WebSocketMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Setter;

@Getter @Setter
public class WebSocketMessage {
public class WebSocketMessage<T> {
private String topic;
private String data;
private T data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class WebSocketMessageSingleton {
@Getter
private static final WebSocketMessageSingleton instance = new WebSocketMessageSingleton();
private static final Set<WebSocketConnection> registeredConnections = new HashSet<>();
private ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = new ObjectMapper();

public void registerConnection(WebSocketConnection conn) {
boolean modified = registeredConnections.add(conn);
Expand All @@ -39,7 +39,7 @@ public void sendToAll(String s) {
registeredConnections.forEach(conn -> conn.send(s));
}

public void sendToAll(WebSocketMessage message) {
public void sendToAll(WebSocketMessage<?> message) {
try {
String json = mapper.writeValueAsString(message);
this.sendToAll(json);
Expand Down

0 comments on commit f6aab9f

Please sign in to comment.