diff options
author | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-08-03 10:18:16 +0200 |
---|---|---|
committer | Michael Meeks <michael.meeks@suse.com> | 2012-08-06 10:23:09 +0100 |
commit | 3213339132be43ce8ca502924c741cfac53ed09e (patch) | |
tree | b2b85e65b3d4291c42223919b148ee585ce1b174 /android | |
parent | 5e365b05683402dcdcf527e74e472e844cff15a0 (diff) |
Server list caching.
Change-Id: I8d4508ab54a0dc0240bb677e6a9dcfdf449c5094
Diffstat (limited to 'android')
-rw-r--r-- | android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java | 24 | ||||
-rw-r--r-- | android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java | 39 |
2 files changed, 42 insertions, 21 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index e1669cc8497c..432946042b96 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -42,6 +42,8 @@ public class CommunicationService extends Service { private Receiver mReceiver = new Receiver(); + private ServerFinder mFinder = new ServerFinder(); + public void setActivityMessenger(Messenger aActivityMessenger) { mReceiver.setActivityMessenger(aActivityMessenger); } @@ -55,8 +57,7 @@ public class CommunicationService extends Service { @Override public void onCreate() { // TODO Create a notification (if configured). - ServerFinder aFinder = new ServerFinder(); - aFinder.startFinding(); + mFinder.startFinding(); } @Override @@ -68,6 +69,10 @@ public class CommunicationService extends Service { return mTransmitter; } + public Server[] getServers() { + return mFinder.getServerList(); + } + /** * Connect to a specific server. This method cannot be called on the main * activity thread. @@ -102,10 +107,6 @@ public class CommunicationService extends Service { } - public Server[] getServers() { - return null; - } - public void disconnect() { mClient.closeConnection(); } @@ -118,15 +119,18 @@ public class CommunicationService extends Service { /** * Class describing a remote server. */ - public class Server { + public static class Server { private Protocol mProtocol; private String mAddress; private String mName; + private long mTimeDiscovered; - protected Server(Protocol aProtocol, String aAddress, String aName) { + protected Server(Protocol aProtocol, String aAddress, String aName, + long aTimeDiscovered) { mProtocol = aProtocol; mAddress = aAddress; mName = aName; + mTimeDiscovered = aTimeDiscovered; } public Protocol getProtocol() { @@ -146,6 +150,10 @@ public class CommunicationService extends Service { return mName; } + public long getTimeDiscovered() { + return mTimeDiscovered; + } + } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java index b78a9a43feb0..768eba599adb 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java @@ -6,6 +6,9 @@ import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; +import java.util.Vector; + +import org.libreoffice.impressremote.communication.CommunicationService.Server; public class ServerFinder { @@ -16,6 +19,10 @@ public class ServerFinder { private Thread mListenerThread = null; + private boolean mFinishRequested = false; + + private Vector<Server> mServerList = new Vector<Server>(); + public ServerFinder() { } @@ -40,17 +47,14 @@ public class ServerFinder { if (i == aBuffer.length || !aCommand.equals("LOREMOTE_ADVERTISE")) { return; } - System.out.println("SF: " + aPacket.getAddress().toString()); - - // for (int j = i + 1; j < aBuffer.length; j++) { - // if (aPacket.getData()[j] == '\n') { - // aAddress = new String(aPacket.getData(), i + 1, j, CHARSET); - // } - // } - // - // if (aAddress != null) { - // System.out.println("Address is :" + aAddress + "\n"); - // } + Server aServer = new Server(CommunicationService.Protocol.NETWORK, + aPacket.getAddress().toString(), "NONAME", + System.currentTimeMillis()); + mServerList.add(aServer); + + } 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(); @@ -62,6 +66,8 @@ public class ServerFinder { if (mSocket != null) return; + mFinishRequested = false; + if (mListenerThread == null) { mListenerThread = new Thread() { @Override @@ -76,7 +82,8 @@ public class ServerFinder { PORT); mSocket.send(aPacket); System.out.println("SF:sent packet\n"); - while (true) { + mSocket.setSoTimeout(1000 * 10); + while (!mFinishRequested) { listenForServer(); } } catch (SocketException e) { @@ -99,7 +106,13 @@ public class ServerFinder { public void stopFinding() { if (mListenerThread != null) { - + mFinishRequested = true; + mListenerThread = null; } } + + public Server[] getServerList() { + return (Server[]) mServerList.toArray(); + } + } |