UDPHeartBeat { public void start() { pleaseStop = false; if (thread != null && thread.isAlive()) return; else { thread = new Thread(new Runnable() { @Override public void run() { long timeToWait = interval; DatagramSocket socket = null; try { socket = new DatagramSocket(); socket.setBroadcast(true); socket.setSoTimeout(socketTimeout); } catch (SocketException e) { Logger.error(TAG, "Local Socket Port Error %d", e, socketPort); return; } while (!pleaseStop) { setStatus(Status.BEATING); try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) interfaces.nextElement(); if (networkInterface.isLoopback() || !networkInterface.isUp()) { continue; } for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { if (interfaceAddress.getBroadcast() == null) { continue; } try { UDPOperations.send(socket, interfaceAddress.getBroadcast(), serverPort, me.serialize()); byte[] response = UDPOperations.receiveData(socket); if (beatListener != null) beatListener.onBeat(response); timeToWait = interval; } catch (Exception e) { Logger.error(TAG, "Server Error %s:%d", e, interfaceAddress.getBroadcast(), serverPort); timeToWait = failedInterval; if (beatListener != null) beatListener.onError(e); } } } } catch (SocketException e) { Logger.error(TAG, "Local Socket Port Error %d", e, socketPort); if (e instanceof BindException) break; timeToWait = failedInterval; } try { setStatus(Status.WAITING); Thread.sleep(timeToWait); } catch (InterruptedException e) { } } } }); thread.start(); } } UDPHeartBeat(HeartBeatConfiguration configuration, HeartBeatInfo me);  UDPHeartBeat(HeartBeatConfiguration configuration, HeartBeatInfo me, BeatListener beatListener); void stop(); void start();  }
@Test public void testStart() throws Exception { UDPHeartBeat heartBeat = new UDPHeartBeat(conf, me); heartBeat.start(); DatagramSocket receiver = new DatagramSocket(conf.getServerPort()); assert new String(UDPOperations.receiveData(receiver)).equals("TestNode"); heartBeat.stop(); receiver.close(); }