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.
119 lines
4.5 KiB
Swift
119 lines
4.5 KiB
Swift
6 years ago
|
import PromiseKit
|
||
|
|
||
|
private typealias Callback = () -> Void
|
||
|
|
||
|
public extension LokiAPI {
|
||
|
private static var isLongPolling = false
|
||
|
private static var shouldStopPolling = false
|
||
6 years ago
|
private static var usedSnodes = [LokiAPITarget]()
|
||
6 years ago
|
private static var cancels = [Callback]()
|
||
|
|
||
|
private static let hexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
|
||
|
|
||
|
/// Start long polling.
|
||
|
/// This will send a notification if new messages were received
|
||
|
@objc public static func startLongPollingIfNecessary() {
|
||
|
guard !isLongPolling else { return }
|
||
|
isLongPolling = true
|
||
|
shouldStopPolling = false
|
||
|
|
||
|
Logger.info("[Loki] Started long polling")
|
||
|
|
||
|
longPoll()
|
||
|
}
|
||
|
|
||
|
/// Stop long polling
|
||
|
@objc public static func stopLongPolling() {
|
||
|
shouldStopPolling = true
|
||
|
isLongPolling = false
|
||
|
usedSnodes.removeAll()
|
||
|
cancelAllPromises()
|
||
|
|
||
|
Logger.info("[Loki] Stopped long polling")
|
||
|
}
|
||
|
|
||
|
/// The long polling loop
|
||
|
private static func longPoll() {
|
||
|
// This is here so we can stop the infinite loop
|
||
|
guard !shouldStopPolling else { return }
|
||
|
|
||
6 years ago
|
getSwarm(for: hexEncodedPublicKey).then { _ -> Guarantee<[Result<Void>]> in
|
||
6 years ago
|
var promises = [Promise<Void>]()
|
||
|
let connections = 3
|
||
|
for i in 0..<connections {
|
||
|
let (promise, cancel) = openConnection()
|
||
|
promises.append(promise)
|
||
|
cancels.append(cancel)
|
||
|
}
|
||
|
return when(resolved: promises)
|
||
|
}.done { _ in
|
||
|
// Since all promises are complete, we can clear the cancels
|
||
|
cancelAllPromises()
|
||
|
|
||
|
// Keep long polling until it is stopped
|
||
|
longPoll()
|
||
|
}.retainUntilComplete()
|
||
|
}
|
||
|
|
||
|
private static func cancelAllPromises() {
|
||
|
cancels.forEach { cancel in cancel() }
|
||
|
cancels.removeAll()
|
||
|
}
|
||
|
|
||
6 years ago
|
private static func getUnusedSnodes() -> [LokiAPITarget] {
|
||
6 years ago
|
let snodes = LokiAPI.swarmCache[hexEncodedPublicKey] ?? []
|
||
6 years ago
|
return snodes.filter { !usedSnodes.contains($0) }
|
||
|
}
|
||
|
|
||
|
/// Open a connection to an unused snode and get messages from it
|
||
|
private static func openConnection() -> (Promise<Void>, cancel: Callback) {
|
||
|
var isCancelled = false
|
||
|
|
||
|
let cancel = {
|
||
|
isCancelled = true
|
||
|
}
|
||
|
|
||
|
func connectToNextSnode() -> Promise<Void> {
|
||
|
guard let nextSnode = getUnusedSnodes().first else {
|
||
|
// We don't have anymore unused snodes
|
||
|
return Promise.value(())
|
||
|
}
|
||
|
|
||
|
// Add the snode to the used array
|
||
|
usedSnodes.append(nextSnode)
|
||
|
|
||
6 years ago
|
func getMessagesInfinitely(from target: LokiAPITarget) -> Promise<Void> {
|
||
6 years ago
|
// The only way to exit the infinite loop is to throw an error 3 times or cancel
|
||
6 years ago
|
return getRawMessages(from: target, useLongPolling: true).then { rawResponse -> Promise<Void> in
|
||
6 years ago
|
// Check if we need to abort
|
||
|
guard !isCancelled else { throw PMKError.cancelled }
|
||
|
|
||
6 years ago
|
// Process the messages
|
||
6 years ago
|
let messages = parseRawMessagesResponse(rawResponse, from: target)
|
||
6 years ago
|
|
||
|
// Send our messages as a notification
|
||
6 years ago
|
NotificationCenter.default.post(name: .newMessagesReceived, object: nil, userInfo: ["messages": messages])
|
||
6 years ago
|
|
||
6 years ago
|
// Continue fetching if we haven't cancelled
|
||
|
return getMessagesInfinitely(from: target)
|
||
|
}.retryingIfNeeded(maxRetryCount: 3)
|
||
|
}
|
||
|
|
||
|
// Keep getting messages for this snode
|
||
|
// If we errored out then connect to the next snode
|
||
|
return getMessagesInfinitely(from: nextSnode).recover { _ -> Promise<Void> in
|
||
|
// Cancelled, so just return successfully
|
||
6 years ago
|
guard !isCancelled else { return Promise.value(()) }
|
||
|
|
||
|
// Connect to the next snode if we haven't cancelled
|
||
|
// We also need to remove the cached snode so we don't contact it again
|
||
6 years ago
|
dropIfNeeded(nextSnode, hexEncodedPublicKey: hexEncodedPublicKey)
|
||
6 years ago
|
return connectToNextSnode()
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Keep connecting to snodes
|
||
|
return (connectToNextSnode(), cancel)
|
||
|
}
|
||
|
}
|