From 947390013409e297cab8ec991212473ae4b76802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Mon, 13 Feb 2012 18:05:13 +0100 Subject: Android Broadcaster implements BroadcastInterface. --- .../java/com/orbekk/discovery/Broadcaster.java | 94 ------------------- .../com/orbekk/same/android/net/Broadcaster.java | 102 +++++++++++++++++++++ 2 files changed, 102 insertions(+), 94 deletions(-) delete mode 100644 same-android/src/main/java/com/orbekk/discovery/Broadcaster.java create mode 100644 same-android/src/main/java/com/orbekk/same/android/net/Broadcaster.java (limited to 'same-android/src/main/java') diff --git a/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java b/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java deleted file mode 100644 index 6744651..0000000 --- a/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.orbekk.discovery; - -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; -import java.net.SocketException; -import java.net.UnknownHostException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import android.content.Context; -import android.net.DhcpInfo; -import android.net.wifi.WifiManager; - -public class Broadcaster { - private Context context; - private Logger logger = LoggerFactory.getLogger(getClass()); - private DatagramSocket socket = null; - - public Broadcaster(Context context) { - this.context = context; - } - - public InetAddress fromInt(int ip) { - byte[] quads = new byte[4]; - for (int k = 0; k < 4; k++) - quads[k] = (byte) ((ip >> k * 8) & 0xFF); - try { - return InetAddress.getByAddress(quads); - } catch (UnknownHostException e) { - logger.warn("Failed to find broadcast address."); - return null; - } - } - - public DhcpInfo getDhcpInfo() { - WifiManager wifi = - (WifiManager)context.getSystemService(Context.WIFI_SERVICE); - return wifi.getDhcpInfo(); - } - - public synchronized InetAddress getWlanAddress() { - return fromInt(getDhcpInfo().ipAddress); - } - - public synchronized InetAddress getBroadcastAddress() { - DhcpInfo dhcp = getDhcpInfo(); - int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; - return fromInt(broadcast); - } - - public synchronized boolean sendUdpData(byte[] data, InetAddress ip, int port) { - try { - socket = new DatagramSocket(0); - socket.setBroadcast(true); - DatagramPacket packet = new DatagramPacket(data, data.length, ip, port); - socket.send(packet); - return true; - } catch (SocketException e) { - logger.warn("Failed to send broadcast.", e.fillInStackTrace()); - return false; - } catch (IOException e) { - logger.warn("Error when sending broadcast.", e.fillInStackTrace()); - return false; - } finally { - socket.close(); - socket = null; - } - } - - public synchronized DatagramPacket receiveBroadcast(int port) { - try { - socket = new DatagramSocket(port); - byte[] data = new byte[1024]; - DatagramPacket packet = new DatagramPacket(data, data.length); - socket.receive(packet); - return packet; - } catch (IOException e) { - logger.warn("Failed to receive broadcast.", e); - return null; - } finally { - socket.close(); - socket = null; - } - } - - public void interrupt() { - if (socket != null) { - socket.close(); - } - } -} \ No newline at end of file diff --git a/same-android/src/main/java/com/orbekk/same/android/net/Broadcaster.java b/same-android/src/main/java/com/orbekk/same/android/net/Broadcaster.java new file mode 100644 index 0000000..6e426e8 --- /dev/null +++ b/same-android/src/main/java/com/orbekk/same/android/net/Broadcaster.java @@ -0,0 +1,102 @@ +package com.orbekk.same.android.net; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.SocketException; +import java.net.UnknownHostException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.orbekk.net.BroadcasterInterface; + +import android.content.Context; +import android.net.DhcpInfo; +import android.net.wifi.WifiManager; + +public class Broadcaster implements BroadcasterInterface { + private Context context; + private Logger logger = LoggerFactory.getLogger(getClass()); + private DatagramSocket socket = null; + + public Broadcaster(Context context) { + this.context = context; + } + + public InetAddress fromInt(int ip) { + byte[] quads = new byte[4]; + for (int k = 0; k < 4; k++) + quads[k] = (byte) ((ip >> k * 8) & 0xFF); + try { + return InetAddress.getByAddress(quads); + } catch (UnknownHostException e) { + logger.warn("Failed to find broadcast address."); + return null; + } + } + + public DhcpInfo getDhcpInfo() { + WifiManager wifi = + (WifiManager)context.getSystemService(Context.WIFI_SERVICE); + return wifi.getDhcpInfo(); + } + + public synchronized InetAddress getWlanAddress() { + return fromInt(getDhcpInfo().ipAddress); + } + + public synchronized InetAddress getBroadcastAddress() { + DhcpInfo dhcp = getDhcpInfo(); + int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; + return fromInt(broadcast); + } + + public synchronized boolean sendUdpData(byte[] data, InetAddress ip, int port) { + try { + socket = new DatagramSocket(0); + socket.setBroadcast(true); + DatagramPacket packet = new DatagramPacket(data, data.length, ip, port); + socket.send(packet); + return true; + } catch (SocketException e) { + logger.warn("Failed to send broadcast.", e.fillInStackTrace()); + return false; + } catch (IOException e) { + logger.warn("Error when sending broadcast.", e.fillInStackTrace()); + return false; + } finally { + socket.close(); + socket = null; + } + } + + public synchronized DatagramPacket receiveBroadcast(int port) { + try { + socket = new DatagramSocket(port); + byte[] data = new byte[1024]; + DatagramPacket packet = new DatagramPacket(data, data.length); + socket.receive(packet); + return packet; + } catch (IOException e) { + logger.warn("Failed to receive broadcast.", e); + return null; + } finally { + socket.close(); + socket = null; + } + } + + public void interrupt() { + if (socket != null) { + socket.close(); + } + } + + @Override + public synchronized boolean sendBroadcast(int port, byte[] data) { + InetAddress broadcastAddress = getBroadcastAddress(); + return sendUdpData(data, broadcastAddress, port); + } +} \ No newline at end of file -- cgit v1.2.3