Skip to content

Commit ded6b7b

Browse files
committed
Architecture improvement
1 parent 5f4d8c7 commit ded6b7b

File tree

13 files changed

+503
-330
lines changed

13 files changed

+503
-330
lines changed

PlaneTalk.xcodeproj/project.pbxproj

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
EF3D9D98241D0DCA009079FC /* Device.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF3D9D97241D0DCA009079FC /* Device.swift */; };
11+
EF3D9D9A241D10FC009079FC /* BroadcastDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF3D9D99241D10FC009079FC /* BroadcastDevice.swift */; };
12+
EF3D9D9F241D163C009079FC /* UDP.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF3D9D9E241D163C009079FC /* UDP.swift */; };
13+
EF3D9DA1241D1650009079FC /* ServerTransmission.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF3D9DA0241D1650009079FC /* ServerTransmission.swift */; };
14+
EF3D9DA3241D1682009079FC /* ClientTransmission.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF3D9DA2241D1682009079FC /* ClientTransmission.swift */; };
1015
EF50585423FEDB4B001020FD /* InterfaceFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585323FEDB4B001020FD /* InterfaceFinder.swift */; };
1116
EF50585623FEDC84001020FD /* Interface.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585523FEDC84001020FD /* Interface.swift */; };
1217
EF50585823FEDEF4001020FD /* Server.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585723FEDEF4001020FD /* Server.swift */; };
1318
EF50585A23FEDF0C001020FD /* Client.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585923FEDF0C001020FD /* Client.swift */; };
1419
EF50585C23FEF5C3001020FD /* Manager.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585B23FEF5C3001020FD /* Manager.swift */; };
15-
EF50585E23FF0B2B001020FD /* Device.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585D23FF0B2B001020FD /* Device.swift */; };
20+
EF50585E23FF0B2B001020FD /* NetworkDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585D23FF0B2B001020FD /* NetworkDevice.swift */; };
1621
EF5058612400A5A1001020FD /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF5058602400A5A1001020FD /* Utilities.swift */; };
1722
EF884EC123F76403002CB0EE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF884EC023F76403002CB0EE /* AppDelegate.swift */; };
1823
EF884EC323F76403002CB0EE /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF884EC223F76403002CB0EE /* SceneDelegate.swift */; };
@@ -39,12 +44,17 @@
3944
/* End PBXBuildFile section */
4045

4146
/* Begin PBXFileReference section */
47+
EF3D9D97241D0DCA009079FC /* Device.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Device.swift; sourceTree = "<group>"; };
48+
EF3D9D99241D10FC009079FC /* BroadcastDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BroadcastDevice.swift; sourceTree = "<group>"; };
49+
EF3D9D9E241D163C009079FC /* UDP.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UDP.swift; sourceTree = "<group>"; };
50+
EF3D9DA0241D1650009079FC /* ServerTransmission.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerTransmission.swift; sourceTree = "<group>"; };
51+
EF3D9DA2241D1682009079FC /* ClientTransmission.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientTransmission.swift; sourceTree = "<group>"; };
4252
EF50585323FEDB4B001020FD /* InterfaceFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterfaceFinder.swift; sourceTree = "<group>"; };
4353
EF50585523FEDC84001020FD /* Interface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Interface.swift; sourceTree = "<group>"; };
4454
EF50585723FEDEF4001020FD /* Server.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Server.swift; sourceTree = "<group>"; };
4555
EF50585923FEDF0C001020FD /* Client.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Client.swift; sourceTree = "<group>"; };
4656
EF50585B23FEF5C3001020FD /* Manager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Manager.swift; sourceTree = "<group>"; };
47-
EF50585D23FF0B2B001020FD /* Device.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Device.swift; sourceTree = "<group>"; };
57+
EF50585D23FF0B2B001020FD /* NetworkDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkDevice.swift; sourceTree = "<group>"; };
4858
EF5058602400A5A1001020FD /* Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
4959
EF884EBD23F76403002CB0EE /* PlaneTalk.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PlaneTalk.app; sourceTree = BUILT_PRODUCTS_DIR; };
5060
EF884EC023F76403002CB0EE /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -83,12 +93,40 @@
8393
/* End PBXFrameworksBuildPhase section */
8494

