Advertisement
Guest User

longpoll actor

a guest
Dec 27th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.63 KB | None | 0 0
  1. package com.github.dogeshibu.akkarin.actors
  2.  
  3. import akka.actor.Actor
  4. import akka.actor._
  5. import akka.pattern._
  6. import com.github.dogeshibu.akkarin.Vk
  7. import Vk._
  8. import com.github.dogeshibu.akkarin.actors.LongPollingActor._
  9. import com.github.dogeshibu.akkarin.messages.Messages.Message
  10. import org.json4s.JsonAST.{JString, JInt, JArray, JObject}
  11. import scala.util.{Failure, Success}
  12. import com.github.dogeshibu.akkarin.Vk
  13. import com.github.dogeshibu.akkarin.Vk.LongPolling._
  14.  
  15.  
  16. class LongPollingActor extends Actor {
  17.   import context._
  18.  
  19.   def receive: Actor.Receive = {
  20.     case Start(token, listener) =>
  21.  
  22.       val vk = new Vk(token)
  23.  
  24.       vk.longPolling().onComplete {
  25.         case Success(Some(LongPollResponse(key,server,firstTs))) =>
  26.           become {
  27.  
  28.             case Stop() =>
  29.               context.stop(self)
  30.  
  31.             case RequestLongPoll(ts) =>
  32.               vk.longPoll(LongPollResponse(key,server,ts)) collect {
  33.  
  34.                 case Some((newTs,messages)) =>
  35.                   messages foreach (listener ! _)
  36.                   self ! RequestLongPoll(newTs)
  37.  
  38.                 case other =>
  39.                   listener ! ConnectionOrDeserializationError()
  40.                   context.stop(self)
  41.  
  42.               }
  43.           }
  44.  
  45.           self ! RequestLongPoll(firstTs)
  46.  
  47.         case Success(other) =>
  48.         case Failure(exp) =>
  49.           listener ! ConnectionOrDeserializationError()
  50.       }
  51.   }
  52. }
  53.  
  54. object LongPollingActor {
  55.   case class Start(token : String, listener : ActorRef)
  56.   case class Stop()
  57.  
  58.   case class ConnectionOrDeserializationError()
  59.  
  60.   private case class RequestLongPoll(ts : String)
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement