summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicoleYarroch <nicole@livio.io>2019-05-29 11:06:29 -0400
committerNicoleYarroch <nicole@livio.io>2019-05-29 11:06:29 -0400
commit2f36335509cdc8596ac87f7730e3400cbc5fcb62 (patch)
treecd9ae5905702d0d696f184e782c32cb95f8cfaa9
parentbe4a932493cdbc5731d190f95c7cf5db2bde06a2 (diff)
downloadsdl_ios-2f36335509cdc8596ac87f7730e3400cbc5fcb62.tar.gz
Renamed the stop session methods
-rw-r--r--SmartDeviceLink/SDLIAPControlSession.m54
-rw-r--r--SmartDeviceLink/SDLIAPDataSession.m49
2 files changed, 62 insertions, 41 deletions
diff --git a/SmartDeviceLink/SDLIAPControlSession.m b/SmartDeviceLink/SDLIAPControlSession.m
index 84ef38748..36c043417 100644
--- a/SmartDeviceLink/SDLIAPControlSession.m
+++ b/SmartDeviceLink/SDLIAPControlSession.m
@@ -28,7 +28,6 @@ int const ProtocolIndexTimeoutSeconds = 10;
@end
-
@interface SDLIAPSession (exposeIAPSessionPrivateMethods)
@property (nonatomic, assign) BOOL isInputStreamOpen;
@@ -41,9 +40,10 @@ int const ProtocolIndexTimeoutSeconds = 10;
@end
-
@implementation SDLIAPControlSession
+#pragma mark - Session lifecycle
+
- (instancetype)initWithAccessory:(nullable EAAccessory *)accessory delegate:(id<SDLIAPControlSessionDelegate>)delegate {
SDLLogV(@"SDLIAPControlSession init");
@@ -56,6 +56,8 @@ int const ProtocolIndexTimeoutSeconds = 10;
return self;
}
+#pragma mark Start
+
- (void)startSession {
if (self.accessory == nil) {
SDLLogW(@"There is no control session in progress, attempting to create a new control session.");
@@ -87,17 +89,30 @@ int const ProtocolIndexTimeoutSeconds = 10;
return YES;
}
-- (void)sdl_stopStreamsAndDestroySession {
+#pragma mark Stop
+
+- (void)destroySession {
+ SDLLogD(@"Destroying the control session");
+ [self sdl_destroySession];
+}
+
+/**
+ * Makes sure the session is closed and destroyed on the main thread.
+ */
+- (void)sdl_destroySession {
if ([NSThread isMainThread]) {
- [self sdl_stop];
+ [self sdl_stopAndDestroySession];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
- [self sdl_stop];
+ [self sdl_stopAndDestroySession];
});
}
}
-- (void)sdl_stop {
+/**
+ * Closes the session streams and then destroys the session.
+ */
+- (void)sdl_stopAndDestroySession {
NSAssert(NSThread.isMainThread, @"%@ must only be called on the main thread", NSStringFromSelector(_cmd));
[super stopStream:self.eaSession.outputStream];
@@ -105,23 +120,6 @@ int const ProtocolIndexTimeoutSeconds = 10;
[super closeSession];
}
-- (void)destroySession {
- if (self.accessory == nil) {
- SDLLogV(@"Attempting to stop the control session but the accessory is nil");
- return;
- }
-
- SDLLogD(@"Destroying the control session");
- [self sdl_stopStreamsAndDestroySession];
-}
-
-/**
- * Starts a timer for the session. Core has ~10 seconds to send the protocol string, otherwise the control session is closed and the delegate will be notified that it should attempt to establish a new control session.
- */
-- (void)sdl_startSessionTimer {
- if (self.protocolIndexTimer == nil) { return; }
- [self.protocolIndexTimer start];
-}
#pragma mark - NSStreamDelegate
@@ -248,7 +246,7 @@ int const ProtocolIndexTimeoutSeconds = 10;
void (^elapsedBlock)(void) = ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
SDLLogW(@"Control session failed to get the protocol string from Core after %d seconds, retrying.", ProtocolIndexTimeoutSeconds);
- [strongSelf sdl_stopStreamsAndDestroySession];
+ [strongSelf sdl_destroySession];
if (strongSelf.delegate == nil) { return; }
[strongSelf.delegate retryControlSession];
@@ -258,6 +256,14 @@ int const ProtocolIndexTimeoutSeconds = 10;
return protocolIndexTimer;
}
+/**
+ * Starts a timer for the session. Core has ~10 seconds to send the protocol string, otherwise the control session is closed and the delegate will be notified that it should attempt to establish a new control session.
+ */
+- (void)sdl_startSessionTimer {
+ if (self.protocolIndexTimer == nil) { return; }
+ [self.protocolIndexTimer start];
+}
+
#pragma mark - Lifecycle Destruction
- (void)dealloc {
diff --git a/SmartDeviceLink/SDLIAPDataSession.m b/SmartDeviceLink/SDLIAPDataSession.m
index 83ceca530..05490a397 100644
--- a/SmartDeviceLink/SDLIAPDataSession.m
+++ b/SmartDeviceLink/SDLIAPDataSession.m
@@ -29,7 +29,6 @@ NS_ASSUME_NONNULL_BEGIN
@end
-
@interface SDLIAPSession (exposeIAPSessionPrivateMethods)
@property (nonatomic, assign) BOOL isInputStreamOpen;
@@ -44,6 +43,8 @@ NS_ASSUME_NONNULL_BEGIN
@implementation SDLIAPDataSession
+#pragma mark - Session lifecycle
+
- (instancetype)initWithAccessory:(nullable EAAccessory *)accessory delegate:(id<SDLIAPDataSessionDelegate>)delegate forProtocol:(NSString *)protocol; {
SDLLogV(@"SDLIAPDataSession init");
@@ -57,6 +58,8 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
+#pragma mark Start
+
- (void)startSession {
if (self.accessory == nil) {
SDLLogW(@"Failed to setup data session");
@@ -78,17 +81,30 @@ NS_ASSUME_NONNULL_BEGIN
}
}
-- (void)sdl_stopStreamsAndDestroySession {
+#pragma mark Stop
+
+- (void)destroySession {
+ SDLLogD(@"Destroying the data session");
+ [self sdl_destroySession];
+}
+
+/**
+ * Makes sure the session is closed and destroyed on the main thread.
+ */
+- (void)sdl_destroySession {
if ([NSThread isMainThread]) {
- [self sdl_stop];
+ [self sdl_stopAndDestroySession];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
- [self sdl_stop];
+ [self sdl_stopAndDestroySession];
});
}
}
-- (void)sdl_stop {
+/**
+ * Waits for the session streams to close on the I/O Thread and then destroys the session.
+ */
+- (void)sdl_stopAndDestroySession {
NSAssert(NSThread.isMainThread, @"%@ must only be called on the main thread", NSStringFromSelector(_cmd));
if (self.ioStreamThread == nil) {
@@ -109,16 +125,6 @@ NS_ASSUME_NONNULL_BEGIN
}];
}
-- (void)destroySession {
- if (self.accessory == nil) {
- SDLLogV(@"Attempting to stop the data session but the session is nil");
- return;
- }
-
- SDLLogD(@"Destroying the data session");
- [self sdl_stopStreamsAndDestroySession];
-}
-
/**
* Wait for the data session to detroy its input and output streams. The data EASession can not be destroyed until both streams have closed.
*
@@ -136,7 +142,7 @@ NS_ASSUME_NONNULL_BEGIN
}
}
-#pragma mark - data send methods
+#pragma mark - Sending data
- (void)sendData:(NSData *)data {
// Enqueue the data for transmission on the IO thread
@@ -145,6 +151,9 @@ NS_ASSUME_NONNULL_BEGIN
[self performSelector:@selector(sdl_dequeueAndWriteToOutputStream) onThread:self.ioStreamThread withObject:nil waitUntilDone:NO];
}
+/**
+ * Sends any queued data to Core on the output stream for the session.
+ */
- (void)sdl_dequeueAndWriteToOutputStream {
if ([self.ioStreamThread isCancelled]) {
SDLLogV(@"Attempted to send data on I/O thread but the thread is cancelled.");
@@ -179,6 +188,11 @@ NS_ASSUME_NONNULL_BEGIN
}
}
+/**
+ * Handles an output stream error when attempting to write to the output stream.
+ *
+ * @param error The output stream error
+ */
- (void)sdl_handleOutputStreamWriteError:(NSError *)error {
SDLLogE(@"Output stream error: %@", error);
// TODO: We should look at the domain and the code as a tuple and decide how to handle the error based on both values. For now, if the stream is closed, we will flush the send queue and leave it as-is otherwise so that temporary error conditions can be dealt with by retrying
@@ -189,6 +203,7 @@ NS_ASSUME_NONNULL_BEGIN
}
}
+
#pragma mark - NSStreamDelegate
/**
@@ -322,7 +337,7 @@ NS_ASSUME_NONNULL_BEGIN
});
}
-#pragma mark - background I/O for data session
+#pragma mark - Stream lifecycle
// Data session I/O thread
- (void)sdl_accessoryEventLoop {