Added Receive type
This commit is contained in:
parent
f6ef127391
commit
ffedcf427b
8 changed files with 31 additions and 24 deletions
|
|
@ -124,7 +124,7 @@ trait Producer { this: Actor =>
|
|||
* the protected produce methods depending on the return values of
|
||||
* <code>oneway</code> and <code>async</code>.
|
||||
*/
|
||||
protected def produce: PartialFunction[Any, Unit] = {
|
||||
protected def produce: Receive = {
|
||||
case msg => {
|
||||
if ( oneway && !async) produceOnewaySync(msg)
|
||||
else if ( oneway && async) produceOnewayAsync(msg)
|
||||
|
|
|
|||
|
|
@ -30,15 +30,15 @@ import java.util.{HashSet => JHashSet}
|
|||
trait ActorWithNestedReceive extends Actor {
|
||||
import Actor.actor
|
||||
private var nestedReactsProcessors: List[ActorRef] = Nil
|
||||
private val processNestedReacts: PartialFunction[Any, Unit] = {
|
||||
private val processNestedReacts: Receive = {
|
||||
case message if !nestedReactsProcessors.isEmpty =>
|
||||
val processors = nestedReactsProcessors.reverse
|
||||
processors.head forward message
|
||||
nestedReactsProcessors = processors.tail.reverse
|
||||
}
|
||||
|
||||
protected def react: PartialFunction[Any, Unit]
|
||||
protected def reactAgain(pf: PartialFunction[Any, Unit]) = nestedReactsProcessors ::= actor(pf)
|
||||
protected def react: Receive
|
||||
protected def reactAgain(pf: Receive) = nestedReactsProcessors ::= actor(pf)
|
||||
protected def receive = processNestedReacts orElse react
|
||||
}
|
||||
*/
|
||||
|
|
@ -67,7 +67,7 @@ abstract class RemoteActor(hostname: String, port: Int) extends Actor {
|
|||
|
||||
// Life-cycle messages for the Actors
|
||||
@serializable sealed trait LifeCycleMessage
|
||||
case class HotSwap(code: Option[PartialFunction[Any, Unit]]) extends LifeCycleMessage
|
||||
case class HotSwap(code: Option[Actor.Receive]) extends LifeCycleMessage
|
||||
case class Restart(reason: Throwable) extends LifeCycleMessage
|
||||
case class Exit(dead: ActorRef, killer: Throwable) extends LifeCycleMessage
|
||||
case class Link(child: ActorRef) extends LifeCycleMessage
|
||||
|
|
@ -88,6 +88,11 @@ object Actor extends Logging {
|
|||
val TIMEOUT = config.getInt("akka.actor.timeout", 5000)
|
||||
val SERIALIZE_MESSAGES = config.getBool("akka.actor.serialize-messages", false)
|
||||
|
||||
/** A Receive is the type that defines actor message behavior
|
||||
* currently modeled as a PartialFunction[Any,Unit]
|
||||
*/
|
||||
type Receive = PartialFunction[Any,Unit]
|
||||
|
||||
private[actor] val actorRefInCreation = new scala.util.DynamicVariable[Option[ActorRef]](None)
|
||||
|
||||
/**
|
||||
|
|
@ -134,10 +139,10 @@ object Actor extends Logging {
|
|||
* }
|
||||
* </pre>
|
||||
*/
|
||||
def actor(body: PartialFunction[Any, Unit]): ActorRef =
|
||||
def actor(body: Receive): ActorRef =
|
||||
actorOf(new Actor() {
|
||||
self.lifeCycle = Some(LifeCycle(Permanent))
|
||||
def receive: PartialFunction[Any, Unit] = body
|
||||
def receive: Receive = body
|
||||
}).start
|
||||
|
||||
/**
|
||||
|
|
@ -156,10 +161,10 @@ object Actor extends Logging {
|
|||
* }
|
||||
* </pre>
|
||||
*/
|
||||
def transactor(body: PartialFunction[Any, Unit]): ActorRef =
|
||||
def transactor(body: Receive): ActorRef =
|
||||
actorOf(new Transactor() {
|
||||
self.lifeCycle = Some(LifeCycle(Permanent))
|
||||
def receive: PartialFunction[Any, Unit] = body
|
||||
def receive: Receive = body
|
||||
}).start
|
||||
|
||||
/**
|
||||
|
|
@ -176,7 +181,7 @@ object Actor extends Logging {
|
|||
* }
|
||||
* </pre>
|
||||
*/
|
||||
def temporaryActor(body: PartialFunction[Any, Unit]): ActorRef =
|
||||
def temporaryActor(body: Receive): ActorRef =
|
||||
actorOf(new Actor() {
|
||||
self.lifeCycle = Some(LifeCycle(Temporary))
|
||||
def receive = body
|
||||
|
|
@ -201,7 +206,7 @@ object Actor extends Logging {
|
|||
*/
|
||||
def init[A](body: => Unit) = {
|
||||
def handler[A](body: => Unit) = new {
|
||||
def receive(handler: PartialFunction[Any, Unit]) =
|
||||
def receive(handler: Receive) =
|
||||
actorOf(new Actor() {
|
||||
self.lifeCycle = Some(LifeCycle(Permanent))
|
||||
body
|
||||
|
|
@ -252,6 +257,8 @@ object Actor extends Logging {
|
|||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
trait Actor extends Logging {
|
||||
//Type alias because traits cannot have companion objects...
|
||||
type Receive = Actor.Receive
|
||||
|
||||
/**
|
||||
* For internal use only, functions as the implicit sender references when invoking
|
||||
|
|
@ -305,7 +312,7 @@ trait Actor extends Logging {
|
|||
* }
|
||||
* </pre>
|
||||
*/
|
||||
protected def receive: PartialFunction[Any, Unit]
|
||||
protected def receive: Receive
|
||||
|
||||
/**
|
||||
* User overridable callback/setting.
|
||||
|
|
@ -351,9 +358,9 @@ trait Actor extends Logging {
|
|||
// ==== INTERNAL IMPLEMENTATION DETAILS ====
|
||||
// =========================================
|
||||
|
||||
private[akka] def base: PartialFunction[Any, Unit] = lifeCycles orElse (self.hotswap getOrElse receive)
|
||||
private[akka] def base: Receive = lifeCycles orElse (self.hotswap getOrElse receive)
|
||||
|
||||
private val lifeCycles: PartialFunction[Any, Unit] = {
|
||||
private val lifeCycles: Receive = {
|
||||
case HotSwap(code) => self.hotswap = code
|
||||
case Restart(reason) => self.restart(reason)
|
||||
case Exit(dead, reason) => self.handleTrapExit(dead, reason)
|
||||
|
|
|
|||
|
|
@ -186,7 +186,7 @@ trait ActorRef extends TransactionManagement {
|
|||
/**
|
||||
* Holds the hot swapped partial function.
|
||||
*/
|
||||
protected[akka] var hotswap: Option[PartialFunction[Any, Unit]] = None // FIXME: _hotswap should be a stack
|
||||
protected[akka] var hotswap: Option[Actor.Receive] = None // FIXME: _hotswap should be a stack
|
||||
|
||||
/**
|
||||
* User overridable callback/setting.
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ trait Listeners { self : Actor =>
|
|||
import se.scalablesolutions.akka.actor.Agent
|
||||
private lazy val listeners = Agent(Set[ActorRef]())
|
||||
|
||||
protected def listenerManagement : PartialFunction[Any,Unit] = {
|
||||
protected def listenerManagement : Receive = {
|
||||
case Listen(l) => listeners( _ + l)
|
||||
case Deafen(l) => listeners( _ - l )
|
||||
case WithListeners(f) => listeners foreach f
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ trait Dispatcher { this: Actor =>
|
|||
|
||||
protected def routes: PartialFunction[Any, ActorRef]
|
||||
|
||||
protected def dispatch: PartialFunction[Any, Unit] = {
|
||||
protected def dispatch: Receive = {
|
||||
case a if routes.isDefinedAt(a) =>
|
||||
if (self.replyTo.isDefined) routes(a).forward(transform(a))(Some(self))
|
||||
else routes(a).!(transform(a))(None)
|
||||
|
|
|
|||
|
|
@ -187,7 +187,7 @@ trait AuthenticationActor[C <: Credentials] extends Actor {
|
|||
* and a se3curity context is created for the ContainerRequest
|
||||
* this should ensure good integration with current Jersey security
|
||||
*/
|
||||
protected val authenticate: PartialFunction[Any, Unit] = {
|
||||
protected val authenticate: Receive = {
|
||||
case Authenticate(req, roles) => {
|
||||
verify(extractCredentials(req)) match {
|
||||
case Some(u: UserInfo) => {
|
||||
|
|
@ -254,7 +254,7 @@ trait DigestAuthenticationActor extends AuthenticationActor[DigestCredentials] w
|
|||
val nonceMap = mkNonceMap
|
||||
|
||||
//Discards old nonces
|
||||
protected val invalidateNonces: PartialFunction[Any, Unit] = {
|
||||
protected val invalidateNonces: Receive = {
|
||||
case InvalidateNonces =>
|
||||
val ts = System.currentTimeMillis
|
||||
nonceMap.filter(tuple => (ts - tuple._2) < nonceValidityPeriod)
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ class BankAccountActor extends Transactor {
|
|||
private lazy val accountState = MongoStorage.newMap
|
||||
private lazy val txnLog = MongoStorage.newVector
|
||||
|
||||
def receive: PartialFunction[Any, Unit] = {
|
||||
def receive: Receive = {
|
||||
// check balance
|
||||
case Balance(accountNo) =>
|
||||
txnLog.add("Balance:" + accountNo)
|
||||
|
|
|
|||
|
|
@ -127,7 +127,7 @@ trait SessionManagement { this: Actor =>
|
|||
val storage: ActorRef // needs someone to provide the ChatStorage
|
||||
val sessions = new HashMap[String, ActorRef]
|
||||
|
||||
protected def sessionManagement: PartialFunction[Any, Unit] = {
|
||||
protected def sessionManagement: Receive = {
|
||||
case Login(username) =>
|
||||
log.info("User [%s] has logged in", username)
|
||||
val session = actorOf(new Session(username, storage))
|
||||
|
|
@ -153,7 +153,7 @@ trait SessionManagement { this: Actor =>
|
|||
trait ChatManagement { this: Actor =>
|
||||
val sessions: HashMap[String, ActorRef] // needs someone to provide the Session map
|
||||
|
||||
protected def chatManagement: PartialFunction[Any, Unit] = {
|
||||
protected def chatManagement: Receive = {
|
||||
case msg @ ChatMessage(from, _) => sessions(from) ! msg
|
||||
case msg @ GetChatLog(from) => sessions(from) forward msg
|
||||
}
|
||||
|
|
@ -181,8 +181,8 @@ trait ChatServer extends Actor {
|
|||
def receive = sessionManagement orElse chatManagement
|
||||
|
||||
// abstract methods to be defined somewhere else
|
||||
protected def chatManagement: PartialFunction[Any, Unit]
|
||||
protected def sessionManagement: PartialFunction[Any, Unit]
|
||||
protected def chatManagement: Receive
|
||||
protected def sessionManagement: Receive
|
||||
protected def shutdownSessions: Unit
|
||||
|
||||
override def shutdown = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue