summaryrefslogtreecommitdiff
path: root/jsonrpc/src/main
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-10 17:36:00 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-10 17:36:49 +0100
commit4825f9142e872f8572b628d6e9b1e671575a6873 (patch)
treea8a16176c73ab4fa7df6420a73253e483c6c5f44 /jsonrpc/src/main
parent6596cf75382de7fd7f2a8b868a70cfa0442a4ea5 (diff)
Switch from log4j to slf4j.
Slf4j works on Android easily.
Diffstat (limited to 'jsonrpc/src/main')
-rw-r--r--jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java22
1 files changed, 13 insertions, 9 deletions
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) {