2020-09-22 18:45:16 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 mark.nellemann@gmail.com
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2020-09-22 18:33:22 +00:00
|
|
|
package biz.nellemann.syslogd;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.DatagramPacket;
|
|
|
|
import java.net.DatagramSocket;
|
2020-10-14 06:39:29 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-09-22 18:33:22 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class UdpServer extends Thread {
|
|
|
|
|
2020-10-14 06:39:29 +00:00
|
|
|
protected DatagramSocket socket;
|
2020-09-22 18:33:22 +00:00
|
|
|
protected boolean listen = true;
|
|
|
|
|
|
|
|
public UdpServer() throws IOException {
|
|
|
|
this(514);
|
|
|
|
}
|
|
|
|
|
|
|
|
public UdpServer(int port) throws IOException {
|
|
|
|
super("SyslogServer");
|
|
|
|
socket = new DatagramSocket(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
2020-10-05 08:40:42 +00:00
|
|
|
byte[] buf = new byte[4096];
|
2020-09-22 18:33:22 +00:00
|
|
|
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
|
|
|
|
|
|
|
while (listen) {
|
|
|
|
try {
|
|
|
|
socket.receive(packet);
|
2020-10-14 06:39:29 +00:00
|
|
|
String packetData = new String(packet.getData(), packet.getOffset(), packet.getLength(), StandardCharsets.UTF_8);
|
2020-09-22 18:33:22 +00:00
|
|
|
sendEvent(packetData);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
listen = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void sendEvent(String message) {
|
2020-09-22 18:45:16 +00:00
|
|
|
LogEvent event = new LogEvent( this, message);
|
2020-12-01 12:43:36 +00:00
|
|
|
Iterator<LogListener> listeners = eventListeners.iterator();
|
2020-09-22 18:33:22 +00:00
|
|
|
while( listeners.hasNext() ) {
|
2020-12-01 12:43:36 +00:00
|
|
|
listeners.next().onLogEvent( event );
|
2020-09-22 18:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event Listener Configuration
|
|
|
|
*/
|
|
|
|
|
|
|
|
protected List<LogListener> eventListeners = new ArrayList<>();
|
|
|
|
|
|
|
|
public synchronized void addEventListener( LogListener l ) {
|
|
|
|
eventListeners.add( l );
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void removeEventListener( LogListener l ) {
|
|
|
|
eventListeners.remove( l );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|