This commit is contained in:
Ryan ZHAO
2021-01-29 11:08:55 +11:00
12 changed files with 188 additions and 55 deletions

View File

@@ -24,7 +24,7 @@ class AttachmentDownloadJob(val attachmentID: Long, val tsIncomingMessageID: Lon
// Settings
override val maxFailureCount: Int = 20
companion object {
val collection: String = "AttachmentDownloadJobCollection"
val KEY: String = "AttachmentDownloadJob"
//keys used for database storage purpose
private val KEY_ATTACHMENT_ID = "attachment_id"
@@ -89,17 +89,18 @@ class AttachmentDownloadJob(val attachmentID: Long, val tsIncomingMessageID: Lon
//database functions
override fun serialize(): Data {
val builder = this.createJobDataBuilder()
return builder.putLong(KEY_ATTACHMENT_ID, attachmentID)
return Data.Builder().putLong(KEY_ATTACHMENT_ID, attachmentID)
.putLong(KEY_TS_INCOMING_MESSAGE_ID, tsIncomingMessageID)
.build();
}
override fun getFactoryKey(): String {
return KEY
}
class Factory: Job.Factory<AttachmentDownloadJob> {
override fun create(data: Data): AttachmentDownloadJob {
val job = AttachmentDownloadJob(data.getLong(KEY_ATTACHMENT_ID), data.getLong(KEY_TS_INCOMING_MESSAGE_ID))
job.initJob(data)
return job
return AttachmentDownloadJob(data.getLong(KEY_ATTACHMENT_ID), data.getLong(KEY_TS_INCOMING_MESSAGE_ID))
}
}
}

View File

@@ -29,8 +29,8 @@ class AttachmentUploadJob(val attachmentID: Long, val threadID: String, val mess
override val maxFailureCount: Int = 20
companion object {
val TAG = AttachmentUploadJob::class.qualifiedName
val KEY: String = "AttachmentUploadJob"
val collection: String = "AttachmentUploadJobCollection"
val maxFailureCount: Int = 20
//keys used for database storage purpose
@@ -104,7 +104,6 @@ class AttachmentUploadJob(val attachmentID: Long, val threadID: String, val mess
//database functions
override fun serialize(): Data {
val builder = this.createJobDataBuilder()
//serialize Message property
val kryo = Kryo()
kryo.isRegistrationRequired = false
@@ -112,13 +111,17 @@ class AttachmentUploadJob(val attachmentID: Long, val threadID: String, val mess
val output = Output(serializedMessage)
kryo.writeObject(output, message)
output.close()
return builder.putLong(KEY_ATTACHMENT_ID, attachmentID)
return Data.Builder().putLong(KEY_ATTACHMENT_ID, attachmentID)
.putString(KEY_THREAD_ID, threadID)
.putByteArray(KEY_MESSAGE, serializedMessage)
.putString(KEY_MESSAGE_SEND_JOB_ID, messageSendJobID)
.build();
}
override fun getFactoryKey(): String {
return AttachmentDownloadJob.KEY
}
class Factory: Job.Factory<AttachmentUploadJob> {
override fun create(data: Data): AttachmentUploadJob {
val serializedMessage = data.getByteArray(KEY_MESSAGE)
@@ -127,9 +130,7 @@ class AttachmentUploadJob(val attachmentID: Long, val threadID: String, val mess
val input = Input(serializedMessage)
val message: Message = kryo.readObject(input, Message::class.java)
input.close()
val job = AttachmentUploadJob(data.getLong(KEY_ATTACHMENT_ID), data.getString(KEY_THREAD_ID)!!, message, data.getString(KEY_MESSAGE_SEND_JOB_ID)!!)
job.initJob(data)
return job
return AttachmentUploadJob(data.getLong(KEY_ATTACHMENT_ID), data.getString(KEY_THREAD_ID)!!, message, data.getString(KEY_MESSAGE_SEND_JOB_ID)!!)
}
}
}

View File

@@ -19,15 +19,10 @@ interface Job {
fun serialize(): Data
fun initJob(data: Data) {
id = data.getString(KEY_ID)
failureCount = data.getInt(KEY_FAILURE_COUNT)
}
fun createJobDataBuilder(): Data.Builder {
return Data.Builder().putString(KEY_ID, id)
.putInt(KEY_FAILURE_COUNT, failureCount)
}
/**
* Returns the key that can be used to find the relevant factory needed to create your job.
*/
fun getFactoryKey(): String
interface Factory<T : Job> {
fun create(data: Data): T

View File

@@ -16,8 +16,7 @@ class MessageReceiveJob(val data: ByteArray, val isBackgroundPoll: Boolean, val
override val maxFailureCount: Int = 10
companion object {
val TAG = MessageReceiveJob::class.qualifiedName
val collection: String = "MessageReceiveJobCollection"
val KEY: String = "AttachmentUploadJob"
//keys used for database storage purpose
private val KEY_DATA = "data"
@@ -64,20 +63,20 @@ class MessageReceiveJob(val data: ByteArray, val isBackgroundPoll: Boolean, val
//database functions
override fun serialize(): Data {
val builder = this.createJobDataBuilder()
builder.putByteArray(KEY_DATA, data)
val builder = Data.Builder().putByteArray(KEY_DATA, data)
.putBoolean(KEY_IS_BACKGROUND_POLL, isBackgroundPoll)
openGroupMessageServerID?.let { builder.putLong(KEY_OPEN_GROUP_MESSAGE_SERVER_ID, openGroupMessageServerID) }
openGroupID?.let { builder.putString(KEY_OPEN_GROUP_ID, openGroupID) }
return builder.build();
}
override fun getFactoryKey(): String {
return AttachmentDownloadJob.KEY
}
class Factory: Job.Factory<MessageReceiveJob> {
override fun create(data: Data): MessageReceiveJob {
val job = MessageReceiveJob(data.getByteArray(KEY_DATA), data.getBoolean(KEY_IS_BACKGROUND_POLL), data.getLong(KEY_OPEN_GROUP_MESSAGE_SERVER_ID), data.getString(KEY_OPEN_GROUP_ID))
job.initJob(data)
return job
return MessageReceiveJob(data.getByteArray(KEY_DATA), data.getBoolean(KEY_IS_BACKGROUND_POLL), data.getLong(KEY_OPEN_GROUP_MESSAGE_SERVER_ID), data.getString(KEY_OPEN_GROUP_ID))
}
}
}

View File

@@ -20,8 +20,7 @@ class MessageSendJob(val message: Message, val destination: Destination) : Job {
override val maxFailureCount: Int = 10
companion object {
val TAG = MessageSendJob::class.qualifiedName
val collection: String = "MessageSendJobCollection"
val KEY: String = "MessageSendJob"
//keys used for database storage purpose
private val KEY_MESSAGE = "message"
@@ -77,7 +76,6 @@ class MessageSendJob(val message: Message, val destination: Destination) : Job {
//database functions
override fun serialize(): Data {
val builder = this.createJobDataBuilder()
//serialize Message and Destination properties
val kryo = Kryo()
kryo.isRegistrationRequired = false
@@ -89,11 +87,15 @@ class MessageSendJob(val message: Message, val destination: Destination) : Job {
output = Output(serializedDestination)
kryo.writeObject(output, destination)
output.close()
return builder.putByteArray(KEY_MESSAGE, serializedMessage)
return Data.Builder().putByteArray(KEY_MESSAGE, serializedMessage)
.putByteArray(KEY_DESTINATION, serializedDestination)
.build();
}
override fun getFactoryKey(): String {
return AttachmentDownloadJob.KEY
}
class Factory: Job.Factory<MessageSendJob> {
override fun create(data: Data): MessageSendJob {
val serializedMessage = data.getByteArray(KEY_MESSAGE)
@@ -106,9 +108,7 @@ class MessageSendJob(val message: Message, val destination: Destination) : Job {
input = Input(serializedDestination)
val destination: Destination = kryo.readObject(input, Destination::class.java)
input.close()
val job = MessageSendJob(message, destination)
job.initJob(data)
return job
return MessageSendJob(message, destination)
}
}
}

View File

@@ -24,7 +24,7 @@ class NotifyPNServerJob(val message: SnodeMessage) : Job {
// Settings
override val maxFailureCount: Int = 20
companion object {
val collection: String = "NotifyPNServerJobCollection"
val KEY: String = "NotifyPNServerJob"
//keys used for database storage purpose
private val KEY_MESSAGE = "message"
@@ -64,7 +64,6 @@ class NotifyPNServerJob(val message: SnodeMessage) : Job {
//database functions
override fun serialize(): Data {
val builder = this.createJobDataBuilder()
//serialize SnodeMessage property
val kryo = Kryo()
kryo.isRegistrationRequired = false
@@ -72,10 +71,14 @@ class NotifyPNServerJob(val message: SnodeMessage) : Job {
val output = Output(serializedMessage)
kryo.writeObject(output, message)
output.close()
return builder.putByteArray(KEY_MESSAGE, serializedMessage)
return Data.Builder().putByteArray(KEY_MESSAGE, serializedMessage)
.build();
}
override fun getFactoryKey(): String {
return AttachmentDownloadJob.KEY
}
class Factory: Job.Factory<NotifyPNServerJob> {
override fun create(data: Data): NotifyPNServerJob {
val serializedMessage = data.getByteArray(KEY_MESSAGE)
@@ -84,9 +87,7 @@ class NotifyPNServerJob(val message: SnodeMessage) : Job {
val input = Input(serializedMessage)
val message: SnodeMessage = kryo.readObject(input, SnodeMessage::class.java)
input.close()
val job = NotifyPNServerJob(message)
job.initJob(data)
return job
return NotifyPNServerJob(message)
}
}
}

View File

@@ -0,0 +1,14 @@
package org.session.libsession.messaging.jobs
import java.util.*
class SessionJobInstantiator(private val jobFactories: Map<String, Job.Factory<out Job>>) {
fun instantiate(jobFactoryKey: String, data: Data): Job {
if (jobFactories.containsKey(jobFactoryKey)) {
return jobFactories[jobFactoryKey]?.create(data) ?: throw IllegalStateException("Tried to instantiate a job with key '$jobFactoryKey', but no matching factory was found.")
} else {
throw IllegalStateException("Tried to instantiate a job with key '$jobFactoryKey', but no matching factory was found.")
}
}
}

View File

@@ -0,0 +1,18 @@
package org.session.libsession.messaging.jobs
import java.util.*
class SessionJobManagerFactories {
companion object {
fun getSessionJobFactories(): Map<String, Job.Factory<out Job>> {
return mapOf(
AttachmentDownloadJob.KEY to AttachmentDownloadJob.Factory(),
AttachmentUploadJob.KEY to AttachmentUploadJob.Factory(),
MessageReceiveJob.KEY to MessageReceiveJob.Factory(),
MessageSendJob.KEY to MessageSendJob.Factory(),
NotifyPNServerJob.KEY to NotifyPNServerJob.Factory()
)
}
}
}

View File

@@ -18,7 +18,7 @@ class VisibleMessage : Message() {
var profile: Profile? = null
companion object {
const val TAG = "BaseVisibleMessage"
const val TAG = "VisibleMessage"
fun fromProto(proto: SignalServiceProtos.Content): VisibleMessage? {
val dataMessage = proto.dataMessage ?: return null