Use an executor for polling; fixed comparison bugs in PollerImpl.

This commit is contained in:
akwizgran
2012-11-12 14:32:31 +00:00
parent 3940614203
commit 538c3e1b08

View File

@@ -1,15 +1,16 @@
package net.sf.briar.plugins; package net.sf.briar.plugins;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import java.util.Collection; import java.util.Collection;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.plugins.Plugin; import net.sf.briar.api.plugins.Plugin;
import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.transport.ConnectionRegistry; import net.sf.briar.api.transport.ConnectionRegistry;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -17,13 +18,16 @@ import com.google.inject.Inject;
class PollerImpl implements Poller, Runnable { class PollerImpl implements Poller, Runnable {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(PollerImpl.class.getName()); Logger.getLogger(PollerImpl.class.getName());
private final ExecutorService pluginExecutor;
private final ConnectionRegistry connRegistry; private final ConnectionRegistry connRegistry;
private final SortedSet<PollTime> pollTimes; private final SortedSet<PollTime> pollTimes;
@Inject @Inject
PollerImpl(ConnectionRegistry connRegistry) { PollerImpl(@PluginExecutor ExecutorService pluginExecutor,
ConnectionRegistry connRegistry) {
this.pluginExecutor = pluginExecutor;
this.connRegistry = connRegistry; this.connRegistry = connRegistry;
pollTimes = new TreeSet<PollTime>(); pollTimes = new TreeSet<PollTime>();
} }
@@ -49,19 +53,24 @@ class PollerImpl implements Poller, Runnable {
public void run() { public void run() {
while(true) { while(true) {
synchronized(this) { synchronized(this) {
if(pollTimes.isEmpty()) return; if(pollTimes.isEmpty()) {
PollTime p = pollTimes.first(); if(LOG.isLoggable(INFO)) LOG.info("Finished polling");
return;
}
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if(now <= p.time) { final PollTime p = pollTimes.first();
pollTimes.remove(p); if(now >= p.time) {
Collection<ContactId> connected = boolean removed = pollTimes.remove(p);
connRegistry.getConnectedContacts(p.plugin.getId()); assert removed;
try { final Collection<ContactId> connected =
p.plugin.poll(connected); connRegistry.getConnectedContacts(p.plugin.getId());
} catch(RuntimeException e) { if(LOG.isLoggable(INFO))
if(LOG.isLoggable(WARNING)) LOG.info("Polling " + p.plugin.getClass().getName());
LOG.warning("Plugin " + p.plugin.getId() + " " + e); pluginExecutor.submit(new Runnable() {
} public void run() {
p.plugin.poll(connected);
}
});
schedule(p.plugin); schedule(p.plugin);
} else { } else {
try { try {
@@ -92,5 +101,19 @@ class PollerImpl implements Poller, Runnable {
if(time > p.time) return 1; if(time > p.time) return 1;
return 0; return 0;
} }
@Override
public int hashCode() {
return (int) time;
}
@Override
public boolean equals(Object o) {
if(o instanceof PollTime) {
PollTime p = (PollTime) o;
return time == p.time && plugin == p.plugin;
}
return false;
}
} }
} }