summaryrefslogtreecommitdiff
path: root/same-android/src/main/java/com/orbekk/discovery
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-31 14:19:39 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-31 14:19:39 +0100
commiteacd71d700c402e79edec9427b2fe1fb5e6b278e (patch)
treeb02eaea91df8d76634c0319409ab147e67b7f7a6 /same-android/src/main/java/com/orbekk/discovery
parentc53846bfad9c75ab24ae9051470b4d102e083538 (diff)
Support custom search address.
Diffstat (limited to 'same-android/src/main/java/com/orbekk/discovery')
-rw-r--r--same-android/src/main/java/com/orbekk/discovery/Broadcaster.java4
-rw-r--r--same-android/src/main/java/com/orbekk/discovery/SameService.java28
2 files changed, 24 insertions, 8 deletions
diff --git a/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java b/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java
index 167eda1..6744651 100644
--- a/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java
+++ b/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java
@@ -51,11 +51,11 @@ public class Broadcaster {
return fromInt(broadcast);
}
- public synchronized boolean sendBroadcast(byte[] data, int port) {
+ 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, getBroadcastAddress(), port);
+ DatagramPacket packet = new DatagramPacket(data, data.length, ip, port);
socket.send(packet);
return true;
} catch (SocketException e) {
diff --git a/same-android/src/main/java/com/orbekk/discovery/SameService.java b/same-android/src/main/java/com/orbekk/discovery/SameService.java
index abc92e8..0dd6593 100644
--- a/same-android/src/main/java/com/orbekk/discovery/SameService.java
+++ b/same-android/src/main/java/com/orbekk/discovery/SameService.java
@@ -1,6 +1,8 @@
package com.orbekk.discovery;
import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import android.app.Service;
import android.content.Intent;
@@ -46,7 +48,7 @@ public class SameService extends Service {
String port = words[1];
String url = "http://" + packet.getAddress().getHostAddress() +
- ":" + port + "/";
+ ":" + port + "/ClientService.json";
listener.discover(url);
Message message = Message.obtain();
@@ -79,13 +81,22 @@ public class SameService extends Service {
}
}
- private void sendBroadcastDiscovery() {
+ private void sendBroadcastDiscovery(InetAddress ip) {
+ Broadcaster broadcaster = new Broadcaster(this);
String message = "Discover " + (PORT + 2);
byte[] data = message.getBytes();
- new Broadcaster(this).sendBroadcast(data, PORT);
+ 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 joinNetwork() {
+ private void joinNetwork(InetAddress ip) {
sameController.getClient().setNetworkListener(
new NetworkNotificationListener() {
@Override
@@ -96,7 +107,7 @@ public class SameService extends Service {
toastHandler.sendMessage(message);
}
});
- sendBroadcastDiscovery();
+ sendBroadcastDiscovery(ip);
}
@Override
@@ -125,7 +136,12 @@ public class SameService extends Service {
if (intent.getAction().equals("create")) {
createNetwork();
} else if (intent.getAction().equals("join")) {
- joinNetwork();
+ try {
+ InetAddress ip = InetAddress.getByName(intent.getExtras().getString("ip"));
+ joinNetwork(ip);
+ } catch (UnknownHostException e) {
+ logger.error("Unknown host.", e);
+ }
}
return START_STICKY;
}