Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vk.doge.shibu.akkarin.actors
- import akka.actor.Actor
- import akka.actor._
- import akka.pattern._
- import com.vk.doge.shibu.akkarin.Vk._
- import com.vk.doge.shibu.akkarin.actors.LongPollingActor._
- import com.vk.doge.shibu.akkarin.Vk
- import com.vk.doge.shibu.akkarin.messages.Messages._
- import org.json4s.JsonAST.{JString, JInt, JArray, JObject}
- import scala.util.{Failure, Success}
- class LongPollingActor extends Actor {
- import context._
- def receive: Actor.Receive = {
- case Start(token, listener) =>
- new Vk(token).longPolling().onComplete {
- case Success(Some(Vk.LongPollResponse(key,server,firstTs))) =>
- become {
- case Stop() =>
- context.stop(self)
- case RequestLongPoll(ts) =>
- longPoll(LongPollResponse(key,server,ts)) foreach {
- case Some(JObject(List(("ts",JInt(value)),("updates",JArray(values))))) =>
- values
- .collect {
- case JArray(List(JInt(bigInt),_,_,_,_,_,JString(text),attachments))
- if bigInt == BigInt(4) =>
- attachments \ "from" match {
- case JString(fromId) =>
- Message(fromId.toLong,text)
- case _ =>
- }
- case JArray(List(JInt(bigInt),_,_,JInt(fromId),_,_,JString(text),_)) if bigInt == BigInt(4) =>
- Message(fromId.toLong,text)
- }
- .foreach { message => listener ! message }
- self ! RequestLongPoll(value.toString())
- case _ =>
- listener ! ConnectionOrDeserializationError()
- context.stop(self)
- }
- }
- self ! RequestLongPoll(firstTs)
- case _ =>
- listener ! ConnectionOrDeserializationError()
- context.stop(self)
- }
- }
- }
- 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