From 4825f9142e872f8572b628d6e9b1e671575a6873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 10 Jan 2012 17:36:00 +0100 Subject: Switch from log4j to slf4j. Slf4j works on Android easily. --- .../java/com/orbekk/net/BroadcastListener.java | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'jsonrpc/src') diff --git a/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java b/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java index 50cdbc7..c0b66e0 100644 --- a/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java +++ b/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java @@ -5,40 +5,44 @@ import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class BroadcastListener { private int port; - private Logger logger = Logger.getLogger(getClass()); + private Logger logger = LoggerFactory.getLogger(getClass()); public BroadcastListener(int port) { this.port = port; } - public boolean listen() { - logger.info("Waiting for broadcast on port " + port); + public DatagramPacket listen() { + logger.debug("Waiting for broadcast on port " + port); DatagramSocket socket; try { socket = new DatagramSocket(port); } catch (SocketException e) { logger.warn("Failed to create socket.", e.fillInStackTrace()); - return true; + return null; } try { socket.setBroadcast(true); } catch (SocketException e) { - logger.warn(e.fillInStackTrace()); + logger.warn("Exception: {}", e); } byte[] buffer = new byte[2048]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); try { socket.receive(packet); } catch (IOException e) { - logger.warn(e.fillInStackTrace()); + logger.warn("Exception when listening for broadcast: {}", e); + return null; } - logger.info("Received broadcast from " + packet.getAddress() + + + String address = packet.getAddress().getHostAddress(); + logger.debug("Received broadcast from " + address + ": " + new String(packet.getData())); - return true; + return packet; } public static void main(String[] args) { -- cgit v1.2.3