-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.swift
176 lines (138 loc) · 5.58 KB
/
example.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// example.swift
// Shapeshifter-Swift-TransportsPackageDescription
//
// Created by Mafalda on 9/16/19.
//
import Foundation
import Datable
import Protean
import ProteanSwift
import Optimizer
import Net
/// This is example code to help illustrate how to use the transports provided in this library.
/// This is an ongoing work in progress :)
// MARK: Protean
func exampleSequenceConfig() -> ByteSequenceShaper.Config?
{
let sequence = Data(string: "OH HELLO")
guard let sequenceModel = ByteSequenceShaper.SequenceModel(index: 0, offset: 0, sequence: sequence, length: 256)
else
{
return nil
}
let sequenceConfig = ByteSequenceShaper.Config(addSequences: [sequenceModel], removeSequences: [sequenceModel])
return sequenceConfig
}
public func exampleEncryptionConfig() -> EncryptionShaper.Config
{
let bytes = Data(count: 32)
let encryptionConfig = EncryptionShaper.Config(key: bytes)
return encryptionConfig
}
public func exampleHeaderConfig() -> HeaderShaper.Config
{
// Creates a sample (non-random) config, suitable for testing.
let header = Data([139, 210, 37])
let headerConfig = HeaderShaper.Config(addHeader: header, removHeader: header)
return headerConfig
}
func proteanExample()
{
let ipv4Address = IPv4Address("10.10.10.10")!
let portUInt = UInt16("7007")!
let port = NWEndpoint.Port(rawValue: portUInt)!
let host = NWEndpoint.Host.ipv4(ipv4Address)
// Create a Protean config using your chosen sequence, encryption, and header
let proteanConfig = Protean.Config(
byteSequenceConfig: exampleSequenceConfig(),
encryptionConfig: exampleEncryptionConfig(),
headerConfig: exampleHeaderConfig())
// Create the connection factory providing your config and the desires IP and port
let proteanConnectionFactory = ProteanConnectionFactory(host: host, port: port, config: proteanConfig)
// Create a connection using the Protean connection factory
guard var connection = proteanConnectionFactory.connect
else
{
print("Failed to create a Protean connection object.")
return
}
// Set up your state update handler.
connection.stateUpdateHandler =
{
(newState) in
switch newState
{
case .ready:
print("Connection is read")
connected1.fulfill()
case .failed(let error):
print("Connection Failed")
print("Failure Error: \(error.localizedDescription)\n")
connected1.fulfill()
default:
print("Connection Other State: \(newState)")
}
}
// Tell the connection to start.
connection.start(queue: DispatchQueue(label: "TestQueue"))
}
// MARK: Optimizer
/// This is an example of creating and starting a network connection using Optimizer's CoreML Strategy.
/// This example also shows the way to get instances of some of our other transports.
func coreMLStrategyExample()
{
let ipv4Address = IPv4Address("10.10.10.10")!
let portUInt = UInt16("7007")!
let port = NWEndpoint.Port(rawValue: portUInt)!
let host = NWEndpoint.Host.ipv4(ipv4Address)
let logQueue = Queue<String>()
let certString = "examplecertstring"
let serverPublicKey = Data(base64Encoded: "exampleserverpublickeystring")!
// Create a Protean Transport Instance
let proteanConfig = Protean.Config(byteSequenceConfig: exampleSequenceConfig(),
encryptionConfig: exampleEncryptionConfig(),
headerConfig: exampleHeaderConfig())
let proteanTransport = ProteanConnectionFactory(host: host, port: port, config: proteanConfig)
// Create a Replicant transport instance.
guard let replicantClientConfig = ReplicantConfig(serverPublicKey: serverPublicKey, chunkSize: 2000, chunkTimeout: 1000, toneBurst: nil)
else
{
print("\nUnable to create ReplicantClient config.\n")
return
}
let replicantTransport = ReplicantConnectionFactory(host: host, port: port, config: replicantClientConfig, logQueue: logQueue)
// Create a Wisp transport instance.
let wispTransport = WispConnectionFactory(host: host, port: port, cert: certString, iatMode: false)
// Create an array with all of the transports that Optimizer should choose from.
let possibleTransports:[ConnectionFactory] = [wispTransport, replicantTransport, proteanTransport]
// Create an instance of your chosen Strategy using the array of transports
let strategy = CoreMLStrategy(transports: possibleTransports)
// Create an OptimizerConnectionFactory instance using your Strategy.
let connectionFactory = OptimizerConnectionFactory(strategy: strategy)
// Create the connection using the OptimizerConnectionFactory instance.
guard var connection = connectionFactory!.connect(using: .tcp)
else
{
return
}
// Set up your state update handler.
connection.stateUpdateHandler =
{
(newState) in
switch newState
{
case .ready:
print("Connection is read")
connected1.fulfill()
case .failed(let error):
print("Connection Failed")
print("Failure Error: \(error.localizedDescription)\n")
connected1.fulfill()
default:
print("Connection Other State: \(newState)")
}
}
// Tell the connection to start.
connection.start(queue: DispatchQueue(label: "TestQueue"))
}