Derek Wyatt
Posts: 69
Nickname: dwyatt
Registered: Oct, 2012
|
|
Re: Minor inconsistency in Section 7.4
|
Posted: Feb 19, 2013 6:09 PM
|
|
> I have noticed the following minor inconsistencies in > Section 7.4. > > 1) In the receive method of the class Pilot, the case > class > Controls(controlSurfaces) is not defined. > > I think object Pilot should include > case class Controls(controlSurface:ActorRef) > > Also, in object Pilot, > case object RelinquishControl > is not used in Chapter 7.
Damn. This is defined on the Plane companion object. At this point in the book it should look like this:
object Plane { // Returns the control surface to the Actor that // asks for them case object GiveMeControl case class Controls(controls: ActorRef)
// Eases creation of the Plane def apply() = new Plane with AltimeterProvider with PilotProvider with LeadFlightAttendantProvider }
I'll have to fix that. Thanks.
> > 2) In trait PilotProvider and class Plane, there are > references to class AutoPilot. But class AutoPilot is not > defined.
Yeah that one is actually meant to be done by you... it's a simple hiccup. I'm getting the impression from people, though, that it's not cool.
> > 3) The trait PilotProvider is not used in Chapter 7.
As you can see above, it should be, but I've messed something up in here somewhere.
The whole Plane.scala file should be, at this point, the following:
package zzz.akka.avionics
import akka.actor.{Actor, ActorRef, Props, ActorLogging} import akka.actor.SupervisorStrategy._ import akka.pattern.ask import akka.util.Timeout import scala.concurrent.Await import scala.concurrent.duration._
object Plane { // Returns the control surface to the Actor that // asks for them case object GiveMeControl case class Controls(controls: ActorRef)
// Eases creation of the Plane def apply() = new Plane with AltimeterProvider with PilotProvider with LeadFlightAttendantProvider }
// We want the Plane to own the Altimeter and we're going to // do that by passing in a specific factory we can use to // build the Altimeter class Plane extends Actor with ActorLogging { this: AltimeterProvider with PilotProvider with LeadFlightAttendantProvider => import IsolatedLifeCycleSupervisor._ import Altimeter._ import Plane._
// There's going to be a couple of asks below and // a timeout is necessary for that. implicit val askTimeout = Timeout(1.second) val config = context.system.settings.config
// We're going to want to remember the names of our crew val cfgstr = "zzz.akka.avionics.flightcrew" val pilotName = config.getString(s"$cfgstr.pilotName") val copilotName = config.getString(s"$cfgstr.copilotName") val attendantName = config.getString( s"$cfgstr.leadAttendantName")
// Helps us look up Actors within the "Pilots" Supervisor def actorForPilots(name: String) = context.actorFor("Pilots/" + name) // Helps us look up Actors within the "Equipment" Supervisor def actorForControls(name: String) = context.actorFor("Equipment/" + name)
def startPeople() { val plane = self // Note how we depend on the Actor structure beneath // us here by using actorFor(). This should be // resilient to change, since we'll probably be the // ones making the changes val controls = actorForControls("ControlSurfaces") val autopilot = actorForControls("AutoPilot") val altimeter = actorForControls("Altimeter") val people = context.actorOf( Props(new IsolatedStopSupervisor with OneForOneStrategyFactory { def childStarter() { // These children get implicitly added // to the hierarchy context.actorOf( Props(newCoPilot(plane, autopilot, altimeter)), copilotName) context.actorOf( Props(newPilot(plane, autopilot, controls, altimeter)), pilotName) } }), "Pilots") // Use the default strategy here, which // restarts indefinitely context.actorOf(Props(newFlightAttendant), attendantName) Await.result(people ? WaitForStart, 1.second) }
// Equipment started here using a simple strategy that // resumes children def startEquipment() { val controls = context.actorOf( Props(new IsolatedResumeSupervisor with OneForOneStrategyFactory { def childStarter() { val alt = context.actorOf( Props(newAltimeter), "Altimeter") // These children get implicitly added to the // hierarchy context.actorOf(Props(newAutopilot), "AutoPilot") context.actorOf(Props(new ControlSurfaces(alt)), "ControlSurfaces") } }), "Equipment") Await.result(controls ? WaitForStart, 1.second) }
override def preStart() { import EventSource.RegisterListener import Pilots.ReadyToGo // Get our children going. Order is important here. startEquipment() startPeople() // Bootstrap the system actorForControls("Altimeter") ! RegisterListener(self) actorForPilots(pilotName) ! ReadyToGo actorForPilots(copilotName) ! ReadyToGo }
def receive = { case AltitudeUpdate(altitude) => log info(s"Altitude is now: $altitude") case GiveMeControl => sender ! Controls(actorForControls("ControlSurfaces")) } }
|
|