first of all, i’m learning scala and new to the java world.
I want to create a console and run this console as a service that you could start and stop.
I was able to run a ConsoleReader into an Actor but i don’t know how to stop properly the ConsoleReader.
Here is the code :
import eu.badmood.util.trace
import scala.actors.Actor._
import tools.jline.console.ConsoleReader
object Main {
def main(args:Array[String]){
//start the console
Console.start(message => {
//handle console inputs
message match {
case "exit" => Console.stop()
case _ => trace(message)
}
})
//try to stop the console after a time delay
Thread.sleep(2000)
Console.stop()
}
}
object Console {
private val consoleReader = new ConsoleReader()
private var running = false
def start(handler:(String)=>Unit){
running = true
actor{
while (running){
handler(consoleReader.readLine("\33[32m> \33[0m"))
}
}
}
def stop(){
//how to cancel an active call to ConsoleReader.readLine ?
running = false
}
}
I’m also looking for any advice concerning this code !
The underlying call to read a characters from the input is blocking. On non-Windows platform, it will use
System.in.read()and on Windows it will useorg.fusesource.jansi.internal.WindowsSupport.readByte.So your challenge is to cause that blocking call to return when you want to stop your console service. See http://www.javaspecialists.eu/archive/Issue153.html and Is it possible to read from a InputStream with a timeout? for some ideas… Once you figure that out, have
readreturn-1when your console service stops, so thatConsoleReaderthinks it’s done. You’ll needConsoleReaderto use your version of that call:tools.jline.AnsiWindowsTerminaland use theConsoleReaderconstructor that takes aTerminal(otherwiseAnsiWindowsTerminalwill just use WindowsSupport.readByte` directly)ConsoleReaderconstructor that takes anInputStream, you could provide your own wrapper aroundSystem.inA few more thoughts:
scala.Consoleobject already, so for less confusion name yours differently.System.inis a unique resource, so you probably need to ensure that only one caller usesConsole.readLineat a time. Right nowstartwill directly callreadLineand multiple callers can callstart. Probably the console service canreadLineand maintain a list of handlers.