-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide State and expose Observed State instead
- Loading branch information
1 parent
b1271e7
commit 5c13b7e
Showing
13 changed files
with
201 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// ObservedState+Extensions.swift | ||
// PacketTunnelCore | ||
// | ||
// Created by pronebird on 16/10/2023. | ||
// Copyright © 2023 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MullvadTypes | ||
|
||
extension ObservedState { | ||
public var packetTunnelStatus: PacketTunnelStatus { | ||
var status = PacketTunnelStatus() | ||
|
||
switch self { | ||
case let .connecting(connState), | ||
let .connected(connState), | ||
let .reconnecting(connState), | ||
let .disconnecting(connState): | ||
switch connState.networkReachability { | ||
case .reachable: | ||
status.isNetworkReachable = true | ||
case .unreachable: | ||
status.isNetworkReachable = false | ||
case .undetermined: | ||
// TODO: fix me | ||
status.isNetworkReachable = true | ||
} | ||
|
||
status.numberOfFailedAttempts = connState.connectionAttemptCount | ||
status.tunnelRelay = connState.selectedRelay.packetTunnelRelay | ||
|
||
case .disconnected, .initial: | ||
break | ||
|
||
case let .error(blockedState): | ||
status.blockedStateReason = blockedState.reason | ||
} | ||
|
||
return status | ||
} | ||
|
||
public var relayConstraints: RelayConstraints? { | ||
switch self { | ||
case let .connecting(connState), let .connected(connState), let .reconnecting(connState): | ||
return connState.relayConstraints | ||
|
||
case let .error(blockedState): | ||
return blockedState.relayConstraints | ||
|
||
case .initial, .disconnecting, .disconnected: | ||
return nil | ||
} | ||
} | ||
|
||
public var name: String { | ||
switch self { | ||
case .connected: | ||
return "Connected" | ||
case .connecting: | ||
return "Connecting" | ||
case .reconnecting: | ||
return "Reconnecting" | ||
case .disconnecting: | ||
return "Disconnecting" | ||
case .disconnected: | ||
return "Disconnected" | ||
case .initial: | ||
return "Initial" | ||
case .error: | ||
return "Error" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// ObservedState.swift | ||
// PacketTunnelCore | ||
// | ||
// Created by pronebird on 11/10/2023. | ||
// Copyright © 2023 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import MullvadTypes | ||
import Network | ||
|
||
/// A serializable representation of internal state. | ||
public enum ObservedState: Equatable, Codable { | ||
case initial | ||
case connecting(ObservedConnectionState) | ||
case reconnecting(ObservedConnectionState) | ||
case connected(ObservedConnectionState) | ||
case disconnecting(ObservedConnectionState) | ||
case disconnected | ||
case error(ObservedBlockedState) | ||
} | ||
|
||
/// A serializable representation of internal connection state. | ||
public struct ObservedConnectionState: Equatable, Codable { | ||
public var selectedRelay: SelectedRelay | ||
public var relayConstraints: RelayConstraints | ||
public var networkReachability: NetworkReachability | ||
public var connectionAttemptCount: UInt | ||
} | ||
|
||
/// A serializable representation of internal blocked state. | ||
public struct ObservedBlockedState: Equatable, Codable { | ||
public var reason: BlockedStateReason | ||
public var relayConstraints: RelayConstraints? | ||
} | ||
|
||
extension State { | ||
/// Map `State` to `ObservedState`. | ||
var observedState: ObservedState { | ||
switch self { | ||
case .initial: | ||
return .initial | ||
case let .connecting(connState): | ||
return .connecting(connState.observedConnectionState) | ||
case let .connected(connState): | ||
return .connected(connState.observedConnectionState) | ||
case let .reconnecting(connState): | ||
return .reconnecting(connState.observedConnectionState) | ||
case let .disconnecting(connState): | ||
return .disconnecting(connState.observedConnectionState) | ||
case .disconnected: | ||
return .disconnected | ||
case let .error(blockedState): | ||
return .error(blockedState.observedBlockedState) | ||
} | ||
} | ||
} | ||
|
||
extension ConnectionState { | ||
/// Map `ConnectionState` to `ObservedConnectionState`. | ||
var observedConnectionState: ObservedConnectionState { | ||
ObservedConnectionState( | ||
selectedRelay: selectedRelay, | ||
relayConstraints: relayConstraints, | ||
networkReachability: networkReachability, | ||
connectionAttemptCount: connectionAttemptCount | ||
) | ||
} | ||
} | ||
|
||
extension BlockedState { | ||
/// Map `BlockedState` to `ObservedBlockedState` | ||
var observedBlockedState: ObservedBlockedState { | ||
return ObservedBlockedState(reason: reason, relayConstraints: relayConstraints) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.