2011-09-14 16:09:17 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.remote.testconductor
|
|
|
|
|
|
2012-05-02 21:56:26 +02:00
|
|
|
import akka.actor.{ Actor, ActorRef, ActorSystem, LoggingFSM, Props }
|
2011-09-14 16:09:17 +02:00
|
|
|
import RemoteConnection.getAddrString
|
|
|
|
|
import akka.util.duration._
|
|
|
|
|
import TestConductorProtocol._
|
|
|
|
|
import org.jboss.netty.channel.{ Channel, SimpleChannelUpstreamHandler, ChannelHandlerContext, ChannelStateEvent, MessageEvent }
|
|
|
|
|
import com.eaio.uuid.UUID
|
2012-05-02 21:56:26 +02:00
|
|
|
import com.typesafe.config.ConfigFactory
|
|
|
|
|
import akka.util.Timeout
|
|
|
|
|
import akka.util.Duration
|
|
|
|
|
import java.util.concurrent.TimeUnit.MILLISECONDS
|
|
|
|
|
import akka.pattern.ask
|
|
|
|
|
import akka.dispatch.Await
|
|
|
|
|
import scala.util.control.NoStackTrace
|
|
|
|
|
import akka.actor.Status
|
|
|
|
|
import akka.event.LoggingAdapter
|
|
|
|
|
import akka.actor.PoisonPill
|
|
|
|
|
import akka.event.Logging
|
2011-09-14 16:09:17 +02:00
|
|
|
|
2012-05-03 20:48:27 +02:00
|
|
|
trait Player extends BarrierSync { this: TestConductorExt ⇒
|
2011-09-14 16:09:17 +02:00
|
|
|
|
2012-05-03 20:48:27 +02:00
|
|
|
private var _client: ActorRef = _
|
|
|
|
|
private def client = _client match {
|
|
|
|
|
case null ⇒ throw new IllegalStateException("TestConductor client not yet started")
|
|
|
|
|
case x ⇒ x
|
|
|
|
|
}
|
2012-05-02 21:56:26 +02:00
|
|
|
|
2012-05-03 20:48:27 +02:00
|
|
|
def startClient(port: Int) {
|
|
|
|
|
import ClientFSM._
|
|
|
|
|
import akka.actor.FSM._
|
|
|
|
|
import Settings.BarrierTimeout
|
|
|
|
|
|
|
|
|
|
if (_client ne null) throw new IllegalStateException("TestConductorClient already started")
|
|
|
|
|
_client = system.actorOf(Props(new ClientFSM(port)), "TestConductorClient")
|
|
|
|
|
val a = system.actorOf(Props(new Actor {
|
|
|
|
|
var waiting: ActorRef = _
|
|
|
|
|
def receive = {
|
|
|
|
|
case fsm: ActorRef ⇒ waiting = sender; fsm ! SubscribeTransitionCallBack(self)
|
|
|
|
|
case Transition(_, Connecting, Connected) ⇒ waiting ! "okay"
|
|
|
|
|
case t: Transition[_] ⇒ waiting ! Status.Failure(new RuntimeException("unexpected transition: " + t))
|
|
|
|
|
case CurrentState(_, Connected) ⇒ waiting ! "okay"
|
|
|
|
|
case _: CurrentState[_] ⇒
|
|
|
|
|
}
|
|
|
|
|
}))
|
2012-05-02 21:56:26 +02:00
|
|
|
|
2012-05-03 20:48:27 +02:00
|
|
|
Await.result(a ? client, Duration.Inf)
|
2012-05-02 21:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
2011-09-14 16:09:17 +02:00
|
|
|
override def enter(name: String*) {
|
2012-05-02 21:56:26 +02:00
|
|
|
system.log.debug("entering barriers " + name.mkString("(", ", ", ")"))
|
2011-09-14 16:09:17 +02:00
|
|
|
name foreach { b ⇒
|
2012-05-02 21:56:26 +02:00
|
|
|
import Settings.BarrierTimeout
|
2012-05-03 20:48:27 +02:00
|
|
|
Await.result(client ? EnterBarrier(b), Duration.Inf)
|
2012-05-02 21:56:26 +02:00
|
|
|
system.log.debug("passed barrier {}", b)
|
2011-09-14 16:09:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object ClientFSM {
|
|
|
|
|
sealed trait State
|
|
|
|
|
case object Connecting extends State
|
|
|
|
|
case object Connected extends State
|
|
|
|
|
|
2012-05-03 20:48:27 +02:00
|
|
|
case class Data(channel: Channel, barrier: Option[(String, ActorRef)])
|
2011-09-14 16:09:17 +02:00
|
|
|
|
|
|
|
|
class ConnectionFailure(msg: String) extends RuntimeException(msg) with NoStackTrace
|
|
|
|
|
case object Disconnected
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-03 20:48:27 +02:00
|
|
|
class ClientFSM(port: Int) extends Actor with LoggingFSM[ClientFSM.State, ClientFSM.Data] {
|
2011-09-14 16:09:17 +02:00
|
|
|
import ClientFSM._
|
|
|
|
|
|
2012-05-03 20:48:27 +02:00
|
|
|
val settings = TestConductor().Settings
|
2012-05-02 21:56:26 +02:00
|
|
|
|
|
|
|
|
val handler = new PlayerHandler(self, Logging(context.system, "PlayerHandler"))
|
2011-09-14 16:09:17 +02:00
|
|
|
|
2012-05-03 20:48:27 +02:00
|
|
|
startWith(Connecting, Data(RemoteConnection(Client, settings.host, port, handler), None))
|
2011-09-14 16:09:17 +02:00
|
|
|
|
|
|
|
|
when(Connecting, stateTimeout = 10 seconds) {
|
2012-05-03 20:48:27 +02:00
|
|
|
case Event(msg: ClientOp, _) ⇒
|
|
|
|
|
stay replying Status.Failure(new IllegalStateException("not connected yet"))
|
|
|
|
|
case Event(Connected, d @ Data(channel, _)) ⇒
|
|
|
|
|
val hello = Hello.newBuilder.setName(settings.name).setAddress(TestConductor().address).build
|
2011-09-14 16:09:17 +02:00
|
|
|
channel.write(Wrapper.newBuilder.setHello(hello).build)
|
2012-05-03 20:48:27 +02:00
|
|
|
goto(Connected)
|
2011-09-14 16:09:17 +02:00
|
|
|
case Event(_: ConnectionFailure, _) ⇒
|
|
|
|
|
// System.exit(1)
|
|
|
|
|
stop
|
|
|
|
|
case Event(StateTimeout, _) ⇒
|
2012-05-02 21:56:26 +02:00
|
|
|
log.error("connect timeout to TestConductor")
|
2011-09-14 16:09:17 +02:00
|
|
|
// System.exit(1)
|
|
|
|
|
stop
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
when(Connected) {
|
|
|
|
|
case Event(Disconnected, _) ⇒
|
2012-05-02 21:56:26 +02:00
|
|
|
log.info("disconnected from TestConductor")
|
2011-09-14 16:09:17 +02:00
|
|
|
throw new ConnectionFailure("disconnect")
|
|
|
|
|
case Event(msg: EnterBarrier, Data(channel, _)) ⇒
|
|
|
|
|
sendMsg(channel)(msg)
|
2012-05-03 20:48:27 +02:00
|
|
|
stay using Data(channel, Some(msg.name, sender))
|
|
|
|
|
case Event(msg: Wrapper, Data(channel, Some((barrier, sender)))) if msg.getAllFields.size == 1 ⇒
|
2011-09-14 16:09:17 +02:00
|
|
|
if (msg.hasBarrier) {
|
|
|
|
|
val b = msg.getBarrier.getName
|
|
|
|
|
if (b != barrier) {
|
2012-05-02 21:56:26 +02:00
|
|
|
sender ! Status.Failure(new RuntimeException("wrong barrier " + b + " received while waiting for " + barrier))
|
2011-09-14 16:09:17 +02:00
|
|
|
} else {
|
|
|
|
|
sender ! b
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-03 20:48:27 +02:00
|
|
|
stay using Data(channel, None)
|
2011-09-14 16:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onTermination {
|
|
|
|
|
case StopEvent(_, _, Data(channel, _)) ⇒
|
|
|
|
|
channel.close()
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-03 20:48:27 +02:00
|
|
|
initialize
|
|
|
|
|
|
2011-09-14 16:09:17 +02:00
|
|
|
private def sendMsg(channel: Channel)(msg: ClientOp) {
|
|
|
|
|
msg match {
|
|
|
|
|
case EnterBarrier(name) ⇒
|
|
|
|
|
val enter = TestConductorProtocol.EnterBarrier.newBuilder.setName(name).build
|
|
|
|
|
channel.write(Wrapper.newBuilder.setBarrier(enter).build)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-02 21:56:26 +02:00
|
|
|
class PlayerHandler(fsm: ActorRef, log: LoggingAdapter) extends SimpleChannelUpstreamHandler {
|
2011-09-14 16:09:17 +02:00
|
|
|
|
|
|
|
|
import ClientFSM._
|
|
|
|
|
|
|
|
|
|
override def channelConnected(ctx: ChannelHandlerContext, event: ChannelStateEvent) = {
|
|
|
|
|
val channel = event.getChannel
|
2012-05-02 21:56:26 +02:00
|
|
|
log.debug("connected to {}", getAddrString(channel))
|
2011-09-14 16:09:17 +02:00
|
|
|
fsm ! Connected
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def channelDisconnected(ctx: ChannelHandlerContext, event: ChannelStateEvent) = {
|
|
|
|
|
val channel = event.getChannel
|
2012-05-02 21:56:26 +02:00
|
|
|
log.debug("disconnected from {}", getAddrString(channel))
|
|
|
|
|
fsm ! PoisonPill
|
2011-09-14 16:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def messageReceived(ctx: ChannelHandlerContext, event: MessageEvent) = {
|
|
|
|
|
val channel = event.getChannel
|
2012-05-02 21:56:26 +02:00
|
|
|
log.debug("message from {}: {}", getAddrString(channel), event.getMessage)
|
2011-09-14 16:09:17 +02:00
|
|
|
event.getMessage match {
|
|
|
|
|
case msg: Wrapper if msg.getAllFields.size == 1 ⇒
|
|
|
|
|
fsm ! msg
|
|
|
|
|
case msg ⇒
|
2012-05-02 21:56:26 +02:00
|
|
|
log.info("server {} sent garbage '{}', disconnecting", getAddrString(channel), msg)
|
2011-09-14 16:09:17 +02:00
|
|
|
channel.close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|