https://github.com/bethibande/actors
Simple actor framework for kotlin
https://github.com/bethibande/actors
Last synced: 2 months ago
JSON representation
Simple actor framework for kotlin
- Host: GitHub
- URL: https://github.com/bethibande/actors
- Owner: Bethibande
- License: apache-2.0
- Created: 2024-01-26T15:06:58.000Z (over 2 years ago)
- Default Branch: development
- Last Pushed: 2024-04-13T19:38:59.000Z (about 2 years ago)
- Last Synced: 2024-04-14T00:37:51.816Z (about 2 years ago)
- Language: Kotlin
- Homepage:
- Size: 165 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://sonar.bethibande.com/dashboard?id=actors)
[](https://sonar.bethibande.com/dashboard?id=actors)
[](https://sonar.bethibande.com/dashboard?id=actors)
[](https://sonar.bethibande.com/dashboard?id=actors)
[](https://sonar.bethibande.com/dashboard?id=actors)
[](https://wakatime.com/badge/user/72047d25-7643-4124-9850-1dd48ddf85f0/project/018d4022-e313-4df5-8fc4-262be0c59caf)
# Actors
A simple actor based programming framework for kotlin.
Currently only compatible with kotlin jvm.
> [!WARNING]
> This project is still experimental and lacking in features.
> The implementation still needs some work and refactoring, especially the KSP processor.
> This framework is not ready for production use, there are no official maven releases yet!
### Example (Experimental)
Please note, this API is still experimental.
[PersonState.kt](example/src/main/kotlin/com/bethibande/example/person/PersonState.kt)
```kotlin
import com.bethibande.actors.annotations.ActorState
/**
* An actor implementation will be generated from this class.
*/
@ActorState
data class PersonState(
var name: String,
var age: Int,
)
```
[Main.kt](example/src/main/kotlin/com/bethibande/example/Main.kt), usage of the generated code
```kotlin
runBlocking {
// Create actor-system
val system = Person.localActorSystem()
// Create a new actor
val person: Person = system.new(PersonState("Max", 17))
// Use the actor
println("${person.getName()}: ${person.getAge()}")
person.setAge(18)
println("${person.getName()}: ${person.getAge()}")
// Custom behavior/command (see com.bethibande.example.person.CustomFunctionality.kt)
val (name, age) = person.getNameAndAge()
println("Custom: $name, $age")
// Send close command
person.close()
}
```
[CustomFunctionality.kt](example/src/main/kotlin/com/bethibande/example/person/CustomFunctionality.kt), adds custom functionallity / commands to the generated actor
```kotlin
import com.bethibande.actors.Actor
import com.bethibande.actors.behavior.Behavior
import com.bethibande.example.person.command.PersonCommand
import kotlinx.coroutines.CompletableDeferred
data class PersonCommandGetNameAndAge(
val deferred: CompletableDeferred>
): PersonCommand {
companion object: Behavior {
init {
// Adds the behavior to all actors of the Person type, also affects existing actors.
Person.BehaviorMap.add(PersonCommandGetNameAndAge::class.java, this)
}
override suspend fun accept(
command: PersonCommandGetNameAndAge,
state: PersonState,
actor: Actor
) {
command.deferred.complete(state.name to state.age)
}
}
}
suspend fun Person.getNameAndAge(): Pair {
val deferred = CompletableDeferred>()
send(PersonCommandGetNameAndAge(deferred))
return deferred.await()
}
```
### Dependencies
- [Google KSP](https://github.com/google/ksp) for processing symbols like annotations
- [Kotlinpoet](https://github.com/square/kotlinpoet) for generating kotlin source code
- [Kotlinx Coroutines](https://github.com/Kotlin/kotlinx.coroutines) for asynchronous programming