Fixed @tommi27 synchronization frenzy for sensors

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](dd0933def0)
```
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
```
This commit is contained in:
britea 2020-03-25 17:10:27 +01:00
parent 03bbf6b824
commit 60c17b3dd8

View file

@ -8,6 +8,8 @@ import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps; import com.google.common.collect.Multimaps;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.io.IOException;
import java.util.*; import java.util.*;
import javax.websocket.*; import javax.websocket.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -57,19 +59,29 @@ public class SensorSocketEndpoint extends Endpoint {
* @param u the user to which to send the message * @param u the user to which to send the message
* @return number of successful transfer * @return number of successful transfer
*/ */
public long broadcast(Object message, User u) { public void broadcast(Object message, User u) {
final HashSet<Session> sessions = new HashSet<>(authorizedClients.get(u)); final HashSet<Session> sessions = new HashSet<>(authorizedClients.get(u));
return sessions.stream()
sessions.stream()
.filter( .filter(
s -> { s -> {
if (s.isOpen()) return true; if (s.isOpen()) return true;
sessions.remove(s); sessions.remove(s);
return false; return false;
}) })
.filter(didThrow(s -> s.getBasicRemote().sendText(gson.toJson(message)))) .forEach(s -> {
.count(); synchronized (this){
try {
s.getBasicRemote().sendText(gson.toJson(message));
} catch (IOException e) {
e.printStackTrace();
} }
}
});
}
/** /**
* Handles the opening of a socket session with a client * Handles the opening of a socket session with a client