Rename P2PDetails → P2PState

pull/26/head
Niels Andriesse 6 years ago
parent a11074ba5a
commit 209996f86d

@ -67,11 +67,12 @@ import PromiseKit
return Set(swarm.map { sendLokiMessage(lokiMessageWithPoW, to: $0) }) return Set(swarm.map { sendLokiMessage(lokiMessageWithPoW, to: $0) })
} }
} }
if let p2pDetails = LokiP2PManager.getDetails(forContact: destination), (lokiMessage.isPing || p2pDetails.isOnline) { if let p2pState = LokiP2PManager.getState(for: destination), (lokiMessage.isPing || p2pState.isOnline) {
return Promise.value([ p2pDetails.target ]).mapValues { sendLokiMessage(lokiMessage, to: $0) }.map { Set($0) }.retryingIfNeeded(maxRetryCount: maxRetryCount).get { _ in let target = Target(address: p2pState.address, port: p2pState.port)
LokiP2PManager.setOnline(true, forContact: destination) return Promise.value([ target ]).mapValues { sendLokiMessage(lokiMessage, to: $0) }.map { Set($0) }.retryingIfNeeded(maxRetryCount: maxRetryCount).get { _ in
LokiP2PManager.markOnline(destination)
}.recover { error -> Promise<Set<RawResponsePromise>> in }.recover { error -> Promise<Set<RawResponsePromise>> in
LokiP2PManager.setOnline(false, forContact: destination) LokiP2PManager.markOffline(destination)
if lokiMessage.isPing { if lokiMessage.isPing {
Logger.warn("[Loki] Failed to ping \(destination); marking contact as offline.") Logger.warn("[Loki] Failed to ping \(destination); marking contact as offline.")
if let nsError = error as? NSError { if let nsError = error as? NSError {

@ -8,23 +8,19 @@
private static let offlinePingTime = 2 * kMinuteInterval private static let offlinePingTime = 2 * kMinuteInterval
/// A p2p state struct /// A p2p state struct
internal struct P2PDetails { internal struct P2PState {
var address: String var address: String
var port: UInt16 var port: UInt16
var isOnline: Bool var isOnline: Bool
var timerDuration: Double var timerDuration: Double
var pingTimer: Timer? = nil var pingTimer: Timer? = nil
var target: LokiAPI.Target {
return LokiAPI.Target(address: address, port: port)
}
} }
/// Our p2p address /// Our p2p address
private static var ourP2PAddress: LokiAPI.Target? = nil private static var ourP2PAddress: LokiAPI.Target? = nil
/// This is where we store the p2p details of our contacts /// This is where we store the p2p details of our contacts
private static var contactP2PDetails = [String: P2PDetails]() private static var contactP2PStates = [String:P2PState]()
// MARK: - Public functions // MARK: - Public functions
@ -81,8 +77,8 @@
/// ///
/// - Parameter pubKey: The contact hex pubkey /// - Parameter pubKey: The contact hex pubkey
/// - Returns: The P2P Details or nil if they don't exist /// - Returns: The P2P Details or nil if they don't exist
internal static func getDetails(forContact pubKey: String) -> P2PDetails? { internal static func getState(for hexEncodedPublicKey: String) -> P2PState? {
return contactP2PDetails[pubKey] return contactP2PStates[hexEncodedPublicKey]
} }
/// Get the `LokiAddressMessage` for the given thread. /// Get the `LokiAddressMessage` for the given thread.
@ -105,12 +101,12 @@
let timerDuration = pubKey < ourHexEncodedPubKey ? 1 * kMinuteInterval : 2 * kMinuteInterval let timerDuration = pubKey < ourHexEncodedPubKey ? 1 * kMinuteInterval : 2 * kMinuteInterval
// Get out current contact details // Get out current contact details
let oldContactDetails = contactP2PDetails[pubKey] let oldContactDetails = contactP2PStates[pubKey]
// Set the new contact details // Set the new contact details
// A contact is always assumed to be offline unless the specific conditions below are met // A contact is always assumed to be offline unless the specific conditions below are met
let details = P2PDetails(address: address, port: port, isOnline: false, timerDuration: timerDuration, pingTimer: nil) let details = P2PState(address: address, port: port, isOnline: false, timerDuration: timerDuration, pingTimer: nil)
contactP2PDetails[pubKey] = details contactP2PStates[pubKey] = details
// Set up our checks // Set up our checks
let oldContactExists = oldContactDetails != nil let oldContactExists = oldContactDetails != nil
@ -141,15 +137,23 @@
ping(contact: pubKey) ping(contact: pubKey)
} }
internal static func markOnline(_ hexEncodedPublicKey: String) {
setOnline(true, forContact: hexEncodedPublicKey)
}
internal static func markOffline(_ hexEncodedPublicKey: String) {
setOnline(false, forContact: hexEncodedPublicKey)
}
/// Mark a contact as online or offline. /// Mark a contact as online or offline.
/// ///
/// - Parameters: /// - Parameters:
/// - isOnline: Whether to set the contact to online or offline. /// - isOnline: Whether to set the contact to online or offline.
/// - pubKey: The contact hexh pubKey /// - pubKey: The contact hex pubKey
@objc internal static func setOnline(_ isOnline: Bool, forContact pubKey: String) { @objc internal static func setOnline(_ isOnline: Bool, forContact pubKey: String) {
// Make sure we are on the main thread // Make sure we are on the main thread
DispatchQueue.main.async { DispatchQueue.main.async {
guard var details = contactP2PDetails[pubKey] else { return } guard var details = contactP2PStates[pubKey] else { return }
let interval = isOnline ? details.timerDuration : offlinePingTime let interval = isOnline ? details.timerDuration : offlinePingTime
@ -158,7 +162,7 @@
details.pingTimer = WeakTimer.scheduledTimer(timeInterval: interval, target: self, userInfo: nil, repeats: true) { _ in ping(contact: pubKey) } details.pingTimer = WeakTimer.scheduledTimer(timeInterval: interval, target: self, userInfo: nil, repeats: true) { _ in ping(contact: pubKey) }
details.isOnline = isOnline details.isOnline = isOnline
contactP2PDetails[pubKey] = details contactP2PStates[pubKey] = details
} }
} }

Loading…
Cancel
Save