8595
/* Begin PBXGroup section */
96+
EF3D9D9B241D1603009079FC /* Transmission */ = {
97+
isa = PBXGroup;
98+
children = (
99+
EF3D9D9D241D1623009079FC /* TCP */,
100+
EF3D9D9C241D161B009079FC /* UDP */,
101+
);
102+
path = Transmission;
103+
sourceTree = "<group>";
104+
};
105+
EF3D9D9C241D161B009079FC /* UDP */ = {
106+
isa = PBXGroup;
107+
children = (
108+
EF3D9D9E241D163C009079FC /* UDP.swift */,
109+
);
110+
path = UDP;
111+
sourceTree = "<group>";
112+
};
113+
EF3D9D9D241D1623009079FC /* TCP */ = {
114+
isa = PBXGroup;
115+
children = (
116+
EF3D9DA0241D1650009079FC /* ServerTransmission.swift */,
117+
EF3D9DA2241D1682009079FC /* ClientTransmission.swift */,
118+
);
119+
path = TCP;
120+
sourceTree = "<group>";
121+
};
86122
EF50585F23FF0BEB001020FD /* Devices */ = {
87123
isa = PBXGroup;
88124
children = (
89125
EF50585723FEDEF4001020FD /* Server.swift */,
90126
EF50585923FEDF0C001020FD /* Client.swift */,
91-
EF50585D23FF0B2B001020FD /* Device.swift */,
127+
EF3D9D97241D0DCA009079FC /* Device.swift */,
128+
EF3D9D99241D10FC009079FC /* BroadcastDevice.swift */,
129+
EF50585D23FF0B2B001020FD /* NetworkDevice.swift */,
92130
);
93131
path = Devices;
94132
sourceTree = "<group>";
@@ -157,6 +195,7 @@
157195
EF95F8112402E3660002C047 /* Model */ = {
158196
isa = PBXGroup;
159197
children = (
198+
EF3D9D9B241D1603009079FC /* Transmission */,
160199
EF50585F23FF0BEB001020FD /* Devices */,
161200
EF95F80F2402E35C0002C047 /* User.swift */,
162201
EF50585523FEDC84001020FD /* Interface.swift */,
@@ -253,15 +292,19 @@
253292
EF9C42E5241A889000564AC2 /* NibLoadable.swift in Sources */,
254293
EF884EC523F76403002CB0EE /* ChatViewController.swift in Sources */,
255294
EF95F80A2402CDF30002C047 /* Connector.swift in Sources */,
295+
EF3D9DA3241D1682009079FC /* ClientTransmission.swift in Sources */,
256296
EF50585A23FEDF0C001020FD /* Client.swift in Sources */,
257297
EF50585623FEDC84001020FD /* Interface.swift in Sources */,
258298
EF8CBE5E241AB547009AB2D0 /* Reusable.swift in Sources */,
259299
EF8CBE62241ABDCD009AB2D0 /* ChatMessage.swift in Sources */,
260300
EF9C42E9241A89B300564AC2 /* UIView+Ext.swift in Sources */,
261-
EF50585E23FF0B2B001020FD /* Device.swift in Sources */,
301+
EF50585E23FF0B2B001020FD /* NetworkDevice.swift in Sources */,
262302
EF9C42E1241A858F00564AC2 /* TextBoardView.swift in Sources */,
303+
EF3D9D9F241D163C009079FC /* UDP.swift in Sources */,
263304
EF50585423FEDB4B001020FD /* InterfaceFinder.swift in Sources */,
264305
EF8CBE64241AC1E3009AB2D0 /* MessageFactory.swift in Sources */,
306+
EF3D9D9A241D10FC009079FC /* BroadcastDevice.swift in Sources */,
307+
EF3D9DA1241D1650009079FC /* ServerTransmission.swift in Sources */,
265308
EF8CBE60241AB582009AB2D0 /* UITableView+Ext.swift in Sources */,
266309
EF95F830240309370002C047 /* MessageTableViewCell.swift in Sources */,
267310
EF884EC123F76403002CB0EE /* AppDelegate.swift in Sources */,
@@ -273,6 +316,7 @@
273316
EF9C42E7241A899C00564AC2 /* Collection+Ext.swift in Sources */,
274317
EF5058612400A5A1001020FD /* Utilities.swift in Sources */,
275318
EF884EC323F76403002CB0EE /* SceneDelegate.swift in Sources */,
319+
EF3D9D98241D0DCA009079FC /* Device.swift in Sources */,
276320
);
277321
runOnlyForDeploymentPostprocessing = 0;
278322
};

PlaneTalk/AppDelegate.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import UIKit
1111
@UIApplicationMain
1212
class AppDelegate: UIResponder, UIApplicationDelegate {
1313

14-
15-
1614
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1715
// Override point for customization after application launch.
1816
return true

PlaneTalk/ChatViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ final class ChatViewController: UIViewController {
5252

5353
private func initialiseDeviceWith(_ connectedInterface: Interface) {
5454
print("Connected to \(connectedInterface.name)")
55-
let currentDevice = Client(
55+
let currentDevice = Device(
5656
ip: connectedInterface.ip,
5757
broadcastIP: connectedInterface.broadcastIP
5858
)
5959

60-
self.manager = Manager(currentDevice: currentDevice)
60+
self.manager = Manager(broadcastDevice: currentDevice)
6161
manager?.presenter = self
6262

6363
currentDevice.udpCommunicationDelegate = manager
6464
currentDevice.roleGrantDelegate = manager
6565

66-
manager?.allowClientToUDPCommunication()
66+
manager?.allowBroadcastDeviceTransmissionReceptionUDPMessages()
6767
}
6868

6969
// MARK: - Utilities

PlaneTalk/Controllers/Manager.swift

Lines changed: 82 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,78 +12,89 @@ protocol ServerIPProvider: AnyObject {
1212
var serverIP: String? { get }
1313
}
1414

15-
protocol UDPCommunicationDelegate: AnyObject {
16-
var discoveryServerString: String { get }
17-
func deviceDidReceiveBroadcastMessage(_ text: String, from sender: String)
15+
protocol GrantRoleDelegate: AnyObject {
16+
func deviceAsksServerPermissions(_ device: NetworkDevice)
1817
}
1918

20-
protocol ServerTCPCommunicationDelegate: UDPCommunicationDelegate {
21-
func serverWantsToSendTCPText(_ text: String) -> MessageType
22-
func serverDidReceiveClientTCPText(_ text: String, senderIP: String) -> MessageType
23-
24-
func serverDidSendText(_ text: String)
25-
func serverDidSendClientText(_ text: String, clientIP: String)
26-
func serverDidSendInformationText(_ text: String)
19+
enum DeviceType {
20+
case broadcastDevice(BroadcastDevice)
21+
case client(Client)
22+
case server(Server)
2723
}
2824

29-
protocol ClientTCPCommunicationDelegate: UDPCommunicationDelegate {
30-
func clientDidReceiveTCPText(_ text: String)
31-
func clientDidSendText(_ text: String)
32-
}
25+
final class Manager: ServerIPProvider {
26+
// The instance of the current device
27+
private var currentDevice: DeviceType
28+
29+
private var currentDeviceIP: String {
30+
switch currentDevice {
31+
case .client(let client):
32+
return client.ip
33+
case .server(let server):
34+
return server.ip
35+
case .broadcastDevice(let broadcastDevice):
36+
return broadcastDevice.ip
37+
}
38+
}
3339

40+
private var broadcastDevice: BroadcastDevice? {
41+
guard case .broadcastDevice(let device) = currentDevice else {
42+
return nil
43+
}
44+
return device
45+
}
3446

35-
protocol GrantRoleDelegate: AnyObject {
36-
func deviceAsksServerPermissions(_ device: Device)
37-
}
47+
private var client: Client? {
48+
guard case .client(let client) = currentDevice else {
49+
return nil
50+
}
51+
return client
52+
}
3853

39-
final class Manager: ServerIPProvider {
54+
private var server: Server? {
55+
guard case .server(let server) = currentDevice else {
56+
return nil
57+
}
58+
return server
59+
}
4060

41-
// The instance of the current device
42-
private var currentDevice: BroadcastDevice
43-
// If the current device is also the server, then this var wont be nil
44-
private var server: Server?
45-
// The serverIP as it's ServerIPProvider
4661
var serverIP: String?
4762

48-
private let messageFactory: MessageFactory
63+
var isServer: Bool {
64+
return server != nil
65+
}
4966

50-
init(currentDevice: BroadcastDevice) {
51-
self.currentDevice = currentDevice
52-
self.messageFactory = MessageFactory(device: currentDevice)
67+
var isThereServer: Bool {
68+
return serverIP != nil
5369
}
5470

5571
var presenter: MessagePresenter?
5672

57-
var isServer: Bool {
58-
return currentDevice.ip == serverIP
73+
private let messageFactory: MessageFactory
74+
75+
init(broadcastDevice: BroadcastDevice) {
76+
self.currentDevice = .broadcastDevice(broadcastDevice)
77+
self.messageFactory = MessageFactory(device: broadcastDevice)
5978
}
6079

61-
var isThereServer: Bool {
62-
return serverIP != nil
80+
func allowBroadcastDeviceTransmissionReceptionUDPMessages() {
81+
// Open a socket, create a queue for handling the oncoming events, enable transmission of broadcast messages and find a server
82+
broadcastDevice?.enableReceptionAndTransmissionUDPMessages()
83+
// Find a server
84+
broadcastDevice?.findServer()
6385
}
6486

6587
func send(_ message: String) {
66-
if
67-
!isServer,
68-
let client = currentDevice as? Client
69-
{
88+
switch currentDevice {
89+
case .client(let client):
7090
client.sendToServerTCP(message)
71-
} else if
72-
let server = server,
73-
isServer
74-
{
91+
case .server(let server):
7592
server.sendServerText(message)
93+
case .broadcastDevice(_):
94+
break
7695
}
7796
}
7897

79-
func allowClientToUDPCommunication() {
80-
// Open a socket, create a queue for handling the oncoming events, enable transmission of broadcast messages and find a server
81-
currentDevice.bindForUDPMessages()
82-
currentDevice.createBroadcastKqueue()
83-
currentDevice.enableTransmissionToBroadcast()
84-
currentDevice.findServer()
85-
}
86-
8798
private func presentMessage(chatMessage: ChatMessage) {
8899
DispatchQueue.main.async { [weak self] in
89100
guard let _self = self else { return }
@@ -103,7 +114,7 @@ extension Manager: UDPCommunicationDelegate {
103114
// Generate Message
104115
let message = messageFactory.receivedUDPMessage(text, from: sender)
105116
// Since it's broadcast, you can receive your message back then skip it
106-
guard message.senderIP != currentDevice.ip else { return }
117+
guard message.senderIP != currentDeviceIP else { return }
107118

108119
if isServer {
109120
serverHasReceivedBroadcastMessage(message)
@@ -119,7 +130,7 @@ extension Manager: UDPCommunicationDelegate {
119130
case messageFactory.discoveryMessage:
120131
// The server will reply sending back its IP
121132
let serverResponse = messageFactory.serverBroadcastAuthenticationResponse
122-
currentDevice.sendBroadcastMessage(serverResponse)
133+
server?.sendBroadcastMessage(serverResponse)
123134
print("New client " + message.senderIP)
124135
default:
125136
assertionFailure("Server has received an unknown message \(message.text)")
@@ -141,12 +152,18 @@ extension Manager: UDPCommunicationDelegate {
141152
let serverIP = message.senderIP
142153
self.serverIP = serverIP
143154
print("Found out a server @ " + message.senderIP)
155+
broadcastDevice?.clearKqueueEvents()
156+
157+
let client = Client(
158+
ip: currentDeviceIP,
159+
serverIP: serverIP
160+
)
161+
162+
currentDevice = .client(client)
144163

145164
// Unbind from the UDP port and connect to the server via TCP
146-
if let client = currentDevice as? Client {
147-
client.clearReceptionBroadcastKQueue()
148-
client.startTCPconnectionToServer(serverIP: serverIP)
149-
}
165+
client.clientTCPCommunicationDelegate = self
166+
client.startTCPconnectionToServer()
150167
default:
151168
assertionFailure("Client has received an unknown message \(message.text)")
152169
break
@@ -200,37 +217,32 @@ extension Manager: ClientTCPCommunicationDelegate {
200217
}
201218

202219
extension Manager: GrantRoleDelegate {
203-
func deviceAsksServerPermissions(_ device: Device) {
220+
func deviceAsksServerPermissions(_ device: NetworkDevice) {
204221
// When a client claims to become the server, check if there is already a device which is the current server
205-
guard !isThereServer else {
206-
(currentDevice as? Client)?.clientTCPCommunicationDelegate = self
222+
guard
223+
!isThereServer,
224+
let device = broadcastDevice
225+
else {
207226
return
208227
}
209228

210-
// The current device is about to become server.
211-
// Clear the queue of events
212-
currentDevice.clearReceptionBroadcastKQueue()
213229
// Set up your IP as serverIP
214230
serverIP = device.ip
215231

216232
print(Constant.Message.presentMeAsServer)
217233

218234
// Create an instance of the Server class
219-
let server = Server(ip: currentDevice.ip,
220-
broadcastIP: currentDevice.broadcastIP,
221-
udp_broadcast_message_socket: currentDevice.udp_broadcast_message_socket,
222-
udp_reception_message_socket: currentDevice.udp_reception_message_socket
235+
let server = Server(ip: device.ip,
236+
broadcastIP: device.broadcastIP,
237+
udp_broadcast_message_socket: device.udp_broadcast_message_socket,
238+
udp_reception_message_socket: device.udp_reception_message_socket
223239
)
224240

225-
self.server = server
241+
currentDevice = .server(server)
226242
server.serverTCPCommunicationDelegate = self
227-
server.udpCommunicationDelegate = currentDevice.udpCommunicationDelegate
228-
currentDevice = server
243+
server.udpCommunicationDelegate = device.udpCommunicationDelegate
229244

230-
// Reenable the reception and transmission of the broadcast messages
231-
server.createBroadcastKqueue()
232-
server.enableTransmissionToBroadcast()
233245
// Create a TCP socket to accept clients requests
234-
server.createTCPSocket()
246+
server.enableTCPCommunication()
235247
}
236248
}

PlaneTalk/Controllers/MessageFactory.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class MessageFactory {
2121
static let nicknameRegex = "^/name: ([0-z]{4,})$"
2222
}
2323

24-
private let device: Device
24+
private let device: NetworkDevice
2525

26-
init(device: Device) {
26+
init(device: NetworkDevice) {
2727
self.device = device
2828
}
2929

0 commit comments

Comments
 (0)