-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow connection to environment daemon from another port
- Loading branch information
Showing
4 changed files
with
144 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
akka { | ||
actor { | ||
provider = "akka.remote.RemoteActorRefProvider" | ||
allow-java-serialization = on | ||
} | ||
remote { | ||
enabled-transports = ["akka.remote.netty.tcp"] | ||
netty.tcp { | ||
hostname = "127.0.0.1" | ||
port = 0 | ||
artery { | ||
enabled = on | ||
transport = tcp | ||
canonical.hostname = "127.0.0.1" | ||
canonical.port = 0 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ai.newmap.interpreter | ||
|
||
import akka.actor.{Actor, ActorSystem, ExtendedActorSystem, Props} | ||
import akka.pattern.after | ||
import ai.newmap.util.{Success, Failure} | ||
import scala.concurrent.{Await, Future} | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
class DaemonActor extends Actor { | ||
private val envInterpreter = new EnvironmentInterpreter() | ||
|
||
def getBoundPort(): Future[Int] = { | ||
implicit val actorSystem: ActorSystem = context.system | ||
context.system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress.port match { | ||
case Some(port) => Future.successful(port) | ||
case None => { | ||
after(100.millis, actorSystem.scheduler) { | ||
getBoundPort() | ||
} | ||
} | ||
} | ||
} | ||
|
||
def receive = { | ||
case (code: String) => sender() ! passCode(code) | ||
case (_: Unit) => sender() ! { | ||
val boundPort = Await.result(getBoundPort(), 400.millis) | ||
//val boundPortStr = boundPort.toOption.map(_.toString).getOrElse("not avilable") | ||
s"Connected to the Environment Daemon on port $boundPort at path ${self.path}" | ||
} | ||
case other => sender() ! s"Can't interpret: $other" | ||
} | ||
|
||
def passCode(code: String): DaemonActor.CodeResponse = { | ||
val response = envInterpreter(code) | ||
|
||
response match { | ||
case Success(s) => DaemonActor.CodeResponse(s, (s == ":exit")) | ||
case Failure(s) => DaemonActor.CodeResponse("Error:\n" + s) | ||
} | ||
} | ||
} | ||
|
||
object DaemonActor { | ||
case class CodeResponse( | ||
response: String, | ||
timeToQuit: Boolean = false | ||
) | ||
} |
111 changes: 73 additions & 38 deletions
111
src/main/scala/ai/newmap/interpreter/EnvironmentDaemon.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,90 @@ | ||
package ai.newmap.interpreter | ||
|
||
import akka.actor.{Actor, ActorSystem, ExtendedActorSystem, Props} | ||
import akka.actor.{Actor, ActorRef, ActorSystem, ActorIdentity, ExtendedActorSystem, Identify, Props} | ||
import akka.pattern.after | ||
import akka.util.Timeout | ||
import ai.newmap.util.{Success, Failure} | ||
import scala.concurrent.{Await, Future} | ||
import scala.concurrent.duration._ | ||
import java.net.Socket | ||
import java.net.InetSocketAddress | ||
import akka.pattern.ask | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
class EnvironmentDaemon() { | ||
val system = ActorSystem("DaemonActorSystem") | ||
val daemonActor = system.actorOf(Props[DaemonActor], name = "daemonActor") | ||
} | ||
|
||
class DaemonActor extends Actor { | ||
private val envInterpreter = new EnvironmentInterpreter() | ||
|
||
def getBoundPort(): Future[Int] = { | ||
implicit val actorSystem: ActorSystem = context.system | ||
context.system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress.port match { | ||
case Some(port) => Future.successful(port) | ||
case None => { | ||
after(100.millis, actorSystem.scheduler) { | ||
getBoundPort() | ||
} | ||
/** | ||
* This handles either creating an environment daemon, or connecting to | ||
* an existing daemon on another port. | ||
*/ | ||
class EnvironmentDaemon(portOpt: Option[Int]) { | ||
implicit val timeout: Timeout = 5.seconds | ||
|
||
def isPortOpen(host: String, port: Int, timeoutMillis: Int = 2000): Boolean = { | ||
val socket = new Socket() | ||
try { | ||
socket.connect(new InetSocketAddress(host, port), timeoutMillis) | ||
true | ||
} catch { | ||
case _: Throwable => false | ||
} finally { | ||
try { | ||
socket.close() | ||
} catch { | ||
case _: Throwable => // Ignore | ||
} | ||
} | ||
} | ||
|
||
def receive = { | ||
case (code: String) => sender() ! passCode(code) | ||
case (_: Unit) => sender() ! { | ||
val boundPort = Await.result(getBoundPort(), 400.millis) | ||
//val boundPortStr = boundPort.toOption.map(_.toString).getOrElse("not avilable") | ||
s"Connected to the Environment Daemon on port $boundPort" | ||
} | ||
case other => sender() ! s"Can't interpret: $other" | ||
} | ||
val unit: Unit = () | ||
|
||
def passCode(code: String): DaemonActor.CodeResponse = { | ||
val response = envInterpreter(code) | ||
// This ensures that the environment daemon is initialized | ||
val resultF: Future[(ActorRef, ActorSystem)] = { | ||
val actorSystem: ActorSystem = ActorSystem("DaemonActorSystem") | ||
for { | ||
result: ActorRef <- portOpt match { | ||
case None => { | ||
val actorRef = actorSystem.actorOf(Props[DaemonActor], name = "daemonActor") | ||
|
||
for { | ||
pingResponse <- actorRef ? unit | ||
} yield { | ||
println(pingResponse) | ||
actorRef | ||
} | ||
} | ||
case Some(port) if !isPortOpen("localhost", port) => { | ||
println(s"port $port not open, creating a new EnvironemntDaemon") | ||
val actorRef = actorSystem.actorOf(Props[DaemonActor], name = "daemonActor") | ||
|
||
for { | ||
pingResponse <- actorRef ? unit | ||
} yield { | ||
println(pingResponse) | ||
actorRef | ||
} | ||
} | ||
case Some(port) => { | ||
val daemonActorPath = s"akka://[email protected]:$port/user/daemonActor" | ||
println(s"Connecting to path $daemonActorPath") | ||
val selection = actorSystem.actorSelection(daemonActorPath) | ||
|
||
response match { | ||
case Success(s) => DaemonActor.CodeResponse(s, (s == ":exit")) | ||
case Failure(s) => DaemonActor.CodeResponse("Error:\n" + s) | ||
} | ||
for { | ||
pingResponse <- (selection ? unit) | ||
actorF = (selection ? Identify(None)).mapTo[ActorIdentity] | ||
actor <- actorF | ||
} yield { | ||
println(pingResponse) | ||
actor.ref.get | ||
} | ||
} | ||
} | ||
} yield (result -> actorSystem) | ||
} | ||
} | ||
|
||
object DaemonActor { | ||
case class CodeResponse( | ||
response: String, | ||
timeToQuit: Boolean = false | ||
) | ||
val result = Await.result(resultF, 5.seconds) | ||
|
||
val (daemon, actorSystemRef) = result | ||
|
||
def terminate(): Unit = actorSystemRef.terminate() | ||
|
||
def sendCode(code: String): Future[Any] = daemon ? code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters