UDP Input

Receive and Send datagram packets

  • UDP datagrams are very simple and effective way to send / receive information in a network
  • Generally UDP protocol is not reliable, but if you are designing a system which doesn't care care about reliable communication like heartbeats or frequent repeatable data broadcast, it's good to pick a UDP communication

  • Let's take a small example of a UDP client sending machine information like system available CPU, ram, free memory etc. We can quickly build one page dashboard to see all the metrics of your network systems

UDP Input Rule

SysInfo

def vals = new String(packet.getData()).trim().split(",");

def rec = [:];

rec['machine'] = vals[0];
rec['stamp'] = Long.valueOf(vals[1]);
rec['cpu'] = Integer.valueOf(vals[2]);
rec['ramsize'] = Long.valueOf(vals[3]);
rec['freememory'] = Long.valueOf(vals[4]);
rec['jvmmemory'] = Long.valueOf(vals[5]);

log.info("%s", rec);

A simple UDP Client Example

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.UUID;

public class SysInfoClient implements Runnable {
	
	private static final SysInfoClient instance = new SysInfoClient();
	
	static {
		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
			
			@Override
			public void run() {
				instance.stop();
			}
		}));
	}
	
	private Thread _thread;
	private boolean _stopped;
	
	private String id = UUID.randomUUID().toString();
	private String host = "boodskap.xyz";
	private int port = 5656;
	private long sleep = 10000;
	
	private SysInfoClient() {
	}
	
	public static final SysInfoClient get() {
		return instance;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public long getSleep() {
		return sleep;
	}

	public void setSleep(long sleep) {
		this.sleep = sleep;
	}

	public void start() {
		
		if(null != _thread) return;
		
		_stopped = false;
		
		_thread = new Thread(this);
		
		_thread.start();
	}
	
	public void stop() {
		
		if(null == _thread) return;
		
		_stopped = true;
		
		_thread.interrupt();
	}
	
	public void run() {
		
		
		DatagramSocket _socket = null;
		
		try {
			
			_socket = new DatagramSocket();
			
			while(!Thread.currentThread().isInterrupted()) {
				
				String data = String.format("%s,%d,%d,%d,%d,%d", 
						id, 
						System.currentTimeMillis(), 
						Runtime.getRuntime().availableProcessors(),
						Runtime.getRuntime().maxMemory(),
						Runtime.getRuntime().freeMemory(),
						Runtime.getRuntime().totalMemory()
						);
				
				byte[] bdata = data.getBytes();
				
				DatagramPacket packet = new DatagramPacket(bdata, bdata.length, new InetSocketAddress(host, port));
				
				_socket.send(packet);
				
				System.out.format("Heartbeat %s sent to %s:%d\n", data, host, port);
				
				Thread.sleep(sleep);
			}
			
		}catch(Exception ex) {
			if(!_stopped) {
				ex.printStackTrace();
			}
		}finally {
			
		}
	}
	
	/**
	 * args[0] -> Unique ID
	 * args[1] -> UDP Server Host / IP
	 * args[2] -> UDP Server Port
	 * args[3] -> Sleep Interval
	 * @param args
	 */
	public static void main(String[] args) {
		
		SysInfoClient client = SysInfoClient.get();
		
		if(args.length >= 1) {
			client.setId(args[0]);
		}
		
		if(args.length >= 2) {
			client.setHost(args[1]);
		}
		
		if(args.length >= 3) {
			client.setPort(Integer.valueOf(args[2]));
		}
		
		if(args.length >= 4) {
			client.setSleep(Long.valueOf(args[3]));
		}
		
		client.start();
		
	}
}

Related Context APIs

Packet Context
Inputs Context

Related REST APIs

Create / Update Input
Retrieve Input
List Inputs
Count All Inputs
Start Stop Restart an Input
Delete Input
Delete All Inputs