Skip to content

Commit f9add5e

Browse files
committed
Refactor DownloadManager and UpdateManager
Move corresponding authorization requests to Download and Update manager To handle them in their own actor Check if the artifacts are already downloaded before asking for authorization or even starting the download again Signed-off-by: Saeed Rezaee <[email protected]>
1 parent 5463049 commit f9add5e

File tree

3 files changed

+317
-214
lines changed

3 files changed

+317
-214
lines changed

src/main/kotlin/org/eclipse/hara/ddiclient/api/actors/DeploymentManager.kt

Lines changed: 6 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -19,143 +19,37 @@ import org.eclipse.hara.ddiclient.api.actors.ActionManager.Companion.Message.Upd
1919
import org.eclipse.hara.ddiclient.api.actors.ConnectionManager.Companion.Message.Out.DeploymentCancelInfo
2020
import org.eclipse.hara.ddiclient.api.actors.ConnectionManager.Companion.Message.Out.DeploymentInfo
2121
import org.eclipse.hara.ddiclient.api.MessageListener
22-
import kotlinx.coroutines.Dispatchers
2322
import kotlinx.coroutines.Job
2423
import kotlinx.coroutines.ObsoleteCoroutinesApi
25-
import kotlinx.coroutines.launch
26-
import org.eclipse.hara.ddiclient.api.DeploymentPermitProvider
2724

