summaryrefslogtreecommitdiff
path: root/android/sdremote
diff options
context:
space:
mode:
authorAndrzej J.R. Hunt <andrzej@ahunt.org>2012-08-06 10:53:19 +0200
committerMichael Meeks <michael.meeks@suse.com>2012-08-06 10:23:12 +0100
commitb2fd8230172641edbd2a4f67e214c44ce740b4b2 (patch)
tree7489efa7fdeda666b8d5034b6d2e67ad79ae3cc0 /android/sdremote
parentc56b0a2fe8a54aef8229a7b32a8d62cc23bbd9e6 (diff)
Some minor fixes.
Change-Id: Id405b97440efc7160e76d535bddfd5580923fe24
Diffstat (limited to 'android/sdremote')
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java280
1 files changed, 143 insertions, 137 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java
index 5b1b33462925..29ae7de98286 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java
@@ -16,141 +16,147 @@ import android.support.v4.content.LocalBroadcastManager;
public class ServerFinder {
- private Context mContext;
-
- private static final int PORT = 1598;
- private static final String CHARSET = "UTF-8";
-
- private static final long SEARCH_INTERVAL = 1000 * 20;
-
- private DatagramSocket mSocket = null;
-
- private Thread mListenerThread = null;
-
- private boolean mFinishRequested = false;
-
- private HashMap<String, Server> mServerList = new HashMap<String, Server>();
-
- public ServerFinder(Context aContext) {
- mContext = aContext;
- }
-
- private void listenForServer() {
- byte[] aBuffer = new byte[500];
- DatagramPacket aPacket = new DatagramPacket(aBuffer, aBuffer.length);
-
- try {
- String aCommand = null;
- String aName = null;
- mSocket.receive(aPacket);
- int i;
- for (i = 0; i < aBuffer.length; i++) {
- if (aPacket.getData()[i] == '\n') {
- aCommand = new String(aPacket.getData(), 0, i, CHARSET);
- break;
- }
- }
- if (i == aBuffer.length || !aCommand.equals("LOREMOTE_ADVERTISE")) {
- return;
- }
- for (int j = i; j < aBuffer.length; j++) {
- if (aPacket.getData()[i] == '\n') {
- aName = new String(aPacket.getData(), i + 1, j, CHARSET);
- break;
- }
- }
- if (aName == null) {
- return;
- }
- Server aServer = new Server(CommunicationService.Protocol.NETWORK,
- aPacket.getAddress().toString(), aName,
- System.currentTimeMillis());
- mServerList.put(aServer.getAddress(), aServer);
-
- // System.out.println("SF FOUND: IP="
- // + aPacket.getAddress().toString() + " HOSTNAME="
- // + aName);
-
- Intent aIntent = new Intent(
- CommunicationService.MSG_SERVERLIST_CHANGED);
- LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent);
-
- } catch (java.net.SocketTimeoutException e) {
- // Ignore -- we want to timeout to enable checking whether we
- // should stop listening periodically
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- public void startFinding() {
- if (mSocket != null)
- return;
-
- mFinishRequested = false;
-
- if (mListenerThread == null) {
- mListenerThread = new Thread() {
- @Override
- public void run() {
- long aTime = 0;
- try {
- mSocket = new DatagramSocket();
- mSocket.setSoTimeout(1000 * 10);
- while (!mFinishRequested) {
- if (System.currentTimeMillis() - aTime > SEARCH_INTERVAL) {
- String aString = "LOREMOTE_SEARCH\n";
- DatagramPacket aPacket = new DatagramPacket(
- aString.getBytes(CHARSET),
- aString.length(),
- InetAddress.getByName("239.0.0.1"),
- PORT);
- mSocket.send(aPacket);
- aTime = System.currentTimeMillis();
- for (Server aServer : mServerList.values()) {
- if (System.currentTimeMillis()
- - aServer.getTimeDiscovered() > 60 * 1000) {
- mServerList.remove(aServer.getAddress());
- Intent aIntent = new Intent(
- CommunicationService.MSG_SERVERLIST_CHANGED);
- LocalBroadcastManager.getInstance(
- mContext)
- .sendBroadcast(aIntent);
-
- }
- }
- }
-
- listenForServer();
- }
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- };
- mListenerThread.start();
- }
-
- }
-
- public void stopFinding() {
- if (mListenerThread != null) {
- mFinishRequested = true;
- mListenerThread = null;
- }
- }
-
- public Server[] getServerList() {
- return mServerList.values().toArray(new Server[mServerList.size()]);
- }
+ private Context mContext;
+
+ private static final int PORT = 1598;
+ private static final String GROUPADDRESS = "239.0.0.1";
+
+ private static final String CHARSET = "UTF-8";
+
+ private static final long SEARCH_INTERVAL = 1000 * 20;
+
+ private DatagramSocket mSocket = null;
+
+ private Thread mListenerThread = null;
+
+ private boolean mFinishRequested = false;
+
+ private HashMap<String, Server> mServerList = new HashMap<String, Server>();
+
+ public ServerFinder(Context aContext) {
+ mContext = aContext;
+ }
+
+ private void listenForServer() {
+ byte[] aBuffer = new byte[500];
+ DatagramPacket aPacket = new DatagramPacket(aBuffer, aBuffer.length);
+
+ try {
+ String aCommand = null;
+ String aName = null;
+ System.out.println("SF:Reading");
+ mSocket.receive(aPacket);
+ System.out.println("SF:Received");
+ int i;
+ for (i = 0; i < aBuffer.length; i++) {
+ if (aPacket.getData()[i] == '\n') {
+ aCommand = new String(aPacket.getData(), 0, i, CHARSET);
+ break;
+ }
+ }
+ if (i == aBuffer.length || !aCommand.equals("LOREMOTE_ADVERTISE")) {
+ return;
+ }
+ for (int j = i; j < aBuffer.length; j++) {
+ if (aPacket.getData()[i] == '\n') {
+ aName = new String(aPacket.getData(), i + 1, j, CHARSET);
+ break;
+ }
+ }
+ if (aName == null) {
+ return;
+ }
+ Server aServer = new Server(CommunicationService.Protocol.NETWORK,
+ aPacket.getAddress().toString(), aName,
+ System.currentTimeMillis());
+ mServerList.put(aServer.getAddress(), aServer);
+
+ //System.out.println("SF FOUND: IP="
+ //+ aPacket.getAddress().toString() + " HOSTNAME="
+ //+ aName);
+
+ Intent aIntent = new Intent(
+ CommunicationService.MSG_SERVERLIST_CHANGED);
+ LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent);
+
+ } catch (java.net.SocketTimeoutException e) {
+ // Ignore -- we want to timeout to enable checking whether we
+ // should stop listening periodically
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
+ public void startFinding() {
+ if (mSocket != null)
+ return;
+
+ mFinishRequested = false;
+
+ if (mListenerThread == null) {
+ mListenerThread = new Thread() {
+ @Override
+ public void run() {
+ long aTime = 0;
+ try {
+ mSocket = new DatagramSocket();
+ mSocket.setSoTimeout(1000 * 10);
+ while (!mFinishRequested) {
+ System.out.println("SF:Looping");
+ if (System.currentTimeMillis() - aTime > SEARCH_INTERVAL) {
+ System.out.println("SF:Sending");
+ String aString = "LOREMOTE_SEARCH\n";
+ DatagramPacket aPacket = new DatagramPacket(
+ aString.getBytes(CHARSET),
+ aString.length(),
+ InetAddress.getByName(GROUPADDRESS),
+ PORT);
+ mSocket.send(aPacket);
+ aTime = System.currentTimeMillis();
+ for (Server aServer : mServerList.values()) {
+ if (System.currentTimeMillis()
+ - aServer.getTimeDiscovered() > 60 * 1000) {
+ mServerList.remove(aServer.getAddress());
+ Intent aIntent = new Intent(
+ CommunicationService.MSG_SERVERLIST_CHANGED);
+ LocalBroadcastManager.getInstance(
+ mContext)
+ .sendBroadcast(aIntent);
+
+ }
+ }
+ }
+
+ listenForServer();
+ }
+ } catch (SocketException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (UnsupportedEncodingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+ };
+ mListenerThread.start();
+ }
+
+ }
+
+ public void stopFinding() {
+ if (mListenerThread != null) {
+ mFinishRequested = true;
+ mListenerThread = null;
+ }
+ }
+
+ public Server[] getServerList() {
+ return mServerList.values().toArray(new Server[mServerList.size()]);
+ }
}