diff options
Diffstat (limited to 'vfd-uav/src/main/scala/vfd/uav/Connection.scala')
-rw-r--r-- | vfd-uav/src/main/scala/vfd/uav/Connection.scala | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/vfd-uav/src/main/scala/vfd/uav/Connection.scala b/vfd-uav/src/main/scala/vfd/uav/Connection.scala index afc1c1a..38e1836 100644 --- a/vfd-uav/src/main/scala/vfd/uav/Connection.scala +++ b/vfd-uav/src/main/scala/vfd/uav/Connection.scala @@ -1,45 +1,62 @@ package vfd.uav import scala.collection.mutable.ArrayBuffer - import akka.actor.Actor import akka.actor.ActorRef import akka.actor.Terminated import akka.actor.actorRef2Scala import akka.util.ByteString +import org.mavlink.Assembler +import org.mavlink.messages.Message +import org.mavlink.Parser +import org.mavlink.Packet +import akka.actor.ActorLogging +/** Protocol definition. */ object Connection { trait Event trait Command - //received data from the uav (or any other systems on the link) + /** Received data from the uav (or any other systems on the link) */ case class Received(bstr: ByteString) extends Event - //the connection closed or could be opened + /** The connection closed or could not be opened */ case class Closed(message: String) extends Event - //register the sender to be notified on events + /** Register the sender to be notified on events */ case object Register extends Command - //send given bytes out to the uav (or any other systems on the link) + /** Send given bytes out to the uav (or any other systems on the link) */ case class Send(bstr: ByteString) extends Command } -trait Connection { that: Actor => - private val _clients = new ArrayBuffer[ActorRef] +/** Common behavior of connection actors. */ +trait Connection { myself: Actor => + /** Current clients that should be notified on incoming messages. */ + private val _clients = new ArrayBuffer[ActorRef] def clients = _clients.toSeq - def register(client: ActorRef) = { + /** Adds a client to the client list and acquires a deathwatch. */ + protected def register(client: ActorRef) = { _clients += client; - that.context.watch(client) + myself.context.watch(client) } - def unregister(client: ActorRef) = _clients -= client + /** Remove client and release deathwatch. */ + protected def unregister(client: ActorRef) = { + _clients -= client + myself.context.unwatch(client) + } - def sendAll(msg: Any) = clients foreach (_ ! msg) + /** Sends a message to all registered clients. */ + protected def sendAll(msg: Any) = clients foreach (_ ! msg) - def registration: Receive = { + /** + * Common registration behavior. Manages the events `Register` and `Terminated` by + * registering and unregistering clients. + */ + protected def registration: Receive = { case Connection.Register => register(sender) case Terminated(client) if clients contains client => unregister(client) } |