|
|
|
@ -9,7 +9,6 @@ public final class LokiPoller : NSObject {
|
|
|
|
|
private var usedSnodes = Set<LokiAPITarget>()
|
|
|
|
|
|
|
|
|
|
// MARK: Settings
|
|
|
|
|
private static let pollInterval: TimeInterval = 1
|
|
|
|
|
private static let retryInterval: TimeInterval = 4
|
|
|
|
|
|
|
|
|
|
// MARK: Initialization
|
|
|
|
@ -24,7 +23,7 @@ public final class LokiPoller : NSObject {
|
|
|
|
|
print("[Loki] Started polling.")
|
|
|
|
|
hasStarted = true
|
|
|
|
|
hasStopped = false
|
|
|
|
|
openConnections()
|
|
|
|
|
setUpPolling()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc public func stopIfNeeded() {
|
|
|
|
@ -36,7 +35,7 @@ public final class LokiPoller : NSObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Private API
|
|
|
|
|
private func openConnections() {
|
|
|
|
|
private func setUpPolling() {
|
|
|
|
|
guard !hasStopped else { return }
|
|
|
|
|
LokiAPI.getSwarm(for: getUserHexEncodedPublicKey()).then { [weak self] _ -> Promise<Void> in
|
|
|
|
|
guard let strongSelf = self else { return Promise { $0.fulfill(()) } }
|
|
|
|
@ -45,10 +44,10 @@ public final class LokiPoller : NSObject {
|
|
|
|
|
strongSelf.pollNextSnode(seal: seal)
|
|
|
|
|
return promise
|
|
|
|
|
}.ensure { [weak self] in
|
|
|
|
|
guard let strongSelf = self else { return }
|
|
|
|
|
guard let strongSelf = self, !strongSelf.hasStopped else { return }
|
|
|
|
|
Timer.scheduledTimer(withTimeInterval: LokiPoller.retryInterval, repeats: false) { _ in
|
|
|
|
|
guard let strongSelf = self else { return }
|
|
|
|
|
strongSelf.openConnections()
|
|
|
|
|
strongSelf.setUpPolling()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -62,7 +61,9 @@ public final class LokiPoller : NSObject {
|
|
|
|
|
let nextSnode = unusedSnodes.randomElement()!
|
|
|
|
|
usedSnodes.insert(nextSnode)
|
|
|
|
|
print("[Loki] Polling \(nextSnode).")
|
|
|
|
|
poll(nextSnode, seal: seal).catch(on: LokiAPI.errorHandlingQueue) { [weak self] error in
|
|
|
|
|
poll(nextSnode, seal: seal).done(on: DispatchQueue.global()) {
|
|
|
|
|
seal.fulfill(())
|
|
|
|
|
}.catch(on: LokiAPI.errorHandlingQueue) { [weak self] error in
|
|
|
|
|
print("[Loki] Polling \(nextSnode) failed; dropping it and switching to next snode.")
|
|
|
|
|
LokiAPI.dropIfNeeded(nextSnode, hexEncodedPublicKey: userHexEncodedPublicKey)
|
|
|
|
|
self?.pollNextSnode(seal: seal)
|
|
|
|
@ -72,12 +73,12 @@ public final class LokiPoller : NSObject {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func poll(_ target: LokiAPITarget, seal: Resolver<Void>) -> Promise<Void> {
|
|
|
|
|
private func poll(_ target: LokiAPITarget, seal longTermSeal: Resolver<Void>) -> Promise<Void> {
|
|
|
|
|
return LokiAPI.getRawMessages(from: target, usingLongPolling: false).then(on: DispatchQueue.global()) { [weak self] rawResponse -> Promise<Void> in
|
|
|
|
|
guard let strongSelf = self, !strongSelf.hasStopped else { return Promise.value(()) }
|
|
|
|
|
guard let strongSelf = self, !strongSelf.hasStopped else { return Promise { $0.fulfill(()) } }
|
|
|
|
|
let messages = LokiAPI.parseRawMessagesResponse(rawResponse, from: target)
|
|
|
|
|
strongSelf.onMessagesReceived(messages)
|
|
|
|
|
return strongSelf.poll(target, seal: seal)
|
|
|
|
|
return strongSelf.poll(target, seal: longTermSeal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|