Skip to content

Commit 194354a

Browse files
committed
Improve project structure
1 parent ded6b7b commit 194354a

File tree

14 files changed

+102
-61
lines changed

14 files changed

+102
-61
lines changed

PlaneTalk.xcodeproj/project.pbxproj

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
EF3D9D9F241D163C009079FC /* UDP.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF3D9D9E241D163C009079FC /* UDP.swift */; };
1313
EF3D9DA1241D1650009079FC /* ServerTransmission.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF3D9DA0241D1650009079FC /* ServerTransmission.swift */; };
1414
EF3D9DA3241D1682009079FC /* ClientTransmission.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF3D9DA2241D1682009079FC /* ClientTransmission.swift */; };
15+
EF3D9DA7241D6427009079FC /* SocketUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF3D9DA6241D6427009079FC /* SocketUtilities.swift */; };
1516
EF50585423FEDB4B001020FD /* InterfaceFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585323FEDB4B001020FD /* InterfaceFinder.swift */; };
1617
EF50585623FEDC84001020FD /* Interface.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585523FEDC84001020FD /* Interface.swift */; };
1718
EF50585823FEDEF4001020FD /* Server.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50585723FEDEF4001020FD /* Server.swift */; };
@@ -31,7 +32,7 @@
3132
EF8CBE62241ABDCD009AB2D0 /* ChatMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF8CBE61241ABDCD009AB2D0 /* ChatMessage.swift */; };
3233
EF8CBE64241AC1E3009AB2D0 /* MessageFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF8CBE63241AC1E3009AB2D0 /* MessageFactory.swift */; };
3334
EF95F80A2402CDF30002C047 /* Connector.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF95F8092402CDF30002C047 /* Connector.swift */; };
34-
EF95F80C2402D5D20002C047 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF95F80B2402D5D20002C047 /* Extensions.swift */; };
35+
EF95F80C2402D5D20002C047 /* Notification+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF95F80B2402D5D20002C047 /* Notification+Ext.swift */; };
3536
EF95F8102402E35C0002C047 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF95F80F2402E35C0002C047 /* User.swift */; };
3637
EF95F830240309370002C047 /* MessageTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF95F82E240309370002C047 /* MessageTableViewCell.swift */; };
3738
EF95F831240309370002C047 /* MessageTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = EF95F82F240309370002C047 /* MessageTableViewCell.xib */; };
@@ -49,6 +50,7 @@
4950
EF3D9D9E241D163C009079FC /* UDP.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UDP.swift; sourceTree = "<group>"; };
5051
EF3D9DA0241D1650009079FC /* ServerTransmission.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerTransmission.swift; sourceTree = "<group>"; };
5152
EF3D9DA2241D1682009079FC /* ClientTransmission.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientTransmission.swift; sourceTree = "<group>"; };
53+
EF3D9DA6241D6427009079FC /* SocketUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketUtilities.swift; sourceTree = "<group>"; };
5254
EF50585323FEDB4B001020FD /* InterfaceFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterfaceFinder.swift; sourceTree = "<group>"; };
5355
EF50585523FEDC84001020FD /* Interface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Interface.swift; sourceTree = "<group>"; };
5456
EF50585723FEDEF4001020FD /* Server.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Server.swift; sourceTree = "<group>"; };
@@ -70,7 +72,7 @@
7072
EF8CBE61241ABDCD009AB2D0 /* ChatMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatMessage.swift; sourceTree = "<group>"; };
7173
EF8CBE63241AC1E3009AB2D0 /* MessageFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageFactory.swift; sourceTree = "<group>"; };
7274
EF95F8092402CDF30002C047 /* Connector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Connector.swift; sourceTree = "<group>"; };
73-
EF95F80B2402D5D20002C047 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
75+
EF95F80B2402D5D20002C047 /* Notification+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+Ext.swift"; sourceTree = "<group>"; };
7476
EF95F80F2402E35C0002C047 /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
7577
EF95F82E240309370002C047 /* MessageTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageTableViewCell.swift; sourceTree = "<group>"; };
7678
EF95F82F240309370002C047 /* MessageTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MessageTableViewCell.xib; sourceTree = "<group>"; };
@@ -119,6 +121,36 @@
119121
path = TCP;
120122
sourceTree = "<group>";
121123
};
124+
EF3D9DA4241D63AF009079FC /* Extensions */ = {
125+
isa = PBXGroup;
126+
children = (
127+
EF8CBE5F241AB582009AB2D0 /* UITableView+Ext.swift */,
128+
EF9C42E6241A899C00564AC2 /* Collection+Ext.swift */,
129+
EF9C42E8241A89B200564AC2 /* UIView+Ext.swift */,
130+
EF95F80B2402D5D20002C047 /* Notification+Ext.swift */,
131+
);
132+
path = Extensions;
133+
sourceTree = "<group>";
134+
};
135+
EF3D9DA5241D63D8009079FC /* Protocols */ = {
136+
isa = PBXGroup;
137+
children = (
138+
EF9C42DA241A564F00564AC2 /* ViewDataConfigurable.swift */,
139+
EF9C42E4241A889000564AC2 /* NibLoadable.swift */,
140+
EF8CBE5D241AB547009AB2D0 /* Reusable.swift */,
141+
);
142+
path = Protocols;
143+
sourceTree = "<group>";
144+
};
145+
EF3D9DA8241D6443009079FC /* Utilities */ = {
146+
isa = PBXGroup;
147+
children = (
148+
EF3D9DA6241D6427009079FC /* SocketUtilities.swift */,
149+
EF5058602400A5A1001020FD /* Utilities.swift */,
150+
);
151+
path = Utilities;
152+
sourceTree = "<group>";
153+
};
122154
EF50585F23FF0BEB001020FD /* Devices */ = {
123155
isa = PBXGroup;
124156
children = (
@@ -164,15 +196,9 @@
164196
EF95F80D2402D65D0002C047 /* Supporting Files */ = {
165197
isa = PBXGroup;
166198
children = (
167-
EF9C42DA241A564F00564AC2 /* ViewDataConfigurable.swift */,
168-
EF8CBE5F241AB582009AB2D0 /* UITableView+Ext.swift */,
169-
EF8CBE5B241AB343009AB2D0 /* PlatformUtils.swift */,
170-
EF9C42E4241A889000564AC2 /* NibLoadable.swift */,
171-
EF9C42E6241A899C00564AC2 /* Collection+Ext.swift */,
172-
EF9C42E8241A89B200564AC2 /* UIView+Ext.swift */,
173-
EF8CBE5D241AB547009AB2D0 /* Reusable.swift */,
174-
EF5058602400A5A1001020FD /* Utilities.swift */,
175-
EF95F80B2402D5D20002C047 /* Extensions.swift */,
199+
EF3D9DA5241D63D8009079FC /* Protocols */,
200+
EF3D9DA4241D63AF009079FC /* Extensions */,
201+
EF3D9DA8241D6443009079FC /* Utilities */,
176202
EF884EC623F76403002CB0EE /* Main.storyboard */,
177203
EF884ECB23F76408002CB0EE /* LaunchScreen.storyboard */,
178204
EF884EC923F76408002CB0EE /* Assets.xcassets */,
@@ -200,6 +226,7 @@
200226
EF95F80F2402E35C0002C047 /* User.swift */,
201227
EF50585523FEDC84001020FD /* Interface.swift */,
202228
EF8CBE61241ABDCD009AB2D0 /* ChatMessage.swift */,
229+
EF8CBE5B241AB343009AB2D0 /* PlatformUtils.swift */,
203230
);
204231
path = Model;
205232
sourceTree = "<group>";
@@ -310,7 +337,8 @@
310337
EF884EC123F76403002CB0EE /* AppDelegate.swift in Sources */,
311338
EF8CBE5C241AB343009AB2D0 /* PlatformUtils.swift in Sources */,
312339
EF9C42DB241A564F00564AC2 /* ViewDataConfigurable.swift in Sources */,
313-
EF95F80C2402D5D20002C047 /* Extensions.swift in Sources */,
340+
EF95F80C2402D5D20002C047 /* Notification+Ext.swift in Sources */,
341+
EF3D9DA7241D6427009079FC /* SocketUtilities.swift in Sources */,
314342
EF50585C23FEF5C3001020FD /* Manager.swift in Sources */,
315343
EF50585823FEDEF4001020FD /* Server.swift in Sources */,
316344
EF9C42E7241A899C00564AC2 /* Collection+Ext.swift in Sources */,

PlaneTalk/ChatViewController.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class ChatViewController: UIViewController {
2929
}
3030

3131
private var textBoardView = TextBoardView.instantiateFromNib()
32-
private var manager: Manager?
32+
private var manager: ManagerInterface?
3333

3434
private var messages: [ChatMessage] = [] {
3535
didSet {
@@ -42,28 +42,32 @@ final class ChatViewController: UIViewController {
4242
registerForKeyboard()
4343
addGestureRecognizer()
4444
setupUI()
45+
start()
46+
}
4547

48+
private func start() {
49+
// Find the connected interface
4650
guard let connectedInterface = Connector.connect() else {
4751
assertionFailure("Error: interface not found")
4852
return
4953
}
50-
initialiseDeviceWith(connectedInterface)
51-
}
5254

53-
private func initialiseDeviceWith(_ connectedInterface: Interface) {
5455
print("Connected to \(connectedInterface.name)")
56+
// Create a device object
5557
let currentDevice = Device(
5658
ip: connectedInterface.ip,
5759
broadcastIP: connectedInterface.broadcastIP
5860
)
5961

60-
self.manager = Manager(broadcastDevice: currentDevice)
61-
manager?.presenter = self
62+
// Initialise the manager with the currentDevice
63+
let manager = Manager(device: currentDevice)
64+
manager.presenter = self
6265

6366
currentDevice.udpCommunicationDelegate = manager
6467
currentDevice.roleGrantDelegate = manager
6568

66-
manager?.allowBroadcastDeviceTransmissionReceptionUDPMessages()
69+
manager.allowBroadcastDeviceTransmissionReceptionUDPMessages()
70+
self.manager = manager
6771
}
6872

6973
// MARK: - Utilities

PlaneTalk/Controllers/Manager.swift

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
import Foundation
1010

11-
protocol ServerIPProvider: AnyObject {
12-
var serverIP: String? { get }
13-
}
14-
1511
protocol GrantRoleDelegate: AnyObject {
1612
func deviceAsksServerPermissions(_ device: NetworkDevice)
1713
}
@@ -22,7 +18,12 @@ enum DeviceType {
2218
case server(Server)
2319
}
2420

25-
final class Manager: ServerIPProvider {
21+
protocol ManagerInterface {
22+
func allowBroadcastDeviceTransmissionReceptionUDPMessages()
23+
func send(_ message: String)
24+
}
25+
26+
final class Manager: ManagerInterface {
2627
// The instance of the current device
2728
private var currentDevice: DeviceType
2829

@@ -58,23 +59,23 @@ final class Manager: ServerIPProvider {
5859
return server
5960
}
6061

61-
var serverIP: String?
62+
private var serverIP: String?
6263

63-
var isServer: Bool {
64+
private var isServer: Bool {
6465
return server != nil
6566
}
6667

67-
var isThereServer: Bool {
68+
private var isThereServer: Bool {
6869
return serverIP != nil
6970
}
7071

71-
var presenter: MessagePresenter?
72+
weak var presenter: MessagePresenter?
7273

7374
private let messageFactory: MessageFactory
7475

75-
init(broadcastDevice: BroadcastDevice) {
76-
self.currentDevice = .broadcastDevice(broadcastDevice)
77-
self.messageFactory = MessageFactory(device: broadcastDevice)
76+
init(device: BroadcastDevice) {
77+
self.currentDevice = .broadcastDevice(device)
78+
self.messageFactory = MessageFactory(device: device)
7879
}
7980

8081
func allowBroadcastDeviceTransmissionReceptionUDPMessages() {
@@ -142,21 +143,17 @@ extension Manager: UDPCommunicationDelegate {
142143
private func clientHasReceivedBroadcastMessage(_ message: Message) {
143144
switch message.text {
144145
// If it's the server response to the discovery message
145-
case let string where string.contains(messageFactory.serverBroadcastAuthenticationResponseTemplate):
146-
guard !isThereServer else {
147-
assertionFailure("A server is already available @ \(serverIP!)")
148-
return
149-
}
150-
146+
case let string where string.contains(messageFactory.serverBroadcastAuthenticationResponseTemplate) && !isThereServer:
151147
// Save the server IP
152-
let serverIP = message.senderIP
153-
self.serverIP = serverIP
148+
self.serverIP = message.senderIP
154149
print("Found out a server @ " + message.senderIP)
155-
broadcastDevice?.clearKqueueEvents()
150+
151+
//
152+
broadcastDevice?.closeUDPSockets()
156153

157154
let client = Client(
158155
ip: currentDeviceIP,
159-
serverIP: serverIP
156+
serverIP: message.senderIP
160157
)
161158

162159
currentDevice = .client(client)
@@ -239,8 +236,8 @@ extension Manager: GrantRoleDelegate {
239236
)
240237

241238
currentDevice = .server(server)
239+
server.udpCommunicationDelegate = self
242240
server.serverTCPCommunicationDelegate = self
243-
server.udpCommunicationDelegate = device.udpCommunicationDelegate
244241

245242
// Create a TCP socket to accept clients requests
246243
server.enableTCPCommunication()

PlaneTalk/Model/Devices/BroadcastDevice.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protocol BroadcastDevice: NetworkDevice {
2222
var udpCommunicationDelegate: UDPCommunicationDelegate? { get }
2323

2424
func enableReceptionAndTransmissionUDPMessages()
25-
func clearKqueueEvents()
25+
func closeUDPSockets()
2626

2727
func findServer()
2828

@@ -205,7 +205,7 @@ extension BroadcastDevice {
205205
}
206206
}
207207

208-
func clearKqueueEvents() {
208+
func closeUDPSockets() {
209209
var sockKevent = kevent(
210210
ident: UInt(udp_reception_message_socket),
211211
filter: Int16(EVFILT_READ),
@@ -220,5 +220,7 @@ extension BroadcastDevice {
220220
}
221221
// Even if there is no connection in UDP, close will free the fd from the kernel
222222
close(udpEventsKQueue)
223+
close(udp_broadcast_message_socket)
224+
close(udp_reception_message_socket)
223225
}
224226
}

PlaneTalk/Supporting Files/PlatformUtils.swift renamed to PlaneTalk/Model/PlatformUtils.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public protocol PlatformUtilsProtocol {
1515
}
1616

1717
public class PlatformUtils: PlatformUtilsProtocol {
18-
public init() {
19-
}
18+
public init() {}
2019

2120
enum Constant {
2221
/// The height of the iPhone X and iPhone XS and iPhone 11 Pro in pixels.
File renamed without changes.
File renamed without changes.

PlaneTalk/Supporting Files/UITableView+Ext.swift renamed to PlaneTalk/Supporting Files/Extensions/UITableView+Ext.swift

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)