Added deletion of stale detected servers, UI fixed.

Change-Id: I I97a833b45e0c95a217004ae4a36e72a314d68d9f
This commit is contained in:
Andrzej J.R. Hunt
2012-08-03 14:59:51 +02:00
committed by Michael Meeks
parent ad72b47df4
commit c56b0a2fe8
4 changed files with 48 additions and 22 deletions

View File

@@ -9,19 +9,19 @@
android:layout_margin="10dip" android:layout_margin="10dip"
android:orientation="vertical" > android:orientation="vertical" >
<TextView
android:id="@+id/selector_label_none"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selector_noservers"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout <LinearLayout
android:id="@+id/selector_container_bluetooth" android:id="@+id/selector_container_bluetooth"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" > android:orientation="vertical" >
<TextView
android:id="@+id/selector_label_none"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selector_noservers"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView <TextView
android:id="@+id/selector_label_bluetooth" android:id="@+id/selector_label_bluetooth"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View File

@@ -70,6 +70,9 @@ public class SelectorActivity extends Activity {
protected void onPause() { protected void onPause() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
super.onPause(); super.onPause();
if (mCommunicationService != null) {
mCommunicationService.stopFindingServers();
}
doUnbindService(); doUnbindService();
} }
@@ -89,6 +92,7 @@ public class SelectorActivity extends Activity {
IBinder aService) { IBinder aService) {
mCommunicationService = ((CommunicationService.CBinder) aService) mCommunicationService = ((CommunicationService.CBinder) aService)
.getService(); .getService();
mCommunicationService.startFindingServers();
} }
@Override @Override
@@ -160,9 +164,8 @@ public class SelectorActivity extends Activity {
.setVisibility((mNetworkServers.size() != 0) ? View.VISIBLE .setVisibility((mNetworkServers.size() != 0) ? View.VISIBLE
: View.GONE); : View.GONE);
mNoServerLabel.setVisibility((mBluetoothServers.size() == 0) mNoServerLabel.setVisibility(((mBluetoothServers.size() == 0) && (mNetworkServers
&& (mNetworkServers.size() == 0) ? View.VISIBLE .size() == 0)) ? View.VISIBLE : View.GONE);
: View.GONE);
} }
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@@ -59,7 +59,6 @@ public class CommunicationService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
// TODO Create a notification (if configured). // TODO Create a notification (if configured).
mFinder.startFinding();
} }
@Override @Override
@@ -75,6 +74,14 @@ public class CommunicationService extends Service {
return mFinder.getServerList(); return mFinder.getServerList();
} }
public void startFindingServers() {
mFinder.startFinding();
}
public void stopFindingServers() {
mFinder.stopFinding();
}
/** /**
* Connect to a specific server. This method cannot be called on the main * Connect to a specific server. This method cannot be called on the main
* activity thread. * activity thread.

View File

@@ -21,6 +21,8 @@ public class ServerFinder {
private static final int PORT = 1598; private static final int PORT = 1598;
private static final String CHARSET = "UTF-8"; private static final String CHARSET = "UTF-8";
private static final long SEARCH_INTERVAL = 1000 * 20;
private DatagramSocket mSocket = null; private DatagramSocket mSocket = null;
private Thread mListenerThread = null; private Thread mListenerThread = null;
@@ -40,9 +42,7 @@ public class ServerFinder {
try { try {
String aCommand = null; String aCommand = null;
String aName = null; String aName = null;
System.out.println("SF:listening for packet\n");
mSocket.receive(aPacket); mSocket.receive(aPacket);
System.out.println("SF:received packet\n");
int i; int i;
for (i = 0; i < aBuffer.length; i++) { for (i = 0; i < aBuffer.length; i++) {
if (aPacket.getData()[i] == '\n') { if (aPacket.getData()[i] == '\n') {
@@ -95,18 +95,34 @@ public class ServerFinder {
mListenerThread = new Thread() { mListenerThread = new Thread() {
@Override @Override
public void run() { public void run() {
long aTime = 0;
try { try {
mSocket = new DatagramSocket(); mSocket = new DatagramSocket();
String aString = "LOREMOTE_SEARCH\n";
DatagramPacket aPacket = new DatagramPacket(
aString.getBytes(CHARSET),
aString.length(),
InetAddress.getByName("239.0.0.1"),
PORT);
mSocket.send(aPacket);
System.out.println("SF:sent packet\n");
mSocket.setSoTimeout(1000 * 10); mSocket.setSoTimeout(1000 * 10);
while (!mFinishRequested) { 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(); listenForServer();
} }
} catch (SocketException e) { } catch (SocketException e) {
@@ -135,6 +151,6 @@ public class ServerFinder {
} }
public Server[] getServerList() { public Server[] getServerList() {
return mServerList.entrySet().toArray(new Server[mServerList.size()]); return mServerList.values().toArray(new Server[mServerList.size()]);
} }
} }