diff options
author | Jakob Odersky <jodersky@gmail.com> | 2015-12-12 11:44:39 -0800 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2015-12-16 21:55:45 -0800 |
commit | 7453888b0188a2aeff06b8e9893118b889f8d828 (patch) | |
tree | 8e6ef2498ff2aaeac7241f3df71200c422f0f1cb /site/jekyll/_snippet.md | |
parent | eab2e148c7605ca0fd836bbe9230734895cce7d0 (diff) | |
download | akka-serial-7453888b0188a2aeff06b8e9893118b889f8d828.tar.gz akka-serial-7453888b0188a2aeff06b8e9893118b889f8d828.tar.bz2 akka-serial-7453888b0188a2aeff06b8e9893118b889f8d828.zip |
create website
Diffstat (limited to 'site/jekyll/_snippet.md')
-rw-r--r-- | site/jekyll/_snippet.md | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/site/jekyll/_snippet.md b/site/jekyll/_snippet.md new file mode 100644 index 0000000..65598ec --- /dev/null +++ b/site/jekyll/_snippet.md @@ -0,0 +1,57 @@ +{% highlight scala %} +import akka.actor.{Actor, ActorLogging, ActorRef, Props, Terminated} +import akka.io.IO +import akka.util.ByteString +import com.github.jodersky.flow.{Serial, SerialSettings} + +/** + * Sample actor representing a simple terminal. + */ +class Terminal(port: String, settings: SerialSettings) extends Actor with ActorLogging { + import context._ + + override def preStart() = { + log.info(s"Requesting manager to open port: ${port}, baud: ${settings.baud}") + IO(Serial) ! Serial.Open(port, settings) + } + + def receive: Receive = { + + case Serial.CommandFailed(cmd, reason) => + log.error(s"Connection failed, stopping terminal. Reason: ${reason}") + context stop self + + case Serial.Opened(port) => + log.info(s"Port ${port} is now open.") + context become opened(sender) + context watch sender // get notified in the event the operator crashes + + } + + def opened(operator: ActorRef): Receive = { + + case Serial.Received(data) => + log.info(s"Received data: " + data) + + case Serial.Closed => + log.info("Operator closed normally, exiting terminal.") + context stop self + + case Terminated(`operator`) => + log.error("Operator crashed unexpectedly, exiting terminal.") + context stop self + + case ":q" => + operator ! Serial.Close + + case str: String => + operator ! Serial.Write(ByteString(str)) + + } + +} + +object Terminal { + def apply(port: String, settings: SerialSettings) = Props(classOf[Terminal], port, settings) +} +{% endhighlight %} |