diff options
author | Jakob Odersky <jodersky@gmail.com> | 2013-08-04 22:01:24 +0200 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2013-08-04 22:01:24 +0200 |
commit | 10c5c4e68d027e6bbabaad17381c79df24401027 (patch) | |
tree | 00b980f9cbe4e66eee265de36d8abb57614af514 /flow-samples/terminal | |
parent | 9efeb44063001d4df0fb7a665a52bfcc52d5a290 (diff) | |
download | akka-serial-10c5c4e68d027e6bbabaad17381c79df24401027.tar.gz akka-serial-10c5c4e68d027e6bbabaad17381c79df24401027.tar.bz2 akka-serial-10c5c4e68d027e6bbabaad17381c79df24401027.zip |
group serial settings in class, cosmetic changes & documentation
Diffstat (limited to 'flow-samples/terminal')
3 files changed, 47 insertions, 27 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 index 6b06206..c4401e7 100644 --- 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 @@ -1,19 +1,33 @@ package com.github.jodersky.flow.samples.terminal import akka.actor._ +import java.io.BufferedReader +import java.io.InputStreamReader -case object Read -case class ConsoleInput(in: String) class ConsoleReader extends Actor { import context._ - + import ConsoleReader._ + def receive = { - case Read => read() + case Read => read() match { + case Some(input) => parent ! ConsoleInput(input) + case None => parent ! EOT + } } - - def read() = { - val in = Console.readLine() - parent ! ConsoleInput(in) + + 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 index b6f45f3..27686ce 100644 --- 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 @@ -8,14 +8,12 @@ import akka.actor.actorRef2Scala import akka.util.ByteString object Main { - - def defaultIfEmpty(in: String, default: String): String = if (in.isEmpty) default else in - + def ask(label: String, default: String) = { print(s"${label} [${default}]: ") val in = Console.readLine() println("") - defaultIfEmpty(in, default) + if (in.isEmpty) default else in } def main(args: Array[String]): Unit = { @@ -23,12 +21,14 @@ object Main { 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 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.")) - val terminal = system.actorOf(Props(classOf[Terminal], port, baud, cs, tsb, parity), name = "terminal") } }
\ 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 index 2997e77..3c1eee6 100644 --- 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 @@ -10,16 +10,17 @@ 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(port: String, baud: Int, cs: Int, tsb: Boolean, parity: Parity.Parity) extends Actor with ActorLogging { +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: ${port}, baud: ${baud}") - IO(Serial) ! Serial.Open(port, baud) + log.info(s"Requesting manager to open port: ${settings.port}, baud: ${settings.baud}") + IO(Serial) ! Serial.Open(settings) } override def postStop() = { @@ -31,23 +32,23 @@ class Terminal(port: String, baud: Int, cs: Int, tsb: Boolean, parity: Parity.Pa log.error(s"Connection failed, stopping terminal. Reason: ${reason}") context stop self } - case Opened(port, _, _, _, _) => { - log.info(s"Port ${port} is now open.") + 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 ! Read + reader ! ConsoleReader.Read } } def opened(operator: ActorRef): Receive = { case Received(data) => { - log.info(s"Received data: ${formatData(data)} (${new String(data.toArray, "UTF-8")})") + log.info(s"Received data: ${formatData(data)}") } - case Wrote(data) => log.info(s"Wrote data: ${formatData(data)} (${new String(data.toArray, "UTF-8")})") + case Wrote(data) => log.info(s"Wrote data: ${formatData(data)}") case Closed => { log.info("Operator closed normally, exiting terminal.") @@ -60,22 +61,27 @@ class Terminal(port: String, baud: Int, cs: Int, tsb: Boolean, parity: Parity.Pa context stop self } - case ConsoleInput(":q") => { + case ConsoleReader.EOT => { log.info("Initiating close.") operator ! Close } - case ConsoleInput(input) => { + case ConsoleReader.ConsoleInput(input) => { val data = ByteString(input.getBytes) operator ! Write(data, Wrote(data)) - reader ! Read + reader ! ConsoleReader.Read } } - private def formatData(data: ByteString) = data.mkString("[", ",", "]") + } 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 |