pekko/akka-core/src/main/scala/routing/Patterns.scala

50 lines
1.6 KiB
Scala
Raw Normal View History

/**
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package se.scalablesolutions.akka.patterns
2010-02-13 21:45:35 +01:00
import se.scalablesolutions.akka.actor.{Actor, ActorID}
import se.scalablesolutions.akka.actor.Actor._
2010-02-13 21:45:35 +01:00
object Patterns {
type PF[A, B] = PartialFunction[A, B]
2010-02-13 21:45:35 +01:00
/**
* Creates a new PartialFunction whose isDefinedAt is a combination
* of the two parameters, and whose apply is first to call filter.apply and then filtered.apply
*/
def filter[A, B](filter: PF[A, Unit], filtered: PF[A, B]): PF[A, B] = {
case a: A if filtered.isDefinedAt(a) && filter.isDefinedAt(a) =>
2010-02-13 21:45:35 +01:00
filter(a)
filtered(a)
}
/**
* Interceptor is a filter(x,y) where x.isDefinedAt is considered to be always true
*/
2010-03-17 17:48:46 +01:00
def intercept[A, B](interceptor: (A) => Unit, interceptee: PF[A, B]): PF[A, B] =
filter({case a if a.isInstanceOf[A] => interceptor(a)}, interceptee)
2010-02-13 21:45:35 +01:00
//FIXME 2.8, use default params with CyclicIterator
2010-05-05 13:26:31 +02:00
def loadBalancerActor(actors: => InfiniteIterator[ActorID]): ActorID =
newActor(() => new Actor with LoadBalancer {
start
val seq = actors
})
def dispatcherActor(routing: PF[Any, ActorID], msgTransformer: (Any) => Any): ActorID =
newActor(() => new Actor with Dispatcher {
start
override def transform(msg: Any) = msgTransformer(msg)
def routes = routing
})
def dispatcherActor(routing: PF[Any, ActorID]): ActorID = newActor(() => new Actor with Dispatcher {
start
def routes = routing
})
2010-02-13 21:45:35 +01:00
def loggerActor(actorToLog: ActorID, logger: (Any) => Unit): ActorID =
2010-03-17 17:48:46 +01:00
dispatcherActor({case _ => actorToLog}, logger)
2010-04-24 14:56:50 +02:00
}