summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-10 17:31:25 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-10 17:31:25 +0100
commit6596cf75382de7fd7f2a8b868a70cfa0442a4ea5 (patch)
tree1f483cde5a4c05c3757dc9c307d1b54dd32ee372
parenta63e922375434b9d8f7effffe14845d240b430f9 (diff)
Add Broadcaster for non-Android Java.
-rw-r--r--jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java77
1 files changed, 77 insertions, 0 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java b/jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java
new file mode 100644
index 0000000..95e279c
--- /dev/null
+++ b/jsonrpc/src/main/java/com/orbekk/net/Broadcaster.java
@@ -0,0 +1,77 @@
+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());
+ }
+}