@@ -28,14 +28,16 @@ class TrafficGenerator {
28
28
using: params
29
29
)
30
30
setupConnection ( )
31
+ setupOtherHandlers ( )
31
32
}
32
33
33
34
func reconnect( ) {
34
35
print ( " Attempting to reconnect " )
35
- self . connection. forceCancel ( )
36
+ connection. forceCancel ( )
36
37
37
38
connection = recreateConnection ( )
38
- self . setupConnection ( )
39
+ setupConnection ( )
40
+ setupOtherHandlers ( )
39
41
}
40
42
41
43
func recreateConnection( ) -> NWConnection {
@@ -47,6 +49,24 @@ class TrafficGenerator {
47
49
)
48
50
}
49
51
52
+ func setupOtherHandlers( ) {
53
+ connection. pathUpdateHandler = { newPath in
54
+ let availableInterfaces = newPath. availableInterfaces. map { $0. customDebugDescription }
55
+ let availableGateways = newPath. gateways. map { $0. customDebugDescription }
56
+
57
+ print ( " New interfaces available: \( availableInterfaces) " )
58
+ print ( " New gateways available: \( availableGateways) " )
59
+ }
60
+
61
+ connection. viabilityUpdateHandler = { newViability in
62
+ print ( " Connection is viable: \( newViability) " )
63
+ }
64
+
65
+ connection. betterPathUpdateHandler = { betterPathAvailable in
66
+ print ( " A better path is available: \( betterPathAvailable) " )
67
+ }
68
+ }
69
+
50
70
func setupConnection( ) {
51
71
print ( " Setting up connection... " )
52
72
let doneAttemptingConnectExpecation = XCTestExpectation ( description: " Done attemping to connect " )
@@ -84,7 +104,7 @@ class TrafficGenerator {
84
104
sendDataTimer. schedule ( deadline: . now( ) , repeating: interval)
85
105
86
106
sendDataTimer. setEventHandler {
87
- let data = " dGhpcyBpcyBqdXN0IHNvbWUgZHVtbXkgZGF0YSB0aGlzIGlzIGp1c3Qgc29tZSBkdW " . data ( using : . utf8)
107
+ let data = Data ( " dGhpcyBpcyBqdXN0IHNvbWUgZHVtbXkgZGF0YSB0aGlzIGlzIGp1c3Qgc29tZSBkdW " . utf8)
88
108
89
109
print ( " Attempting to send data... " )
90
110
@@ -108,3 +128,51 @@ class TrafficGenerator {
108
128
sendDataTimer. cancel ( )
109
129
}
110
130
}
131
+
132
+ extension NWInterface {
133
+ var customDebugDescription : String {
134
+ " type: \( type) name: \( self . name) index: \( index) "
135
+ }
136
+ }
137
+
138
+ extension NWInterface . InterfaceType : @retroactive CustomDebugStringConvertible {
139
+ public var debugDescription : String {
140
+ switch self {
141
+ case . cellular: " Cellular "
142
+ case . loopback: " Loopback "
143
+ case . other: " Other "
144
+ case . wifi: " Wifi "
145
+ case . wiredEthernet: " Wired Ethernet "
146
+ @unknown default : " Unknown interface type "
147
+ }
148
+ }
149
+ }
150
+
151
+ extension NWEndpoint {
152
+ var customDebugDescription : String {
153
+ switch self {
154
+ case let . hostPort( host, port) : " host: \( host. customDebugDescription) port: \( port) "
155
+ case let . opaque( endpoint) : " opaque: \( endpoint. description) "
156
+ case let . url( url) : " url: \( url) "
157
+ case let . service(
158
+ name,
159
+ type,
160
+ domain,
161
+ interface
162
+ ) : " service named: \( name) , type: \( type) , domain: \( domain) , interface: \( interface? . customDebugDescription ?? " [No interface] " ) "
163
+ case let . unix( path) : " unix: \( path) "
164
+ @unknown default : " Unknown NWEndpoint type "
165
+ }
166
+ }
167
+ }
168
+
169
+ extension NWEndpoint . Host {
170
+ var customDebugDescription : String {
171
+ switch self {
172
+ case let . ipv4( IPv4Address) : " IPv4: \( IPv4Address) "
173
+ case let . ipv6( IPv6Address) : " IPv6: \( IPv6Address) "
174
+ case let . name( name, interface) : " named: \( name) , \( interface? . customDebugDescription ?? " [No interface] " ) "
175
+ @unknown default : " Unknown host "
176
+ }
177
+ }
178
+ }
0 commit comments