import akka.actor.{ActorSystem, Props}
import com.typesafe.config.ConfigFactoryimport com.uplooking.bigdata.p5.akka.{Header, Shutdown, Start}
/**
* Created by thinkpad on 2017/3/31.
*/
object RemoteAkkaClientOps extends App {
val clientActorSystem = ActorSystem("RemoteAkkaClient",
ConfigFactory.load().getConfig("MyRemoteClientSideActor"))
val clientActorRef = clientActorSystem.actorOf(Props[RemoteClientActor], "remoteClientActor")
clientActorRef ! Start
Thread.sleep(2000)
clientActorRef ! Header("1234", 12, true)
Thread.sleep(2000)
clientActorRef ! Shutdown(2)
clientActorSystem.shutdown()
}
//---------------------------------------------------------------------------------------------------------------------------------------------
/**
* 定义客户端和服务端之间通信的协议
*/
object Start extends Serializable
object Stop extends Serializable
trait Message {
val id: String
}
case class Shutdown(waitSecs: Int) extends Serializable
case class Heartbeat(id: String, magic:Int) extends Message with Serializable
case class Header(id: String, len: Int, encrypted: Boolean) extends Message with Serializable
case class Packet(id: String, seq: Long, content: String) extends Message with Serializable
//----------------------------------------------------------------------------------------------------------------------------------------------
/**
* 专门用于Teacher接受的协议样例类
*/
object TeacherProtocol {
case class Request()
case class StudentRequest(request:String)
case class Response(response: String)
}
/**
* 专门用于Student接受的协议样例类
*/
object StudentProtocol {
case class InitSignl()
}
//----------------------------------------------------------------------------------------------------------------------------
/**
* 远程通信的服务端
*/
class RemoteServerActor extends Actor with ActorLogging{
override def receive: Receive = {
case Start => {
log.info("----server Start----")
}
case Shutdown(waitSecs) => {
log.info("系统将在" + waitSecs + "s后停止运行")
Thread.sleep(waitSecs * 1000)
//ActorSystem.shut,从整个ActorSystem程序上下文中获取ActorSystem对象,执行shutdown的动作
context.system.shutdown()
}
case Header(id, len, encrypted) => {
log.info("---client发送的header消息---" + (id, len, encrypted))
}
}
}
/**
* 1、在类路径下面创建application.conf配置文件,指定服务端和客户端的配置信息
* 2、定义的服务端和客户端的通信协议Message
* 3、编写服务端、客户端
* 4、启动整个应用程序
*
*/
object RemoteServerAkkaOps extends App {
/**
* 我们在这里是要创建一个服务端的应用程序,供客户端进行连接
*
*/
val remoteActor = ActorSystem("remote-system",
ConfigFactory.load().getConfig("MyRemoteServerSideActor"))
val remoteActorRef = remoteActor.actorOf(Props[RemoteServerActor], "remoteActor")
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
/**
* 远程通讯客户端
*/
class RemoteClientActor extends Actor with ActorLogging{
//要通过客户端去访问服务端,所以需要在客户端持有服务端的一个代理对象
//远程的这种方式,我们需要通过netty的方式从整个应用程序上下文对象中得到
//构建server在网络中的地址
//netty格式:akka.tcp://actorsytemName@hostname:port/user/remoteActorRefName
// akka.<protocol>://<actor system>@<hostname>:<port>/<actor path>
val path = "akka.tcp://remote-system@192.168.100.8:2556/user/remoteActor"
val actorSelection = context.actorSelection(path)
override def receive: Receive = {
case Start => {
log.info("客户端发送给服务端的Start消息")
actorSelection ! Start
}
case Shutdown(waitSecs) => {
log.info("客户端发送给服务端的Shutdown消息")
actorSelection ! Shutdown(waitSecs)
}
case Header(id, len, encrypted) => {
log.info("客户端发送给服务端的Header消息")
actorSelection ! Header(id, len, encrypted)
}
}
}
/**
* Created by thinkpad on 2017/3/31.
*/
object RemoteAkkaClientOps extends App {
val clientActorSystem = ActorSystem("RemoteAkkaClient",
ConfigFactory.load().getConfig("MyRemoteClientSideActor"))
val clientActorRef = clientActorSystem.actorOf(Props[RemoteClientActor], "remoteClientActor")
clientActorRef ! Start
Thread.sleep(2000)
clientActorRef ! Header("1234", 12, true)
Thread.sleep(2000)
clientActorRef ! Shutdown(2)
clientActorSystem.shutdown()
}