package rsol.stream.demo;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class SysLogWriter {
private InetAddress address;
private final int port;
private final String host;
private DatagramSocket ds;
public SysLogWriter(final String syslogHost, final int sysLogPort) {
if (syslogHost == null) {
throw new IllegalArgumentException("syslogHost cannot be null");
}
host = syslogHost;
port = sysLogPort;
try {
this.address = InetAddress.getByName(syslogHost);
} catch (UnknownHostException e) {
System.out.println("Could not find " + syslogHost + ". Metrics logging will FAIL.");
}
try {
this.ds = new DatagramSocket();
} catch (SocketException e) {
System.out.println("Could not instantiate DatagramSocket to " + syslogHost + ". Metrics logging will FAIL.");
e.printStackTrace(System.out);
}
}
public int getPort() {
return port;
}
public void write(String string) {
try {
if (this.ds != null && this.address != null) {
byte[] bytes = string.getBytes();
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, port);
ds.send(packet);
} else {
System.out.println(string);
}
} catch (Exception e) {
System.out.println("Error writing metrics to: " + host + ":" + port);
e.printStackTrace(System.out);
}
}
public static void main(String[] args) {
String host = "host.rsol.test.org";
// host = "localhost";
int port = 8000;
port = 61908;
String data = "some data to be sent";
data = "<86>test string 3";
System.out.println(data);
SysLogWriter logger = new SysLogWriter(host, port);
System.out.println(".... about to write...");
// data = getLargeString();
for (int i = 0; i < 2000; i++) {
logger.write(data);
}
System.out.println(".... done...");
logger.ds.close();
}
public static String getLargeString() {
String text = "This is a sample text generated";
int length = text.length();
int desired = (70 * 1024) / length;
StringBuilder builder = new StringBuilder(text);
for (int i = 0; i < desired; i++) {
builder.append(text);
}
String response = builder.toString();
int size = response.length();
if (size > (1024 * 60))
response = response.substring(0, (1024 * 60));
System.out.println(response.length());
return response;
}
}
package rsol.stream.demo;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class SysLogReceiver {
public SysLogReceiver() {
}
public void read(int port) throws Exception {
DatagramSocket dsr = null;
try {
byte[] buf = new byte[1000];
dsr = new DatagramSocket(port);
DatagramPacket dp = new DatagramPacket(buf, buf.length);
while (true) {
dsr.receive(dp);
String rcvd = "rcvd from " + dp.getAddress() + ", "
+ dp.getPort() + ": "
+ new String(dp.getData(), 0, dp.getLength());
System.out.println(rcvd);
}
} catch (Exception e) {
System.out.println("Error receiving messge");
e.printStackTrace(System.out);
} finally {
dsr.close();
}
}
public static void main(String[] args) throws Exception {
SysLogReceiver r = new SysLogReceiver();
System.out.println("listening...");
r.read(8000);
}
}