Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.github.dogeshibu.akkarin.actors
- import akka.actor.Actor
- import akka.actor._
- import akka.pattern._
- import com.github.dogeshibu.akkarin.Vk
- import Vk._
- import com.github.dogeshibu.akkarin.actors.LongPollingActor._
- import com.github.dogeshibu.akkarin.messages.Messages.Message
- import org.json4s.JsonAST.{JString, JInt, JArray, JObject}
- import scala.util.{Failure, Success}
- import com.github.dogeshibu.akkarin.Vk
- import com.github.dogeshibu.akkarin.Vk.LongPolling._
- class LongPollingActor extends Actor {
- import context._
- def receive: Actor.Receive = {
- case Start(token, listener) =>
- val vk = new Vk(token)
- vk.longPolling().onComplete {
- case Success(Some(LongPollResponse(key,server,firstTs))) =>
- become {
- case Stop() =>
- context.stop(self)
- case RequestLongPoll(ts) =>
- vk.longPoll(LongPollResponse(key,server,ts)) collect {
- case Some((newTs,messages)) =>
- messages foreach (listener ! _)
- self ! RequestLongPoll(newTs)
- case other =>
- listener ! ConnectionOrDeserializationError()
- context.stop(self)
- }
- }
- self ! RequestLongPoll(firstTs)
- case Success(other) =>
- case Failure(exp) =>
- listener ! ConnectionOrDeserializationError()
- }
- }
- }
- object LongPollingActor {
- case class Start(token : String, listener : ActorRef)
- case class Stop()
- case class ConnectionOrDeserializationError()
- private case class RequestLongPoll(ts : String)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement