diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-14 11:01:02 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-14 11:01:02 +0100 |
commit | 93bd1063a403fedfb79e334c5dcb81565afa655d (patch) | |
tree | 5bf2f80db092ec18104f91b721ae6befa308c62a | |
parent | 4de25e837653bacee4a5fd6bb8e3ce49e075805a (diff) |
Network creation and discovery in Android app.
5 files changed, 41 insertions, 14 deletions
diff --git a/same-android/src/main/java/com/orbekk/same/SameControllerActivity.java b/same-android/src/main/java/com/orbekk/same/SameControllerActivity.java index 041260c..31d3e18 100644 --- a/same-android/src/main/java/com/orbekk/same/SameControllerActivity.java +++ b/same-android/src/main/java/com/orbekk/same/SameControllerActivity.java @@ -57,10 +57,13 @@ public class SameControllerActivity extends Activity { }; public void createNetwork(View unused) { - logger.info("Creating network"); - Intent intent = new Intent(this, SameService.class); - intent.setAction("create"); - startService(intent); + Message message = Message.obtain(null, SameService.CREATE_NETWORK); + try { + sameService.send(message); + } catch (RemoteException e) { + logger.error("Failed to create network", e); + throw new RuntimeException(e); + } } public void joinNetwork(View unused) { diff --git a/same-android/src/main/java/com/orbekk/same/SameService.java b/same-android/src/main/java/com/orbekk/same/SameService.java index ba29585..3237e61 100644 --- a/same-android/src/main/java/com/orbekk/same/SameService.java +++ b/same-android/src/main/java/com/orbekk/same/SameService.java @@ -23,7 +23,8 @@ import com.orbekk.same.config.Configuration; public class SameService extends Service { public final static int DISPLAY_MESSAGE = 1; public final static int SEARCH_NETWORKS = 2; - + public final static int CREATE_NETWORK = 3; + public final static String AVAILABLE_NETWORKS_UPDATE = "com.orbekk.same.SameService.action.AVAILABLE_NETWORKS_UPDATE"; public final static String AVAILABLE_NETWORKS = @@ -65,6 +66,10 @@ public class SameService extends Service { logger.info("SEARCH_NETWORKS"); sameController.searchNetworks(); break; + case CREATE_NETWORK: + logger.info("CREATE_NETWORK"); + create(); + break; default: super.handleMessage(message); } @@ -75,15 +80,25 @@ public class SameService extends Service { private void initializeConfiguration() { Properties properties = new Properties(); + String localIp = new Broadcaster(this) + .getWlanAddress().getHostAddress(); + String localMaster = "http://" + localIp + ":" + SERVICE_PORT + + "/MasterService.json"; properties.setProperty("port", ""+SERVICE_PORT); - properties.setProperty("localIp", - new Broadcaster(this).getWlanAddress().getHostAddress()); - properties.setProperty("masterUrl", "http://10.0.0.6:10010/MasterService.json"); + properties.setProperty("localIp", localIp); + properties.setProperty("masterUrl", localMaster); properties.setProperty("enableDiscovery", "true"); properties.setProperty("discoveryPort", ""+DISCOVERY_PORT); + properties.setProperty("networkName", "AndroidNetwork"); configuration = new Configuration(properties); } + /** Create a public network. */ + private void create() { + sameController.getClient().joinNetwork( + configuration.get("masterUrl")); + } + @Override public IBinder onBind(Intent intent) { logger.info("onBind()"); diff --git a/same/src/main/java/com/orbekk/same/Client.java b/same/src/main/java/com/orbekk/same/Client.java index 7730f3e..b97ab91 100644 --- a/same/src/main/java/com/orbekk/same/Client.java +++ b/same/src/main/java/com/orbekk/same/Client.java @@ -173,6 +173,15 @@ public class Client implements DiscoveryListener { @Override public void discover(String url) { + String networkName = state.getDataOf(".networkName"); + if (networkName.equals(".InvalidClientNetwork")) { + logger.warn("Client not joined to a network. Ignoring discovery"); + return; + } else if (networkName.equals(".Private")) { + logger.info("Ignoring broadcast to .Private network."); + return; + } + if (!url.equals(myUrl)) { try { connections.getClient(url) diff --git a/same/src/main/java/com/orbekk/same/Master.java b/same/src/main/java/com/orbekk/same/Master.java index eaf0a8f..ed04b4d 100644 --- a/same/src/main/java/com/orbekk/same/Master.java +++ b/same/src/main/java/com/orbekk/same/Master.java @@ -18,8 +18,8 @@ public class Master implements MasterService, Runnable { private Thread workerThread = null; public static Master create(ConnectionManager connections, - Broadcaster broadcaster, String myUrl) { - State state = new State("DefaultMaster"); + Broadcaster broadcaster, String myUrl, String networkName) { + State state = new State(networkName); state.update(".masterUrl", myUrl, 1); return new Master(state, connections, broadcaster); } diff --git a/same/src/main/java/com/orbekk/same/SameController.java b/same/src/main/java/com/orbekk/same/SameController.java index d012d64..7f7a5d0 100644 --- a/same/src/main/java/com/orbekk/same/SameController.java +++ b/same/src/main/java/com/orbekk/same/SameController.java @@ -34,7 +34,7 @@ public class SameController { int port = configuration.getInt("port"); ConnectionManagerImpl connections = new ConnectionManagerImpl( timeout, timeout); - State state = new State("Default"); + State clientState = new State(".InvalidClientNetwork"); Broadcaster broadcaster = BroadcasterImpl.getDefaultBroadcastRunner(); String baseUrl = String.format("http://%s:%s/", @@ -43,10 +43,10 @@ public class SameController { String masterUrl = baseUrl + "MasterService.json"; String clientUrl = baseUrl + "ClientService.json"; - Master master = Master.create( - connections, broadcaster, masterUrl); + Master master = Master.create(connections, broadcaster, + masterUrl, configuration.get("networkName")); - Client client = new Client(state, connections, + Client client = new Client(clientState, connections, clientUrl); PaxosServiceImpl paxos = new PaxosServiceImpl(""); |