diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java new file mode 100644 index 0000000..7d04538 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java @@ -0,0 +1,26 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.socket; + +import javax.websocket.server.ServerEndpointConfig; + +public class SensorSocketConfig extends ServerEndpointConfig.Configurator { + + public static SensorSocketEndpoint getInstance() { + return instance; + } + + private static SensorSocketEndpoint instance = new SensorSocketEndpoint(); + + @Override + public T getEndpointInstance(Class endpointClass) throws InstantiationException { + try { + @SuppressWarnings("unchecked") + final T instance = (T) SensorSocketConfig.instance; + return instance; + } catch (ClassCastException e) { + final var e2 = + new InstantiationException("Cannot cast SensorSocketEndpoint to desired type"); + e2.initCause(e); + throw e2; + } + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketDecoder.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketDecoder.java new file mode 100644 index 0000000..ad3a76e --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketDecoder.java @@ -0,0 +1,32 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.socket; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import javax.websocket.*; + +public class SensorSocketDecoder implements Decoder.Text { + private Gson decoder; + + @Override + public void init(EndpointConfig endpointConfig) { + decoder = new Gson(); + } + + @Override + public void destroy() {} + + @Override + public JsonObject decode(String s) throws DecodeException { + try { + return decoder.fromJson(s, JsonObject.class); + } catch (JsonSyntaxException e) { + throw new DecodeException(s, "Cannot decode sensor message", e); + } + } + + @Override + public boolean willDecode(String s) { + return true; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEncoder.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEncoder.java new file mode 100644 index 0000000..be04293 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEncoder.java @@ -0,0 +1,24 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.socket; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import javax.websocket.EncodeException; +import javax.websocket.Encoder; +import javax.websocket.EndpointConfig; + +public class SensorSocketEncoder implements Encoder.Text { + private Gson encoder; + + @Override + public String encode(JsonObject object) throws EncodeException { + return encoder.toJson(object); + } + + @Override + public void init(EndpointConfig endpointConfig) { + encoder = new Gson(); + } + + @Override + public void destroy() {} +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java new file mode 100644 index 0000000..82ab767 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java @@ -0,0 +1,41 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.socket; + +import com.google.gson.JsonObject; +import java.io.IOException; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import javax.websocket.*; +import javax.websocket.server.ServerEndpoint; + +@ServerEndpoint( + value = "/service", + configurator = SensorSocketConfig.class, + encoders = SensorSocketEncoder.class, + decoders = SensorSocketDecoder.class) +public class SensorSocketEndpoint { + + private Set clients = Collections.synchronizedSet(new HashSet<>()); + + public Set getClients() { + return clients; + } + + @OnOpen + public void onOpen(Session userSession) { + clients.add(userSession); + } + + @OnClose + public void onClose(Session userSession) { + clients.remove(userSession); + } + + public int broadcast(JsonObject message) throws IOException, EncodeException { + for (Session session : clients) { + System.out.println(message); + session.getBasicRemote().sendObject(message); + } + return clients.size(); + } +}