summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/net
diff options
context:
space:
mode:
Diffstat (limited to 'same/src/main/java/com/orbekk/net')
-rw-r--r--same/src/main/java/com/orbekk/net/BroadcastListener.java53
-rw-r--r--same/src/main/java/com/orbekk/net/Broadcaster.java77
-rw-r--r--same/src/main/java/com/orbekk/net/HttpUtil.java26
3 files changed, 156 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/net/BroadcastListener.java b/same/src/main/java/com/orbekk/net/BroadcastListener.java
new file mode 100644
index 0000000..c0b66e0
--- /dev/null
+++ b/same/src/main/java/com/orbekk/net/BroadcastListener.java
@@ -0,0 +1,53 @@
+package com.orbekk.net;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.SocketException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BroadcastListener {
+ private int port;
+ private Logger logger = LoggerFactory.getLogger(getClass());
+
+ public BroadcastListener(int port) {
+ this.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 null;
+ }
+ try {
+ socket.setBroadcast(true);
+ } catch (SocketException e) {
+ 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("Exception when listening for broadcast: {}", e);
+ return null;
+ }
+
+ String address = packet.getAddress().getHostAddress();
+ logger.debug("Received broadcast from " + address +
+ ": " + new String(packet.getData()));
+ return packet;
+ }
+
+ public static void main(String[] args) {
+ int port = Integer.parseInt(args[0]);
+ BroadcastListener listener = new BroadcastListener(port);
+ System.out.println("Received broadcast: " + listener.listen());
+ }
+}
diff --git a/same/src/main/java/com/orbekk/net/Broadcaster.java b/same/src/main/java/com/orbekk/net/Broadcaster.java
new file mode 100644
index 0000000..95e279c
--- /dev/null
+++ b/same/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());
+ }
+}
diff --git a/same/src/main/java/com/orbekk/net/HttpUtil.java b/same/src/main/java/com/orbekk/net/HttpUtil.java
new file mode 100644
index 0000000..b4bb887
--- /dev/null
+++ b/same/src/main/java/com/orbekk/net/HttpUtil.java
@@ -0,0 +1,26 @@
+package com.orbekk.net;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HttpUtil {
+ private static final Logger logger =
+ LoggerFactory.getLogger(HttpUtil.class);
+
+ public static void sendHttpRequest(String url) {
+ try {
+ URL pingUrl = new URL(url);
+ pingUrl.openStream();
+ // URLConnection connection = pingUrl.openConnection();
+ // connection.connect();
+ } catch (MalformedURLException e) {
+ logger.warn("Unable to send ping to {}: {}.", url, e);
+ } catch (IOException e) {
+ logger.warn("Error when sending ping: {}", e);
+ }
+ }
+}