diff options
11 files changed, 57 insertions, 261 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 diff --git a/same/src/main/java/com/orbekk/net/Broadcaster.java b/same/src/main/java/com/orbekk/net/Broadcaster.java deleted file mode 100644 index cfec0ae..0000000 --- a/same/src/main/java/com/orbekk/net/Broadcaster.java +++ /dev/null @@ -1,78 +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 implements BroadcasterInterface { - 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; - } - - @Override - 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/BroadcasterFactory.java b/same/src/main/java/com/orbekk/net/BroadcasterFactory.java deleted file mode 100644 index 30dd0ae..0000000 --- a/same/src/main/java/com/orbekk/net/BroadcasterFactory.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.orbekk.net; - -public interface BroadcasterFactory { - BroadcasterInterface create(); -} diff --git a/same/src/main/java/com/orbekk/net/BroadcasterInterface.java b/same/src/main/java/com/orbekk/net/BroadcasterInterface.java deleted file mode 100644 index 08a7e80..0000000 --- a/same/src/main/java/com/orbekk/net/BroadcasterInterface.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.orbekk.net; - -public interface BroadcasterInterface { - boolean sendBroadcast(int port, byte[] data); -} diff --git a/same/src/main/java/com/orbekk/net/DefaultBroadcasterFactory.java b/same/src/main/java/com/orbekk/net/DefaultBroadcasterFactory.java deleted file mode 100644 index 018d334..0000000 --- a/same/src/main/java/com/orbekk/net/DefaultBroadcasterFactory.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.orbekk.net; - -public class DefaultBroadcasterFactory implements BroadcasterFactory { - @Override - public BroadcasterInterface create() { - return new Broadcaster(); - } -} diff --git a/same/src/main/java/com/orbekk/same/App.java b/same/src/main/java/com/orbekk/same/App.java index 464668d..ca12fa2 100644 --- a/same/src/main/java/com/orbekk/same/App.java +++ b/same/src/main/java/com/orbekk/same/App.java @@ -13,7 +13,6 @@ public class App { SameController controller = SameController.create(configuration); try { controller.start(); - controller.searchNetworks(); if ("true".equals(configuration.get("isMaster"))) { controller.createNetwork(configuration.get("networkName")); } else { diff --git a/same/src/main/java/com/orbekk/same/SameController.java b/same/src/main/java/com/orbekk/same/SameController.java index c1c7901..95b8c29 100644 --- a/same/src/main/java/com/orbekk/same/SameController.java +++ b/same/src/main/java/com/orbekk/same/SameController.java @@ -3,10 +3,6 @@ package com.orbekk.same; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.orbekk.net.BroadcastListener; -import com.orbekk.net.BroadcasterFactory; -import com.orbekk.net.BroadcasterInterface; -import com.orbekk.net.DefaultBroadcasterFactory; import com.orbekk.paxos.PaxosService; import com.orbekk.paxos.PaxosServiceImpl; import com.orbekk.same.config.Configuration; @@ -23,7 +19,6 @@ public class SameController { private Master master; private Client client; private PaxosServiceImpl paxos; - private BroadcasterFactory broadcasterFactory; private Configuration configuration; private ConnectionManager connections; private Broadcaster serviceBroadcaster; @@ -57,8 +52,7 @@ public class SameController { } }; - public static SameController create(BroadcasterFactory broadcasterFactory, - Configuration configuration) { + public static SameController create(Configuration configuration) { int port = configuration.getInt("port"); ConnectionManagerImpl connections = new ConnectionManagerImpl( timeout, timeout); @@ -91,14 +85,10 @@ public class SameController { SameController controller = new SameController( configuration, connections, server, master, client, - paxos, broadcaster, broadcasterFactory); + paxos, broadcaster); return controller; } - public static SameController create(Configuration configuration) { - return create(new DefaultBroadcasterFactory(), configuration); - } - public SameController( Configuration configuration, ConnectionManager connections, @@ -106,8 +96,7 @@ public class SameController { MasterServiceProxy master, Client client, PaxosServiceImpl paxos, - Broadcaster serviceBroadcaster, - BroadcasterFactory broadcasterFactory) { + Broadcaster serviceBroadcaster) { this.configuration = configuration; this.connections = connections; this.server = server; @@ -115,7 +104,6 @@ public class SameController { this.client = client; this.paxos = paxos; this.serviceBroadcaster = serviceBroadcaster; - this.broadcasterFactory = broadcasterFactory; } public void start() throws Exception { @@ -151,13 +139,6 @@ public class SameController { "MasterService.json"; joinNetwork(masterUrl); } - - public void searchNetworks() { - BroadcasterInterface broadcaster = broadcasterFactory.create(); - String message = "Discover " + client.getUrl(); - broadcaster.sendBroadcast(configuration.getInt("discoveryPort"), - message.getBytes()); - } public void joinNetwork(String url) { client.joinNetwork(url); |