diff options
Diffstat (limited to 'flow-samples/terminal')
3 files changed, 0 insertions, 154 deletions
diff --git a/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/ConsoleReader.scala b/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/ConsoleReader.scala deleted file mode 100644 index c4401e7..0000000 --- a/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/ConsoleReader.scala +++ /dev/null @@ -1,33 +0,0 @@ -package com.github.jodersky.flow.samples.terminal - -import akka.actor._ -import java.io.BufferedReader -import java.io.InputStreamReader - -class ConsoleReader extends Actor { - import context._ - import ConsoleReader._ - - def receive = { - case Read => read() match { - case Some(input) => parent ! ConsoleInput(input) - case None => parent ! EOT - } - } - - def read(): Option[String] = { - val eot = 4 - val line = Console.readLine - if (line == ":q") None else Some(line) - } - -} - -object ConsoleReader { - - case object Read - - case object EOT - case class ConsoleInput(in: String) - -}
\ No newline at end of file diff --git a/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/Main.scala b/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/Main.scala deleted file mode 100644 index 27686ce..0000000 --- a/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/Main.scala +++ /dev/null @@ -1,34 +0,0 @@ -package com.github.jodersky.flow -package samples.terminal - -import com.github.jodersky.flow._ -import akka.actor.ActorSystem -import akka.actor.Props -import akka.actor.actorRef2Scala -import akka.util.ByteString - -object Main { - - def ask(label: String, default: String) = { - print(s"${label} [${default}]: ") - val in = Console.readLine() - println("") - if (in.isEmpty) default else in - } - - def main(args: Array[String]): Unit = { - val port = ask("Device", "/dev/ttyACM0") - val baud = ask("Baud rate", "115200").toInt - val cs = ask("Char size", "8").toInt - val tsb = ask("Use two stop bits", "false").toBoolean - val parity = Parity(ask("Parity (0=None, 1=Odd, 2=Even)", "0").toInt) - - val settings = SerialSettings(port, baud, cs, tsb, parity) - - println("Starting terminal system, enter :q to exit.") - internal.InternalSerial.debug(true) - val system = ActorSystem("flow") - val terminal = system.actorOf(Terminal(settings), name = "terminal") - system.registerOnTermination(println("Stopped terminal system.")) - } -}
\ No newline at end of file diff --git a/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/Terminal.scala b/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/Terminal.scala deleted file mode 100644 index 3c1eee6..0000000 --- a/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/Terminal.scala +++ /dev/null @@ -1,87 +0,0 @@ -package com.github.jodersky.flow.samples.terminal - -import com.github.jodersky.flow.Serial._ -import akka.actor.Actor -import akka.actor.ActorLogging -import akka.actor.ActorRef -import akka.util.ByteString -import akka.io.IO -import com.github.jodersky.flow.Serial -import akka.actor.Terminated -import com.github.jodersky.flow.Parity -import akka.actor.Props -import com.github.jodersky.flow.SerialSettings - -class Terminal(settings: SerialSettings) extends Actor with ActorLogging { - import Terminal._ - import context._ - - val reader = actorOf(Props[ConsoleReader]) - - override def preStart() = { - log.info(s"Requesting manager to open port: ${settings.port}, baud: ${settings.baud}") - IO(Serial) ! Serial.Open(settings) - } - - override def postStop() = { - system.shutdown() - } - - def receive = { - case CommandFailed(cmd, reason) => { - log.error(s"Connection failed, stopping terminal. Reason: ${reason}") - context stop self - } - case Opened(s, _) => { - log.info(s"Port ${s.port} is now open.") - val operator = sender - context become opened(operator) - context watch operator - operator ! Register(self) - reader ! ConsoleReader.Read - } - } - - def opened(operator: ActorRef): Receive = { - - case Received(data) => { - log.info(s"Received data: ${formatData(data)}") - } - - case Wrote(data) => log.info(s"Wrote data: ${formatData(data)}") - - case Closed => { - log.info("Operator closed normally, exiting terminal.") - context unwatch operator - context stop self - } - - case Terminated(`operator`) => { - log.error("Operator crashed, exiting terminal.") - context stop self - } - - case ConsoleReader.EOT => { - log.info("Initiating close.") - operator ! Close - } - - case ConsoleReader.ConsoleInput(input) => { - val data = ByteString(input.getBytes) - operator ! Write(data, Wrote(data)) - reader ! ConsoleReader.Read - } - } - - - -} - -object Terminal { - case class Wrote(data: ByteString) extends Event - - def apply(settings: SerialSettings) = Props(classOf[Terminal], settings) - - private def formatData(data: ByteString) = data.mkString("[", ",", "]") + " " + (new String(data.toArray, "UTF-8")) - -}
\ No newline at end of file |