2010-12-14 18:22:46 +01:00
|
|
|
/**
|
2011-07-14 16:03:08 +02:00
|
|
|
* Copyright (C) 2009-2010 Typesafe Inc. <http://www.typesafe.com>
|
2010-12-14 18:22:46 +01:00
|
|
|
*/
|
|
|
|
|
|
2011-09-20 21:44:50 +02:00
|
|
|
package akka.remote
|
2010-12-14 18:22:46 +01:00
|
|
|
|
2010-12-15 17:52:31 +01:00
|
|
|
import akka.actor._
|
2011-10-06 21:19:46 +02:00
|
|
|
import akka.{ AkkaException, AkkaApplication }
|
2011-03-24 12:48:40 +01:00
|
|
|
|
|
|
|
|
import scala.reflect.BeanProperty
|
2011-11-10 17:39:04 +01:00
|
|
|
import java.io.{ PrintWriter, PrintStream }
|
2011-03-24 12:48:40 +01:00
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress
|
2011-11-10 17:39:04 +01:00
|
|
|
|
|
|
|
|
object RemoteAddress {
|
|
|
|
|
def apply(host: String, port: Int): RemoteAddress = apply(new InetSocketAddress(host, port))
|
|
|
|
|
def apply(inetAddress: InetSocketAddress): RemoteAddress = inetAddress match {
|
|
|
|
|
case null ⇒ null
|
|
|
|
|
case inet ⇒
|
2011-11-10 19:03:18 +01:00
|
|
|
val host = inet.getAddress match {
|
|
|
|
|
case null ⇒ inet.getHostName //Fall back to given name
|
|
|
|
|
case other ⇒ other.getHostAddress
|
2011-11-10 17:39:04 +01:00
|
|
|
}
|
2011-11-10 19:03:18 +01:00
|
|
|
val portNo = inet.getPort
|
|
|
|
|
RemoteAddress(portNo, host)
|
2011-11-10 17:39:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-10 19:03:18 +01:00
|
|
|
case class RemoteAddress private[akka] (port: Int, hostname: String) {
|
2011-11-10 18:41:51 +01:00
|
|
|
@transient
|
|
|
|
|
override lazy val toString = "" + hostname + ":" + port
|
2011-11-10 19:03:18 +01:00
|
|
|
|
2011-11-10 17:39:04 +01:00
|
|
|
}
|
2010-12-15 17:52:31 +01:00
|
|
|
|
2011-09-15 10:20:18 +02:00
|
|
|
class RemoteException(message: String) extends AkkaException(message)
|
|
|
|
|
|
2011-02-28 22:54:32 +01:00
|
|
|
trait RemoteModule {
|
2011-10-27 12:23:01 +02:00
|
|
|
protected[akka] def notifyListeners(message: RemoteLifeCycleEvent): Unit
|
2010-12-15 17:52:31 +01:00
|
|
|
}
|
|
|
|
|
|
2011-08-29 11:44:33 +02:00
|
|
|
/**
|
|
|
|
|
* Remote life-cycle events.
|
|
|
|
|
*/
|
|
|
|
|
sealed trait RemoteLifeCycleEvent
|
|
|
|
|
|
2010-12-29 16:08:43 +01:00
|
|
|
/**
|
|
|
|
|
* Life-cycle events for RemoteClient.
|
|
|
|
|
*/
|
2011-08-29 11:44:33 +02:00
|
|
|
trait RemoteClientLifeCycleEvent extends RemoteLifeCycleEvent {
|
2011-11-10 17:39:04 +01:00
|
|
|
def remoteAddress: RemoteAddress
|
2011-08-29 11:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteClientError(
|
2010-12-30 14:59:00 +01:00
|
|
|
@BeanProperty cause: Throwable,
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty remoteAddress: RemoteAddress) extends RemoteClientLifeCycleEvent
|
2011-09-08 19:45:16 +02:00
|
|
|
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteClientDisconnected(
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty remoteAddress: RemoteAddress) extends RemoteClientLifeCycleEvent
|
2011-09-08 19:45:16 +02:00
|
|
|
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteClientConnected(
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty remoteAddress: RemoteAddress) extends RemoteClientLifeCycleEvent
|
2011-09-08 19:45:16 +02:00
|
|
|
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteClientStarted(
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty remoteAddress: RemoteAddress) extends RemoteClientLifeCycleEvent
|
2011-09-08 19:45:16 +02:00
|
|
|
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteClientShutdown(
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty remoteAddress: RemoteAddress) extends RemoteClientLifeCycleEvent
|
2011-09-08 19:45:16 +02:00
|
|
|
|
2010-12-30 14:59:00 +01:00
|
|
|
case class RemoteClientWriteFailed(
|
|
|
|
|
@BeanProperty request: AnyRef,
|
|
|
|
|
@BeanProperty cause: Throwable,
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty remoteAddress: RemoteAddress) extends RemoteClientLifeCycleEvent
|
2010-12-29 16:08:43 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Life-cycle events for RemoteServer.
|
|
|
|
|
*/
|
2011-08-29 11:44:33 +02:00
|
|
|
trait RemoteServerLifeCycleEvent extends RemoteLifeCycleEvent
|
|
|
|
|
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteServerStarted(
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport) extends RemoteServerLifeCycleEvent
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteServerShutdown(
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport) extends RemoteServerLifeCycleEvent
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteServerError(
|
|
|
|
|
@BeanProperty val cause: Throwable,
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport) extends RemoteServerLifeCycleEvent
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteServerClientConnected(
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty val clientAddress: Option[RemoteAddress]) extends RemoteServerLifeCycleEvent
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteServerClientDisconnected(
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty val clientAddress: Option[RemoteAddress]) extends RemoteServerLifeCycleEvent
|
2010-12-29 16:08:43 +01:00
|
|
|
case class RemoteServerClientClosed(
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty remote: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty val clientAddress: Option[RemoteAddress]) extends RemoteServerLifeCycleEvent
|
2011-01-03 14:44:15 +01:00
|
|
|
case class RemoteServerWriteFailed(
|
|
|
|
|
@BeanProperty request: AnyRef,
|
|
|
|
|
@BeanProperty cause: Throwable,
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty server: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
@BeanProperty remoteAddress: Option[RemoteAddress]) extends RemoteServerLifeCycleEvent
|
2010-12-29 16:08:43 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Thrown for example when trying to send a message using a RemoteClient that is either not started or shut down.
|
|
|
|
|
*/
|
2011-03-24 16:23:37 +01:00
|
|
|
class RemoteClientException private[akka] (
|
|
|
|
|
message: String,
|
2011-11-10 12:23:39 +01:00
|
|
|
@BeanProperty val client: RemoteSupport,
|
2011-11-10 17:39:04 +01:00
|
|
|
val remoteAddress: RemoteAddress, cause: Throwable = null) extends AkkaException(message, cause)
|
2010-12-29 16:08:43 +01:00
|
|
|
|
|
|
|
|
/**
|
2011-03-31 15:55:30 +02:00
|
|
|
* Thrown when the remote server actor dispatching fails for some reason.
|
|
|
|
|
*/
|
|
|
|
|
class RemoteServerException private[akka] (message: String) extends AkkaException(message)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Thrown when a remote exception sent over the wire cannot be loaded and instantiated
|
2010-12-29 16:08:43 +01:00
|
|
|
*/
|
2011-03-25 16:25:36 +01:00
|
|
|
case class CannotInstantiateRemoteExceptionDueToRemoteProtocolParsingErrorException private[akka] (cause: Throwable, originalClassName: String, originalMessage: String)
|
2011-03-24 16:23:37 +01:00
|
|
|
extends AkkaException("\nParsingError[%s]\nOriginalException[%s]\nOriginalMessage[%s]"
|
2011-05-18 17:25:30 +02:00
|
|
|
.format(cause.toString, originalClassName, originalMessage)) {
|
|
|
|
|
override def printStackTrace = cause.printStackTrace
|
2011-03-24 16:23:37 +01:00
|
|
|
override def printStackTrace(printStream: PrintStream) = cause.printStackTrace(printStream)
|
|
|
|
|
override def printStackTrace(printWriter: PrintWriter) = cause.printStackTrace(printWriter)
|
|
|
|
|
}
|
2010-12-15 17:52:31 +01:00
|
|
|
|
2011-11-10 12:23:39 +01:00
|
|
|
abstract class RemoteSupport(val app: AkkaApplication) {
|
2010-12-14 18:22:46 +01:00
|
|
|
/**
|
2011-11-10 12:23:39 +01:00
|
|
|
* Shuts down the remoting
|
2010-12-14 18:22:46 +01:00
|
|
|
*/
|
2011-11-10 12:23:39 +01:00
|
|
|
def shutdown(): Unit
|
2010-12-14 18:22:46 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the name of the server instance
|
|
|
|
|
*/
|
|
|
|
|
def name: String
|
|
|
|
|
|
2011-01-05 17:03:40 +01:00
|
|
|
/**
|
2011-11-10 12:23:39 +01:00
|
|
|
* Starts up the remoting
|
2011-01-05 17:03:40 +01:00
|
|
|
*/
|
2011-11-10 12:23:39 +01:00
|
|
|
def start(loader: Option[ClassLoader]): Unit
|
2010-12-29 17:59:38 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shuts down a specific client connected to the supplied remote address returns true if successful
|
|
|
|
|
*/
|
2011-11-10 17:39:04 +01:00
|
|
|
def shutdownClientConnection(address: RemoteAddress): Boolean
|
2010-12-29 17:59:38 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restarts a specific client connected to the supplied remote address, but only if the client is not shut down
|
|
|
|
|
*/
|
2011-11-10 17:39:04 +01:00
|
|
|
def restartClientConnection(address: RemoteAddress): Boolean
|
2010-12-29 17:59:38 +01:00
|
|
|
|
|
|
|
|
/** Methods that needs to be implemented by a transport **/
|
2010-12-15 17:52:31 +01:00
|
|
|
|
2011-11-03 18:33:57 +01:00
|
|
|
protected[akka] def send(message: Any,
|
|
|
|
|
senderOption: Option[ActorRef],
|
2011-11-10 17:39:04 +01:00
|
|
|
remoteAddress: RemoteAddress,
|
2011-11-03 18:33:57 +01:00
|
|
|
recipient: ActorRef,
|
|
|
|
|
loader: Option[ClassLoader]): Unit
|
2011-11-10 12:23:39 +01:00
|
|
|
|
|
|
|
|
protected[akka] def notifyListeners(message: RemoteLifeCycleEvent): Unit = app.mainbus.publish(message)
|
|
|
|
|
|
|
|
|
|
override def toString = name
|
2011-04-01 14:29:26 +02:00
|
|
|
}
|