Moved lifecycle management into briar-core and reconfigured executors.

CryptoExecutor and DatabaseExecutor now use bounded thread pools with
unbounded queues, since running too many tasks in parallel is likely to
harm performance; IncomingConnectionExecutor, PluginExecutor and
ReliabilityExecutor use unbounded thread pools with direct handoff,
since their tasks may run indefinitely. There are no longer any bounded
executors, and all executors discard tasks when shutting down, which
fixes issue #3612189.

Responsibility for starting and stopping services has been moved from
BriarService in briar-android to LifecycleManagerImpl in briar-core.
However, BriarService is still responsible for stopping the
Android-specific executors, which is ugly. It would be better if
executors registered themselves with LifecycleManager.
This commit is contained in:
akwizgran
2013-05-04 01:25:30 +01:00
parent e1842e11c5
commit 673d7fa0c3
44 changed files with 432 additions and 583 deletions

View File

@@ -1,11 +1,16 @@
package net.sf.briar.android;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import net.sf.briar.api.android.AndroidExecutor;
import net.sf.briar.api.android.DatabaseUiExecutor;
@@ -34,15 +39,22 @@ public class AndroidModule extends AbstractModule {
bind(AndroidExecutor.class).to(AndroidExecutorImpl.class);
bind(ReferenceManager.class).to(ReferenceManagerImpl.class).in(
Singleton.class);
// Use a single thread so DB accesses from the UI don't overlap, with
// an unbounded queue so submissions don't block
bind(Executor.class).annotatedWith(DatabaseUiExecutor.class).toInstance(
Executors.newSingleThreadExecutor());
// The queue is unbounded, so tasks can be dependent
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
// Discard tasks that are submitted during shutdown
RejectedExecutionHandler policy =
new ThreadPoolExecutor.DiscardPolicy();
// Use a single thread so DB accesses from the UI don't overlap
ExecutorService e = new ThreadPoolExecutor(1, 1, 60, SECONDS, queue,
policy);
bind(Executor.class).annotatedWith(
DatabaseUiExecutor.class).toInstance(e);
bind(ExecutorService.class).annotatedWith(
DatabaseUiExecutor.class).toInstance(e);
}
@Provides
SimplexPluginConfig getSimplexPluginConfig(
@PluginExecutor ExecutorService pluginExecutor) {
SimplexPluginConfig getSimplexPluginConfig() {
return new SimplexPluginConfig() {
public Collection<SimplexPluginFactory> getFactories() {
return Collections.emptyList();
@@ -52,7 +64,7 @@ public class AndroidModule extends AbstractModule {
@Provides
DuplexPluginConfig getDuplexPluginConfig(
@PluginExecutor ExecutorService pluginExecutor,
@PluginExecutor Executor pluginExecutor,
AndroidExecutor androidExecutor, Context appContext,
CryptoComponent crypto, ShutdownManager shutdownManager) {
DuplexPluginFactory droidtooth = new DroidtoothPluginFactory(

View File

@@ -3,18 +3,15 @@ package net.sf.briar.android;
import static android.app.PendingIntent.FLAG_ONE_SHOT;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.api.crypto.KeyManager;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseConfig;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.plugins.PluginManager;
import net.sf.briar.api.android.AndroidExecutor;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.lifecycle.LifecycleManager;
import roboguice.service.RoboService;
import android.app.PendingIntent;
import android.content.ComponentName;
@@ -31,15 +28,12 @@ public class BriarService extends RoboService {
private static final Logger LOG =
Logger.getLogger(BriarService.class.getName());
private final CountDownLatch dbLatch = new CountDownLatch(1);
private final CountDownLatch startupLatch = new CountDownLatch(1);
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
private final Binder binder = new BriarBinder();
@Inject private DatabaseConfig databaseConfig = null;
@Inject private DatabaseComponent db = null;
@Inject private KeyManager keyManager = null;
@Inject private PluginManager pluginManager = null;
// Fields that are accessed from background threads must be volatile
@Inject private volatile LifecycleManager lifecycleManager;
@Inject private volatile AndroidExecutor androidExecutor;
@Inject @DatabaseUiExecutor private volatile ExecutorService dbUiExecutor;
@Override
public void onCreate() {
@@ -63,7 +57,7 @@ public class BriarService extends RoboService {
new Thread() {
@Override
public void run() {
startServices();
lifecycleManager.startServices();
}
}.start();
}
@@ -71,11 +65,11 @@ public class BriarService extends RoboService {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(LOG.isLoggable(INFO)) LOG.info("Started");
return START_STICKY;
return START_NOT_STICKY; // Don't restart automatically if killed
}
@Override
public IBinder onBind(Intent intent) {
if(LOG.isLoggable(INFO)) LOG.info("Bound");
return binder;
}
@@ -87,71 +81,38 @@ public class BriarService extends RoboService {
new Thread() {
@Override
public void run() {
stopServices();
// FIXME: This is ugly - executors should register themselves
// with the lifecycle manager
androidExecutor.shutdown();
dbUiExecutor.shutdown();
lifecycleManager.stopServices();
}
}.start();
}
private void startServices() {
if(databaseConfig.getEncryptionKey() == null)
throw new IllegalStateException();
try {
if(LOG.isLoggable(INFO)) LOG.info("Starting");
boolean reopened = db.open();
if(LOG.isLoggable(INFO)) {
if(reopened) LOG.info("Database reopened");
else LOG.info("Database created");
}
dbLatch.countDown();
keyManager.start();
if(LOG.isLoggable(INFO)) LOG.info("Key manager started");
int pluginsStarted = pluginManager.start();
if(LOG.isLoggable(INFO))
LOG.info(pluginsStarted + " plugins started");
startupLatch.countDown();
} catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
private void stopServices() {
try {
if(LOG.isLoggable(INFO)) LOG.info("Shutting down");
int pluginsStopped = pluginManager.stop();
if(LOG.isLoggable(INFO))
LOG.info(pluginsStopped + " plugins stopped");
keyManager.stop();
if(LOG.isLoggable(INFO)) LOG.info("Key manager stopped");
db.close();
if(LOG.isLoggable(INFO)) LOG.info("Database closed");
shutdownLatch.countDown();
} catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
/** Waits for the database to be opened before returning. */
public void waitForDatabase() throws InterruptedException {
dbLatch.await();
lifecycleManager.waitForDatabase();
}
/** Waits for all services to start before returning. */
public void waitForStartup() throws InterruptedException {
startupLatch.await();
lifecycleManager.waitForStartup();
}
/** Waits for all services to stop before returning. */
public void waitForShutdown() throws InterruptedException {
shutdownLatch.await();
lifecycleManager.waitForShutdown();
}
/** Starts the shutdown process. */
public void shutdown() {
stopSelf();
stopSelf(); // This will call onDestroy()
}
public class BriarBinder extends Binder {
/** Returns the bound service. */
public BriarService getService() {
return BriarService.this;
}
@@ -170,19 +131,10 @@ public class BriarService extends RoboService {
public void onServiceDisconnected(ComponentName name) {}
/** Waits for the service to connect and returns its binder. */
public IBinder waitForBinder() throws InterruptedException {
binderLatch.await();
return binder;
}
public void waitForDatabase() throws InterruptedException {
waitForBinder();
((BriarBinder) binder).getService().waitForDatabase();
}
public void waitForStartup() throws InterruptedException {
waitForBinder();
((BriarBinder) binder).getService().waitForStartup();
}
}
}

View File

@@ -35,6 +35,7 @@ import net.sf.briar.api.crypto.CryptoExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseConfig;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.util.StringUtils;
import roboguice.activity.RoboActivity;
import android.content.Intent;
@@ -70,18 +71,19 @@ public class HomeScreenActivity extends RoboActivity {
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
@Inject private ReferenceManager referenceManager = null;
@Inject private DatabaseConfig databaseConfig = null;
@Inject @DatabaseUiExecutor private Executor dbUiExecutor = null;
@Inject @CryptoExecutor private Executor cryptoExecutor = null;
@Inject private ReferenceManager referenceManager;
@Inject private DatabaseConfig databaseConfig;
@Inject @DatabaseUiExecutor private Executor dbUiExecutor;
@Inject @CryptoExecutor private Executor cryptoExecutor;
private boolean bound = false;
private TextView enterPassword = null;
private Button continueButton = null;
private ProgressBar progress = null;
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db = null;
@Inject private volatile CryptoComponent crypto = null;
@Inject private volatile CryptoComponent crypto;
@Inject private volatile DatabaseComponent db;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -136,7 +138,7 @@ public class HomeScreenActivity extends RoboActivity {
@Override
public void run() {
try {
// Wait for the service to be bound and started
// Wait for the service to finish starting up
IBinder binder = serviceConnection.waitForBinder();
BriarService service = ((BriarBinder) binder).getService();
service.waitForStartup();
@@ -146,7 +148,7 @@ public class HomeScreenActivity extends RoboActivity {
service.waitForShutdown();
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
LOG.info("Interrupted while waiting for service");
}
// Finish the activity and kill the JVM
runOnUiThread(new Runnable() {
@@ -164,7 +166,7 @@ public class HomeScreenActivity extends RoboActivity {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.addLocalAuthor(a);
db.setRating(a.getId(), GOOD);

View File

@@ -17,8 +17,6 @@ import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.AscendingHeaderComparator;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.ListLoadingProgressBar;
import net.sf.briar.api.Author;
@@ -33,6 +31,7 @@ import net.sf.briar.api.db.event.GroupMessageAddedEvent;
import net.sf.briar.api.db.event.MessageExpiredEvent;
import net.sf.briar.api.db.event.RatingChangedEvent;
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.GroupId;
import roboguice.activity.RoboFragmentActivity;
import android.content.Intent;
@@ -53,9 +52,6 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(BlogActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private String groupName = null;
private boolean postable = false;
private BlogAdapter adapter = null;
@@ -65,6 +61,7 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile GroupId groupId = null;
@Override
@@ -107,10 +104,6 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
layout.addView(composeButton);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -124,7 +117,7 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<GroupMessageHeader> headers =
db.getGroupMessageHeaders(groupId);
@@ -196,12 +189,6 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
db.removeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof GroupMessageAddedEvent) {
GroupMessageAddedEvent g = (GroupMessageAddedEvent) e;

View File

@@ -21,8 +21,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.HorizontalSpace;
import net.sf.briar.android.widgets.ListLoadingProgressBar;
@@ -38,6 +36,7 @@ import net.sf.briar.api.db.event.MessageExpiredEvent;
import net.sf.briar.api.db.event.RemoteSubscriptionsUpdatedEvent;
import net.sf.briar.api.db.event.SubscriptionAddedEvent;
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.Group;
import net.sf.briar.api.messaging.GroupId;
import net.sf.briar.api.messaging.GroupStatus;
@@ -61,9 +60,6 @@ OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(BlogListActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private BlogListAdapter adapter = null;
private ListView list = null;
private ListLoadingProgressBar loading = null;
@@ -73,6 +69,7 @@ OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -126,10 +123,6 @@ OnItemClickListener {
layout.addView(footer);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -144,7 +137,7 @@ OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Set<GroupId> local = new HashSet<GroupId>();
for(Group g : db.getLocalGroups()) local.add(g.getId());
@@ -252,12 +245,6 @@ OnItemClickListener {
db.removeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof GroupMessageAddedEvent) {
Group g = ((GroupMessageAddedEvent) e).getGroup();
@@ -292,7 +279,7 @@ OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<GroupMessageHeader> headers =
db.getGroupMessageHeaders(g.getId());
@@ -333,7 +320,7 @@ OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
int available = 0;
long now = System.currentTimeMillis();
for(GroupStatus s : db.getAvailableGroups()) {

View File

@@ -15,8 +15,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.contact.SelectContactsDialog;
import net.sf.briar.android.invitation.AddContactActivity;
import net.sf.briar.android.messages.NoContactsDialog;
@@ -25,6 +23,7 @@ import net.sf.briar.api.ContactId;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.Group;
import net.sf.briar.api.messaging.GroupId;
import roboguice.activity.RoboFragmentActivity;
@@ -48,9 +47,6 @@ SelectContactsDialog.Listener {
private static final Logger LOG =
Logger.getLogger(ConfigureBlogActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private boolean subscribed = false;
private CheckBox subscribeCheckBox = null;
private RadioGroup radioGroup = null;
@@ -61,6 +57,7 @@ SelectContactsDialog.Listener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile Group group = null;
private volatile Collection<ContactId> selected = Collections.emptyList();
@@ -127,16 +124,6 @@ SelectContactsDialog.Listener {
layout.addView(progress);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onClick(View view) {
@@ -164,7 +151,7 @@ SelectContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;
@@ -208,7 +195,7 @@ SelectContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
if(subscribe) {
if(!wasSubscribed) db.subscribe(group);

View File

@@ -21,8 +21,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.contact.SelectContactsDialog;
import net.sf.briar.android.invitation.AddContactActivity;
import net.sf.briar.android.messages.NoContactsDialog;
@@ -33,6 +31,7 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.CryptoExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.GroupFactory;
import net.sf.briar.api.messaging.LocalGroup;
import roboguice.activity.RoboFragmentActivity;
@@ -60,9 +59,6 @@ SelectContactsDialog.Listener {
private static final Logger LOG =
Logger.getLogger(CreateBlogActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
@Inject @CryptoExecutor private Executor cryptoExecutor;
private EditText nameEntry = null;
private RadioGroup radioGroup = null;
@@ -75,6 +71,7 @@ SelectContactsDialog.Listener {
@Inject private volatile GroupFactory groupFactory;
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile Collection<ContactId> selected = Collections.emptyList();
@Override
@@ -135,10 +132,6 @@ SelectContactsDialog.Listener {
layout.addView(progress);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
private void enableOrDisableCreateButton() {
@@ -149,12 +142,6 @@ SelectContactsDialog.Listener {
createButton.setEnabled(nameNotEmpty && visibilitySelected);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
validateName();
return true;
@@ -197,7 +184,7 @@ SelectContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;
@@ -240,7 +227,7 @@ SelectContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.addLocalGroup(g);
db.subscribe(g);

View File

@@ -13,8 +13,6 @@ import java.util.List;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.ListLoadingProgressBar;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.db.DatabaseComponent;
@@ -24,6 +22,7 @@ import net.sf.briar.api.db.event.DatabaseListener;
import net.sf.briar.api.db.event.RemoteSubscriptionsUpdatedEvent;
import net.sf.briar.api.db.event.SubscriptionAddedEvent;
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.Group;
import net.sf.briar.api.messaging.GroupStatus;
import roboguice.activity.RoboFragmentActivity;
@@ -42,9 +41,6 @@ implements DatabaseListener, OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(ManageBlogsActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private ManageBlogsAdapter adapter = null;
private ListView list = null;
private ListLoadingProgressBar loading = null;
@@ -52,6 +48,7 @@ implements DatabaseListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -66,10 +63,6 @@ implements DatabaseListener, OnItemClickListener {
// Show a progress bar while the list is loading
loading = new ListLoadingProgressBar(this);
setContentView(loading);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -83,7 +76,7 @@ implements DatabaseListener, OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
List<GroupStatus> available = new ArrayList<GroupStatus>();
for(GroupStatus s : db.getAvailableGroups())
@@ -125,12 +118,6 @@ implements DatabaseListener, OnItemClickListener {
db.removeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof RemoteSubscriptionsUpdatedEvent) {
if(LOG.isLoggable(INFO))

View File

@@ -19,14 +19,13 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.HorizontalSpace;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.db.NoSuchMessageException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.GroupId;
import net.sf.briar.api.messaging.MessageId;
import net.sf.briar.api.messaging.Rating;
@@ -55,9 +54,6 @@ implements OnClickListener {
private static final Logger LOG =
Logger.getLogger(ReadBlogPostActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private GroupId groupId = null;
private boolean postable = false;
private Rating rating = UNRATED;
@@ -70,6 +66,7 @@ implements OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile MessageId messageId = null;
@Override
@@ -196,17 +193,13 @@ implements OnClickListener {
layout.addView(footer);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
private void setReadInDatabase(final boolean read) {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.setReadFlag(messageId, read);
long duration = System.currentTimeMillis() - now;
@@ -239,7 +232,7 @@ implements OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
byte[] body = db.getMessageBody(messageId);
long duration = System.currentTimeMillis() - now;
@@ -278,12 +271,6 @@ implements OnClickListener {
state.putBoolean("net.sf.briar.READ", read);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onClick(View view) {
if(view == readButton) {
setReadInDatabase(!read);

View File

@@ -17,8 +17,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.identity.CreateIdentityActivity;
import net.sf.briar.android.identity.LocalAuthorItem;
import net.sf.briar.android.identity.LocalAuthorItemComparator;
@@ -31,6 +29,7 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.KeyParser;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.GroupId;
import net.sf.briar.api.messaging.LocalGroup;
import net.sf.briar.api.messaging.Message;
@@ -58,9 +57,6 @@ implements OnItemSelectedListener, OnClickListener {
private static final Logger LOG =
Logger.getLogger(WriteBlogPostActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
@Inject private CryptoComponent crypto;
@Inject private MessageFactory messageFactory;
private LocalAuthorSpinnerAdapter fromAdapter = null;
@@ -74,6 +70,7 @@ implements OnItemSelectedListener, OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile LocalAuthor localAuthor = null;
private volatile LocalGroup localGroup = null;
private volatile MessageId parentId = null;
@@ -152,10 +149,6 @@ implements OnItemSelectedListener, OnClickListener {
layout.addView(content);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -169,7 +162,7 @@ implements OnItemSelectedListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<LocalAuthor> localAuthors = db.getLocalAuthors();
long duration = System.currentTimeMillis() - now;
@@ -216,7 +209,7 @@ implements OnItemSelectedListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<LocalGroup> groups = db.getLocalGroups();
long duration = System.currentTimeMillis() - now;
@@ -269,12 +262,6 @@ implements OnItemSelectedListener, OnClickListener {
}
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(parent == fromSpinner) {
@@ -350,7 +337,7 @@ implements OnItemSelectedListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.addLocalGroupMessage(m);
long duration = System.currentTimeMillis() - now;

View File

@@ -22,8 +22,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.invitation.AddContactActivity;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.HorizontalSpace;
@@ -37,6 +35,7 @@ import net.sf.briar.api.db.event.ContactAddedEvent;
import net.sf.briar.api.db.event.ContactRemovedEvent;
import net.sf.briar.api.db.event.DatabaseEvent;
import net.sf.briar.api.db.event.DatabaseListener;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.transport.ConnectionListener;
import net.sf.briar.api.transport.ConnectionRegistry;
import roboguice.activity.RoboActivity;
@@ -60,9 +59,6 @@ ConnectionListener {
private static final Logger LOG =
Logger.getLogger(ContactListActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
@Inject private ConnectionRegistry connectionRegistry;
private ContactListAdapter adapter = null;
private ListView list = null;
@@ -72,6 +68,7 @@ ConnectionListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -118,10 +115,6 @@ ConnectionListener {
layout.addView(footer);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -136,7 +129,7 @@ ConnectionListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts();
Map<ContactId, Long> times = db.getLastConnected();
@@ -182,12 +175,6 @@ ConnectionListener {
connectionRegistry.removeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onClick(View view) {
if(view == addContactButton) {
startActivity(new Intent(this, AddContactActivity.class));

View File

@@ -15,8 +15,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.contact.SelectContactsDialog;
import net.sf.briar.android.invitation.AddContactActivity;
import net.sf.briar.android.messages.NoContactsDialog;
@@ -25,6 +23,7 @@ import net.sf.briar.api.ContactId;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.Group;
import net.sf.briar.api.messaging.GroupId;
import roboguice.activity.RoboFragmentActivity;
@@ -48,9 +47,6 @@ SelectContactsDialog.Listener {
private static final Logger LOG =
Logger.getLogger(ConfigureGroupActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private boolean subscribed = false;
private CheckBox subscribeCheckBox = null;
private RadioGroup radioGroup = null;
@@ -61,6 +57,7 @@ SelectContactsDialog.Listener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile Group group = null;
private volatile Collection<ContactId> selected = Collections.emptyList();
@@ -125,16 +122,6 @@ SelectContactsDialog.Listener {
layout.addView(progress);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onClick(View view) {
@@ -162,7 +149,7 @@ SelectContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;
@@ -206,7 +193,7 @@ SelectContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
if(subscribe) {
if(!wasSubscribed) db.subscribe(group);

View File

@@ -20,8 +20,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.contact.SelectContactsDialog;
import net.sf.briar.android.invitation.AddContactActivity;
import net.sf.briar.android.messages.NoContactsDialog;
@@ -30,6 +28,7 @@ import net.sf.briar.api.ContactId;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.Group;
import net.sf.briar.api.messaging.GroupFactory;
import roboguice.activity.RoboFragmentActivity;
@@ -57,9 +56,6 @@ SelectContactsDialog.Listener {
private static final Logger LOG =
Logger.getLogger(CreateGroupActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private EditText nameEntry = null;
private RadioGroup radioGroup = null;
private RadioButton visibleToAll = null, visibleToSome = null;
@@ -70,6 +66,7 @@ SelectContactsDialog.Listener {
@Inject private volatile GroupFactory groupFactory;
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile Collection<ContactId> selected = Collections.emptyList();
@Override
@@ -129,10 +126,6 @@ SelectContactsDialog.Listener {
layout.addView(progress);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
private void enableOrDisableCreateButton() {
@@ -143,12 +136,6 @@ SelectContactsDialog.Listener {
createButton.setEnabled(nameNotEmpty && visibilitySelected);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
validateName();
return true;
@@ -172,7 +159,7 @@ SelectContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
Group g = groupFactory.createGroup(name);
long now = System.currentTimeMillis();
db.subscribe(g);
@@ -206,7 +193,7 @@ SelectContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;

View File

@@ -17,8 +17,6 @@ import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.AscendingHeaderComparator;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.ListLoadingProgressBar;
import net.sf.briar.api.Author;
@@ -33,6 +31,7 @@ import net.sf.briar.api.db.event.GroupMessageAddedEvent;
import net.sf.briar.api.db.event.MessageExpiredEvent;
import net.sf.briar.api.db.event.RatingChangedEvent;
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.GroupId;
import roboguice.activity.RoboActivity;
import android.content.Intent;
@@ -53,9 +52,6 @@ OnClickListener, OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(GroupActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private String groupName = null;
private GroupAdapter adapter = null;
private ListView list = null;
@@ -64,6 +60,7 @@ OnClickListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile GroupId groupId = null;
@Override
@@ -105,10 +102,6 @@ OnClickListener, OnItemClickListener {
layout.addView(composeButton);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -122,7 +115,7 @@ OnClickListener, OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<GroupMessageHeader> headers =
db.getGroupMessageHeaders(groupId);
@@ -194,12 +187,6 @@ OnClickListener, OnItemClickListener {
db.removeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof GroupMessageAddedEvent) {
GroupMessageAddedEvent g = (GroupMessageAddedEvent) e;

View File

@@ -19,8 +19,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.HorizontalSpace;
import net.sf.briar.android.widgets.ListLoadingProgressBar;
@@ -36,6 +34,7 @@ import net.sf.briar.api.db.event.MessageExpiredEvent;
import net.sf.briar.api.db.event.RemoteSubscriptionsUpdatedEvent;
import net.sf.briar.api.db.event.SubscriptionAddedEvent;
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.Group;
import net.sf.briar.api.messaging.GroupId;
import net.sf.briar.api.messaging.GroupStatus;
@@ -59,9 +58,6 @@ OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(GroupListActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private GroupListAdapter adapter = null;
private ListView list = null;
private ListLoadingProgressBar loading = null;
@@ -71,6 +67,7 @@ OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -124,10 +121,6 @@ OnItemClickListener {
layout.addView(footer);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -142,7 +135,7 @@ OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
int available = 0;
long now = System.currentTimeMillis();
for(GroupStatus s : db.getAvailableGroups()) {
@@ -247,12 +240,6 @@ OnItemClickListener {
db.removeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof GroupMessageAddedEvent) {
Group g = ((GroupMessageAddedEvent) e).getGroup();
@@ -287,7 +274,7 @@ OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<GroupMessageHeader> headers =
db.getGroupMessageHeaders(g.getId());
@@ -327,7 +314,7 @@ OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
int available = 0;
long now = System.currentTimeMillis();
for(GroupStatus s : db.getAvailableGroups()) {

View File

@@ -13,8 +13,6 @@ import java.util.List;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.ListLoadingProgressBar;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.db.DatabaseComponent;
@@ -24,6 +22,7 @@ import net.sf.briar.api.db.event.DatabaseListener;
import net.sf.briar.api.db.event.RemoteSubscriptionsUpdatedEvent;
import net.sf.briar.api.db.event.SubscriptionAddedEvent;
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.Group;
import net.sf.briar.api.messaging.GroupStatus;
import roboguice.activity.RoboFragmentActivity;
@@ -42,9 +41,6 @@ implements DatabaseListener, OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(ManageGroupsActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private ManageGroupsAdapter adapter = null;
private ListView list = null;
private ListLoadingProgressBar loading = null;
@@ -52,6 +48,7 @@ implements DatabaseListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -66,10 +63,6 @@ implements DatabaseListener, OnItemClickListener {
// Show a progress bar while the list is loading
loading = new ListLoadingProgressBar(this);
setContentView(loading);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -83,7 +76,7 @@ implements DatabaseListener, OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
List<GroupStatus> available = new ArrayList<GroupStatus>();
for(GroupStatus s : db.getAvailableGroups())
@@ -125,12 +118,6 @@ implements DatabaseListener, OnItemClickListener {
db.removeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof RemoteSubscriptionsUpdatedEvent) {
if(LOG.isLoggable(INFO))

View File

@@ -19,8 +19,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.HorizontalSpace;
import net.sf.briar.api.AuthorId;
@@ -28,6 +26,7 @@ import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.db.NoSuchMessageException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.GroupId;
import net.sf.briar.api.messaging.MessageId;
import net.sf.briar.api.messaging.Rating;
@@ -56,9 +55,6 @@ implements OnClickListener {
private static final Logger LOG =
Logger.getLogger(ReadGroupPostActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private GroupId groupId = null;
private Rating rating = UNRATED;
private boolean read;
@@ -71,6 +67,7 @@ implements OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile MessageId messageId = null;
private volatile AuthorId authorId = null;
@@ -220,17 +217,13 @@ implements OnClickListener {
layout.addView(footer);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
private void setReadInDatabase(final boolean read) {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.setReadFlag(messageId, read);
long duration = System.currentTimeMillis() - now;
@@ -263,7 +256,7 @@ implements OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
byte[] body = db.getMessageBody(messageId);
long duration = System.currentTimeMillis() - now;
@@ -302,12 +295,6 @@ implements OnClickListener {
state.putBoolean("net.sf.briar.READ", read);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onClick(View view) {
if(view == goodButton) {
if(rating == BAD) setRatingInDatabase(UNRATED);
@@ -337,7 +324,7 @@ implements OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.setRating(authorId, r);
long duration = System.currentTimeMillis() - now;

View File

@@ -20,8 +20,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.identity.CreateIdentityActivity;
import net.sf.briar.android.identity.LocalAuthorItem;
import net.sf.briar.android.identity.LocalAuthorItemComparator;
@@ -34,6 +32,7 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.KeyParser;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.Group;
import net.sf.briar.api.messaging.GroupId;
import net.sf.briar.api.messaging.Message;
@@ -61,9 +60,6 @@ implements OnItemSelectedListener, OnClickListener {
private static final Logger LOG =
Logger.getLogger(WriteGroupPostActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
@Inject private CryptoComponent crypto;
@Inject private MessageFactory messageFactory;
private LocalAuthorSpinnerAdapter fromAdapter = null;
@@ -77,6 +73,7 @@ implements OnItemSelectedListener, OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile LocalAuthor localAuthor = null;
private volatile Group group = null;
private volatile MessageId parentId = null;
@@ -155,10 +152,6 @@ implements OnItemSelectedListener, OnClickListener {
layout.addView(content);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -172,7 +165,7 @@ implements OnItemSelectedListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<LocalAuthor> localAuthors = db.getLocalAuthors();
long duration = System.currentTimeMillis() - now;
@@ -219,7 +212,7 @@ implements OnItemSelectedListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
List<Group> groups = new ArrayList<Group>();
long now = System.currentTimeMillis();
for(Group g : db.getSubscriptions())
@@ -274,12 +267,6 @@ implements OnItemSelectedListener, OnClickListener {
}
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(parent == fromSpinner) {
@@ -352,7 +339,7 @@ implements OnItemSelectedListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.addLocalGroupMessage(m);
long duration = System.currentTimeMillis() - now;

View File

@@ -20,8 +20,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.api.AuthorFactory;
import net.sf.briar.api.LocalAuthor;
import net.sf.briar.api.android.DatabaseUiExecutor;
@@ -29,8 +27,8 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.CryptoExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
@@ -51,9 +49,6 @@ implements OnEditorActionListener, OnClickListener {
private static final Logger LOG =
Logger.getLogger(CreateIdentityActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
@Inject @CryptoExecutor private Executor cryptoExecutor;
private EditText nicknameEntry = null;
private Button createButton = null;
@@ -64,6 +59,7 @@ implements OnEditorActionListener, OnClickListener {
@Inject private volatile AuthorFactory authorFactory;
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -109,16 +105,6 @@ implements OnEditorActionListener, OnClickListener {
layout.addView(progress);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
@@ -154,7 +140,7 @@ implements OnEditorActionListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.addLocalAuthor(a);
db.setRating(a.getId(), GOOD);

View File

@@ -12,8 +12,6 @@ import java.util.Collection;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.identity.LocalAuthorItem;
import net.sf.briar.android.identity.LocalAuthorItemComparator;
import net.sf.briar.android.identity.LocalAuthorSpinnerAdapter;
@@ -28,6 +26,7 @@ import net.sf.briar.api.invitation.InvitationListener;
import net.sf.briar.api.invitation.InvitationState;
import net.sf.briar.api.invitation.InvitationTask;
import net.sf.briar.api.invitation.InvitationTaskFactory;
import net.sf.briar.api.lifecycle.LifecycleManager;
import roboguice.activity.RoboActivity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
@@ -46,9 +45,6 @@ implements InvitationListener {
private static final Logger LOG =
Logger.getLogger(AddContactActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
@Inject private CryptoComponent crypto;
@Inject private InvitationTaskFactory invitationTaskFactory;
@Inject private ReferenceManager referenceManager;
@@ -69,6 +65,7 @@ implements InvitationListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -159,10 +156,6 @@ implements InvitationListener {
if(info.getNetworkId() != -1) networkName = info.getSSID();
}
view.wifiStateChanged();
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -190,7 +183,6 @@ implements InvitationListener {
super.onDestroy();
if(task != null) task.removeListener(this);
unregisterReceiver(receiver);
unbindService(serviceConnection);
}
void setView(AddContactView view) {
@@ -216,7 +208,7 @@ implements InvitationListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<LocalAuthor> localAuthors = db.getLocalAuthors();
long duration = System.currentTimeMillis() - now;

View File

@@ -15,8 +15,6 @@ import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.AscendingHeaderComparator;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.ListLoadingProgressBar;
import net.sf.briar.api.AuthorId;
@@ -31,6 +29,7 @@ import net.sf.briar.api.db.event.DatabaseEvent;
import net.sf.briar.api.db.event.DatabaseListener;
import net.sf.briar.api.db.event.MessageExpiredEvent;
import net.sf.briar.api.db.event.PrivateMessageAddedEvent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.os.Bundle;
@@ -50,9 +49,6 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(ConversationActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private String contactName = null;
private ConversationAdapter adapter = null;
private ListView list = null;
@@ -61,6 +57,7 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile ContactId contactId = null;
private volatile AuthorId localAuthorId = null;
@@ -106,10 +103,6 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
layout.addView(composeButton);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -123,7 +116,7 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<PrivateMessageHeader> headers =
db.getPrivateMessageHeaders(contactId);
@@ -196,12 +189,6 @@ implements DatabaseListener, OnClickListener, OnItemClickListener {
db.removeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof ContactRemovedEvent) {
ContactRemovedEvent c = (ContactRemovedEvent) e;

View File

@@ -15,8 +15,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.invitation.AddContactActivity;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.ListLoadingProgressBar;
@@ -32,6 +30,7 @@ import net.sf.briar.api.db.event.DatabaseEvent;
import net.sf.briar.api.db.event.DatabaseListener;
import net.sf.briar.api.db.event.MessageExpiredEvent;
import net.sf.briar.api.db.event.PrivateMessageAddedEvent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import roboguice.activity.RoboFragmentActivity;
import android.content.Intent;
import android.os.Bundle;
@@ -49,9 +48,6 @@ implements OnClickListener, DatabaseListener, NoContactsDialog.Listener {
private static final Logger LOG =
Logger.getLogger(ConversationListActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private ConversationListAdapter adapter = null;
private ListView list = null;
private ListLoadingProgressBar loading = null;
@@ -59,6 +55,7 @@ implements OnClickListener, DatabaseListener, NoContactsDialog.Listener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -90,10 +87,6 @@ implements OnClickListener, DatabaseListener, NoContactsDialog.Listener {
layout.addView(composeButton);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -108,7 +101,7 @@ implements OnClickListener, DatabaseListener, NoContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
for(Contact c : db.getContacts()) {
try {
@@ -191,12 +184,6 @@ implements OnClickListener, DatabaseListener, NoContactsDialog.Listener {
db.removeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onClick(View view) {
if(adapter.isEmpty()) {
NoContactsDialog dialog = new NoContactsDialog();
@@ -225,7 +212,7 @@ implements OnClickListener, DatabaseListener, NoContactsDialog.Listener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Contact contact = db.getContact(c);
Collection<PrivateMessageHeader> headers =

View File

@@ -19,8 +19,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.HorizontalSpace;
import net.sf.briar.api.ContactId;
@@ -28,6 +26,7 @@ import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.db.NoSuchMessageException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.MessageId;
import net.sf.briar.api.messaging.Rating;
import roboguice.activity.RoboActivity;
@@ -55,9 +54,6 @@ implements OnClickListener {
private static final Logger LOG =
Logger.getLogger(ReadPrivateMessageActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private ContactId contactId = null;
private Rating rating = UNRATED;
private boolean read;
@@ -68,6 +64,7 @@ implements OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile MessageId messageId = null;
@Override
@@ -190,17 +187,13 @@ implements OnClickListener {
layout.addView(footer);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
private void setReadInDatabase(final boolean read) {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.setReadFlag(messageId, read);
long duration = System.currentTimeMillis() - now;
@@ -233,7 +226,7 @@ implements OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
byte[] body = db.getMessageBody(messageId);
long duration = System.currentTimeMillis() - now;
@@ -272,12 +265,6 @@ implements OnClickListener {
state.putBoolean("net.sf.briar.READ", read);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onClick(View view) {
if(view == readButton) {
setReadInDatabase(!read);

View File

@@ -17,8 +17,6 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.contact.ContactItem;
import net.sf.briar.android.contact.ContactItemComparator;
import net.sf.briar.android.contact.ContactSpinnerAdapter;
@@ -31,6 +29,7 @@ import net.sf.briar.api.LocalAuthor;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.messaging.Message;
import net.sf.briar.api.messaging.MessageFactory;
import net.sf.briar.api.messaging.MessageId;
@@ -56,9 +55,6 @@ implements OnItemSelectedListener, OnClickListener {
private static final Logger LOG =
Logger.getLogger(WritePrivateMessageActivity.class.getName());
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private TextView from = null;
private ContactSpinnerAdapter adapter = null;
private Spinner spinner = null;
@@ -68,6 +64,7 @@ implements OnItemSelectedListener, OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Inject private volatile MessageFactory messageFactory;
private volatile LocalAuthor localAuthor = null;
private volatile ContactId contactId = null;
@@ -139,10 +136,6 @@ implements OnItemSelectedListener, OnClickListener {
layout.addView(content);
setContentView(layout);
// Bind to the service so we can wait for it to start
bindService(new Intent(BriarService.class.getName()),
serviceConnection, 0);
}
@Override
@@ -155,7 +148,7 @@ implements OnItemSelectedListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;
@@ -201,12 +194,6 @@ implements OnItemSelectedListener, OnClickListener {
state.putInt("net.sf.briar.CONTACT_ID", contactId.getInt());
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
ContactItem item = adapter.getItem(position);
@@ -227,7 +214,7 @@ implements OnItemSelectedListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
localAuthor = db.getLocalAuthor(a);
long duration = System.currentTimeMillis() - now;
@@ -277,7 +264,7 @@ implements OnItemSelectedListener, OnClickListener {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForDatabase();
lifecycleManager.waitForDatabase();
Message m = messageFactory.createPrivateMessage(parentId,
"text/plain", body);
long now = System.currentTimeMillis();

View File

@@ -0,0 +1,19 @@
package net.sf.briar.api.lifecycle;
public interface LifecycleManager {
/** Starts any services that need to be started at startup. */
public void startServices();
/** Stops any services that need to be stopped at shutdown. */
public void stopServices();
/** Waits for the database to be opened before returning. */
public void waitForDatabase() throws InterruptedException;
/** Waits for all services to start before returning. */
public void waitForStartup() throws InterruptedException;
/** Waits for all services to stop before returning. */
public void waitForShutdown() throws InterruptedException;
}

View File

@@ -1,4 +1,4 @@
package net.sf.briar.reliability;
package net.sf.briar.api.reliability;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@@ -12,4 +12,4 @@ import com.google.inject.BindingAnnotation;
@BindingAnnotation
@Target({ PARAMETER })
@Retention(RUNTIME)
@interface ReliabilityExecutor {}
public @interface ReliabilityExecutor {}

View File

@@ -1,4 +1,4 @@
package net.sf.briar.transport;
package net.sf.briar.api.transport;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@@ -14,4 +14,4 @@ import com.google.inject.BindingAnnotation;
@BindingAnnotation
@Target({ PARAMETER })
@Retention(RUNTIME)
@interface IncomingConnectionExecutor {}
public @interface IncomingConnectionExecutor {}

View File

@@ -1,27 +1,22 @@
package net.sf.briar.crypto;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.CryptoExecutor;
import net.sf.briar.util.BoundedExecutor;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
public class CryptoModule extends AbstractModule {
// FIXME: Determine suitable values for these constants empirically
/**
* The maximum number of tasks that can be queued for execution before
* submitting another task will block.
*/
private static final int MAX_QUEUED_EXECUTOR_TASKS = 10;
/** The minimum number of executor threads to keep in the pool. */
private static final int MIN_EXECUTOR_THREADS = 1;
/** The maximum number of executor threads. */
private static final int MAX_EXECUTOR_THREADS =
Runtime.getRuntime().availableProcessors();
@@ -30,9 +25,16 @@ public class CryptoModule extends AbstractModule {
protected void configure() {
bind(CryptoComponent.class).to(
CryptoComponentImpl.class).in(Singleton.class);
// The executor is bounded, so tasks must be independent and short-lived
bind(Executor.class).annotatedWith(CryptoExecutor.class).toInstance(
new BoundedExecutor(MAX_QUEUED_EXECUTOR_TASKS,
MIN_EXECUTOR_THREADS, MAX_EXECUTOR_THREADS));
// The queue is unbounded, so tasks can be dependent
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
// Discard tasks that are submitted during shutdown
RejectedExecutionHandler policy =
new ThreadPoolExecutor.DiscardPolicy();
// Create a limited # of threads and keep them in the pool for 60 secs
ExecutorService e = new ThreadPoolExecutor(0, MAX_EXECUTOR_THREADS,
60, SECONDS, queue, policy);
bind(Executor.class).annotatedWith(CryptoExecutor.class).toInstance(e);
bind(ExecutorService.class).annotatedWith(
CryptoExecutor.class).toInstance(e);
}
}

View File

@@ -1,11 +1,13 @@
package net.sf.briar.db;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.sql.Connection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import net.sf.briar.api.clock.Clock;
@@ -21,24 +23,24 @@ import com.google.inject.Singleton;
public class DatabaseModule extends AbstractModule {
/**
* The maximum number of database threads. When a task is submitted to the
* database executor and no thread is available to run it, the task will be
* queued.
*/
private static final int MAX_DB_THREADS = 10;
/** How many milliseconds to keep idle threads alive. */
private static final int DB_KEEPALIVE = 60 * 1000;
/** The maximum number of executor threads. */
private static final int MAX_EXECUTOR_THREADS = 10;
@Override
protected void configure() {
bind(DatabaseCleaner.class).to(DatabaseCleanerImpl.class);
// Use an unbounded queue to prevent deadlock between submitted tasks
// The queue is unbounded, so tasks can be dependent
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
bind(Executor.class).annotatedWith(DatabaseExecutor.class).toInstance(
new ThreadPoolExecutor(MAX_DB_THREADS, MAX_DB_THREADS,
DB_KEEPALIVE, MILLISECONDS, queue));
// Discard tasks that are submitted during shutdown
RejectedExecutionHandler policy =
new ThreadPoolExecutor.DiscardPolicy();
// Create a limited # of threads and keep them in the pool for 60 secs
ExecutorService e = new ThreadPoolExecutor(0, MAX_EXECUTOR_THREADS,
60, SECONDS, queue, policy);
bind(Executor.class).annotatedWith(
DatabaseExecutor.class).toInstance(e);
bind(ExecutorService.class).annotatedWith(
DatabaseExecutor.class).toInstance(e);
}
@Provides

View File

@@ -0,0 +1,116 @@
package net.sf.briar.lifecycle;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;
import net.sf.briar.api.crypto.CryptoExecutor;
import net.sf.briar.api.crypto.KeyManager;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseExecutor;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.PluginManager;
import net.sf.briar.api.reliability.ReliabilityExecutor;
import net.sf.briar.api.transport.IncomingConnectionExecutor;
import com.google.inject.Inject;
class LifecycleManagerImpl implements LifecycleManager {
private static final Logger LOG =
Logger.getLogger(LifecycleManagerImpl.class.getName());
private final DatabaseComponent db;
private final KeyManager keyManager;
private final PluginManager pluginManager;
private final ExecutorService cryptoExecutor;
private final ExecutorService dbExecutor;
private final ExecutorService connExecutor;
private final ExecutorService pluginExecutor;
private final ExecutorService reliabilityExecutor;
private final CountDownLatch dbLatch = new CountDownLatch(1);
private final CountDownLatch startupLatch = new CountDownLatch(1);
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
@Inject
LifecycleManagerImpl(DatabaseComponent db, KeyManager keyManager,
PluginManager pluginManager,
@CryptoExecutor ExecutorService cryptoExecutor,
@DatabaseExecutor ExecutorService dbExecutor,
@IncomingConnectionExecutor ExecutorService connExecutor,
@PluginExecutor ExecutorService pluginExecutor,
@ReliabilityExecutor ExecutorService reliabilityExecutor) {
this.db = db;
this.keyManager = keyManager;
this.pluginManager = pluginManager;
this.cryptoExecutor = cryptoExecutor;
this.dbExecutor = dbExecutor;
this.connExecutor = connExecutor;
this.pluginExecutor = pluginExecutor;
this.reliabilityExecutor = reliabilityExecutor;
}
public void startServices() {
try {
if(LOG.isLoggable(INFO)) LOG.info("Starting");
boolean reopened = db.open();
if(LOG.isLoggable(INFO)) {
if(reopened) LOG.info("Database reopened");
else LOG.info("Database created");
}
dbLatch.countDown();
keyManager.start();
if(LOG.isLoggable(INFO)) LOG.info("Key manager started");
int pluginsStarted = pluginManager.start();
if(LOG.isLoggable(INFO))
LOG.info(pluginsStarted + " plugins started");
startupLatch.countDown();
} catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
public void stopServices() {
try {
if(LOG.isLoggable(INFO)) LOG.info("Shutting down");
int pluginsStopped = pluginManager.stop();
if(LOG.isLoggable(INFO))
LOG.info(pluginsStopped + " plugins stopped");
keyManager.stop();
if(LOG.isLoggable(INFO)) LOG.info("Key manager stopped");
db.close();
if(LOG.isLoggable(INFO)) LOG.info("Database closed");
cryptoExecutor.shutdownNow();
dbExecutor.shutdownNow();
connExecutor.shutdownNow();
pluginExecutor.shutdownNow();
reliabilityExecutor.shutdownNow();
if(LOG.isLoggable(INFO)) LOG.info("Executors shut down");
shutdownLatch.countDown();
} catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
public void waitForDatabase() throws InterruptedException {
dbLatch.await();
}
public void waitForStartup() throws InterruptedException {
startupLatch.await();
}
public void waitForShutdown() throws InterruptedException {
shutdownLatch.await();
}
}

View File

@@ -1,14 +1,18 @@
package net.sf.briar.lifecycle;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.lifecycle.ShutdownManager;
import net.sf.briar.util.OsUtils;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
public class LifecycleModule extends AbstractModule {
@Override
protected void configure() {
bind(LifecycleManager.class).to(LifecycleManagerImpl.class).in(
Singleton.class);
if(OsUtils.isWindows())
bind(ShutdownManager.class).to(WindowsShutdownManagerImpl.class);
else bind(ShutdownManager.class).to(ShutdownManagerImpl.class);

View File

@@ -2,7 +2,7 @@ package net.sf.briar.plugins;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executor;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.lifecycle.ShutdownManager;
@@ -28,7 +28,7 @@ public class JavaSePluginsModule extends AbstractModule {
@Provides
SimplexPluginConfig getSimplexPluginConfig(
@PluginExecutor ExecutorService pluginExecutor) {
@PluginExecutor Executor pluginExecutor) {
SimplexPluginFactory removable =
new RemovableDrivePluginFactory(pluginExecutor);
final Collection<SimplexPluginFactory> factories =
@@ -42,7 +42,7 @@ public class JavaSePluginsModule extends AbstractModule {
@Provides
DuplexPluginConfig getDuplexPluginConfig(
@PluginExecutor ExecutorService pluginExecutor,
@PluginExecutor Executor pluginExecutor,
CryptoComponent crypto, ReliabilityLayerFactory reliabilityFactory,
ShutdownManager shutdownManager) {
DuplexPluginFactory bluetooth = new BluetoothPluginFactory(

View File

@@ -11,7 +11,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
@@ -19,7 +19,6 @@ import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.android.AndroidExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.plugins.Plugin;
@@ -49,8 +48,7 @@ class PluginManagerImpl implements PluginManager {
private static final Logger LOG =
Logger.getLogger(PluginManagerImpl.class.getName());
private final ExecutorService pluginExecutor;
private final AndroidExecutor androidExecutor;
private final Executor pluginExecutor;
private final SimplexPluginConfig simplexPluginConfig;
private final DuplexPluginConfig duplexPluginConfig;
private final DatabaseComponent db;
@@ -61,14 +59,12 @@ class PluginManagerImpl implements PluginManager {
private final List<DuplexPlugin> duplexPlugins;
@Inject
PluginManagerImpl(@PluginExecutor ExecutorService pluginExecutor,
AndroidExecutor androidExecutor,
PluginManagerImpl(@PluginExecutor Executor pluginExecutor,
SimplexPluginConfig simplexPluginConfig,
DuplexPluginConfig duplexPluginConfig, DatabaseComponent db,
Poller poller, ConnectionDispatcher dispatcher,
UiCallback uiCallback) {
this.pluginExecutor = pluginExecutor;
this.androidExecutor = androidExecutor;
this.simplexPluginConfig = simplexPluginConfig;
this.duplexPluginConfig = duplexPluginConfig;
this.db = db;
@@ -144,10 +140,6 @@ class PluginManagerImpl implements PluginManager {
Thread.currentThread().interrupt();
return 0;
}
// Shut down the executors
if(LOG.isLoggable(INFO)) LOG.info("Stopping executors");
pluginExecutor.shutdown();
androidExecutor.shutdown();
// Return the number of plugins successfully stopped
return stopped.get();
}

View File

@@ -1,7 +1,13 @@
package net.sf.briar.plugins;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.PluginManager;
@@ -13,12 +19,19 @@ public class PluginsModule extends AbstractModule {
@Override
protected void configure() {
// The executor is unbounded, so tasks can be dependent or long-lived
ExecutorService e = Executors.newCachedThreadPool();
bind(ExecutorService.class).annotatedWith(
PluginExecutor.class).toInstance(e);
bind(PluginManager.class).to(
PluginManagerImpl.class).in(Singleton.class);
bind(Poller.class).to(PollerImpl.class);
// The thread pool is unbounded, so use direct handoff
BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>();
// Discard tasks that are submitted during shutdown
RejectedExecutionHandler policy =
new ThreadPoolExecutor.DiscardPolicy();
// Create threads as required and keep them in the pool for 60 seconds
ExecutorService e = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60, SECONDS, queue, policy);
bind(Executor.class).annotatedWith(PluginExecutor.class).toInstance(e);
bind(ExecutorService.class).annotatedWith(
PluginExecutor.class).toInstance(e);
}
}

View File

@@ -5,7 +5,7 @@ import static java.util.logging.Level.INFO;
import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.sf.briar.api.ContactId;
@@ -21,13 +21,13 @@ class PollerImpl implements Poller, Runnable {
private static final Logger LOG =
Logger.getLogger(PollerImpl.class.getName());
private final ExecutorService pluginExecutor;
private final Executor pluginExecutor;
private final ConnectionRegistry connRegistry;
private final Clock clock;
private final SortedSet<PollTime> pollTimes;
@Inject
PollerImpl(@PluginExecutor ExecutorService pluginExecutor,
PollerImpl(@PluginExecutor Executor pluginExecutor,
ConnectionRegistry connRegistry, Clock clock) {
this.pluginExecutor = pluginExecutor;
this.connRegistry = connRegistry;
@@ -71,7 +71,7 @@ class PollerImpl implements Poller, Runnable {
connRegistry.getConnectedContacts(p.plugin.getId());
if(LOG.isLoggable(INFO))
LOG.info("Polling " + p.plugin.getClass().getName());
pluginExecutor.submit(new Runnable() {
pluginExecutor.execute(new Runnable() {
public void run() {
p.plugin.poll(connected);
}

View File

@@ -4,6 +4,7 @@ import java.util.concurrent.Executor;
import net.sf.briar.api.clock.Clock;
import net.sf.briar.api.clock.SystemClock;
import net.sf.briar.api.reliability.ReliabilityExecutor;
import net.sf.briar.api.reliability.ReliabilityLayer;
import net.sf.briar.api.reliability.ReliabilityLayerFactory;
import net.sf.briar.api.reliability.WriteHandler;

View File

@@ -1,8 +1,15 @@
package net.sf.briar.reliability;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import net.sf.briar.api.reliability.ReliabilityExecutor;
import net.sf.briar.api.reliability.ReliabilityLayerFactory;
import com.google.inject.AbstractModule;
@@ -11,11 +18,19 @@ public class ReliabilityModule extends AbstractModule {
@Override
protected void configure() {
// The executor is unbounded - tasks are expected to be long-lived
Executor e = Executors.newCachedThreadPool();
bind(Executor.class).annotatedWith(
ReliabilityExecutor.class).toInstance(e);
bind(ReliabilityLayerFactory.class).to(
ReliabilityLayerFactoryImpl.class);
// The thread pool is unbounded, so use direct handoff
BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>();
// Discard tasks that are submitted during shutdown
RejectedExecutionHandler policy =
new ThreadPoolExecutor.DiscardPolicy();
// Create threads as required and keep them in the pool for 60 seconds
ExecutorService e = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60, SECONDS, queue, policy);
bind(Executor.class).annotatedWith(
ReliabilityExecutor.class).toInstance(e);
bind(ExecutorService.class).annotatedWith(
ReliabilityExecutor.class).toInstance(e);
}
}

View File

@@ -20,6 +20,7 @@ import net.sf.briar.api.plugins.simplex.SimplexTransportWriter;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionDispatcher;
import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.IncomingConnectionExecutor;
import com.google.inject.Inject;

View File

@@ -1,7 +1,13 @@
package net.sf.briar.transport;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import net.sf.briar.api.crypto.KeyManager;
import net.sf.briar.api.transport.ConnectionDispatcher;
@@ -9,6 +15,7 @@ import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.ConnectionRegistry;
import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.api.transport.IncomingConnectionExecutor;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
@@ -25,10 +32,18 @@ public class TransportModule extends AbstractModule {
bind(ConnectionRegistry.class).toInstance(new ConnectionRegistryImpl());
bind(ConnectionWriterFactory.class).to(
ConnectionWriterFactoryImpl.class);
// The executor is unbounded, so tasks can be dependent or long-lived
Executor e = Executors.newCachedThreadPool();
bind(KeyManager.class).to(KeyManagerImpl.class).in(Singleton.class);
// The thread pool is unbounded, so use direct handoff
BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>();
// Discard tasks that are submitted during shutdown
RejectedExecutionHandler policy =
new ThreadPoolExecutor.DiscardPolicy();
// Create threads as required and keep them in the pool for 60 seconds
ExecutorService e = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60, SECONDS, queue, policy);
bind(Executor.class).annotatedWith(
IncomingConnectionExecutor.class).toInstance(e);
bind(KeyManager.class).to(KeyManagerImpl.class).in(Singleton.class);
bind(ExecutorService.class).annotatedWith(
IncomingConnectionExecutor.class).toInstance(e);
}
}

View File

@@ -1,58 +0,0 @@
package net.sf.briar.util;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Logger;
/**
* An executor that limits the number of concurrently executing tasks and the
* number of tasks queued for execution.
*/
public class BoundedExecutor implements Executor {
private static final Logger LOG =
Logger.getLogger(BoundedExecutor.class.getName());
private final Semaphore semaphore;
private final BlockingQueue<Runnable> queue;
private final Executor executor;
public BoundedExecutor(int maxQueued, int minThreads, int maxThreads) {
semaphore = new Semaphore(maxQueued + maxThreads);
queue = new LinkedBlockingQueue<Runnable>();
executor = new ThreadPoolExecutor(minThreads, maxThreads, 60, SECONDS,
queue);
}
public void execute(final Runnable r) {
try {
semaphore.acquire();
executor.execute(new Runnable() {
public void run() {
try {
r.run();
} finally {
semaphore.release();
}
}
});
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while queueing task");
Thread.currentThread().interrupt();
throw new RejectedExecutionException();
} catch(RejectedExecutionException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
semaphore.release();
throw e;
}
}
}

View File

@@ -48,6 +48,9 @@ import net.sf.briar.lifecycle.LifecycleModule;
import net.sf.briar.messaging.MessagingModule;
import net.sf.briar.messaging.duplex.DuplexMessagingModule;
import net.sf.briar.messaging.simplex.SimplexMessagingModule;
import net.sf.briar.plugins.JavaSePluginsModule;
import net.sf.briar.plugins.PluginsModule;
import net.sf.briar.reliability.ReliabilityModule;
import net.sf.briar.serial.SerialModule;
import net.sf.briar.transport.TransportModule;
@@ -79,9 +82,11 @@ public class ProtocolIntegrationTest extends BriarTestCase {
public ProtocolIntegrationTest() throws Exception {
super();
Injector i = Guice.createInjector(new TestDatabaseModule(),
new ClockModule(), new CryptoModule(), new DatabaseModule(),
new LifecycleModule(), new MessagingModule(),
new DuplexMessagingModule(), new SimplexMessagingModule(),
new TestUiModule(), new ClockModule(), new CryptoModule(),
new DatabaseModule(), new LifecycleModule(),
new MessagingModule(), new DuplexMessagingModule(),
new SimplexMessagingModule(), new PluginsModule(),
new JavaSePluginsModule(), new ReliabilityModule(),
new SerialModule(), new TransportModule());
connectionReaderFactory = i.getInstance(ConnectionReaderFactory.class);
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);

View File

@@ -0,0 +1,24 @@
package net.sf.briar;
import net.sf.briar.api.ui.UiCallback;
import com.google.inject.AbstractModule;
public class TestUiModule extends AbstractModule {
@Override
protected void configure() {
bind(UiCallback.class).toInstance(new UiCallback() {
public int showChoice(String[] options, String... message) {
return -1;
}
public boolean showConfirmationMessage(String... message) {
return false;
}
public void showMessage(String... message) {}
});
}
}

View File

@@ -9,6 +9,7 @@ import java.util.Random;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestDatabaseModule;
import net.sf.briar.TestUiModule;
import net.sf.briar.TestUtils;
import net.sf.briar.api.Author;
import net.sf.briar.api.AuthorId;
@@ -38,6 +39,9 @@ import net.sf.briar.lifecycle.LifecycleModule;
import net.sf.briar.messaging.MessagingModule;
import net.sf.briar.messaging.duplex.DuplexMessagingModule;
import net.sf.briar.plugins.ImmediateExecutor;
import net.sf.briar.plugins.JavaSePluginsModule;
import net.sf.briar.plugins.PluginsModule;
import net.sf.briar.reliability.ReliabilityModule;
import net.sf.briar.serial.SerialModule;
import net.sf.briar.transport.TransportModule;
@@ -80,11 +84,13 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
}
private Injector createInjector(File dir) {
return Guice.createInjector(new ClockModule(), new CryptoModule(),
return Guice.createInjector(new TestDatabaseModule(dir),
new TestUiModule(), new ClockModule(), new CryptoModule(),
new DatabaseModule(), new LifecycleModule(),
new MessagingModule(), new SerialModule(),
new TestDatabaseModule(dir), new SimplexMessagingModule(),
new TransportModule(), new DuplexMessagingModule());
new MessagingModule(), new DuplexMessagingModule(),
new SimplexMessagingModule(), new PluginsModule(),
new JavaSePluginsModule(), new ReliabilityModule(),
new SerialModule(), new TransportModule());
}
@Test

View File

@@ -1,13 +1,12 @@
package net.sf.briar.plugins;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.android.AndroidExecutor;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.plugins.duplex.DuplexPlugin;
import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
@@ -29,9 +28,7 @@ public class PluginManagerImplTest extends BriarTestCase {
@Test
public void testStartAndStop() throws Exception {
Mockery context = new Mockery();
final ExecutorService pluginExecutor = Executors.newCachedThreadPool();
final AndroidExecutor androidExecutor =
context.mock(AndroidExecutor.class);
final Executor pluginExecutor = Executors.newCachedThreadPool();
final SimplexPluginConfig simplexPluginConfig =
context.mock(SimplexPluginConfig.class);
final DuplexPluginConfig duplexPluginConfig =
@@ -119,12 +116,10 @@ public class PluginManagerImplTest extends BriarTestCase {
// Stop the plugins
oneOf(simplexPlugin).stop();
oneOf(duplexPlugin).stop();
// Shut down the executor
oneOf(androidExecutor).shutdown();
}});
PluginManagerImpl p = new PluginManagerImpl(pluginExecutor,
androidExecutor, simplexPluginConfig, duplexPluginConfig, db,
poller, dispatcher, uiCallback);
simplexPluginConfig, duplexPluginConfig, db, poller,
dispatcher, uiCallback);
// Two plugins should be started and stopped
assertEquals(2, p.start());
assertEquals(2, p.stop());