Getting an ActorRef when a WebSocket is closed in Play - scala

According to the Play framework documentation, we have the option of overriding the postStop method, but it has no ActorRef. I need an ActorRef because I am using ActorRef as an identifier in a HashMap containing the mappings of actors to connected clients: on disconnect, I want to remove that mapping from the HashMap.
Edit:-
Here HashMap works as an authentication pool.The very first message from client is for Authentication, and on validation the instance of ActorRef is added to HashMap. On the following events/messages authorization is verified by checking the existence of ActorRef in HashMap, have a look at the following code:-
def authenticate(actorRef: ActorRef, message: SocketParsedMessage) {
(message.data \ "token").validate[String] match {
case s: JsSuccess[String] => {
val token = jwt.parse(s.get)
if (jwt.verify(token,jwtSecret)) {
val userId = UUID.fromString(jwt.getSubject(token))
hashMapU2A += (UUID.fromString(jwt.getSubject(token)) -> actorRef)
hashMapA2U += (actorRef -> userId)
actorRef ! SocketParsedMessage(AllowedSocketMessageTypes.AUTHENTICATE, Json.obj(
"success" -> true, "message" -> "Authorized for making further requests request")).toString
publishUserStatus(userId)
} else {
actorRef ! SocketParsedMessage(AllowedSocketMessageTypes.AUTHENTICATE, JsObject(
Seq("success" -> JsBoolean(false), "message" -> JsString("Invalid token"))
)).toString
}
}
case e: JsError => {
actorRef ! SocketParsedMessage(AllowedSocketMessageTypes.AUTHENTICATE, Json.obj(
"success" -> false, "message" -> "Token not supplied with request")).toString
actorRef ! PoisonPill
}
}
}
val hashMapA2U: mutable.HashMap[ActorRef, UUID] = mutable.HashMap()

My Stupidity.
Actually, ActorRef is already provided in my custom implementation of Actor, and I can use it.
class MyWebSocketActor(sh: HandleSocket, out: ActorRef) extends Actor {
def receive = {
case msg: String => {
Json.fromJson[SocketParsedMessage](Json.parse(msg)) match {
case s: JsSuccess[SocketParsedMessage] => {
sh.HandleSocketMessages(out, s.get)
}
case _: JsError => {
out ! PoisonPill
}
}
// println(parsedMsg)
// out ! (msg)
}
}
override def postStop(): Unit = {
// super.postStop()
sh.clientDisconnected(out)
}
}

Related

I want to send second message to Actor as new Message

I have two actors for example, sender:
class ActorSender(actroReciever: ActorRef) extends Actor{
implicit val timeout = Timeout(100, TimeUnit.SECONDS)
override def receive: Receive = {
case "RUN" => {
val resp = Await.result(actroReciever ? "Msg", 100.seconds)
println("receive response " + resp)
};
case str:String => println(str)
case _ => println("Error type msg")
}
}
reciever:
class ActroReciever extends Actor{
override def receive: Receive = {
case str:String => {
val snd = sender()
snd ! "MessageFirst"
snd ! "MessageSecond"
}
}
}
And class for starting:
object Tester extends App {
val system = ActorSystem("system")
val receiver = system.actorOf(Props[ActroReciever](new ActroReciever()), "receiver")
val sender = system.actorOf(Props[ActorSender](new ActorSender(receiver)), "sender")
sender ! "RUN"
}
I want to send two messages to sender(), first as reply for "ask", second msg as "new Message", which ActorSender execute as "matching", how I can do it? Thanks
First, you know you should not use Await, right?
Second, ask (?) is not meant to be used within actors. ask creates a temporary actor which can only receive a single message. It is not your ActorSender receiving the answer, but the temporary actor created by ask. That's why you have the feeling you can only send 1 answer.
You are doing this wrong, you should simply send your message using actroReciever ! "Msg". No need to change anything on your ActroReciever.

postRestart and preRestart methods are not getting invoke in akka actots

