diff options
Diffstat (limited to 'same-android/src')
5 files changed, 54 insertions, 142 deletions
diff --git a/same-android/src/main/java/com/orbekk/same/android/SameControllerActivity.java b/same-android/src/main/java/com/orbekk/same/android/SameControllerActivity.java index 8aacba5..2fd0d92 100644 --- a/same-android/src/main/java/com/orbekk/same/android/SameControllerActivity.java +++ b/same-android/src/main/java/com/orbekk/same/android/SameControllerActivity.java @@ -24,7 +24,7 @@ import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; -import com.orbekk.same.android.net.Broadcaster; +import com.orbekk.same.android.net.Networking; import com.orbekk.same.android.widget.NetworkListAdapter; public class SameControllerActivity extends Activity { @@ -117,7 +117,7 @@ public class SameControllerActivity extends Activity { private void showIpAddress() { TextView t = (TextView)findViewById(R.id.ipAddress); t.setText("My IP: "); - t.append(new Broadcaster(this).getWlanAddress().getHostAddress()); + t.append(new Networking(this).getWlanAddress().getHostAddress()); } public void doneClicked(View unused) { @@ -125,13 +125,7 @@ public class SameControllerActivity extends Activity { } public void searchNetworks(View unused) { - logger.info("SearchNetworks()"); - Message searchMessage = Message.obtain(null, SameService.SEARCH_NETWORKS); - try { - sameService.send(searchMessage); - } catch (RemoteException e) { - logger.error("Failed to send message", e); - } + logger.info("SearchNetworks() not working"); } @Override diff --git a/same-android/src/main/java/com/orbekk/same/android/SameService.java b/same-android/src/main/java/com/orbekk/same/android/SameService.java index 3eda862..f2af359 100644 --- a/same-android/src/main/java/com/orbekk/same/android/SameService.java +++ b/same-android/src/main/java/com/orbekk/same/android/SameService.java @@ -22,14 +22,12 @@ import com.orbekk.same.SameController; import com.orbekk.same.State; import com.orbekk.same.State.Component; import com.orbekk.same.StateChangedListener; -import com.orbekk.same.android.net.AndroidBroadcasterFactory; -import com.orbekk.same.android.net.Broadcaster; +import com.orbekk.same.android.net.Networking; import com.orbekk.same.config.Configuration; import com.orbekk.same.discovery.DirectoryService; import com.orbekk.util.DelayedOperation; public class SameService extends Service { - public final static int SEARCH_NETWORKS = 2; public final static int CREATE_NETWORK = 3; /** @@ -97,10 +95,6 @@ public class SameService extends Service { class InterfaceHandler extends Handler { @Override public void handleMessage(Message message) { switch (message.what) { - case SEARCH_NETWORKS: - logger.info("SEARCH_NETWORKS"); - sameController.searchNetworks(); - break; case CREATE_NETWORK: logger.info("CREATE_NETWORK"); create(); @@ -226,7 +220,7 @@ public class SameService extends Service { private void initializeConfiguration() { Properties properties = new Properties(); - String localIp = new Broadcaster(this) + String localIp = new Networking(this) .getWlanAddress().getHostAddress(); String localMaster = "http://" + localIp + ":" + SERVICE_PORT + "/MasterService.json"; @@ -279,9 +273,7 @@ public class SameService extends Service { if (sameController == null) { initializeConfiguration(); - sameController = SameController.create( - new AndroidBroadcasterFactory(this), - configuration); + sameController = SameController.create(configuration); try { sameController.start(); sameController.getClient().setNetworkListener(networkListener); diff --git a/same-android/src/main/java/com/orbekk/same/android/net/AndroidBroadcasterFactory.java b/same-android/src/main/java/com/orbekk/same/android/net/AndroidBroadcasterFactory.java deleted file mode 100644 index 3d2bdef..0000000 --- a/same-android/src/main/java/com/orbekk/same/android/net/AndroidBroadcasterFactory.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.orbekk.same.android.net; - -import android.content.Context; - -import com.orbekk.net.BroadcasterFactory; -import com.orbekk.net.BroadcasterInterface; - -public class AndroidBroadcasterFactory implements BroadcasterFactory { - Context context; - - public AndroidBroadcasterFactory(Context context) { - this.context = context; - } - - @Override - public BroadcasterInterface create() { - return new Broadcaster(context); - } - -} 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 deleted file mode 100644 index 6e426e8..0000000 --- a/same-android/src/main/java/com/orbekk/same/android/net/Broadcaster.java +++ /dev/null @@ -1,102 +0,0 @@ -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 diff --git a/same-android/src/main/java/com/orbekk/same/android/net/Networking.java b/same-android/src/main/java/com/orbekk/same/android/net/Networking.java new file mode 100644 index 0000000..e335bb3 --- /dev/null +++ b/same-android/src/main/java/com/orbekk/same/android/net/Networking.java @@ -0,0 +1,48 @@ +package com.orbekk.same.android.net; + +import java.net.InetAddress; +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 Networking { + private Context context; + private Logger logger = LoggerFactory.getLogger(getClass()); + + public Networking(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 synchronized 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); + } +}
\ No newline at end of file |