View difference between Paste ID: mNVi509p and 8xDerhbk
SHOW: | | - or go back to the newest paste.
1
package com.vk.doge.shibu.akkarin.actors
2
3
import akka.actor.Actor
4
import akka.actor._
5
import akka.pattern._
6
import com.vk.doge.shibu.akkarin.Vk._
7
import com.vk.doge.shibu.akkarin.actors.LongPollingActor._
8
import com.vk.doge.shibu.akkarin.Vk
9
import com.vk.doge.shibu.akkarin.messages.Messages._
10
import org.json4s.JsonAST.{JString, JInt, JArray, JObject}
11
import scala.util.{Failure, Success}
12
13
14
class LongPollingActor extends Actor {
15
  import context._
16
17
  def receive: Actor.Receive = {
18
    case Start(token, listener) =>
19
      new Vk(token).longPolling().onComplete {
20
        case Success(Some(Vk.LongPollResponse(key,server,firstTs))) =>
21
          become {
22
23
            case Stop() =>
24
              context.stop(self)
25
26
            case RequestLongPoll(ts) =>
27
              longPoll(LongPollResponse(key,server,ts)) foreach {
28
                case Some(JObject(List(("ts",JInt(value)),("updates",JArray(values))))) =>
29
                  values
30
                    .collect {
31
                      case JArray(List(JInt(bigInt),_,_,_,_,_,JString(text),attachments))
32
                        if bigInt == BigInt(4) =>
33
                        attachments \ "from" match {
34
                          case JString(fromId) =>
35
                            Message(fromId.toLong,text)
36
                          case _ =>
37
                        }
38
                      case JArray(List(JInt(bigInt),_,_,JInt(fromId),_,_,JString(text),_)) if bigInt == BigInt(4) =>
39
                        Message(fromId.toLong,text)
40
                    }
41
                    .foreach { message => listener ! message }
42
                  self ! RequestLongPoll(value.toString())
43
44-
                case other =>
44+
                case _ =>
45
                  listener ! ConnectionOrDeserializationError()
46
                  context.stop(self)
47
              }
48
          }
49
50
          self ! RequestLongPoll(firstTs)
51
52-
        case Success(other) =>
52+
        case _ =>
53-
        case Failure(exp) =>
53+
54
          context.stop(self)
55
      }
56
  }
57
}
58
59
object LongPollingActor {
60
  case class Start(token : String, listener : ActorRef)
61
  case class Stop()
62
63
  case class ConnectionOrDeserializationError()
64
65
  private case class RequestLongPoll(ts : String)
66
}