diff options
Diffstat (limited to 'jsonrpc')
-rw-r--r-- | jsonrpc/pom.xml | 12 | ||||
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java | 22 |
2 files changed, 22 insertions, 12 deletions
diff --git a/jsonrpc/pom.xml b/jsonrpc/pom.xml index 4d06cfd..7d2bba7 100644 --- a/jsonrpc/pom.xml +++ b/jsonrpc/pom.xml @@ -44,9 +44,15 @@ </dependency> <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.16</version> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.6.4</version> + </dependency> + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.6.4</version> </dependency> <dependency> 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) { |