I am following this tutorial here is my code
case class ArtGroupDeleteFromES (uuidList:List[String])
class ArtGroupDeleteESActor extends Actor{
val log = LoggerFactory.getLogger(this.getClass)
override def preStart() {
log.debug("preStart Starting ArtGroupDeleteESActor instance hashcode # {}",
this.hashCode())
}
override def postStop() {
log.debug("postStop Stopping ArtGroupDeleteESActor instance hashcode # {}",
this.hashCode())
}
override def preRestart(reason: Throwable, message: Option[Any]) {
log.debug("I am restarting")
log.debug("ArtGroupDeleteESActor: preRestart")
log.debug(s" MESSAGE: ${message.getOrElse("")}")
log.debug(s" REASON: ${reason.getMessage}")
super.preRestart(reason, message)
}
override def postRestart(reason: Throwable) {
log.debug("restart completed!")
log.debug("ArtGroupDeleteESActor: postRestart")
log.debug(s" REASON: ${reason.getMessage}")
super.postRestart(reason)
}
def receive = {
case ArtGroupDeleteFromES(uuidList) =>
throw new Exception("Booom")
sender ! true
}
case message =>
log.warn("Received unknown message: {}", message)
unhandled(message)
}
}
and here is the how i am sending this actor a message
class ArtGroupDeletionActor extends Actor{
val log = LoggerFactory.getLogger(this.getClass)
override val supervisorStrategy = OneForOneStrategy(
maxNrOfRetries = 10, withinTimeRange = 10 seconds) {
case _:Exception => Restart
}
val artGroupDeleteESActor=context.actorOf(Props[ArtGroupDeleteESActor]
.withDispatcher("akka.actor.ArtGroupDeleteESActor-dispatcher")
,name = "ArtGroupDeleteESActor")
def receive = {
case DeleteArtGroup(uuidList) =>
val future1 = ask(artGroupDeleteESActor, ArtGroupDeleteFromES(uuidList)).mapTo[Boolean]
var isDeletedfromES = Await.result(future1, timeout.duration)
case message =>
log.warn("Unhandled message received : {}", message)
unhandled(message)
}
}
object test extends App{
val artGroupDeletionActor=system.actorOf(Props[ArtGroupDeletionActor]
.withDispatcher("akka.actor.ArtGroupDeletionActor-dispatcher")
,name = "ArtGroupDeletionActor")
artGroupDeletionActor ! DeleteArtGroup(List("123"))
}
the PostRestart() and preRestart() methods are not invoking,but preStart() and postStop() gets called, please guide me where i am doing wrong
(for simplicity I'll call your actors Parent and Child from now on)
What happens here is that when an exception occurs inside Child.receive, it doesn't send a response to Parent, instead, the actor system sends some control instruction for the supervision strategy. However, Parent is blocked on Await waiting for completion of future1, which only happens after the timeout exceeds, and then, in turn, a TimeoutException is thrown inside Parent.receive, killing (restarting) the Parent actor itself, and thus the supervising message of an exception in Child is then passed to deadLetters, never restarting the Child.
You should never, ever, ever block inside an actor, so this is incorrect:
val future1 = ask(artGroupDeleteESActor, ArtGroupDeleteFromES(uuidList)).mapTo[Boolean]
var isDeletedfromES = Await.result(future1, timeout.duration)
Instead, you have to either utilize some kind of message identification to distinguish one reply from another in concurrent environment, or add an onComplete to the Future and send a message to self in the closure (beware: no logic other than sending a message should be executed inside the closure to the Future!).
So, option A:
case class ArtGroupDeleteFromES(id: Long, uuidList: List[String])
case class ArtGroupDeleteFromESResult(id: Long, success: Boolean)
class Parent extends Actor {
override val supervisionStrategy = ...
var msgId = 0L
var pendingRequesters = Map.empty[Long, ActorRef]
val child = context.actorOf(Props[Child])
def nextId = {
msgId += 1
msgId
}
def receive = {
case DeleteArtGroup(uuidList) =>
val id = nextId
pendingRequesters += id -> sender() // store a reference to the sender so that you can send it a message when everything completes
child ! DeleteArtGroupFromES(nextId, uuidList)
case ArtGroupDeleteFromESResult(id, success) =>
// process result...
pendingRequesters(id) ! "done"
pendingRequesters -= id
}
}
And option B:
case class ArtGroupDeleteFromES(uuidList: List[String])
case class ArtGroupDeleteFromESResult(replyTo: ActorRef, success: Boolean)
class Parent extends Actor {
override val supervisionStrategy = ...
val child = context.actorOf(Props[Child])
def receive = {
case DeleteArtGroup(uuidList) =>
val requester = sender() // when the future completes, sender may have already changed, so you need to remember it
(child ? DeleteArtGroupFromES(uuidList)).onComplete {
case Success(success) => self ! ArtGroupDeleteFromESResult(requester, success)
case Failure(e) =>
log.warn("Could not delete...", e)
self ! ArtGroupDeleteFromESResult(requester, success = false)
}
}

Akka: Future to Actor communication?

I have a system that spawns a single actor who will spawn many futures. Some of these futures will run into scenarios that need to spawn more futures (but tell the actor about it). How do I send a message from a future to an actor on the completion of the future's operations?
I've looked at the pipeTo documentation but I am having trouble referencing the actors in my system in my future class.
Here is what my Future class looks like:
class crawler(string: String) {
val status: Future[Boolean] = Future[Boolean] {
//Do something with content
println("I am a future working on cert crawling. My cert contents are: " + cert.content)
true
}
status onComplete {
case Success(true) =>
for(chars <- string.toCharArray) {
//send actor a message for each character of the string.
}
case Failure(t) => println("An error has occured: " + t.getMessage)
}
}
Where the actor's receive method does the following:
def receive = {
case c:Char => if(!certCache.containsKey(c)){
println("actor >>>> Need to begin crawl on " + c + ".")
sender() ! new crawler("give sender the future")
case _ => println("That's not the right input!")
}
And, my Actor is spawned like:
object Main extends App {
val system = ActorSystem("MySystem")
val actor = system.actorOf(Props[actorClass], name = "actor")
actor ! 'a'
}
Directly
You could dependency inject the ActorRef into your Future (not recommended, see Abstracted) :
import akka.actor.ActorRef
//dependency injection of the ActorRef with a default value of noSender
class crawler(string : String, actorRef : ActorRef = ActorRef.noSender) {
...
status OnComplete {
//send each Char in string to the actorRef
case Success(true) => string.foreach(actorRef ! _)
...
}
Then in your Actor you can use self to pass the ActorRef into the crawler:
def receive = {
case c : Char => if(!certCache.containsKey(c)) {
sender() ! new crawler("give sender the future", self)
}
}
Abstracted
Further, you could abstract away the use of ActorRef entirely so that crawler doesn't need to know the details of messaging passing. This is the more "functional" approach which has the benefit of being extendable if you ever switch to Futures or even akka.stream.scaladsl.Source for reactive streams (see example):
//no akka imports or dependencies
class crawler(string : String, sendChar : (Char) => Unit) {
...
case Success(true) => string foreach sendChar
}
And in your Actor you can pass an anonymous function to crawler which sends a Char to the Actor via self:
def receive = {
case c : Char => if(!certCache.containsKey(c)) {
sender ! new crawler("give sender the future", self ! _)
}
}
You can even get robust and provide default "do nothing" behavior for your sendChar function:
class crawler(string : String, sendChar : (Char) => Unit = {_=>}) {
...
}
val crawler = crawler("foo") //still get regular Future behavior for status

Error when using Try/Catch in Play Framework with Websocket

I am using web sockets with Play Framework in Scala. I would like to use Try/Catch functionality in my project for catching some Exceptions like Server Exception, Network Exception and etc.
What I did :
WebSocketController.scala
object LoginWS {
def props(out: ActorRef) = Props(new LoginWS(out))
}
class LoginWS(out: ActorRef) extends Actor {
def receive = {
case json_req: JsObject =>
var user_name = (json_req \ "user_name").as[String]
var password = (json_req \ "password").as[String]
var source = (json_req \ "source_type").as[String]
var result = UserLogin.authenticateUser(user_name, password).isDefined
var userID: Int = 0;
if(result) {
userID = UserLogin.getUserRole(user_name, password)
val login_status : String = "Success"
out ! Json.toJson(JsObject(Seq("login_status" -> JsString(login_status), "user_id" -> JsNumber(userID))))
}
else {
val login_status : String = "Failure"
out ! Json.toJson(JsObject(Seq("login_status" -> JsString(login_status), "user_id" -> JsNumber(userID))))
}
}
}
object WebSocketController extends Controller {
def login = WebSocket.acceptWithActor[JsValue, JsValue] { request =>
out => LoginWS.props(out)
}
}
What I tried :
I have used this answer posted by Ende Neu but it shows not found: value APIAction. Note: I added APIAction in routes file too
Code :
class LoginWS(out: ActorRef) extends Actor {
def receive = APIAction { request
case json_req: JsObject =>
.....
....//code here
}
}
object WebSocketController extends Controller {
def login = WebSocket.acceptWithActor[JsValue, JsValue] { request =>
out => LoginWS.props(out)
}
def APIAction(f: Request[AnyContent] => Result): Action[AnyContent] =
Action { request =>
Try(f(request))
.getOrElse(
InternalServerError(Json.obj("code" -> "500", "message" -> "Server error"))
)
}
}
Please help me to implement Try/Catch functionality in Web socket
Login request is handled within actor and I think you should handle your errors there. If you want to catch all exceptions instead of explicitly handling what could go wrong, I suggest doing the following
object SomeUtils {
def catchAll[A](out: ActorRef)(f: => A): Unit = {
val message = Try(f).getOrElse(Json.obj("code" -> "500", "message" -> "Server error"))
out ! message
}
}
import SomeUtils._
class LoginWS(out: ActorRef) extends Actor {
def receive = {
case json_req: JsObject => catchAll(out) {
val userName = (json_req \ "user_name").as[String]
val password = (json_req \ "password").as[String]
val authenticated = UserLogin.authenticateUser(userName, password).isDefined
if (authenticated) {
val role = UserLogin.getUserRole(userName, password)
Json.obj("login_status" -> "Success", "result" -> role)
}
else {
Json.obj("login_status" -> "Failure", "result" -> 0)
}
}
}
}
When handling jsonReq use catchAll method that would expect to get a receiver of the result and this result that could throw an Exception. In case of Exception it would use a default message with internal server error, and send this message to the receiver.
You could also make the out parameter implicit to skip putting it everywhere. Also your code is not really in scala style. Using vars etc...
If json sent to WS couldn't be parsed it would throw exception before reaching your code, solution can be found here:
How do I catch json parse error when using acceptWithActor?

Play 2.2.2-WebSocket / Equivalent of in.onClose() in Scala

I use Play 2.2.2 with Scala.
I have this code in my controller:
def wsTest = WebSocket.using[JsValue] {
implicit request =>
val (out, channel) = Concurrent.broadcast[JsValue]
val in = Iteratee.foreach[JsValue] {
msg => println(msg)
}
userAuthenticatorRequest.tracked match { //detecting wheter the user is authenticated
case Some(u) =>
mySubscriber.start(u.id, channel)
case _ =>
channel push Json.toJson("{error: Sorry, you aren't authenticated yet}")
}
(in, out)
}
calling this code:
object MySubscriber {
def start(userId: String, channel: Concurrent.Channel[JsValue]) {
ctx.getBean(classOf[ActorSystem]).actorOf(Props(classOf[MySubscriber], Seq("comment"), channel), name = "mySubscriber") ! "start"
//a simple refresh would involve a duplication of this actor!
}
}
class MySubscriber(redisChannels: Seq[String], channel: Concurrent.Channel[JsValue]) extends RedisSubscriberActor(new InetSocketAddress("localhost", 6379), redisChannels, Nil) with ActorLogging {
def onMessage(message: Message) {
println(s"message received: $message")
channel.push(Json.parse(message.data))
}
override def onPMessage(pmessage: PMessage) {
//not used
println(s"message received: $pmessage")
}
}
The problem is that when the user refreshes the page, then a new websocket restarts involving a duplication of Actors named mySubscriber.
I noticed that the Play's Java version has a way to detect a closed connection, in order to shutdown an actor.
Example:
// When the socket is closed.
in.onClose(new Callback0() {
public void invoke() {
// Shutdown the actor
defaultRoom.shutdown();
}
});
How to handle the same thing with the Scala WebSocket API? I want to close the actor each time the socket is closed.
As #Mik378 suggested, Iteratee.map serves the role of onClose.
val in = Iteratee.foreach[JsValue] {
msg => println(msg)
} map { _ =>
println("Connection has closed")
}