mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Code cleanup and logging.
This commit is contained in:
@@ -50,24 +50,27 @@ class LifecycleManagerImpl implements LifecycleManager {
|
|||||||
executors = new CopyOnWriteArrayList<ExecutorService>();
|
executors = new CopyOnWriteArrayList<ExecutorService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void registerService(Service s) {
|
public void registerService(Service s) {
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Registering service " + s.getClass().getName());
|
LOG.info("Registering service " + s.getClass().getName());
|
||||||
services.add(s);
|
services.add(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void registerClient(Client c) {
|
public void registerClient(Client c) {
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Registering client " + c.getClass().getName());
|
LOG.info("Registering client " + c.getClass().getName());
|
||||||
clients.add(c);
|
clients.add(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void registerForShutdown(ExecutorService e) {
|
public void registerForShutdown(ExecutorService e) {
|
||||||
if (LOG.isLoggable(INFO))
|
LOG.info("Registering executor");
|
||||||
LOG.info("Registering executor " + e.getClass().getName());
|
|
||||||
executors.add(e);
|
executors.add(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public StartResult startServices() {
|
public StartResult startServices() {
|
||||||
if (!startStopSemaphore.tryAcquire()) {
|
if (!startStopSemaphore.tryAcquire()) {
|
||||||
LOG.info("Already starting or stopping");
|
LOG.info("Already starting or stopping");
|
||||||
@@ -91,7 +94,7 @@ class LifecycleManagerImpl implements LifecycleManager {
|
|||||||
c.createLocalState(txn);
|
c.createLocalState(txn);
|
||||||
duration = System.currentTimeMillis() - start;
|
duration = System.currentTimeMillis() - start;
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Starting " + c.getClass().getName()
|
LOG.info("Starting client " + c.getClass().getName()
|
||||||
+ " took " + duration + " ms");
|
+ " took " + duration + " ms");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,7 +107,7 @@ class LifecycleManagerImpl implements LifecycleManager {
|
|||||||
s.startService();
|
s.startService();
|
||||||
duration = System.currentTimeMillis() - start;
|
duration = System.currentTimeMillis() - start;
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Starting " + s.getClass().getName()
|
LOG.info("Starting service " + s.getClass().getName()
|
||||||
+ " took " + duration + " ms");
|
+ " took " + duration + " ms");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -121,6 +124,7 @@ class LifecycleManagerImpl implements LifecycleManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void stopServices() {
|
public void stopServices() {
|
||||||
try {
|
try {
|
||||||
startStopSemaphore.acquire();
|
startStopSemaphore.acquire();
|
||||||
@@ -132,15 +136,22 @@ class LifecycleManagerImpl implements LifecycleManager {
|
|||||||
LOG.info("Stopping services");
|
LOG.info("Stopping services");
|
||||||
eventBus.broadcast(new ShutdownEvent());
|
eventBus.broadcast(new ShutdownEvent());
|
||||||
for (Service s : services) {
|
for (Service s : services) {
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
s.stopService();
|
s.stopService();
|
||||||
if (LOG.isLoggable(INFO))
|
long duration = System.currentTimeMillis() - start;
|
||||||
LOG.info("Service stopped: " + s.getClass().getName());
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info("Stopping service " + s.getClass().getName()
|
||||||
|
+ " took " + duration + " ms");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (ExecutorService e : executors) e.shutdownNow();
|
for (ExecutorService e : executors) e.shutdownNow();
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info(executors.size() + " executors shut down");
|
LOG.info(executors.size() + " executors shut down");
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
db.close();
|
db.close();
|
||||||
LOG.info("Database closed");
|
long duration = System.currentTimeMillis() - start;
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info("Closing database took " + duration + " ms");
|
||||||
shutdownLatch.countDown();
|
shutdownLatch.countDown();
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||||
@@ -151,14 +162,17 @@ class LifecycleManagerImpl implements LifecycleManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void waitForDatabase() throws InterruptedException {
|
public void waitForDatabase() throws InterruptedException {
|
||||||
dbLatch.await();
|
dbLatch.await();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void waitForStartup() throws InterruptedException {
|
public void waitForStartup() throws InterruptedException {
|
||||||
startupLatch.await();
|
startupLatch.await();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void waitForShutdown() throws InterruptedException {
|
public void waitForShutdown() throws InterruptedException {
|
||||||
shutdownLatch.await();
|
shutdownLatch.await();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,8 +92,7 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
LOG.info("Starting simplex plugins");
|
LOG.info("Starting simplex plugins");
|
||||||
for (SimplexPluginFactory f : simplexFactories) {
|
for (SimplexPluginFactory f : simplexFactories) {
|
||||||
TransportId t = f.getId();
|
TransportId t = f.getId();
|
||||||
SimplexPluginCallback c = new SimplexCallback(t);
|
SimplexPlugin s = f.createPlugin(new SimplexCallback(t));
|
||||||
SimplexPlugin s = f.createPlugin(c);
|
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
if (LOG.isLoggable(WARNING))
|
if (LOG.isLoggable(WARNING))
|
||||||
LOG.warning("Could not create plugin for " + t);
|
LOG.warning("Could not create plugin for " + t);
|
||||||
@@ -108,8 +107,7 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
LOG.info("Starting duplex plugins");
|
LOG.info("Starting duplex plugins");
|
||||||
for (DuplexPluginFactory f : duplexFactories) {
|
for (DuplexPluginFactory f : duplexFactories) {
|
||||||
TransportId t = f.getId();
|
TransportId t = f.getId();
|
||||||
DuplexPluginCallback c = new DuplexCallback(t);
|
DuplexPlugin d = f.createPlugin(new DuplexCallback(t));
|
||||||
DuplexPlugin d = f.createPlugin(c);
|
|
||||||
if (d == null) {
|
if (d == null) {
|
||||||
if (LOG.isLoggable(WARNING))
|
if (LOG.isLoggable(WARNING))
|
||||||
LOG.warning("Could not create plugin for " + t);
|
LOG.warning("Could not create plugin for " + t);
|
||||||
@@ -154,14 +152,12 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<SimplexPlugin> getSimplexPlugins() {
|
public Collection<SimplexPlugin> getSimplexPlugins() {
|
||||||
List<SimplexPlugin> copy = new ArrayList<SimplexPlugin>(simplexPlugins);
|
return Collections.unmodifiableList(simplexPlugins);
|
||||||
return Collections.unmodifiableList(copy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<DuplexPlugin> getDuplexPlugins() {
|
public Collection<DuplexPlugin> getDuplexPlugins() {
|
||||||
List<DuplexPlugin> copy = new ArrayList<DuplexPlugin>(duplexPlugins);
|
return Collections.unmodifiableList(duplexPlugins);
|
||||||
return Collections.unmodifiableList(copy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -199,14 +195,13 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
long duration = System.currentTimeMillis() - start;
|
long duration = System.currentTimeMillis() - start;
|
||||||
if (started) {
|
if (started) {
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
String name = plugin.getClass().getSimpleName();
|
LOG.info("Starting plugin " + plugin.getId()
|
||||||
LOG.info("Starting " + name + " took " +
|
+ " took " + duration + " ms");
|
||||||
duration + " ms");
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (LOG.isLoggable(WARNING)) {
|
if (LOG.isLoggable(WARNING)) {
|
||||||
String name = plugin.getClass().getSimpleName();
|
LOG.warning("Plugin" + plugin.getId()
|
||||||
LOG.warning(name + " did not start");
|
+ " did not start");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -236,8 +231,8 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
plugin.stop();
|
plugin.stop();
|
||||||
long duration = System.currentTimeMillis() - start;
|
long duration = System.currentTimeMillis() - start;
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
String name = plugin.getClass().getSimpleName();
|
LOG.info("Stopping plugin " + plugin.getId()
|
||||||
LOG.info("Stopping " + name + " took " + duration + " ms");
|
+ " took " + duration + " ms");
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||||
|
|||||||
@@ -53,11 +53,11 @@ class Poller implements EventListener {
|
|||||||
ConnectionRegistry connectionRegistry, PluginManager pluginManager,
|
ConnectionRegistry connectionRegistry, PluginManager pluginManager,
|
||||||
SecureRandom random, Clock clock) {
|
SecureRandom random, Clock clock) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
|
this.scheduler = scheduler;
|
||||||
this.connectionManager = connectionManager;
|
this.connectionManager = connectionManager;
|
||||||
this.connectionRegistry = connectionRegistry;
|
this.connectionRegistry = connectionRegistry;
|
||||||
this.pluginManager = pluginManager;
|
this.pluginManager = pluginManager;
|
||||||
this.random = random;
|
this.random = random;
|
||||||
this.scheduler = scheduler;
|
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
lock = new ReentrantLock();
|
lock = new ReentrantLock();
|
||||||
tasks = new HashMap<TransportId, PollTask>();
|
tasks = new HashMap<TransportId, PollTask>();
|
||||||
@@ -147,9 +147,9 @@ class Poller implements EventListener {
|
|||||||
private void schedule(Plugin p, int delay, boolean randomiseNext) {
|
private void schedule(Plugin p, int delay, boolean randomiseNext) {
|
||||||
// Replace any later scheduled task for this plugin
|
// Replace any later scheduled task for this plugin
|
||||||
long due = clock.currentTimeMillis() + delay;
|
long due = clock.currentTimeMillis() + delay;
|
||||||
|
TransportId t = p.getId();
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
TransportId t = p.getId();
|
|
||||||
PollTask scheduled = tasks.get(t);
|
PollTask scheduled = tasks.get(t);
|
||||||
if (scheduled == null || due < scheduled.due) {
|
if (scheduled == null || due < scheduled.due) {
|
||||||
PollTask task = new PollTask(p, due, randomiseNext);
|
PollTask task = new PollTask(p, due, randomiseNext);
|
||||||
@@ -165,9 +165,9 @@ class Poller implements EventListener {
|
|||||||
ioExecutor.execute(new Runnable() {
|
ioExecutor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (LOG.isLoggable(INFO))
|
TransportId t = p.getId();
|
||||||
LOG.info("Polling " + p.getClass().getSimpleName());
|
if (LOG.isLoggable(INFO)) LOG.info("Polling plugin " + t);
|
||||||
p.poll(connectionRegistry.getConnectedContacts(p.getId()));
|
p.poll(connectionRegistry.getConnectedContacts(t));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,12 @@ public class PluginManagerImplTest extends BriarTestCase {
|
|||||||
final TransportId duplexFailId = new TransportId("duplex1");
|
final TransportId duplexFailId = new TransportId("duplex1");
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
|
allowing(simplexPlugin).getId();
|
||||||
|
will(returnValue(simplexId));
|
||||||
|
allowing(simplexFailPlugin).getId();
|
||||||
|
will(returnValue(simplexFailId));
|
||||||
|
allowing(duplexPlugin).getId();
|
||||||
|
will(returnValue(duplexId));
|
||||||
// start()
|
// start()
|
||||||
// First simplex plugin
|
// First simplex plugin
|
||||||
oneOf(pluginConfig).getSimplexFactories();
|
oneOf(pluginConfig).getSimplexFactories();
|
||||||
|
|||||||
Reference in New Issue
Block a user