summaryrefslogtreecommitdiff
path: root/same-android/src
diff options
context:
space:
mode:
Diffstat (limited to 'same-android/src')
-rw-r--r--same-android/src/main/java/com/orbekk/SameControllerActivity.java28
-rw-r--r--same-android/src/main/java/com/orbekk/SameService.java147
2 files changed, 95 insertions, 80 deletions
diff --git a/same-android/src/main/java/com/orbekk/SameControllerActivity.java b/same-android/src/main/java/com/orbekk/SameControllerActivity.java
index 9768cdc..c8c1ebd 100644
--- a/same-android/src/main/java/com/orbekk/SameControllerActivity.java
+++ b/same-android/src/main/java/com/orbekk/SameControllerActivity.java
@@ -88,16 +88,19 @@ public class SameControllerActivity extends Activity {
}
public void searchNetworks(View unused) {
- Toast.makeText(this, "Discovering networks", Toast.LENGTH_SHORT).show();
- final String[] listItems = new String[]{"First item", "Second item"};
- ListView list = (ListView)findViewById(R.id.network_list);
- list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_text_item,
- listItems));
+ logger.info("SearchNetworks()");
+// Toast.makeText(this, "Discovering networks", Toast.LENGTH_SHORT).show();
+// final String[] listItems = new String[]{"First item", "Second item"};
+// ListView list = (ListView)findViewById(R.id.network_list);
+// list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_text_item,
+// listItems));
- Message message = Message.obtain(null, SameService.DISPLAY_MESSAGE,
- "Message from Activity!");
+// Message message = Message.obtain(null, SameService.DISPLAY_MESSAGE,
+// "Message from Activity!");
+
+ Message searchMessage = Message.obtain(null, SameService.SEARCH_NETWORKS);
try {
- sameService.send(message);
+ sameService.send(searchMessage);
} catch (RemoteException e) {
logger.error("Failed to send message", e);
}
@@ -117,7 +120,7 @@ public class SameControllerActivity extends Activity {
bindService(intent, sameConnection, Context.BIND_AUTO_CREATE);
}
- @Override public void onResume() {
+ @Override public void onStart() {
super.onResume();
IntentFilter sameServiceUpdates = new IntentFilter(
@@ -125,6 +128,13 @@ public class SameControllerActivity extends Activity {
registerReceiver(broadcastReceiver, sameServiceUpdates);
}
+ @Override public void onStop() {
+ super.onStop();
+ if (sameService != null) {
+ unbindService(sameConnection);
+ }
+ }
+
@Override
protected void onDestroy() {
super.onDestroy();
diff --git a/same-android/src/main/java/com/orbekk/SameService.java b/same-android/src/main/java/com/orbekk/SameService.java
index dd943cb..5762810 100644
--- a/same-android/src/main/java/com/orbekk/SameService.java
+++ b/same-android/src/main/java/com/orbekk/SameService.java
@@ -34,51 +34,51 @@ public class SameService extends Service {
public final static String AVAILABLE_NETWORKS =
"com.orbekk.same.SameService.action.AVAILABLE_NETWORKS";
- final static int PORT = 15066;
final static int SERVICE_PORT = 15068;
+ final static int DISCOVERY_PORT = 15066;
private Logger logger = LoggerFactory.getLogger(getClass());
private Thread discoveryThread = null;
private SameController sameController = null;
private Configuration configuration = null;
- /** This class should go away >:-/ */
- public final class DiscoveryThread extends Thread {
- Broadcaster broadcast;
- DiscoveryListener listener;
-
- public DiscoveryThread(DiscoveryListener listener) {
- broadcast = new Broadcaster(SameService.this);
- this.listener = listener;
- }
-
- @Override public void run() {
- while (!Thread.interrupted()) {
- DatagramPacket packet = broadcast.receiveBroadcast(PORT);
- String content = new String(packet.getData(), 0, packet.getLength());
- String[] words = content.split(" ");
-
- if (!content.startsWith("Discover") || content.length() < 2) {
- logger.warn("Invalid discovery message: {}", content);
- continue;
- }
-
- String port = words[1];
- String url = "http://" + packet.getAddress().getHostAddress() +
- ":" + port + "/ClientService.json";
- listener.discover(url);
-
- Message message = Message.obtain();
- message.obj = "New client: " + url;
- toastHandler.sendMessage(message);
- }
- }
-
- @Override public void interrupt() {
- super.interrupt();
- broadcast.interrupt();
- }
- }
+// /** This class should go away >:-/ */
+// public final class DiscoveryThread extends Thread {
+// Broadcaster broadcast;
+// DiscoveryListener listener;
+//
+// public DiscoveryThread(DiscoveryListener listener) {
+// broadcast = new Broadcaster(SameService.this);
+// this.listener = listener;
+// }
+//
+// @Override public void run() {
+// while (!Thread.interrupted()) {
+// DatagramPacket packet = broadcast.receiveBroadcast(PORT);
+// String content = new String(packet.getData(), 0, packet.getLength());
+// String[] words = content.split(" ");
+//
+// if (!content.startsWith("Discover") || content.length() < 2) {
+// logger.warn("Invalid discovery message: {}", content);
+// continue;
+// }
+//
+// String port = words[1];
+// String url = "http://" + packet.getAddress().getHostAddress() +
+// ":" + port + "/ClientService.json";
+// listener.discover(url);
+//
+// Message message = Message.obtain();
+// message.obj = "New client: " + url;
+// toastHandler.sendMessage(message);
+// }
+// }
+//
+// @Override public void interrupt() {
+// super.interrupt();
+// broadcast.interrupt();
+// }
+// }
private NetworkNotificationListener networkListener =
new NetworkNotificationListener() {
@@ -128,13 +128,13 @@ public class SameService extends Service {
private final Messenger messenger = new Messenger(new InterfaceHandler());
private void createNetwork() {
- if (discoveryThread == null) {
- synchronized (this) {
- discoveryThread = new DiscoveryThread(sameController.getClient());
- discoveryThread.start();
- }
-
- }
+// if (discoveryThread == null) {
+// synchronized (this) {
+// discoveryThread = new DiscoveryThread(sameController.getClient());
+// discoveryThread.start();
+// }
+//
+// }
}
private void initializeConfiguration() {
@@ -143,23 +143,24 @@ public class SameService extends Service {
properties.setProperty("localIp",
new Broadcaster(this).getWlanAddress().getHostAddress());
properties.setProperty("masterUrl", "http://10.0.0.6:10010/MasterService.json");
+ properties.setProperty("discoveryPort", ""+DISCOVERY_PORT);
configuration = new Configuration(properties);
}
- private void sendBroadcastDiscovery(InetAddress ip) {
- Broadcaster broadcaster = new Broadcaster(this);
- String message = "Discover " + (PORT + 2);
- byte[] data = message.getBytes();
- if (ip.equals(broadcaster.getBroadcastAddress())) {
- broadcaster.sendUdpData(data, ip, PORT);
- } else {
- String remoteAddress =
- String.format("http://%s:%s/ClientService.json",
- ip.getHostAddress(), PORT + 2);
- sameController.getClient().sendDiscoveryRequest(
- remoteAddress);
- }
- }
+// private void sendBroadcastDiscovery(InetAddress ip) {
+// Broadcaster broadcaster = new Broadcaster(this);
+// String message = "Discover " + (PORT + 2);
+// byte[] data = message.getBytes();
+// if (ip.equals(broadcaster.getBroadcastAddress())) {
+// broadcaster.sendUdpData(data, ip, PORT);
+// } else {
+// String remoteAddress =
+// String.format("http://%s:%s/ClientService.json",
+// ip.getHostAddress(), PORT + 2);
+// sameController.getClient().sendDiscoveryRequest(
+// remoteAddress);
+// }
+// }
private void searchNetworks(InetAddress ip) {
sameController.getClient().setNetworkListener(
@@ -172,7 +173,7 @@ public class SameService extends Service {
toastHandler.sendMessage(message);
}
});
- sendBroadcastDiscovery(ip);
+// sendBroadcastDiscovery(ip);
}
@Override
@@ -183,7 +184,21 @@ public class SameService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
logger.info("onStartCommand()");
+
+ // TODO: Move this to the bound interface.
+ if (intent.getAction().equals("create")) {
+ createNetwork();
+ } else if (intent.getAction().equals("join")) {
+ String masterUrl = intent.getExtras().getString("masterUrl");
+ sameController.joinNetwork(masterUrl);
+ }
+ return START_STICKY;
+ }
+
+ @Override
+ public void onCreate() {
+ logger.info("onCreate()");
if (sameController == null) {
initializeConfiguration();
sameController = SameController.create(
@@ -194,23 +209,13 @@ public class SameService extends Service {
sameController.getClient().setNetworkListener(networkListener);
} catch (Exception e) {
logger.error("Failed to start server", e);
- return START_STICKY;
}
}
-
- // TODO: Move this to the bound interface.
- if (intent.getAction().equals("create")) {
- createNetwork();
- } else if (intent.getAction().equals("join")) {
- String masterUrl = intent.getExtras().getString("masterUrl");
- sameController.joinNetwork(masterUrl);
- }
- return START_STICKY;
}
@Override
public void onDestroy() {
- Toast.makeText(this, "service stopped", Toast.LENGTH_SHORT).show();
+ logger.info("onDestroy()");
if (discoveryThread != null) {
discoveryThread.interrupt();
}