64 lines
4.0 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="de"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>EventBus.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">backend</a> &gt; <a href="index.source.html" class="el_package">com.workbenchclassic</a> &gt; <span class="el_source">EventBus.java</span></div><h1>EventBus.java</h1><pre class="source lang-java linenums">package com.workbenchclassic;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.json.JsonObject;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.sse.Sse;
import javax.ws.rs.sse.SseEventSink;
<span class="nc" id="L12">public class EventBus {</span>
<span class="nc" id="L14"> private static final Map&lt;String, Set&lt;Client&gt;&gt; subscribers =</span>
new ConcurrentHashMap&lt;&gt;();
public static void register(String machineID, SseEventSink sink, Sse sse) {
<span class="nc" id="L18"> subscribers</span>
<span class="nc" id="L19"> .computeIfAbsent(machineID, k -&gt; ConcurrentHashMap.newKeySet())</span>
<span class="nc" id="L20"> .add(new Client(sink, sse));</span>
<span class="nc" id="L21"> }</span>
public static void publish(String machineID, JsonObject message) {
<span class="nc" id="L24"> Set&lt;Client&gt; clients = subscribers.get(machineID);</span>
<span class="nc bnc" id="L25" title="All 2 branches missed."> if (clients == null) return;</span>
<span class="nc" id="L27"> clients.removeIf(client -&gt; {</span>
<span class="nc bnc" id="L28" title="All 2 branches missed."> if (client.sink.isClosed()) {</span>
<span class="nc" id="L29"> return true; // remove closed connection</span>
}
try {
<span class="nc" id="L33"> client.sink.send(</span>
<span class="nc" id="L34"> client.sse.newEventBuilder()</span>
<span class="nc" id="L35"> .mediaType(MediaType.APPLICATION_JSON_TYPE)</span>
<span class="nc" id="L36"> .data(JsonObject.class, message)</span>
<span class="nc" id="L37"> .build()</span>
);
<span class="nc" id="L39"> return false;</span>
<span class="nc" id="L40"> } catch (Exception e) {</span>
// Verbindung tot → entfernen
try {
<span class="nc" id="L43"> client.sink.close();</span>
<span class="nc" id="L44"> } catch (Exception ignore) {}</span>
<span class="nc" id="L45"> return true;</span>
}
});
<span class="nc bnc" id="L49" title="All 2 branches missed."> if (clients.isEmpty()) {</span>
<span class="nc" id="L50"> subscribers.remove(machineID);</span>
}
<span class="nc" id="L52"> }</span>
private static class Client {
final SseEventSink sink;
final Sse sse;
<span class="nc" id="L58"> Client(SseEventSink sink, Sse sse) {</span>
<span class="nc" id="L59"> this.sink = sink;</span>
<span class="nc" id="L60"> this.sse = sse;</span>
<span class="nc" id="L61"> }</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.4.201905082037</span></div></body></html>