From 0d79c6a043043ac9034b81b0835aa5af1b1a1f98 Mon Sep 17 00:00:00 2001 From: Saeed Rezaee Date: Mon, 20 May 2024 17:05:55 +0200 Subject: [PATCH] Fix authorization request handling issue This commit addresses an issue where the client sends an immediate authorization response upon receiving the authorization request, which might get ignored, causing the client to be stuck waiting for the authorization response. Signed-off-by: Saeed Rezaee --- .../ddiclient/api/actors/DownloadManager.kt | 16 ++++++++++----- .../ddiclient/api/actors/UpdateManager.kt | 20 +++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/org/eclipse/hara/ddiclient/api/actors/DownloadManager.kt b/src/main/kotlin/org/eclipse/hara/ddiclient/api/actors/DownloadManager.kt index 16987a2..6375e35 100644 --- a/src/main/kotlin/org/eclipse/hara/ddiclient/api/actors/DownloadManager.kt +++ b/src/main/kotlin/org/eclipse/hara/ddiclient/api/actors/DownloadManager.kt @@ -90,13 +90,14 @@ private constructor(scope: ActorScope) : AbstractActor(scope) { LOG.info(message) feedback(msg.info.id, proceeding, Progress(0, 0), none, message) become(waitingDownloadAuthorization(state.copy(deplBaseResp = msg.info))) - notificationManager.send(MessageListener.Message.State - .WaitingDownloadAuthorization(false)) waitingAuthJob?.cancel() waitingAuthJob = launch { - softRequest.onAuthorizationReceive { + softRequest.onAuthorizationReceive(onWaitForAuthorization = { + notificationManager.send(MessageListener.Message.State + .WaitingDownloadAuthorization(false)) + }, onGrantAuthorization = { channel.send(Message.DownloadGranted) - } + }) waitingAuthJob = null } } @@ -127,8 +128,13 @@ private constructor(scope: ActorScope) : AbstractActor(scope) { } private suspend fun DeploymentPermitProvider.onAuthorizationReceive( + onWaitForAuthorization: (suspend ()-> Unit)? = null, onGrantAuthorization: suspend ()-> Unit){ - if(downloadAllowed().await()){ + //Since downloadAllowed function might be a long-running operation, + // it is better to execute it first and then notify the user about the waiting process + val downloadAllowed = downloadAllowed() + onWaitForAuthorization?.invoke() + if(downloadAllowed.await()){ onGrantAuthorization.invoke() } else { LOG.info("Authorization denied for download files") diff --git a/src/main/kotlin/org/eclipse/hara/ddiclient/api/actors/UpdateManager.kt b/src/main/kotlin/org/eclipse/hara/ddiclient/api/actors/UpdateManager.kt index 516adae..464b07f 100644 --- a/src/main/kotlin/org/eclipse/hara/ddiclient/api/actors/UpdateManager.kt +++ b/src/main/kotlin/org/eclipse/hara/ddiclient/api/actors/UpdateManager.kt @@ -105,20 +105,28 @@ private constructor(scope: ActorScope) : AbstractActor(scope) { private suspend fun attemptUpdateDevice(state: State): String { become(waitingUpdateAuthorization(state)) - notificationManager.send( - MessageListener.Message.State.WaitingUpdateAuthorization(state.isUpdateForced)) waitingAuthJob = launch { - softRequest.onAuthorizationReceive { - channel.send(Message.UpdateGranted) - } + softRequest.onAuthorizationReceive( + onWaitForAuthorization = { + notificationManager.send( + MessageListener.Message.State.WaitingUpdateAuthorization( + state.isUpdateForced)) + }, onGrantAuthorization = { + channel.send(Message.UpdateGranted) + }) waitingAuthJob = null } return "Waiting authorization to update" } private suspend fun DeploymentPermitProvider.onAuthorizationReceive( + onWaitForAuthorization: (suspend ()-> Unit)? = null, onGrantAuthorization: suspend ()-> Unit){ - if(updateAllowed().await()){ + //Since updateAllowed function might be a long-running operation, + // it is better to execute it first and then notify the user about the waiting process + val updateAllowed = updateAllowed() + onWaitForAuthorization?.invoke() + if(updateAllowed.await()){ onGrantAuthorization.invoke() } else { LOG.info("Authorization denied for update")