mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
3.3 KiB
Swift
84 lines
3.3 KiB
Swift
6 years ago
|
import PromiseKit
|
||
|
|
||
5 years ago
|
@objc(LKPoller)
|
||
|
public final class LokiPoller : NSObject {
|
||
6 years ago
|
private let onMessagesReceived: ([SSKProtoEnvelope]) -> Void
|
||
|
private let storage = OWSPrimaryStorage.shared()
|
||
|
private var hasStarted = false
|
||
|
private var hasStopped = false
|
||
|
private var usedSnodes = Set<LokiAPITarget>()
|
||
|
|
||
|
// MARK: Settings
|
||
5 years ago
|
private static let pollInterval: TimeInterval = 1
|
||
|
private static let retryInterval: TimeInterval = 4
|
||
6 years ago
|
|
||
|
// MARK: Initialization
|
||
6 years ago
|
@objc public init(onMessagesReceived: @escaping ([SSKProtoEnvelope]) -> Void) {
|
||
6 years ago
|
self.onMessagesReceived = onMessagesReceived
|
||
|
super.init()
|
||
|
}
|
||
|
|
||
|
// MARK: Public API
|
||
6 years ago
|
@objc public func startIfNeeded() {
|
||
6 years ago
|
guard !hasStarted else { return }
|
||
5 years ago
|
print("[Loki] Started polling.")
|
||
6 years ago
|
hasStarted = true
|
||
|
hasStopped = false
|
||
|
openConnections()
|
||
|
}
|
||
|
|
||
6 years ago
|
@objc public func stopIfNeeded() {
|
||
6 years ago
|
guard !hasStopped else { return }
|
||
5 years ago
|
print("[Loki] Stopped polling.")
|
||
6 years ago
|
hasStarted = false
|
||
|
hasStopped = true
|
||
|
usedSnodes.removeAll()
|
||
|
}
|
||
|
|
||
|
// MARK: Private API
|
||
|
private func openConnections() {
|
||
|
guard !hasStopped else { return }
|
||
5 years ago
|
LokiAPI.getSwarm(for: getUserHexEncodedPublicKey()).then { [weak self] _ -> Promise<Void> in
|
||
|
guard let strongSelf = self else { return Promise { $0.fulfill(()) } }
|
||
6 years ago
|
strongSelf.usedSnodes.removeAll()
|
||
5 years ago
|
let (promise, seal) = Promise<Void>.pending()
|
||
|
strongSelf.pollNextSnode(seal: seal)
|
||
|
return promise
|
||
5 years ago
|
}.ensure { [weak self] in
|
||
6 years ago
|
guard let strongSelf = self else { return }
|
||
5 years ago
|
Timer.scheduledTimer(withTimeInterval: LokiPoller.retryInterval, repeats: false) { _ in
|
||
6 years ago
|
guard let strongSelf = self else { return }
|
||
|
strongSelf.openConnections()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
private func pollNextSnode(seal: Resolver<Void>) {
|
||
|
let userHexEncodedPublicKey = getUserHexEncodedPublicKey()
|
||
6 years ago
|
let swarm = LokiAPI.swarmCache[userHexEncodedPublicKey] ?? []
|
||
|
let unusedSnodes = Set(swarm).subtracting(usedSnodes)
|
||
|
if !unusedSnodes.isEmpty {
|
||
5 years ago
|
// randomElement() uses the system's default random generator, which is cryptographically secure
|
||
6 years ago
|
let nextSnode = unusedSnodes.randomElement()!
|
||
|
usedSnodes.insert(nextSnode)
|
||
5 years ago
|
print("[Loki] Polling \(nextSnode).")
|
||
|
poll(nextSnode, seal: seal).catch(on: LokiAPI.errorHandlingQueue) { [weak self] error in
|
||
|
print("[Loki] Polling \(nextSnode) failed; dropping it and switching to next snode.")
|
||
6 years ago
|
LokiAPI.dropIfNeeded(nextSnode, hexEncodedPublicKey: userHexEncodedPublicKey)
|
||
5 years ago
|
self?.pollNextSnode(seal: seal)
|
||
6 years ago
|
}
|
||
|
} else {
|
||
|
seal.fulfill(())
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
private func poll(_ target: LokiAPITarget, seal: Resolver<Void>) -> Promise<Void> {
|
||
|
return LokiAPI.getRawMessages(from: target, usingLongPolling: false).then(on: DispatchQueue.global()) { [weak self] rawResponse -> Promise<Void> in
|
||
6 years ago
|
guard let strongSelf = self, !strongSelf.hasStopped else { return Promise.value(()) }
|
||
|
let messages = LokiAPI.parseRawMessagesResponse(rawResponse, from: target)
|
||
5 years ago
|
strongSelf.onMessagesReceived(messages)
|
||
5 years ago
|
return strongSelf.poll(target, seal: seal)
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|