2011-09-09 14:33:35 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2011-09-20 21:44:50 +02:00
|
|
|
package akka.remote
|
2011-09-09 14:33:35 +02:00
|
|
|
|
|
|
|
|
import akka.dispatch.PinnedDispatcher
|
|
|
|
|
import scala.collection.mutable
|
|
|
|
|
import java.net.InetSocketAddress
|
2011-09-09 17:22:40 +02:00
|
|
|
import akka.actor.{ LocalActorRef, Actor, ActorRef, Props, newUuid }
|
|
|
|
|
import akka.actor.Actor._
|
2011-10-12 09:10:05 +02:00
|
|
|
import akka.AkkaApplication
|
2011-09-09 14:33:35 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stream of all kinds of network events, remote failure and connection events, cluster failure and connection events etc.
|
|
|
|
|
* Also provides API for channel listener management.
|
2011-09-19 14:43:28 +02:00
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2011-09-09 14:33:35 +02:00
|
|
|
*/
|
|
|
|
|
object NetworkEventStream {
|
|
|
|
|
|
|
|
|
|
private sealed trait NetworkEventStreamEvent
|
|
|
|
|
|
|
|
|
|
private case class Register(listener: Listener, connectionAddress: InetSocketAddress)
|
|
|
|
|
extends NetworkEventStreamEvent
|
|
|
|
|
|
|
|
|
|
private case class Unregister(listener: Listener, connectionAddress: InetSocketAddress)
|
|
|
|
|
extends NetworkEventStreamEvent
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base trait for network event listener.
|
|
|
|
|
*/
|
|
|
|
|
trait Listener {
|
|
|
|
|
def notify(event: RemoteLifeCycleEvent)
|
|
|
|
|
}
|
2011-10-12 11:34:35 +02:00
|
|
|
|
2011-09-09 14:33:35 +02:00
|
|
|
/**
|
|
|
|
|
* Channel actor with a registry of listeners.
|
|
|
|
|
*/
|
|
|
|
|
private class Channel extends Actor {
|
|
|
|
|
|
|
|
|
|
val listeners = new mutable.HashMap[InetSocketAddress, mutable.Set[Listener]]() {
|
|
|
|
|
override def default(k: InetSocketAddress) = mutable.Set.empty[Listener]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def receive = {
|
|
|
|
|
case event: RemoteClientLifeCycleEvent ⇒
|
|
|
|
|
listeners(event.remoteAddress) foreach (_ notify event)
|
|
|
|
|
|
|
|
|
|
case event: RemoteServerLifeCycleEvent ⇒ // FIXME handle RemoteServerLifeCycleEvent
|
|
|
|
|
|
|
|
|
|
case Register(listener, connectionAddress) ⇒
|
|
|
|
|
listeners(connectionAddress) += listener
|
|
|
|
|
|
|
|
|
|
case Unregister(listener, connectionAddress) ⇒
|
|
|
|
|
listeners(connectionAddress) -= listener
|
|
|
|
|
|
|
|
|
|
case _ ⇒ //ignore other
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-13 17:42:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NetworkEventStream(val app: AkkaApplication) {
|
|
|
|
|
|
|
|
|
|
import NetworkEventStream._
|
2011-09-09 14:33:35 +02:00
|
|
|
|
2011-10-18 15:39:26 +02:00
|
|
|
// FIXME: check that this supervision is correct
|
2011-10-13 17:42:26 +02:00
|
|
|
private[akka] val channel = app.provider.actorOf(
|
2011-10-18 15:39:26 +02:00
|
|
|
Props[Channel].copy(dispatcher = app.dispatcherFactory.newPinnedDispatcher("NetworkEventStream")),
|
|
|
|
|
app.guardian, Props.randomAddress, systemService = true)
|
2011-09-09 14:33:35 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registers a network event stream listener (asyncronously).
|
|
|
|
|
*/
|
|
|
|
|
def register(listener: Listener, connectionAddress: InetSocketAddress) =
|
|
|
|
|
channel ! Register(listener, connectionAddress)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unregisters a network event stream listener (asyncronously) .
|
|
|
|
|
*/
|
|
|
|
|
def unregister(listener: Listener, connectionAddress: InetSocketAddress) =
|
|
|
|
|
channel ! Unregister(listener, connectionAddress)
|
|
|
|
|
}
|