2825
@OptIn(ObsoleteCoroutinesApi::class, kotlinx.coroutines.ExperimentalCoroutinesApi::class)
2926
class DeploymentManager
3027
private constructor(scope: ActorScope) : AbstractActor(scope) {
3128

32-
private val registry = coroutineContext[HaraClientContext]!!.registry
33-
private val softRequest: DeploymentPermitProvider = coroutineContext[HaraClientContext]!!.softDeploymentPermitProvider
34-
private val forceRequest: DeploymentPermitProvider = coroutineContext[HaraClientContext]!!.forceDeploymentPermitProvider
3529
private val connectionManager = coroutineContext[CMActor]!!.ref
3630
private val notificationManager = coroutineContext[NMActor]!!.ref
3731
private var waitingAuthJob: Job? = null
3832
private fun beginningReceive(state: State): Receive = { msg ->
39-
when {
40-
41-
msg is DeploymentInfo && msg.downloadIs(ProvisioningType.forced) -> {
42-
forceDownloadTheArtifact(state, msg, forceRequest)
43-
}
44-
45-
msg is DeploymentInfo && msg.downloadIs(ProvisioningType.attempt) -> {
46-
attemptDownloadingTheArtifact(state, msg, softRequest)
47-
}
48-
49-
msg is DeploymentInfo && msg.downloadIs(ProvisioningType.skip) -> {
50-
// todo implement download skip option
51-
LOG.warn("skip download not yet implemented (used attempt)")
52-
attemptDownloadingTheArtifact(state, msg, softRequest)
53-
}
54-
55-
msg is DeploymentCancelInfo -> {
56-
stopUpdateAndNotify(msg)
57-
}
58-
59-
else -> unhandled(msg)
60-
}
61-
}
62-
63-
private suspend fun forceDownloadTheArtifact(state: State,
64-
msg: DeploymentInfo,
65-
deploymentPermitProvider: DeploymentPermitProvider) {
66-
val message = "Start downloading artifacts"
67-
LOG.info(message)
68-
sendFeedback(message)
69-
val result = deploymentPermitProvider.downloadAllowed().await()
70-
if (result) {
71-
become(downloadingReceive(state.copy(deplBaseResp = msg.info)))
72-
child("downloadManager")!!.send(msg)
73-
} else {
74-
LOG.info("Authorization denied for download files")
75-
}
76-
}
77-
78-
private suspend fun attemptDownloadingTheArtifact(state: State,
79-
msg: DeploymentInfo,
80-
deploymentPermitProvider: DeploymentPermitProvider) {
81-
val message = "Waiting authorization to download"
82-
LOG.info(message)
83-
sendFeedback(message)
84-
become(waitingDownloadAuthorization(state.copy(deplBaseResp = msg.info)))
85-
notificationManager.send(MessageListener.Message.State
86-
.WaitingDownloadAuthorization(false))
87-
waitingAuthJob?.cancel()
88-
waitingAuthJob = launch {
89-
val result = deploymentPermitProvider.downloadAllowed().await()
90-
if (result) {
91-
channel.send(Message.DownloadGranted)
92-
} else {
93-
LOG.info("Authorization denied for download files")
94-
}
95-
waitingAuthJob = null
96-
}
97-
}
98-
99-
private fun waitingDownloadAuthorization(state: State): Receive = { msg ->
100-
when (msg) {
33+
when(msg) {
10134
is DeploymentInfo -> {
102-
when {
103-
104-
msg.downloadIs(ProvisioningType.attempt) && !msg.forceAuthRequest -> {}
105-
106-
else -> {
107-
become(beginningReceive(state))
108-
channel.send(msg)
109-
}
110-
}
111-
}
112-
113-
is Message.DownloadGranted -> {
114-
val message = "Authorization granted for downloading files"
115-
LOG.info(message)
116-
sendFeedback(message)
117-
become(downloadingReceive(state))
118-
child("downloadManager")!!.send(DeploymentInfo(state.deplBaseResp!!))
35+
become(downloadingReceive(state.copy(deplBaseResp = msg.info)))
36+
child("downloadManager")!!.send(msg)
11937
}
12038

12139
is DeploymentCancelInfo -> {
12240
stopUpdateAndNotify(msg)
12341
}
12442

125-
is CancelForced -> {
126-
stopUpdate()
127-
}
128-
12943
else -> unhandled(msg)
13044
}
13145
}
13246

13347
private fun downloadingReceive(state: State): Receive = { msg ->
13448
when (msg) {
13549
is Message.DownloadFinished -> {
136-
val message: String
137-
if (state.updateIs(ProvisioningType.forced)) {
138-
message = "Start updating the device"
139-
waitingAuthJob = launch(Dispatchers.IO) {
140-
if(forceRequest.updateAllowed().await()){
141-
become(updatingReceive())
142-
child("updateManager")!!.send(DeploymentInfo(state.deplBaseResp!!))
143-
} else {
144-
LOG.info("Authorization denied for update")
145-
}
146-
waitingAuthJob = null
147-
}
148-
} else {
149-
message = "Waiting authorization to update"
150-
become(waitingUpdateAuthorization(state))
151-
notificationManager.send(MessageListener.Message.State.WaitingUpdateAuthorization(state.updateIs(ProvisioningType.forced)))
152-
waitingAuthJob = launch(Dispatchers.IO) {
153-
onAuthorizationReceive(softRequest)
154-
waitingAuthJob = null
155-
}
156-
}
157-
LOG.info(message)
158-
sendFeedback(message)
50+
become(updatingReceive())
51+
child("updateManager")!!.send(DeploymentInfo(state.deplBaseResp!!))
52+
15953
}
16054
is Message.DownloadFailed -> {
16155
LOG.error("download failed")
@@ -171,42 +65,6 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
17165
}
17266
}
17367

174-
private suspend fun onAuthorizationReceive(deploymentPermitProvider: DeploymentPermitProvider){
175-
if(deploymentPermitProvider.updateAllowed().await()){
176-
channel.send(Message.UpdateGranted)
177-
} else {
178-
LOG.info("Authorization denied for update")
179-
}
180-
}
181-
182-
private fun waitingUpdateAuthorization(state: State): Receive = { msg ->
183-
when (msg) {
184-
185-
is DeploymentInfo -> {
186-
become(downloadingReceive(state.copy(deplBaseResp = msg.info)))
187-
channel.send(Message.DownloadFinished)
188-
}
189-
190-
is Message.UpdateGranted -> {
191-
val message = "Authorization granted for update"
192-
LOG.info(message)
193-
sendFeedback(message)
194-
become(updatingReceive())
195-
child("updateManager")!!.send(DeploymentInfo(state.deplBaseResp!!))
196-
}
197-
198-
is DeploymentCancelInfo -> {
199-
stopUpdateAndNotify(msg)
200-
}
201-
202-
is CancelForced -> {
203-
stopUpdate()
204-
}
205-
206-
else -> unhandled(msg)
207-
}
208-
}
209-
21068
private fun updatingReceive(): Receive = { msg ->
21169
when (msg) {
21270

@@ -284,8 +142,6 @@ private constructor(scope: ActorScope) : AbstractActor(scope) {
284142
}
285143

286144
sealed class Message {
287-
object DownloadGranted : Message()
288-
object UpdateGranted : Message()
289145
object DownloadFinished : Message()
290146
data class DownloadFailed(val details: List<String>) : Message()
291147
object UpdateFailed : Message()

0 commit comments

Comments
 (0)