2012-05-15 09:40:13 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2012-09-17 12:54:08 +02:00
|
|
|
package akka.remote.serialization
|
2012-05-15 09:40:13 +02:00
|
|
|
|
2012-09-17 12:54:08 +02:00
|
|
|
import akka.serialization.{ Serializer, SerializationExtension }
|
2012-05-15 09:40:13 +02:00
|
|
|
import java.io.Serializable
|
|
|
|
|
import com.google.protobuf.ByteString
|
2012-06-25 16:29:08 +02:00
|
|
|
import com.typesafe.config.{ Config, ConfigFactory }
|
|
|
|
|
import akka.actor.{ Actor, ActorRef, Deploy, ExtendedActorSystem, NoScopeGiven, Props, Scope }
|
2012-05-15 09:40:13 +02:00
|
|
|
import akka.remote.DaemonMsgCreate
|
2012-06-25 16:29:08 +02:00
|
|
|
import akka.remote.RemoteProtocol.{ DaemonMsgCreateProtocol, DeployProtocol, PropsProtocol }
|
|
|
|
|
import akka.routing.{ NoRouter, RouterConfig }
|
2012-05-15 18:22:40 +02:00
|
|
|
import akka.actor.FromClassCreator
|
2012-06-25 16:29:08 +02:00
|
|
|
import scala.reflect.ClassTag
|
2012-09-06 03:17:51 +02:00
|
|
|
import util.{ Failure, Success }
|
2012-05-15 09:40:13 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Serializes akka's internal DaemonMsgCreate using protobuf
|
|
|
|
|
* for the core structure of DaemonMsgCreate, Props and Deploy.
|
2012-05-15 18:22:40 +02:00
|
|
|
* Serialization of contained RouterConfig, Config, and Scope
|
|
|
|
|
* is done with configured serializer for those classes, by
|
|
|
|
|
* default java.io.Serializable.
|
2012-05-24 12:19:39 +02:00
|
|
|
*
|
|
|
|
|
* INTERNAL API
|
2012-05-15 09:40:13 +02:00
|
|
|
*/
|
2012-05-24 12:19:39 +02:00
|
|
|
private[akka] class DaemonMsgCreateSerializer(val system: ExtendedActorSystem) extends Serializer {
|
2012-05-15 09:40:13 +02:00
|
|
|
import ProtobufSerializer.serializeActorRef
|
|
|
|
|
import ProtobufSerializer.deserializeActorRef
|
|
|
|
|
|
2012-05-15 17:16:46 +02:00
|
|
|
def includeManifest: Boolean = false
|
2012-05-15 09:40:13 +02:00
|
|
|
def identifier = 3
|
|
|
|
|
lazy val serialization = SerializationExtension(system)
|
|
|
|
|
|
|
|
|
|
def toBinary(obj: AnyRef): Array[Byte] = obj match {
|
|
|
|
|
case DaemonMsgCreate(props, deploy, path, supervisor) ⇒
|
|
|
|
|
|
|
|
|
|
def deployProto(d: Deploy): DeployProtocol = {
|
|
|
|
|
val builder = DeployProtocol.newBuilder.setPath(d.path)
|
|
|
|
|
if (d.config != ConfigFactory.empty)
|
|
|
|
|
builder.setConfig(serialize(d.config))
|
|
|
|
|
if (d.routerConfig != NoRouter)
|
|
|
|
|
builder.setRouterConfig(serialize(d.routerConfig))
|
|
|
|
|
if (d.scope != NoScopeGiven)
|
|
|
|
|
builder.setScope(serialize(d.scope))
|
|
|
|
|
builder.build
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def propsProto = {
|
|
|
|
|
val builder = PropsProtocol.newBuilder.
|
|
|
|
|
setDispatcher(props.dispatcher).
|
|
|
|
|
setDeploy(deployProto(props.deploy))
|
2012-05-15 18:22:40 +02:00
|
|
|
props.creator match {
|
|
|
|
|
case FromClassCreator(clazz) ⇒ builder.setFromClassCreator(clazz.getName)
|
|
|
|
|
case creator ⇒ builder.setCreator(serialize(creator))
|
|
|
|
|
}
|
2012-05-15 09:40:13 +02:00
|
|
|
if (props.routerConfig != NoRouter)
|
|
|
|
|
builder.setRouterConfig(serialize(props.routerConfig))
|
|
|
|
|
builder.build
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DaemonMsgCreateProtocol.newBuilder.
|
|
|
|
|
setProps(propsProto).
|
|
|
|
|
setDeploy(deployProto(deploy)).
|
|
|
|
|
setPath(path).
|
|
|
|
|
setSupervisor(serializeActorRef(supervisor)).
|
|
|
|
|
build.toByteArray
|
|
|
|
|
|
|
|
|
|
case _ ⇒
|
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
|
"Can't serialize a non-DaemonMsgCreate message using DaemonMsgCreateSerializer [%s]".format(obj))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = {
|
|
|
|
|
val proto = DaemonMsgCreateProtocol.parseFrom(bytes)
|
|
|
|
|
|
2012-05-24 12:19:39 +02:00
|
|
|
def deploy(protoDeploy: DeployProtocol): Deploy = {
|
2012-05-15 09:40:13 +02:00
|
|
|
val config =
|
|
|
|
|
if (protoDeploy.hasConfig) deserialize(protoDeploy.getConfig, classOf[Config])
|
|
|
|
|
else ConfigFactory.empty
|
|
|
|
|
val routerConfig =
|
|
|
|
|
if (protoDeploy.hasRouterConfig) deserialize(protoDeploy.getRouterConfig, classOf[RouterConfig])
|
|
|
|
|
else NoRouter
|
|
|
|
|
val scope =
|
|
|
|
|
if (protoDeploy.hasScope) deserialize(protoDeploy.getScope, classOf[Scope])
|
|
|
|
|
else NoScopeGiven
|
|
|
|
|
Deploy(protoDeploy.getPath, config, routerConfig, scope)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def props = {
|
2012-05-15 18:22:40 +02:00
|
|
|
val creator =
|
2012-09-06 03:17:51 +02:00
|
|
|
if (proto.getProps.hasFromClassCreator)
|
|
|
|
|
FromClassCreator(system.dynamicAccess.getClassFor[Actor](proto.getProps.getFromClassCreator).get)
|
|
|
|
|
else
|
2012-05-15 18:22:40 +02:00
|
|
|
deserialize(proto.getProps.getCreator, classOf[() ⇒ Actor])
|
|
|
|
|
|
2012-05-15 09:40:13 +02:00
|
|
|
val routerConfig =
|
|
|
|
|
if (proto.getProps.hasRouterConfig) deserialize(proto.getProps.getRouterConfig, classOf[RouterConfig])
|
|
|
|
|
else NoRouter
|
2012-05-15 18:22:40 +02:00
|
|
|
|
2012-05-15 09:40:13 +02:00
|
|
|
Props(
|
2012-05-15 18:22:40 +02:00
|
|
|
creator = creator,
|
2012-05-15 09:40:13 +02:00
|
|
|
dispatcher = proto.getProps.getDispatcher,
|
|
|
|
|
routerConfig = routerConfig,
|
|
|
|
|
deploy = deploy(proto.getProps.getDeploy))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DaemonMsgCreate(
|
|
|
|
|
props = props,
|
|
|
|
|
deploy = deploy(proto.getDeploy),
|
|
|
|
|
path = proto.getPath,
|
|
|
|
|
supervisor = deserializeActorRef(system, proto.getSupervisor))
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-06 03:17:51 +02:00
|
|
|
protected def serialize(any: AnyRef): ByteString = ByteString.copyFrom(serialization.serialize(any).get)
|
2012-05-15 09:40:13 +02:00
|
|
|
|
2012-06-13 15:44:24 +02:00
|
|
|
protected def deserialize[T: ClassTag](data: ByteString, clazz: Class[T]): T = {
|
2012-05-15 09:40:13 +02:00
|
|
|
val bytes = data.toByteArray
|
|
|
|
|
serialization.deserialize(bytes, clazz) match {
|
2012-09-06 03:17:51 +02:00
|
|
|
case Success(x: T) ⇒ x
|
|
|
|
|
case Success(other) ⇒ throw new IllegalArgumentException("Can't deserialize to [%s], got [%s]".format(clazz.getName, other))
|
|
|
|
|
case Failure(e) ⇒
|
2012-05-15 09:40:13 +02:00
|
|
|
// Fallback to the java serializer, because some interfaces don't implement java.io.Serializable,
|
|
|
|
|
// but the impl instance does. This could be optimized by adding java serializers in reference.conf:
|
|
|
|
|
// com.typesafe.config.Config
|
|
|
|
|
// akka.routing.RouterConfig
|
|
|
|
|
// akka.actor.Scope
|
|
|
|
|
serialization.deserialize(bytes, classOf[java.io.Serializable]) match {
|
2012-09-06 03:17:51 +02:00
|
|
|
case Success(x: T) ⇒ x
|
|
|
|
|
case _ ⇒ throw e // the first exception
|
2012-05-15 09:40:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|