mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +01:00
Address review feedback.
This commit is contained in:
@@ -24,6 +24,10 @@ interface MailboxClient {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Assigns a contact to the client for upload.
|
* Assigns a contact to the client for upload.
|
||||||
|
*
|
||||||
|
* @param properties Properties for communicating with the mailbox
|
||||||
|
* managed by this client.
|
||||||
|
* @param folderId The ID of the folder to which files will be uploaded.
|
||||||
*/
|
*/
|
||||||
void assignContactForUpload(ContactId c, MailboxProperties properties,
|
void assignContactForUpload(ContactId c, MailboxProperties properties,
|
||||||
MailboxFolderId folderId);
|
MailboxFolderId folderId);
|
||||||
@@ -35,6 +39,11 @@ interface MailboxClient {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Assigns a contact to the client for download.
|
* Assigns a contact to the client for download.
|
||||||
|
*
|
||||||
|
* @param properties Properties for communicating with the mailbox
|
||||||
|
* managed by this client.
|
||||||
|
* @param folderId The ID of the folder from which files will be
|
||||||
|
* downloaded.
|
||||||
*/
|
*/
|
||||||
void assignContactForDownload(ContactId c, MailboxProperties properties,
|
void assignContactForDownload(ContactId c, MailboxProperties properties,
|
||||||
MailboxFolderId folderId);
|
MailboxFolderId folderId);
|
||||||
|
|||||||
@@ -32,13 +32,24 @@ class OwnMailboxClient implements MailboxClient {
|
|||||||
private final MailboxWorker contactListWorker;
|
private final MailboxWorker contactListWorker;
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload workers: one worker per contact assigned for upload.
|
||||||
|
*/
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private final Map<ContactId, MailboxWorker> uploadWorkers = new HashMap<>();
|
private final Map<ContactId, MailboxWorker> uploadWorkers = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download worker: shared between all contacts assigned for download.
|
||||||
|
* Null if no contacts are assigned for download.
|
||||||
|
*/
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
@Nullable
|
@Nullable
|
||||||
private MailboxWorker downloadWorker = null;
|
private MailboxWorker downloadWorker = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IDs of contacts assigned for download, so that we know when to
|
||||||
|
* create/destroy the download worker.
|
||||||
|
*/
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private final Set<ContactId> assignedForDownload = new HashSet<>();
|
private final Set<ContactId> assignedForDownload = new HashSet<>();
|
||||||
|
|
||||||
@@ -46,6 +57,7 @@ class OwnMailboxClient implements MailboxClient {
|
|||||||
ConnectivityChecker connectivityChecker,
|
ConnectivityChecker connectivityChecker,
|
||||||
TorReachabilityMonitor reachabilityMonitor,
|
TorReachabilityMonitor reachabilityMonitor,
|
||||||
MailboxProperties properties) {
|
MailboxProperties properties) {
|
||||||
|
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||||
this.workerFactory = workerFactory;
|
this.workerFactory = workerFactory;
|
||||||
this.connectivityChecker = connectivityChecker;
|
this.connectivityChecker = connectivityChecker;
|
||||||
this.reachabilityMonitor = reachabilityMonitor;
|
this.reachabilityMonitor = reachabilityMonitor;
|
||||||
@@ -70,6 +82,7 @@ class OwnMailboxClient implements MailboxClient {
|
|||||||
downloadWorker = this.downloadWorker;
|
downloadWorker = this.downloadWorker;
|
||||||
this.downloadWorker = null;
|
this.downloadWorker = null;
|
||||||
}
|
}
|
||||||
|
// Destroy the workers (with apologies to Mr Marx and Mr Engels)
|
||||||
for (MailboxWorker worker : uploadWorkers) worker.destroy();
|
for (MailboxWorker worker : uploadWorkers) worker.destroy();
|
||||||
if (downloadWorker != null) downloadWorker.destroy();
|
if (downloadWorker != null) downloadWorker.destroy();
|
||||||
contactListWorker.destroy();
|
contactListWorker.destroy();
|
||||||
@@ -104,12 +117,14 @@ class OwnMailboxClient implements MailboxClient {
|
|||||||
MailboxProperties properties, MailboxFolderId folderId) {
|
MailboxProperties properties, MailboxFolderId folderId) {
|
||||||
LOG.info("Contact assigned for download");
|
LOG.info("Contact assigned for download");
|
||||||
if (!properties.isOwner()) throw new IllegalArgumentException();
|
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||||
|
// Create a download worker if we don't already have one. The worker
|
||||||
|
// will use the API to discover which folders have files to download,
|
||||||
|
// so it doesn't need to track the set of assigned contacts
|
||||||
MailboxWorker toStart = null;
|
MailboxWorker toStart = null;
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (!assignedForDownload.add(contactId)) {
|
if (!assignedForDownload.add(contactId)) {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
// Create a download worker if we don't already have one
|
|
||||||
if (downloadWorker == null) {
|
if (downloadWorker == null) {
|
||||||
toStart = workerFactory.createDownloadWorkerForOwnMailbox(
|
toStart = workerFactory.createDownloadWorkerForOwnMailbox(
|
||||||
connectivityChecker, reachabilityMonitor, properties);
|
connectivityChecker, reachabilityMonitor, properties);
|
||||||
@@ -122,13 +137,13 @@ class OwnMailboxClient implements MailboxClient {
|
|||||||
@Override
|
@Override
|
||||||
public void deassignContactForDownload(ContactId contactId) {
|
public void deassignContactForDownload(ContactId contactId) {
|
||||||
LOG.info("Contact deassigned for download");
|
LOG.info("Contact deassigned for download");
|
||||||
|
// If there are no more contacts assigned for download, destroy the
|
||||||
|
// download worker
|
||||||
MailboxWorker toDestroy = null;
|
MailboxWorker toDestroy = null;
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (!assignedForDownload.remove(contactId)) {
|
if (!assignedForDownload.remove(contactId)) {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
// If there are no more contacts assigned for download, destroy
|
|
||||||
// the download worker
|
|
||||||
if (assignedForDownload.isEmpty()) {
|
if (assignedForDownload.isEmpty()) {
|
||||||
toDestroy = downloadWorker;
|
toDestroy = downloadWorker;
|
||||||
downloadWorker = null;
|
downloadWorker = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user