Skip to content

Commit

Permalink
Put environment daemon on port and announce port
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsklar committed Oct 2, 2023
1 parent 6da1d58 commit efd3c79
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
12 changes: 12 additions & 0 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 0
}
}
}
58 changes: 40 additions & 18 deletions src/main/scala/ai/newmap/interpreter/EnvironmentDaemon.scala
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
package ai.newmap.interpreter

import akka.actor.{Actor, ActorSystem, Props}
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

object EnvironmentDaemon {
class EnvironmentDaemon() {
val system = ActorSystem("DaemonActorSystem")
val daemonActor = system.actorOf(Props[DaemonActor], name = "daemonActor")
}

case class CodeResponse(
response: String,
timeToQuit: Boolean = false
)
class DaemonActor extends Actor {
private val envInterpreter = new EnvironmentInterpreter()

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() ! s"Connected to the Environment Daemon"
case other => sender() ! s"Can't interpret: $other"
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"
}

def passCode(code: String): CodeResponse = {
val response = envInterpreter(code)
def passCode(code: String): DaemonActor.CodeResponse = {
val response = envInterpreter(code)

response match {
case Success(s) => CodeResponse(s, (s == ":exit"))
case Failure(s) => CodeResponse("Error:\n" + s)
}
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
)
}
9 changes: 5 additions & 4 deletions src/main/scala/ai/newmap/interpreter/repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ object repl extends App {

// This ensures that the environment daemon is initialized
val unit: Unit = ()
val pingResponseF = (EnvironmentDaemon.daemonActor ? unit)
val envDaemon = new EnvironmentDaemon()
val pingResponseF = (envDaemon.daemonActor ? unit)
val pingResponse = Await.result(pingResponseF, 5.seconds)
println(pingResponse)

var continue = true
while(continue) {
val code = lineReader.readLine("> ")

val responseF = EnvironmentDaemon.daemonActor ? code
val responseF = envDaemon.daemonActor ? code

val response = Await.result(responseF, 5.seconds)

response match {
case (s: EnvironmentDaemon.CodeResponse) =>
case (s: DaemonActor.CodeResponse) =>
if (s.timeToQuit) {
history.save()
continue = false
Expand All @@ -54,5 +55,5 @@ object repl extends App {
}
}

EnvironmentDaemon.system.terminate()
envDaemon.system.terminate()
}

0 comments on commit efd3c79

Please sign in to comment.