Skip to content

Instantly share code, notes, and snippets.

@fboldog
Last active February 19, 2016 09:46
Show Gist options
  • Save fboldog/2f17639906503eb1623f to your computer and use it in GitHub Desktop.
Save fboldog/2f17639906503eb1623f to your computer and use it in GitHub Desktop.
Minimalist Kotlin Actors, based on Viktor Klang "Minimalist Java & Scala Actors"
/*
Copyright 2016 Ferenc Boldog
Copyright 2012 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.ferencboldog.minaktors
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.Executor
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger
/** Visibility is achieved by volatile-piggybacking of reads+writes to "on" */
class Actor {
/** An Effect returns a Behavior given a Behavior */
interface Effect: (Behavior) -> Behavior
/** A Behavior is a message (Object) which returns the behavior for the next message */
interface Behavior: (Any) -> Effect
/** An Address is somewhere you can send messages */
interface Address {
fun tell(message: Any): Address
}
/** Defining a composite of AtomcInteger, Runnable and Address */
private abstract class AtomicRunnableAddress: Runnable, Address {
protected val on = AtomicInteger()
}
/** Become is an Effect that returns a captured Behavior no matter what the old Behavior is */
data class Become(val behavior: Behavior): Effect {
override fun invoke(old: Behavior) = behavior
}
companion object Factory {
/** Stay is an Effect that returns the old Behavior when applied. */
final val Stay = object: Effect {
override fun invoke(t: Behavior) = t
}
/** Die is an Effect which replaces the old Behavior with a new one which does nothing, forever. */
final val Die = Become(object: Behavior {
override fun invoke(t: Any): Effect {
println("Dropping msg $t due to severe case of death.")
return Stay
}
})
/** Make self-aware */
public final fun create(initial: (Address) -> Behavior, executor: Executor = Executors.newCachedThreadPool()): Address {
val atomic = object : AtomicRunnableAddress() {
private val queue = ConcurrentLinkedQueue<Any>()
private var behavior: Behavior = object : Behavior {
override fun invoke(t: Any): Effect {
return if (t is Address) {
Become(initial(t))
} else {
Stay
}
}
}
public final override fun tell(message: Any): Address {
if (queue.offer(message)) {
async()
}
return this
}
public final override fun run() {
if (on.get() == 1) {
try {
behavior = behavior(queue.poll())(behavior)
} finally {
on.set(0)
async()
}
}
}
private final fun async() {
if (!queue.isEmpty() && on.compareAndSet(0, 1)) {
try {
executor.execute(this)
} catch (e: RuntimeException) {
on.set(0)
throw e
}
}
}
}
return atomic.tell(atomic)
}
}
}
fun main(args: Array<String>) {
val actor = Actor.create({
object : Actor.Behavior {
override fun invoke(any: Any): Actor.Effect {
if (any is String && any == "someMessage") {
println("call whatever you want ")
}
return Actor.Die;
}
}
})
actor.tell("someMessage")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment