From 60c17b3dd8594541d2e71e3c36ae6efaae098bfb Mon Sep 17 00:00:00 2001 From: britea Date: Wed, 25 Mar 2020 17:10:27 +0100 Subject: [PATCH] Fixed @tommi27 synchronization frenzy for sensors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I have now discovered the Schadenfreude in using git-blame. To quote an old commit message from high school: [Trascrizione domande fisica e correzione di GRAVISSIMI bug presenti nel codici di Maggioni](https://gitlab.com/staccastacca/scuolatest/-/commit/dd0933def0262ce1ca2f2b006d4f7a364fb17272) ``` La trascrizione dei test è stata fatta in modo certosino, simile a quello dei monaci emanuensi. I GRAVISSIMI buggg (3 g perchè fa faigo) potevano rendere il sito incomprensibile ``` --- .../smarthut/socket/SensorSocketEndpoint.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) 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 index 1735b86..2801a74 100644 --- 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 @@ -8,6 +8,8 @@ import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import com.google.gson.Gson; + +import java.io.IOException; import java.util.*; import javax.websocket.*; import org.springframework.beans.factory.annotation.Autowired; @@ -57,20 +59,30 @@ public class SensorSocketEndpoint extends Endpoint { * @param u the user to which to send the message * @return number of successful transfer */ - public long broadcast(Object message, User u) { + public void broadcast(Object message, User u) { final HashSet sessions = new HashSet<>(authorizedClients.get(u)); - return sessions.stream() + + + sessions.stream() .filter( s -> { if (s.isOpen()) return true; sessions.remove(s); return false; }) - .filter(didThrow(s -> s.getBasicRemote().sendText(gson.toJson(message)))) - .count(); + .forEach(s -> { + synchronized (this){ + try { + s.getBasicRemote().sendText(gson.toJson(message)); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); } + /** * Handles the opening of a socket session with a client *