diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-13 10:18:02 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-13 10:18:02 +0100 |
commit | ba7132f3ce5629cff2cc4857fff7bd672511bee8 (patch) | |
tree | dc6f7d1adafb8351004f2de6f3f775ebfe596fc7 /jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java | |
parent | 85a93de08694f25bd049c5236f11f06b8d8e4ff7 (diff) |
Rename projects.
– jsonrpc → same
– master → same-android
Diffstat (limited to 'jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java')
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java | 77 |
1 files changed, 0 insertions, 77 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java b/jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java deleted file mode 100644 index 95e279c..0000000 --- a/jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.orbekk.net; - -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; -import java.net.InterfaceAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.Enumeration; -import java.util.LinkedList; -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class Broadcaster { - private Logger logger = LoggerFactory.getLogger(getClass()); - - public List<InetAddress> getBroadcastAddresses() { - List<InetAddress> broadcastAddresses = new LinkedList<InetAddress>(); - - Enumeration<NetworkInterface> interfaces; - try { - interfaces = NetworkInterface.getNetworkInterfaces(); - } catch (SocketException e) { - logger.warn("Network problem?", e.fillInStackTrace()); - return broadcastAddresses; - } - while (interfaces.hasMoreElements()) { - NetworkInterface iface = interfaces.nextElement(); - try { - if (iface.isLoopback()) { - logger.debug("Ignoring looback device " + iface.getName()); - continue; - } - for (InterfaceAddress address : iface.getInterfaceAddresses()) { - InetAddress broadcast = address.getBroadcast(); - if (broadcast != null) { - broadcastAddresses.add(broadcast); - } - } - } catch (SocketException e) { - logger.info("Ignoring interface " + iface.getName(), e.fillInStackTrace()); - } - } - return broadcastAddresses; - } - - public boolean sendBroadcast(int port, byte[] data) { - boolean successful = false; - for (InetAddress broadcastAddress : getBroadcastAddresses()) { - try { - DatagramSocket socket = new DatagramSocket(); - socket.setBroadcast(true); - DatagramPacket packet = new DatagramPacket(data, data.length, broadcastAddress, port); - socket.send(packet); - successful = true; - } catch (SocketException e) { - logger.warn("Failed to send broadcast to " + broadcastAddress + - ". ", e.fillInStackTrace()); - } catch (IOException e) { - logger.warn("Error when sending broadcast to " + - broadcastAddress + ".", e.fillInStackTrace()); - } - } - return successful; - } - - public static void main(String[] args) { - int port = Integer.parseInt(args[0]); - Broadcaster broadcaster = new Broadcaster(); - String message = "Broadcast from Java broadcaster."; - broadcaster.sendBroadcast(port, message.getBytes()); - } -} |