Skip to content

Commit

Permalink
Prevent ASR from stopping by asr's response #2495
Browse files Browse the repository at this point in the history
* DirectiveProcessorBlocker mechanism
** It will block directive which is blocked by blocker.
  • Loading branch information
sangho.lee authored and shshshl09 committed Jul 1, 2024
1 parent 657ff3d commit 8b68df7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.skt.nugu.sdk.core.interfaces.directive.DirectiveSequencerInterface
import com.skt.nugu.sdk.core.interfaces.message.Directive
import com.skt.nugu.sdk.core.utils.Logger
import com.skt.nugu.sdk.core.utils.LoopThread
import com.skt.nugu.sdk.core.utils.getAsyncKey
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet
Expand Down Expand Up @@ -86,6 +87,7 @@ class DirectiveProcessor(
private var isEnabled = true

private var listeners = CopyOnWriteArraySet<DirectiveSequencerInterface.OnDirectiveHandlingListener>()
private val directiveProcessBlocker = CopyOnWriteArraySet<DirectiveProcessorInterface.Blocker>()

init {
processingLoop.start()
Expand Down Expand Up @@ -147,6 +149,18 @@ class DirectiveProcessor(
}
}

override fun addDirectiveBlocker(blocker: DirectiveProcessorInterface.Blocker) {
Logger.d(TAG, "[addDirectiveBlocker: $blocker]")
directiveProcessBlocker.add(blocker)
}

override fun removeDirectiveBlocker(blocker: DirectiveProcessorInterface.Blocker) {
Logger.d(TAG, "[removeDirectiveBlocker: $blocker]")
if(directiveProcessBlocker.remove(blocker)) {
processingLoop.wakeOne()
}
}

private fun scrub(dialogRequestId: String, targets: Set<NamespaceAndName>? = null) {
Logger.d(TAG, "[scrub] dialogRequestId $dialogRequestId, target ${targets.toString()}")
if (dialogRequestId.isEmpty()) {
Expand Down Expand Up @@ -390,31 +404,42 @@ class DirectiveProcessor(

private fun getNextUnblockedDirectiveLocked(): DirectiveAndPolicy? {
// A medium is considered blocked if a previous blocking directive hasn't been completed yet.
val blockedMediumsMap: MutableMap<String, MutableMap<BlockingPolicy.Medium, Boolean>> = HashMap()

// Mark mediums used by blocking directives being handled as blocked.
directivesBeingHandled.forEach { src ->
blockedMediumsMap[src.key] = HashMap<BlockingPolicy.Medium, Boolean>().also { dst ->
src.value.forEach {
dst[it.key] = true
}
val blockedMediumsMap: MutableMap<String, MutableSet<BlockingPolicy.Medium>> = HashMap<String, MutableSet<BlockingPolicy.Medium>>().apply {

// Mark mediums used by blocking directives being handled as blocked.
putAll(directivesBeingHandled.map {
it.key to it.value.keys
}.associate {
it.first to EnumSet.copyOf(it.second)
})

// Mark mediums used by blocker
directiveProcessBlocker.forEach {
val map = remove(it.dialogRequestId) ?: HashSet()
map.addAll(it.blockingPolicy)
put(it.dialogRequestId, map)
}
}

Logger.d(TAG, "[getNextUnblockedDirectiveLocked] block mediums : $blockedMediumsMap")

for (directiveAndPolicy in handlingQueue) {
val blockedMediums = blockedMediumsMap[directiveAndPolicy.directive.getDialogRequestId()]
val directive = directiveAndPolicy.directive

// first get asyncKey's dialogRequestId, if not exist check dialogRequestId
val dialogRequestId = directive.getAsyncKey()?.eventDialogRequestId ?: directive.getDialogRequestId()

val blockedMediums = blockedMediumsMap[dialogRequestId]
?: return directiveAndPolicy

val policy = directiveAndPolicy.policy

val blocked = policy.blockedBy?.any {
blockedMediums[it] == true
blockedMediums.contains(it)
} ?: false

policy.blocking?.forEach {
blockedMediums[it] = true
blockedMediums.add(it)
}

if (!blocked) {
Expand All @@ -424,4 +449,4 @@ class DirectiveProcessor(

return null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.skt.nugu.sdk.core.directivesequencer

import com.skt.nugu.sdk.core.interfaces.message.Directive
import com.skt.nugu.sdk.core.interfaces.directive.DirectiveHandler
import com.skt.nugu.sdk.core.interfaces.directive.DirectiveProcessorInterface
import com.skt.nugu.sdk.core.interfaces.directive.DirectiveSequencerInterface
import com.skt.nugu.sdk.core.utils.Logger
import com.skt.nugu.sdk.core.utils.LoopThread
Expand Down Expand Up @@ -119,4 +120,12 @@ class DirectiveSequencer :
override fun cancelDialogRequestId(dialogRequestId: String) {
directiveProcessor.cancelDialogRequestId(dialogRequestId)
}
}

override fun addDirectiveBlocker(blocker: DirectiveProcessorInterface.Blocker) {
directiveProcessor.addDirectiveBlocker(blocker)
}

override fun removeDirectiveBlocker(blocker: DirectiveProcessorInterface.Blocker) {
directiveProcessor.removeDirectiveBlocker(blocker)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

package com.skt.nugu.sdk.core.interfaces.directive

import java.util.EnumSet

interface DirectiveProcessorInterface {
fun cancelDialogRequestId(dialogRequestId: String)
}

data class Blocker(val dialogRequestId: String, val blockingPolicy: EnumSet<BlockingPolicy.Medium>)

fun addDirectiveBlocker(blocker: Blocker)
fun removeDirectiveBlocker(blocker: Blocker)
}

0 comments on commit 8b68df7

Please sign in to comment.