Properly fix the timer crash

This commit is contained in:
Cameron Gutman 2014-09-20 02:34:06 -07:00
parent 93bf28b87d
commit 44366db4d5

View File

@ -23,7 +23,6 @@ public class MdnsDiscoveryAgent {
private JmDNS resolver; private JmDNS resolver;
private HashMap<InetAddress, MdnsComputer> computers; private HashMap<InetAddress, MdnsComputer> computers;
private Timer discoveryTimer;
private MdnsDiscoveryListener listener; private MdnsDiscoveryListener listener;
private HashSet<String> pendingResolution; private HashSet<String> pendingResolution;
private boolean stop; private boolean stop;
@ -102,45 +101,44 @@ public class MdnsDiscoveryAgent {
public void startDiscovery(final int discoveryIntervalMs) { public void startDiscovery(final int discoveryIntervalMs) {
stop = false; stop = false;
discoveryTimer = new Timer(); final Timer t = new Timer();
discoveryTimer.schedule(new TimerTask() { t.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
// Close the old resolver synchronized (MdnsDiscoveryAgent.this) {
if (resolver != null) { // Close the old resolver
try { if (resolver != null) {
resolver.close(); try {
} catch (IOException e) {} resolver.close();
resolver = null; } catch (IOException e) {}
} resolver = null;
// Stop if requested
if (stop) {
// There will be no further timer invocations now
if (discoveryTimer != null) {
discoveryTimer.cancel();
discoveryTimer = null;
} }
return;
}
// Create a new resolver // Stop if requested
try { if (stop) {
resolver = JmDNS.create(new InetSocketAddress(0).getAddress()); // There will be no further timer invocations now
} catch (IOException e) { t.cancel();
// This is fine; the network is probably not up return;
return; }
}
// Send another mDNS query // Create a new resolver
resolver.addServiceListener(SERVICE_TYPE, nvstreamListener); try {
resolver.requestServiceInfo(SERVICE_TYPE, null, discoveryIntervalMs); resolver = JmDNS.create(new InetSocketAddress(0).getAddress());
} catch (IOException e) {
// This is fine; the network is probably not up
return;
}
// Run service resolution again for pending machines // Send another mDNS query
ArrayList<String> pendingNames = new ArrayList<String>(pendingResolution); resolver.addServiceListener(SERVICE_TYPE, nvstreamListener);
for (String name : pendingNames) { resolver.requestServiceInfo(SERVICE_TYPE, null, discoveryIntervalMs);
LimeLog.info("mDNS: Retrying service resolution for machine: "+name);
resolver.getServiceInfo(SERVICE_TYPE, name); // Run service resolution again for pending machines
ArrayList<String> pendingNames = new ArrayList<String>(pendingResolution);
for (String name : pendingNames) {
LimeLog.info("mDNS: Retrying service resolution for machine: "+name);
resolver.getServiceInfo(SERVICE_TYPE, name);
}
} }
} }
}, 0, discoveryIntervalMs); }, 0, discoveryIntervalMs);