-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathRelaySelectorWrapper.swift
61 lines (51 loc) · 1.7 KB
/
RelaySelectorWrapper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// RelaySelectorWrapper.swift
// PacketTunnel
//
// Created by pronebird on 08/08/2023.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import MullvadTypes
public final class RelaySelectorWrapper: RelaySelectorProtocol {
let relayCache: RelayCacheProtocol
let tunnelSettingsUpdater: SettingsUpdater
private var tunnelSettings = LatestTunnelSettings()
private var observer: SettingsObserverBlock!
deinit {
self.tunnelSettingsUpdater.removeObserver(observer)
}
public init(
relayCache: RelayCacheProtocol,
tunnelSettingsUpdater: SettingsUpdater
) {
self.relayCache = relayCache
self.tunnelSettingsUpdater = tunnelSettingsUpdater
self.addObserver()
}
public func selectRelays(
connectionAttemptCount: UInt
) throws -> SelectedRelays {
let relays = try relayCache.read().relays
switch tunnelSettings.tunnelMultihopState {
case .off:
return try SinglehopPicker(
constraints: tunnelSettings.relayConstraints,
relays: relays,
connectionAttemptCount: connectionAttemptCount
).pick()
case .on:
return try MultihopPicker(
constraints: tunnelSettings.relayConstraints,
relays: relays,
connectionAttemptCount: connectionAttemptCount
).pick()
}
}
private func addObserver() {
self.observer = SettingsObserverBlock(didUpdateSettings: { [weak self] latestTunnelSettings in
self?.tunnelSettings = latestTunnelSettings
})
tunnelSettingsUpdater.addObserver(observer)
}
}