2011-09-09 13:46:36 +02:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-09-09 13:46:36 +02:00
|
|
|
*/
|
|
|
|
|
|
2011-09-20 21:44:50 +02:00
|
|
|
package akka.remote
|
2011-09-09 13:46:36 +02:00
|
|
|
|
2011-09-20 21:44:50 +02:00
|
|
|
import akka.remote.RemoteProtocol._
|
2011-09-09 13:46:36 +02:00
|
|
|
import com.google.protobuf.ByteString
|
2011-11-10 20:08:00 +01:00
|
|
|
import akka.actor.ActorSystem
|
2011-11-22 13:04:10 +01:00
|
|
|
import akka.serialization.SerializationExtension
|
2011-12-29 16:11:56 +01:00
|
|
|
import akka.util.ReflectiveAccess
|
2011-09-09 13:46:36 +02:00
|
|
|
|
|
|
|
|
object MessageSerializer {
|
|
|
|
|
|
2012-01-27 13:30:43 +01:00
|
|
|
def deserialize(system: ActorSystem, messageProtocol: MessageProtocol, classLoader: ClassLoader): AnyRef = {
|
2011-12-29 16:11:56 +01:00
|
|
|
val clazz = if (messageProtocol.hasMessageManifest) {
|
|
|
|
|
Option(ReflectiveAccess.getClassFor[AnyRef](
|
|
|
|
|
messageProtocol.getMessageManifest.toStringUtf8,
|
2012-01-27 13:30:43 +01:00
|
|
|
classLoader) match {
|
2011-12-29 16:11:56 +01:00
|
|
|
case Left(e) ⇒ throw e
|
|
|
|
|
case Right(r) ⇒ r
|
|
|
|
|
})
|
|
|
|
|
} else None
|
|
|
|
|
SerializationExtension(system).deserialize(
|
|
|
|
|
messageProtocol.getMessage.toByteArray,
|
|
|
|
|
messageProtocol.getSerializerId,
|
|
|
|
|
clazz,
|
|
|
|
|
classLoader) match {
|
|
|
|
|
case Left(e) ⇒ throw e
|
|
|
|
|
case Right(r) ⇒ r
|
|
|
|
|
}
|
2011-09-09 13:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
2011-11-17 12:36:35 +01:00
|
|
|
def serialize(system: ActorSystem, message: AnyRef): MessageProtocol = {
|
2011-12-29 16:11:56 +01:00
|
|
|
val s = SerializationExtension(system)
|
|
|
|
|
val serializer = s.findSerializerFor(message)
|
2011-09-09 13:46:36 +02:00
|
|
|
val builder = MessageProtocol.newBuilder
|
2011-12-29 16:11:56 +01:00
|
|
|
builder.setMessage(ByteString.copyFrom(serializer.toBinary(message)))
|
|
|
|
|
builder.setSerializerId(serializer.identifier)
|
|
|
|
|
if (serializer.includeManifest)
|
|
|
|
|
builder.setMessageManifest(ByteString.copyFromUtf8(message.getClass.getName))
|
2011-09-09 13:46:36 +02:00
|
|
|
builder.build
|
|
|
|
|
}
|
|
|
|
|
}
|