-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientManager.swift
More file actions
150 lines (126 loc) · 3.84 KB
/
ClientManager.swift
File metadata and controls
150 lines (126 loc) · 3.84 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// ClientManager.swift
// MistTray
//
import Foundation
class ClientManager {
static let shared = ClientManager()
private init() {}
// MARK: - Client Operations
func disconnectClient(sessionId: String, completion: @escaping (Result<Void, APIError>) -> Void) {
APIClient.shared.disconnectClient(sessionId: sessionId) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
}
func kickAllViewers(streamName: String, completion: @escaping (Result<Void, APIError>) -> Void) {
print("ClientManager: Kicking all viewers from stream '\(streamName)'")
APIClient.shared.kickAllViewers(streamName: streamName) { result in
DispatchQueue.main.async {
switch result {
case .success:
print("All viewers kicked successfully")
completion(.success(()))
case .failure(let error):
print("Failed to kick viewers: \(error)")
completion(.failure(error))
}
}
}
}
func forceReauthentication(
streamName: String, completion: @escaping (Result<Void, APIError>) -> Void
) {
print("ClientManager: Forcing reauthentication for stream '\(streamName)'")
APIClient.shared.forceReauthentication(streamName: streamName) { result in
DispatchQueue.main.async {
switch result {
case .success:
print("Reauthentication forced successfully")
completion(.success(()))
case .failure(let error):
print("Failed to force reauthentication: \(error)")
completion(.failure(error))
}
}
}
}
func tagSession(
sessionId: String, tag: String, completion: @escaping (Result<Void, APIError>) -> Void
) {
APIClient.shared.tagSession(sessionId: sessionId, tag: tag) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
}
// MARK: - Session Tagging
func tagSessions(
sessionIds: [String], tag: String, completion: @escaping (Result<Int, Error>) -> Void
) {
print("Tagging sessions: \(sessionIds) with tag: \(tag)")
guard !sessionIds.isEmpty else {
completion(.success(0))
return
}
let group = DispatchGroup()
var failureCount = 0
for sessionId in sessionIds {
group.enter()
APIClient.shared.tagSession(sessionId: sessionId, tag: tag) { result in
if case .failure(let error) = result {
print("Failed to tag session \(sessionId): \(error)")
failureCount += 1
}
group.leave()
}
}
group.notify(queue: .main) {
let successCount = sessionIds.count - failureCount
if failureCount == 0 {
completion(.success(successCount))
} else {
let error = NSError(
domain: "ClientManager", code: 1,
userInfo: [
NSLocalizedDescriptionKey:
"Tagged \(successCount) session(s), failed to tag \(failureCount) session(s)"
])
completion(.failure(error))
}
}
}
// MARK: - Utilities
func getTotalViewers(from clientsData: [String: Any]) -> Int {
return UtilityManager.shared.getTotalViewers(from: clientsData)
}
}
// MARK: - Supporting Types
struct ClientInfo {
let sessionId: String
let host: String
let protocolName: String
let stream: String
let connectedTime: Int
let bytesDown: Int
let bytesUp: Int
var formattedConnectedTime: String {
return DataProcessor.shared.formatConnectionTime(connectedTime)
}
var formattedBytesDown: String {
return DataProcessor.shared.formatBytes(bytesDown)
}
var formattedBytesUp: String {
return DataProcessor.shared.formatBytes(bytesUp)
}
var displayName: String {
return "\(host) (\(protocolName))"
}
}