Skip to content

Commit

Permalink
Remove redundant variables
Browse files Browse the repository at this point in the history
Also made the code more readable

Signed-off-by: Saeed Rezaee <[email protected]>
  • Loading branch information
SaeedRe committed Dec 15, 2023
1 parent f9add5e commit 86e905c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ package org.eclipse.hara.ddiclient.api.actors

import org.eclipse.hara.ddi.api.model.CancelFeedbackRequest
import org.eclipse.hara.ddi.api.model.DeploymentBaseResponse
import org.eclipse.hara.ddi.api.model.DeploymentBaseResponse.Deployment.ProvisioningType
import org.eclipse.hara.ddi.api.model.DeploymentFeedbackRequest
import org.eclipse.hara.ddiclient.api.actors.ActionManager.Companion.Message.CancelForced
import org.eclipse.hara.ddiclient.api.actors.ActionManager.Companion.Message.UpdateStopped
import org.eclipse.hara.ddiclient.api.actors.ConnectionManager.Companion.Message.Out.DeploymentCancelInfo
import org.eclipse.hara.ddiclient.api.actors.ConnectionManager.Companion.Message.Out.DeploymentInfo
import org.eclipse.hara.ddiclient.api.MessageListener
import kotlinx.coroutines.Job
import kotlinx.coroutines.ObsoleteCoroutinesApi

@OptIn(ObsoleteCoroutinesApi::class, kotlinx.coroutines.ExperimentalCoroutinesApi::class)
Expand All @@ -28,7 +25,6 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {

private val connectionManager = coroutineContext[CMActor]!!.ref
private val notificationManager = coroutineContext[NMActor]!!.ref
private var waitingAuthJob: Job? = null
private fun beginningReceive(state: State): Receive = { msg ->
when(msg) {
is DeploymentInfo -> {
Expand Down Expand Up @@ -108,38 +104,16 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
parent!!.send(UpdateStopped)
}

private fun DeploymentInfo.downloadIs(level: ProvisioningType): Boolean {
return this.info.deployment.download == level
}

init {
actorOf("downloadManager") { DownloadManager.of(it) }
actorOf("updateManager") { UpdateManager.of(it) }
become(beginningReceive(State()))
channel.invokeOnClose {
waitingAuthJob?.cancel()
}
}

private suspend fun sendFeedback(id: String, vararg messages: String) {
connectionManager.send(
ConnectionManager.Companion.Message.In.DeploymentFeedback(
DeploymentFeedbackRequest.newInstance(id,
DeploymentFeedbackRequest.Status.Execution.proceeding,
DeploymentFeedbackRequest.Status.Result.Progress(0, 0),
DeploymentFeedbackRequest.Status.Result.Finished.none,
*messages
)
)
)
}

companion object {
fun of(scope: ActorScope) = DeploymentManager(scope)

data class State(val deplBaseResp: DeploymentBaseResponse? = null) {
fun updateIs(level: ProvisioningType): Boolean = deplBaseResp!!.deployment.update == level
}
data class State(val deplBaseResp: DeploymentBaseResponse? = null)

sealed class Message {
object DownloadFinished : Message()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,15 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
checkAndHandleFilesAlreadyDownloaded(msg) {

when {
msg.downloadIs(
DeploymentBaseResponse.Deployment.ProvisioningType.forced) -> {
msg.isDownloadForced -> {
forceDownloadTheArtifact(msg)
}

msg.downloadIs(
DeploymentBaseResponse.Deployment.ProvisioningType.attempt) -> {
msg.isDownloadSoft -> {
attemptDownloadingTheArtifact(State(msg.info), msg)
}

msg.downloadIs(
DeploymentBaseResponse.Deployment.ProvisioningType.skip) -> {
msg.isDownloadSkip -> {
// todo implement download skip option
LOG.warn("skip download not yet implemented (used attempt)")
attemptDownloadingTheArtifact(State(msg.info), msg)
Expand Down Expand Up @@ -176,9 +173,7 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
when (msg) {
is DeploymentInfo -> {
when {

msg.downloadIs(DeploymentBaseResponse.Deployment.ProvisioningType.attempt)
&& !msg.forceAuthRequest -> {}
msg.isDownloadSoft && !msg.forceAuthRequest -> {}

else -> {
become(beforeStartReceive())
Expand Down Expand Up @@ -309,9 +304,14 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
parent!!.send(ActionManager.Companion.Message.UpdateStopped)
}

private fun DeploymentInfo.downloadIs(level: DeploymentBaseResponse.Deployment.ProvisioningType): Boolean {
return this.info.deployment.download == level
}
private val DeploymentInfo.isDownloadForced: Boolean
get() = info.deployment.download == forced

private val DeploymentInfo.isDownloadSoft: Boolean
get() = info.deployment.download == attempt

private val DeploymentInfo.isDownloadSkip: Boolean
get() = info.deployment.download == attempt

private fun childName(md5: String) = "fileDownloader_for_$md5"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
when (msg) {

is DeploymentInfo -> {
val state = DeploymentManager.Companion.State(deplBaseResp = msg.info)
val state = State(deplBaseResp = msg.info)
val message = when {
state.updateIs(DeploymentBaseResponse.Deployment.ProvisioningType.forced) -> {
state.isUpdateForced -> {
forceUpdateDevice(state)
}
else -> {
Expand All @@ -66,7 +66,7 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
}
}

private fun waitingUpdateAuthorization(state: DeploymentManager.Companion.State): Receive = { msg ->
private fun waitingUpdateAuthorization(state: State): Receive = { msg ->
when (msg) {

is DeploymentInfo -> {
Expand All @@ -93,7 +93,7 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
}
}

private fun forceUpdateDevice(state: DeploymentManager.Companion.State): String {
private fun forceUpdateDevice(state: State): String {
waitingAuthJob = launch(Dispatchers.IO) {
forceRequest.onAuthorizationReceive {
startUpdateProcedure(DeploymentInfo(state.deplBaseResp!!))
Expand All @@ -103,10 +103,10 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
return "Start updating the device"
}

private suspend fun attemptUpdateDevice(state: DeploymentManager.Companion.State): String {
private suspend fun attemptUpdateDevice(state: State): String {
become(waitingUpdateAuthorization(state))
notificationManager.send(MessageListener.Message.State.WaitingUpdateAuthorization(state.updateIs(
DeploymentBaseResponse.Deployment.ProvisioningType.forced)))
notificationManager.send(
MessageListener.Message.State.WaitingUpdateAuthorization(state.isUpdateForced))
waitingAuthJob = launch(Dispatchers.IO) {
softRequest.onAuthorizationReceive {
channel.send(Message.UpdateGranted)
Expand Down Expand Up @@ -228,6 +228,11 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
companion object {
fun of(scope: ActorScope) = UpdateManager(scope)

data class State(val deplBaseResp: DeploymentBaseResponse? = null) {
val isUpdateForced: Boolean
get() = deplBaseResp!!.deployment.update ==
DeploymentBaseResponse.Deployment.ProvisioningType.forced
}
sealed class Message {
object UpdateGranted : Message()
}
Expand Down

0 comments on commit 86e905c

Please sign in to comment.