Skip to content

Instantly share code, notes, and snippets.

@karno
Created June 4, 2013 11:04
Show Gist options
  • Save karno/5705162 to your computer and use it in GitHub Desktop.
Save karno/5705162 to your computer and use it in GitHub Desktop.
class AgentRuntime(profile: Properties, holder: ServiceBindingHolder[MicroRuntimeServiceBinder]) {
def startAgent(name: String, args: Array[AnyRef]): Future[AgentController] = holder.getServiceBinder() flatMap {
binder => createContainer(binder)
} flatMap {
binder => startAgentCore(name, args, binder)
} flatMap {
_ => future(MicroRuntime.getAgent(name))
}
private def startAgentCore[T <: Agent](name: String, args: Array[AnyRef],
serviceBinder: MicroRuntimeServiceBinder): Future[MicroRuntimeServiceBinder] = {
val p = Promise[MicroRuntimeServiceBinder]
serviceBinder.startAgent(name, classOf[T].getCanonicalName, args, new RuntimeCallback[Void] {
def onFailure(t: Throwable) {
p.failure(t)
}
def onSuccess(_null: Void) {
p.success(serviceBinder)
}
})
p.future
}
private def createContainer(serviceBinder: MicroRuntimeServiceBinder): Future[MicroRuntimeServiceBinder] = {
MicroRuntime.isRunning match {
case true => future(serviceBinder)
case false => {
val p = Promise[MicroRuntimeServiceBinder]()
serviceBinder.startAgentContainer(profile, new RuntimeCallback[Void] {
def onFailure(t: Throwable) {
p.failure(t)
}
def onSuccess(_null: Void) {
p.success(serviceBinder)
}
})
p.future
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment