Compare commits
No commits in common. "8a9b9f9dbad8c2e3991eb9b0f11f73945fae361b" and "8c1c87af603e0de2b9c2307f894c6d46573ba3d3" have entirely different histories.
8a9b9f9dba
...
8c1c87af60
Binary file not shown.
|
|
@ -0,0 +1,12 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Markdown personal settings
|
||||
/markdown.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Ignored default folder with query files
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="vs.pu02" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/vs.pu02/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/vs.pu02/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="Eclipse Paho Repo" />
|
||||
<option name="name" value="Eclipse Paho Repo" />
|
||||
<option name="url" value="https://repo.eclipse.org/content/repositories/paho-releases/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/vs.pu02/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="temurin-25 (2)" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/VS-Aufgabe-2.iml" filepath="$PROJECT_DIR$/.idea/VS-Aufgabe-2.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -4,38 +4,42 @@
|
|||
<artifactId>vs.pu02</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>Eclipse Paho Repo</id>
|
||||
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<version>5.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.websocket</groupId>
|
||||
<artifactId>jakarta.websocket-api</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- NEU: MQTT-Client, kommt mit ins WAR -->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
<!-- Ende NEU -->
|
||||
<dependency><!-- für Unit-Tests -->
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<version>5.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.websocket</groupId>
|
||||
<artifactId>jakarta.websocket-api</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency><!-- für Unit-Tests -->
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency> <!--Paho-->
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
package vs;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* helper class for RFC 5424 (https://datatracker.ietf.org/doc/html/rfc5424)
|
||||
|
|
@ -9,7 +8,7 @@ import java.io.Serializable;
|
|||
* @author Sandro Leuchter
|
||||
*
|
||||
*/
|
||||
public abstract class AsciiChars implements Serializable{
|
||||
public abstract class AsciiChars {
|
||||
private final String value;
|
||||
|
||||
public String value() {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package vs;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -13,8 +12,8 @@ import java.util.List;
|
|||
* @author Sandro Leuchter
|
||||
*
|
||||
*/
|
||||
public class StructuredData implements Serializable {
|
||||
static public class Element implements Serializable {
|
||||
public class StructuredData {
|
||||
static public class Element {
|
||||
private final String name;
|
||||
private List<Param> parameters;
|
||||
|
||||
|
|
@ -102,7 +101,7 @@ public class StructuredData implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
static public class Param implements Serializable {
|
||||
static public class Param {
|
||||
private final String name;
|
||||
// name: printable US-ASCII string ^[@=\]\"\s]+
|
||||
// "@" + private enterpise number "@\d+(\.\d+)*"
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ public class SyslogMessage implements Serializable {
|
|||
DEBUG;
|
||||
}
|
||||
|
||||
public static interface Message extends Serializable {
|
||||
public static interface Message {
|
||||
public Object message();
|
||||
|
||||
public int length();
|
||||
|
|
|
|||
|
|
@ -1,53 +0,0 @@
|
|||
package vs;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
|
||||
import vs.StructuredData.Element;
|
||||
import vs.SyslogMessage.Facility;
|
||||
import vs.SyslogMessage.Severity;
|
||||
import vs.SyslogMessage.TextMessage;
|
||||
|
||||
public class SyslogSender {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String[][] ziele = {
|
||||
{ "tcp://localhost:1883", "vs/a" },
|
||||
{ "tcp://localhost:1884", "vs/b" } };
|
||||
|
||||
for (String[] z : ziele) {
|
||||
MqttClient client = new MqttClient(z[0],
|
||||
MqttClient.generateClientId(), new MemoryPersistence());
|
||||
client.connect();
|
||||
|
||||
for (Severity sev : Severity.values()) {
|
||||
SyslogMessage msg = new SyslogMessage(
|
||||
Facility.SECURITY1, sev,
|
||||
new AsciiChars.L255("mymachine.example.com"),
|
||||
new AsciiChars.L048("su"),
|
||||
new AsciiChars.L128(""),
|
||||
new AsciiChars.L032("ID47"),
|
||||
new StructuredData().add(Element.newTimeQuality(true, true)),
|
||||
new TextMessage(sev + " über " + z[1]));
|
||||
client.publish(z[1], serialisieren(msg), 1, false);
|
||||
}
|
||||
client.publish(z[1], "nur Text".getBytes(), 1, false);
|
||||
System.out.println("gesendet an " + z[0] + " / " + z[1]);
|
||||
|
||||
client.disconnect();
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
static byte[] serialisieren(Object o) throws IOException {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
|
||||
out.writeObject(o);
|
||||
}
|
||||
return bos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
package vs.adapter;
|
||||
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.ServletContextEvent;
|
||||
import jakarta.servlet.ServletContextListener;
|
||||
import jakarta.servlet.annotation.WebListener;
|
||||
|
||||
@WebListener
|
||||
public class AdapterLifecycle implements ServletContextListener {
|
||||
|
||||
static final String ATTR = "mqttAdapter";
|
||||
|
||||
/** Holt den Adapter aus dem ServletContext. */
|
||||
static MqttAdapter adapter(ServletContext ctx) {
|
||||
return (MqttAdapter) ctx.getAttribute(ATTR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
MqttAdapter adapter = new MqttAdapter(SyslogEndpoint::broadcast);
|
||||
sce.getServletContext().setAttribute(ATTR, adapter);
|
||||
System.out.println("MqttAdapter erzeugt");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
MqttAdapter adapter = adapter(sce.getServletContext());
|
||||
if (adapter != null) {
|
||||
adapter.close();
|
||||
}
|
||||
System.out.println("MqttAdapter geschlossen");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
package vs.adapter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
@WebServlet("/config")
|
||||
public class ConfigServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws IOException {
|
||||
req.setCharacterEncoding("UTF-8");
|
||||
MqttAdapter adapter = AdapterLifecycle.adapter(getServletContext());
|
||||
StringBuilder status = new StringBuilder();
|
||||
|
||||
for (int slot = 0; slot < MqttAdapter.SLOTS; slot++) {
|
||||
String server = trim(req.getParameter("server" + (slot + 1)));
|
||||
String topic = trim(req.getParameter("topic" + (slot + 1)));
|
||||
|
||||
if (server.isEmpty()) {
|
||||
adapter.disconnect(slot);
|
||||
status.append("Broker ").append(slot + 1).append(": getrennt\n");
|
||||
continue;
|
||||
}
|
||||
if (!server.contains("://")) {
|
||||
server = "tcp://" + server; // "localhost:1883" -> "tcp://localhost:1883"
|
||||
}
|
||||
try {
|
||||
adapter.connect(slot, server, topic.isEmpty() ? "#" : topic);
|
||||
status.append("Broker ").append(slot + 1).append(": verbunden mit ")
|
||||
.append(server).append(" (").append(topic).append(")\n");
|
||||
} catch (Exception e) {
|
||||
status.append("Broker ").append(slot + 1).append(": FEHLER ")
|
||||
.append(e.getMessage()).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
resp.setContentType("text/plain; charset=UTF-8");
|
||||
resp.getWriter().print(status);
|
||||
}
|
||||
|
||||
private static String trim(String s) {
|
||||
return s == null ? "" : s.strip();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,87 +0,0 @@
|
|||
package vs.adapter;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
||||
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
|
||||
public class MqttAdapter {
|
||||
|
||||
public static final int SLOTS = 2;
|
||||
|
||||
private final MqttClient[] clients = new MqttClient[SLOTS];
|
||||
private final Consumer<String> ziel;
|
||||
|
||||
/** @param ziel bekommt den Text jeder weitergeleiteten Nachricht */
|
||||
public MqttAdapter(Consumer<String> ziel) {
|
||||
this.ziel = ziel;
|
||||
}
|
||||
|
||||
/** Verbindet Slot 0 oder 1 mit einem Broker und abonniert ein Topic. */
|
||||
public synchronized void connect(int slot, String server, String topic)
|
||||
throws MqttException {
|
||||
disconnect(slot); // alte Verbindung in diesem Slot vorher schließen
|
||||
|
||||
MqttClient client = new MqttClient(server,
|
||||
MqttClient.generateClientId(), new MemoryPersistence());
|
||||
|
||||
client.setCallback(new MqttCallback() {
|
||||
@Override
|
||||
public void messageArrived(String t, MqttMessage message) {
|
||||
verarbeite(message.getPayload());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectionLost(Throwable cause) {
|
||||
System.err.println("Slot " + slot + ": Verbindung verloren: " + cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken token) {
|
||||
}
|
||||
});
|
||||
|
||||
client.connect();
|
||||
client.subscribe(topic, 1);
|
||||
clients[slot] = client;
|
||||
}
|
||||
|
||||
/** Trennt die Verbindung in diesem Slot (falls vorhanden). */
|
||||
public synchronized void disconnect(int slot) {
|
||||
MqttClient client = clients[slot];
|
||||
clients[slot] = null;
|
||||
if (client == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (client.isConnected()) {
|
||||
client.disconnect();
|
||||
}
|
||||
client.close();
|
||||
} catch (MqttException e) {
|
||||
System.err.println("Fehler beim Trennen: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized boolean isConnected(int slot) {
|
||||
return clients[slot] != null && clients[slot].isConnected();
|
||||
}
|
||||
|
||||
/** Alle Verbindungen schließen (z.B. beim Herunterfahren). */
|
||||
public synchronized void close() {
|
||||
for (int i = 0; i < SLOTS; i++) {
|
||||
disconnect(i);
|
||||
}
|
||||
}
|
||||
|
||||
/** Auspacken, filtern, weitergeben. */
|
||||
void verarbeite(byte[] payload) {
|
||||
SyslogDecoder.decode(payload)
|
||||
.filter(SyslogDecoder::isRelevant)
|
||||
.ifPresent(m -> ziel.accept(m.toString()));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
package vs.adapter;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.util.Optional;
|
||||
|
||||
import vs.SyslogMessage;
|
||||
import vs.SyslogMessage.Severity;
|
||||
|
||||
public final class SyslogDecoder {
|
||||
|
||||
private SyslogDecoder() {
|
||||
// nur statische Methoden, keine Objekte nötig
|
||||
}
|
||||
|
||||
/** Bytes -> SyslogMessage, oder leer, wenn keine drinsteckt. */
|
||||
public static Optional<SyslogMessage> decode(byte[] payload) {
|
||||
if (payload == null || payload.length == 0) {
|
||||
return Optional.empty();
|
||||
}
|
||||
try (ObjectInputStream in = new ObjectInputStream(
|
||||
new ByteArrayInputStream(payload))) {
|
||||
Object o = in.readObject();
|
||||
if (o instanceof SyslogMessage m) {
|
||||
return Optional.of(m);
|
||||
}
|
||||
return Optional.empty();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Decode-Fehler: " + e);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
/** true für EMERGENCY, ALERT, CRITICAL, ERROR, WARNING */
|
||||
public static boolean isRelevant(SyslogMessage m) {
|
||||
return m.sev() != null
|
||||
&& m.sev().ordinal() <= Severity.WARNING.ordinal();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
package vs.adapter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import jakarta.websocket.OnClose;
|
||||
import jakarta.websocket.OnError;
|
||||
import jakarta.websocket.OnOpen;
|
||||
import jakarta.websocket.Session;
|
||||
import jakarta.websocket.server.ServerEndpoint;
|
||||
|
||||
@ServerEndpoint("/syslog")
|
||||
public class SyslogEndpoint {
|
||||
|
||||
// alle offenen Browser-Verbindungen (für alle Endpoint-Objekte gemeinsam)
|
||||
private static final Set<Session> SESSIONS = new CopyOnWriteArraySet<>();
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session session) {
|
||||
SESSIONS.add(session);
|
||||
System.out.println("WebSocket geöffnet, jetzt " + SESSIONS.size() + " Clients");
|
||||
}
|
||||
|
||||
@OnClose
|
||||
public void onClose(Session session) {
|
||||
SESSIONS.remove(session);
|
||||
}
|
||||
|
||||
@OnError
|
||||
public void onError(Session session, Throwable t) {
|
||||
SESSIONS.remove(session);
|
||||
}
|
||||
|
||||
/** Schickt den Text an alle verbundenen Browser. */
|
||||
public static void broadcast(String text) {
|
||||
for (Session s : SESSIONS) {
|
||||
synchronized (s) {
|
||||
try {
|
||||
if (s.isOpen()) {
|
||||
s.getBasicRemote().sendText(text);
|
||||
}
|
||||
} catch (IOException | IllegalStateException e) {
|
||||
SESSIONS.remove(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Syslog-Adapter</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Syslog-Adapter: MQTT → WebSocket</h1>
|
||||
|
||||
<h2>MQTT-Broker</h2>
|
||||
<form id="cfg">
|
||||
<p>Broker 1:
|
||||
<input name="server1" placeholder="tcp://localhost:1883" size="30">
|
||||
Topic: <input name="topic1" placeholder="vs/a">
|
||||
</p>
|
||||
<p>Broker 2:
|
||||
<input name="server2" placeholder="tcp://localhost:1884" size="30">
|
||||
Topic: <input name="topic2" placeholder="vs/b">
|
||||
</p>
|
||||
<button type="submit">Verbinden</button>
|
||||
</form>
|
||||
<pre id="cfgStatus"></pre>
|
||||
|
||||
<h2>Empfangene Syslog-Nachrichten (≥ WARNING)</h2>
|
||||
<p id="status">Websocket: verbinde …</p>
|
||||
<ul id="log"></ul>
|
||||
|
||||
<script>
|
||||
// ---------- Formular ----------
|
||||
document.getElementById('cfg').addEventListener('submit', async (e) => {
|
||||
e.preventDefault(); // Seite NICHT neu laden, sonst bricht der WebSocket ab
|
||||
const daten = new URLSearchParams(new FormData(e.target));
|
||||
document.getElementById('cfgStatus').textContent = 'verbinde …';
|
||||
const antwort = await fetch('config', { method: 'POST', body: daten });
|
||||
document.getElementById('cfgStatus').textContent = await antwort.text();
|
||||
});
|
||||
|
||||
// ---------- WebSocket-Client ----------
|
||||
const url = new URL('syslog', location.href);
|
||||
url.protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const ws = new WebSocket(url);
|
||||
|
||||
ws.onopen = () => document.getElementById('status').textContent = 'Websocket: verbunden';
|
||||
ws.onclose = () => document.getElementById('status').textContent = 'Websocket: getrennt';
|
||||
ws.onmessage = (event) => {
|
||||
const li = document.createElement('li');
|
||||
li.textContent = event.data;
|
||||
document.getElementById('log').prepend(li);
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
package vs;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
||||
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import vs.SyslogMessage.Severity;
|
||||
|
||||
public class MqttEmpfaenger {
|
||||
|
||||
public static void main(String[] args) throws MqttException {
|
||||
String broker = "tcp://localhost:1883";
|
||||
String topic = "vs/test";
|
||||
|
||||
// 1. Client-Objekt erzeugen
|
||||
MqttClient client = new MqttClient(
|
||||
broker,
|
||||
MqttClient.generateClientId(),
|
||||
new MemoryPersistence());
|
||||
|
||||
// 2. Festlegen, was bei eingehenden Nachrichten passiert
|
||||
client.setCallback(new MqttCallback() {
|
||||
@Override
|
||||
public void messageArrived(String t, MqttMessage message) {
|
||||
byte[] payload = message.getPayload();
|
||||
try (ObjectInputStream in = new ObjectInputStream(
|
||||
new ByteArrayInputStream(payload))) {
|
||||
|
||||
Object o = in.readObject();
|
||||
|
||||
if (o instanceof SyslogMessage m) {
|
||||
if (m.sev().ordinal() <= Severity.WARNING.ordinal()) {
|
||||
System.out.println("WEITERLEITEN: " + m);
|
||||
} else {
|
||||
System.out.println("ignoriert (zu unwichtig): " + m.sev());
|
||||
}
|
||||
} else {
|
||||
System.out.println("ignoriert: Objekt, aber keine SyslogMessage");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("ignoriert: kein serialisiertes Objekt ("
|
||||
+ e.getClass().getSimpleName() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectionLost(Throwable cause) {
|
||||
System.out.println("Verbindung verloren: " + cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken token) {
|
||||
// nur relevant, wenn wir selbst senden
|
||||
}
|
||||
});
|
||||
|
||||
// 3. Verbinden und abonnieren
|
||||
client.connect();
|
||||
client.subscribe(topic);
|
||||
System.out.println("Verbunden mit " + broker + ", warte auf " + topic + " ...");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
package vs;
|
||||
|
||||
import vs.adapter.MqttAdapter;
|
||||
|
||||
public class ZweiBrokerTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
MqttAdapter adapter = new MqttAdapter(text -> System.out.println("→ " + text));
|
||||
|
||||
adapter.connect(0, "tcp://localhost:1883", "vs/a");
|
||||
adapter.connect(1, "tcp://localhost:1884", "vs/b");
|
||||
System.out.println("Slot 0 verbunden: " + adapter.isConnected(0));
|
||||
System.out.println("Slot 1 verbunden: " + adapter.isConnected(1));
|
||||
System.out.println("Warte 60 Sekunden auf Nachrichten ...");
|
||||
|
||||
Thread.sleep(60_000);
|
||||
adapter.close();
|
||||
System.out.println("Beendet.");
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,17 @@
|
|||
vs\AsciiChars$L004.class
|
||||
vs\AsciiChars$L048.class
|
||||
vs\AsciiChars$L128.class
|
||||
vs\SyslogMessage$BinaryMessage.class
|
||||
vs\SyslogMessage$Message.class
|
||||
vs\AsciiChars$L032.class
|
||||
vs\SyslogMessage$Severity.class
|
||||
vs\StructuredData$Param.class
|
||||
vs\StructuredData$Element.class
|
||||
vs\AsciiChars$L255.class
|
||||
vs\AsciiChars$L012.class
|
||||
vs\SyslogMessage$Facility.class
|
||||
vs\AsciiChars.class
|
||||
vs\StructuredData.class
|
||||
vs\SyslogMessage.class
|
||||
vs\SyslogMessage$TextMessage.class
|
||||
vs\MainServlet.class
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
C:\Users\leonl\IdeaProjects\VS-Aufgabe-2\vs.pu02\src\main\java\vs\MainServlet.java
|
||||
C:\Users\leonl\IdeaProjects\VS-Aufgabe-2\vs.pu02\src\main\java\vs\AsciiChars.java
|
||||
C:\Users\leonl\IdeaProjects\VS-Aufgabe-2\vs.pu02\src\main\java\vs\StructuredData.java
|
||||
C:\Users\leonl\IdeaProjects\VS-Aufgabe-2\vs.pu02\src\main\java\vs\SyslogMessage.java
|
||||
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,5 @@
|
|||
Manifest-Version: 1.0
|
||||
Created-By: IntelliJ IDEA
|
||||
Built-By: leonl
|
||||
Build-Jdk: Eclipse Temurin 25.0.5
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue