Skip to content

Commit

Permalink
#thêm name cho actor
Browse files Browse the repository at this point in the history
  • Loading branch information
phuvh committed Jul 5, 2022
1 parent 895ebe3 commit 531ba74
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/main/kotlin/org/magicghostvu/actor/Behavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.channels.ActorScope
import kotlinx.coroutines.channels.SendChannel
import org.magicghostvu.actor.timer.TimerManData
import org.magicghostvu.mlogger.ActorLogger


class MActorRef<in Message>(private val internalChannel: SendChannel<Message>) {
class MActorRef<in Message>(private val internalChannel: SendChannel<Message>, private val name:String) {
public suspend fun tell(message: Message) {
internalChannel.send(message)
try {
internalChannel.send(message)
} catch (e: Exception) {
ActorLogger.logger.error("err while send message to actor $name")
}
}
}

open class Behavior<in T> {

}

@OptIn(ObsoleteCoroutinesApi::class)
abstract class AbstractBehaviour<T>(protected val scope: ActorScope<T>) : Behavior<T>() {
abstract suspend fun onReceive(message: T): Behavior<T>
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/org/magicghostvu/actor/Behaviors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ object Behaviors {
private fun <T> CoroutineScope.spawn(
debug: Boolean = false,
createNewScope: Boolean = false,
name: String,
factory: suspend () -> Behavior<T>
): MActorRef<T> {
val scopeSpawnActor =
Expand Down Expand Up @@ -173,22 +174,24 @@ object Behaviors {
}
}

return MActorRef(internalChannel as SendChannel<T>)
return MActorRef(internalChannel as SendChannel<T>, name)
}

@OptIn(ObsoleteCoroutinesApi::class)
fun <T> ActorScope<*>.spawnChild(
debug: Boolean = false,
name: String,
factory: suspend () -> Behavior<T>
): MActorRef<T> {
return spawn(debug, createNewScope = false, factory)
return spawn(debug, createNewScope = false, name, factory)
}

fun <T> CoroutineScope.spawnNew(
debug: Boolean = false,
name: String,
factory: suspend () -> Behavior<T>
): MActorRef<T> {
return spawn(debug, createNewScope = true, factory)
return spawn(debug, createNewScope = true, name, factory)
}


Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/magicghostvu/run/RunActor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class State1(scope: ActorScope<Msg>, var i: Int) : AbstractBehaviour<Msg>(scope)
if (message is Msg1) {
i++
if (!setChild) {
child = scope.spawnChild {
child = scope.spawnChild(name = "child1") {
State2.setup()
}
logger.info("set child success")
Expand Down Expand Up @@ -110,7 +110,7 @@ fun main(arr: Array<String>) {
}
}

val parent = spawnNew<Msg> {
val parent = spawnNew<Msg>(name = "actor1") {
Behaviors.withTimer { timer ->
timer.startFixedRateTimer(
Msg2("ádasd"),
Expand Down

0 comments on commit 531ba74

Please sign in to comment.