Skip to content

Commit 34bd482

Browse files
committed
Handle reconnection
1 parent 03870a8 commit 34bd482

File tree

9 files changed

+61
-26
lines changed

9 files changed

+61
-26
lines changed

PlaneTalk/ChatViewController.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class ChatViewController: UIViewController {
1313
static let navigatioBarBackgroundColor = UIColor(white: 0.96, alpha: 1)
1414
static let keyboardGap: CGFloat = 38
1515
static let viewBackgroundColor = UIColor(r: 246, g: 245, b: 246)
16-
static let cornerRadiusRatio: CGFloat = 0.08
16+
static let cornerRadiusRatio: CGFloat = 0.02
1717
}
1818

1919
@IBOutlet private var tableView: UIChatTableView!
@@ -157,6 +157,11 @@ final class ChatViewController: UIViewController {
157157
view.backgroundColor = Constant.viewBackgroundColor
158158
navigationController?.navigationBar.backgroundColor = Constant.navigatioBarBackgroundColor
159159

160+
setupOverlay(title: "Welcome to PlaneTalk")
161+
showOverlay()
162+
}
163+
164+
private func setupOverlay(title: String) {
160165
let searchButtonViewData = ConnectView.ViewData.ButtonViewData(
161166
title: "Search server",
162167
color: .white,
@@ -179,12 +184,20 @@ final class ChatViewController: UIViewController {
179184

180185
connectView.configure(
181186
with: .init(
187+
title: title,
182188
searchButtonViewData: searchButtonViewData,
183189
serverButtonViewData: serverButtonViewData
184190
)
185191
)
186192
}
187193

194+
private func showOverlay() {
195+
overlayView.alpha = 1
196+
connectViewContainerView.alpha = 1
197+
overlayView.isHidden = false
198+
connectViewContainerView.isHidden = false
199+
}
200+
188201
private func hideOverlay() {
189202
UIView.animate(withDuration: 0.35, animations: {
190203
self.overlayView.alpha = 0
@@ -224,6 +237,13 @@ extension ChatViewController: UITableViewDelegate, UITableViewDataSource {
224237
}
225238

226239
extension ChatViewController: ChatViewModelDelegate {
240+
func clientDidLeave() {
241+
showOverlay()
242+
243+
setupOverlay(title: "You disconnected")
244+
viewModel?.enableCommunication()
245+
}
246+
227247
func didFindServer() {
228248
connectView.showActivityIndicator(false)
229249
hideOverlay()

PlaneTalk/ChatViewModel.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ protocol MessagePresenter: AnyObject {
2222

2323
protocol ChatViewModelDelegate: AnyObject {
2424
func didFindServer()
25+
func clientDidLeave()
2526
}
2627

2728
final class ChatViewModel: ChatViewModelInterface {
@@ -71,6 +72,10 @@ extension ChatViewModel: ManagerDelegate {
7172
func managerDidFindServer(_ serverIP: String) {
7273
delegate?.didFindServer()
7374
}
75+
76+
func clientDidLeave() {
77+
delegate?.clientDidLeave()
78+
}
7479
}
7580

7681
// MARK: - ServerCommunicationDelegate

PlaneTalk/Communication/CommunicationManager.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protocol ManagerInterface {
2121

2222
protocol ManagerDelegate: AnyObject {
2323
func managerDidFindServer(_ serverIP: String)
24+
func clientDidLeave()
2425
}
2526

2627
final class CommunicationManager: ManagerInterface {
@@ -137,6 +138,11 @@ extension CommunicationManager: BroadcastMessagingDelegate {
137138
}
138139
}
139140

141+
func deviceDidCloseBroadcastSocket() {
142+
broadcastManager?.closeUDPSockets()
143+
broadcastManager = nil
144+
}
145+
140146
// Server has received a client message
141147
private func serverHasReceivedBroadcastText(_ text: String, serverIP: String, senderIP: String) {
142148
guard let broadcastMessagesInterpreter = broadcastMessagesInterpreter else {
@@ -168,7 +174,7 @@ extension CommunicationManager: BroadcastMessagingDelegate {
168174
let broadcastMessageType = type(of: broadcastMessagesInterpreter).isValidBroadcastText(text),
169175
broadcastMessageType == .serverResponse
170176
else {
171-
print("Server has received an unknown message \(text) from: \(senderIP)")
177+
print("Client has received an unknown message \(text) from: \(senderIP)")
172178
return
173179
}
174180

@@ -187,17 +193,12 @@ extension CommunicationManager: BroadcastMessagingDelegate {
187193
// MARK: - ClientCommunicationDelegate
188194
extension CommunicationManager: ClientConnectionDelegate {
189195
func clientDidLoseConnectionWithServer() {
196+
clientCommunicationManager?.closeCommunication()
190197
clientCommunicationManager = nil
191198
broadcastManager = nil
192199
serverIP = nil
193200
currentDevice = nil
194-
195-
do {
196-
print("Retrying start communication")
197-
try enableCommunication()
198-
} catch {
199-
print("Unable to re-establish a connection. Error: \(error.localizedDescription)")
200-
}
201+
delegate?.clientDidLeave()
201202
}
202203
}
203204

PlaneTalk/Communication/TransmissionManager/BroadcastManager.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ final class BroadcastManager: BroadcastInterface {
109109
for i in 0..<status {
110110
if (events[Int(i)].flags & UInt16(EV_EOF)) == EV_EOF {
111111
print("The socket (\(_self.udp_reception_message_socket)) has been closed.")
112+
_self.broadcastMessagingDelegate?.deviceDidCloseBroadcastSocket()
112113
return
113114
}
114115

@@ -139,7 +140,7 @@ final class BroadcastManager: BroadcastInterface {
139140
}
140141
} else {
141142
print("Kqueue error: \(String(cString: strerror(errno)))")
142-
break
143+
return
143144
}
144145
}
145146
}

PlaneTalk/Communication/TransmissionManager/ClientCommunicationManager.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ final class ClientCommunicationManager: ClientCommunicationInterface {
126126
print("Kevent error")
127127
}
128128

129-
print("Server disconnected. Starting a new search again ...")
129+
print("Connection lost ...")
130130
propagationQueue.async { [weak self] in
131131
self?.clientConnectionDelegate?.clientDidLoseConnectionWithServer()
132132
}
@@ -136,9 +136,8 @@ final class ClientCommunicationManager: ClientCommunicationInterface {
136136
}
137137
}
138138
default:
139-
print("Error reading kevent")
140-
close(socketKQueue)
141-
exit(EXIT_FAILURE)
139+
print("Kqueue error: \(String(cString: strerror(errno)))")
140+
return
142141
}
143142
}
144143

PlaneTalk/Model/Delegate/BroadcastMessagingDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ import Foundation
1010

1111
protocol BroadcastMessagingDelegate: AnyObject {
1212
func deviceDidReceiveBroadcastMessage(_ text: String, from sender: String)
13+
func deviceDidCloseBroadcastSocket()
1314
}

PlaneTalk/Supporting Files/Base.lproj/Main.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
<color key="backgroundColor" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
3737
</view>
3838
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="lnw-Yw-Iod">
39-
<rect key="frame" x="62" y="308.5" width="290" height="282"/>
39+
<rect key="frame" x="62" y="304.5" width="290" height="290"/>
4040
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
4141
<constraints>
42-
<constraint firstAttribute="width" secondItem="lnw-Yw-Iod" secondAttribute="height" multiplier="35:34" id="YJc-zj-7pd"/>
42+
<constraint firstAttribute="width" secondItem="lnw-Yw-Iod" secondAttribute="height" multiplier="34:34" id="YJc-zj-7pd"/>
4343
</constraints>
4444
</view>
4545
<view contentMode="scaleToFill" placeholderIntrinsicWidth="414" placeholderIntrinsicHeight="50" translatesAutoresizingMaskIntoConstraints="NO" id="TvX-CB-EWj">

PlaneTalk/UI/ConnectView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ final class ConnectView: UIView, ViewDataConfigurable, NibLoadable {
2525
var tapHanlder: (() -> Void)?
2626
}
2727

28+
var title: String
2829
var searchButtonViewData: ButtonViewData
2930
var serverButtonViewData: ButtonViewData
3031
}
3132

33+
@IBOutlet private var mainLabel: UILabel!
3234
@IBOutlet private var searchServerButton: UIButton!
3335
@IBOutlet private var activityIndicator: UIActivityIndicatorView!
3436
@IBOutlet private var becomeServerButton: UIButton!
@@ -50,6 +52,8 @@ final class ConnectView: UIView, ViewDataConfigurable, NibLoadable {
5052
becomeServerButton.setTitleColor(serverButtonViewData.color, for: .normal)
5153
becomeServerButton.addTarget(self, action: #selector(didTapServerButton), for: .primaryActionTriggered)
5254
serverActionHandler = serverButtonViewData.tapHanlder
55+
56+
mainLabel.text = viewData.title
5357
}
5458

5559
override func layoutSubviews() {

PlaneTalk/UI/ConnectView.xib

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@
1414
<rect key="frame" x="0.0" y="0.0" width="257" height="247"/>
1515
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1616
<subviews>
17-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Welcome to PlaneTalk" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="LbD-R8-DIk">
18-
<rect key="frame" x="54" y="12" width="149" height="18"/>
19-
<fontDescription key="fontDescription" type="system" pointSize="15"/>
17+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Welcome to PlaneTalk" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="LbD-R8-DIk">
18+
<rect key="frame" x="52.5" y="18" width="152.5" height="18"/>
19+
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="15"/>
2020
<nil key="textColor"/>
2121
<nil key="highlightedColor"/>
2222
</label>
2323
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="NVo-YW-7py">
24-
<rect key="frame" x="25.5" y="193" width="206" height="36"/>
24+
<rect key="frame" x="18" y="193" width="221" height="36"/>
2525
<constraints>
2626
<constraint firstAttribute="height" constant="36" id="SxT-0i-Ude"/>
2727
</constraints>
2828
<fontDescription key="fontDescription" type="system" pointSize="11"/>
2929
<state key="normal" title="Become server"/>
3030
</button>
3131
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="C0S-Q9-Vb0">
32-
<rect key="frame" x="25.5" y="145" width="206" height="36"/>
32+
<rect key="frame" x="18" y="139" width="221" height="36"/>
3333
<constraints>
3434
<constraint firstAttribute="height" constant="36" id="ABl-Zo-9Bp"/>
3535
</constraints>
3636
<fontDescription key="fontDescription" type="system" pointSize="11"/>
3737
<state key="normal" title="Search Server"/>
3838
</button>
3939
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="zT4-Rl-rXR">
40-
<rect key="frame" x="118.5" y="36" width="20" height="103"/>
40+
<rect key="frame" x="118.5" y="42" width="20" height="91"/>
4141
<subviews>
4242
<activityIndicatorView hidden="YES" opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" hidesWhenStopped="YES" style="medium" translatesAutoresizingMaskIntoConstraints="NO" id="GgV-Yg-heg">
43-
<rect key="frame" x="0.0" y="41.5" width="20" height="20"/>
43+
<rect key="frame" x="0.0" y="35.5" width="20" height="20"/>
4444
</activityIndicatorView>
4545
</subviews>
4646
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
@@ -53,14 +53,17 @@
5353
</subviews>
5454
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
5555
<constraints>
56+
<constraint firstItem="NVo-YW-7py" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="18" id="4pO-25-39X"/>
57+
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="LbD-R8-DIk" secondAttribute="trailing" constant="18" id="7wz-QA-Kdv"/>
5658
<constraint firstAttribute="bottom" secondItem="NVo-YW-7py" secondAttribute="bottom" constant="18" id="K6U-HY-VwC"/>
57-
<constraint firstItem="LbD-R8-DIk" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="12" id="RDk-CQ-vbB"/>
59+
<constraint firstAttribute="trailing" secondItem="NVo-YW-7py" secondAttribute="trailing" constant="18" id="KWL-WC-XOV"/>
60+
<constraint firstItem="LbD-R8-DIk" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="18" id="RDk-CQ-vbB"/>
5861
<constraint firstItem="NVo-YW-7py" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="VPR-ef-Ka7"/>
5962
<constraint firstItem="zT4-Rl-rXR" firstAttribute="top" secondItem="LbD-R8-DIk" secondAttribute="bottom" constant="6" id="Vn5-SN-GeA"/>
60-
<constraint firstItem="NVo-YW-7py" firstAttribute="width" secondItem="iN0-l3-epB" secondAttribute="width" multiplier="0.8" id="WVp-4L-bTI"/>
6163
<constraint firstItem="LbD-R8-DIk" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="cMn-Ru-Ra4"/>
6264
<constraint firstItem="C0S-Q9-Vb0" firstAttribute="centerX" secondItem="NVo-YW-7py" secondAttribute="centerX" id="sPy-Sn-krz"/>
63-
<constraint firstItem="NVo-YW-7py" firstAttribute="top" secondItem="C0S-Q9-Vb0" secondAttribute="bottom" constant="12" id="u2E-MZ-jdc"/>
65+
<constraint firstItem="LbD-R8-DIk" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="iN0-l3-epB" secondAttribute="leading" constant="18" id="sif-jD-MCB"/>
66+
<constraint firstItem="NVo-YW-7py" firstAttribute="top" secondItem="C0S-Q9-Vb0" secondAttribute="bottom" constant="18" id="u2E-MZ-jdc"/>
6467
<constraint firstItem="zT4-Rl-rXR" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="w4h-fx-jU6"/>
6568
<constraint firstItem="C0S-Q9-Vb0" firstAttribute="width" secondItem="NVo-YW-7py" secondAttribute="width" id="xFs-7r-Zam"/>
6669
<constraint firstItem="C0S-Q9-Vb0" firstAttribute="top" secondItem="zT4-Rl-rXR" secondAttribute="bottom" constant="6" id="xJN-5q-bPG"/>
@@ -71,6 +74,7 @@
7174
<connections>
7275
<outlet property="activityIndicator" destination="GgV-Yg-heg" id="FYL-tW-Kks"/>
7376
<outlet property="becomeServerButton" destination="NVo-YW-7py" id="PyB-M1-md5"/>
77+
<outlet property="mainLabel" destination="LbD-R8-DIk" id="1eE-qb-VRw"/>
7478
<outlet property="searchServerButton" destination="C0S-Q9-Vb0" id="hpe-SX-kPX"/>
7579
</connections>
7680
<point key="canvasLocation" x="-55.797101449275367" y="-129.57589285714286"/>

0 commit comments

Comments
 (0)