diff options
Diffstat (limited to 'jsonrpc/src/main/java/com/orbekk/net')
3 files changed, 0 insertions, 156 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java b/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java deleted file mode 100644 index c0b66e0..0000000 --- a/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java +++ /dev/null @@ -1,53 +0,0 @@ -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/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()); - } -} diff --git a/jsonrpc/src/main/java/com/orbekk/net/HttpUtil.java b/jsonrpc/src/main/java/com/orbekk/net/HttpUtil.java deleted file mode 100644 index b4bb887..0000000 --- a/jsonrpc/src/main/java/com/orbekk/net/HttpUtil.java +++ /dev/null @@ -1,26 +0,0 @@ -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); - } - } -} |