package com.ericsson.tic.vi; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Timer; import java.util.TimerTask; /** * KeepAlive periodically sends a message to the master system to * let it know that the VI is up and running and ready to recieve messages * from the master system. When the master system stops recieving messages, * it will stop sending data. This was added in order to cut out excess * traffic on the network. * * @author Stefan Hellkvist */ public class KeepAlive extends TimerTask { static final int DELAY = 10000; private int serverPort, localPort; InetAddress serverAddress; public KeepAlive( int localPort, String serverAddress, int serverPort ) throws UnknownHostException { this.serverAddress = InetAddress.getByName(serverAddress); this.serverPort = serverPort; this.localPort = localPort; Timer t = new Timer( true ); t.schedule( this, DELAY, DELAY ); } public void run() { try { DatagramSocket socket = new DatagramSocket(); byte[] buffer = ("HELLO " + localPort).getBytes(); DatagramPacket packet = new DatagramPacket( buffer, buffer.length, serverAddress, serverPort ); socket.send(packet); } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }