Mailbox download worker for a contact's mailbox
When a contact is assigned to a contact's mailbox for download, the mailbox client (#2289 (closed)) creates a download worker. The worker is destroyed when the contact is unassigned for download or the client is destroyed.
The worker keeps a reference to its current API task, if any, so the task can be cancelled when the worker is destroyed.
The worker's lifecycle is:
- Check connectivity
- Download and delete all files from the inbox
- Wait until our hidden service has been reachable for at least the overlap duration
- Download and delete all files from the inbox again, in case any arrived during the overlap period
The worker can be in the following states:
- Waiting for connectivity check
- First download
- Waiting for hidden service
- Second download
- Destroyed
When the worker is created:
- Set the current state to "waiting for connectivity check"
- Call the client's connectivity check method
When the worker is destroyed:
- Set the current state to "destroyed"
- Cancel the current API task, if any
When a connectivity check succeeds:
- If the current state is "waiting for connectivity check":
- Set the current state to "first download"
- Start a list-inbox task
When a list-inbox tasks succeeds:
- If the current state is "first download" or "second download":
- If there are files in the inbox:
- Sort the files by timestamp
- Start a download-file task for the first file (#2232 (closed))
- Else if the current state is "first download":
- Set the current state to "waiting for hidden service"
- If there are files in the inbox:
When a download-file task succeeds:
- Pass the local file's path to the mailbox plugin to get a transport connection reader
- Decorate the transport connection reader to handle disposal
- Pass the decorated transport connection reader to the connection manager
- Data will be read from the local file asynchronously
- If the current state is "first download" or "second download":
- Start a delete-file task for the current file (#2233 (closed))
When the transport connection reader is disposed of:
- Delete the local file
When a delete-file task succeeds or tolerably fails:
- If the current state is "first download" or "second download":
- If there are more files to download:
- Start a download-file task for the next file
- Else:
- Start a list-inbox task
- If there are more files to download:
When an event indicates that the hidden service has been reachable for at least the overlap duration:
- If the current state is "waiting for hidden service":
- Set the current state to "second download"
- Start a list-inbox task