#import "AppReachabilityExample.h"
@implementation AppReachabilityExample
@synthesize session_p;
@synthesize reachability;
@synthesize currentNetworkStatus;
@synthesize currentConnectionRequired;
@synthesize receivedReconnectedEvent;
@synthesize isThreadBlocked;
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
self.name = @"AppReachability";
self.description =
@"Demonstrates how to respond to changes in network state on iOS";
[self.parameters addParameter:PARAMETER_NUM_MESSAGES];
[self.parameters addParameter:PARAMETER_DESTINATION_TOPIC];
[self.parameters parameterWithId:PARAMETER_NUM_MESSAGES].value = @"100";
self.reachability = [Reachability reachabilityForInternetConnection];
[self.reachability startNotifier];
self.currentNetworkStatus = [self.reachability currentReachabilityStatus];
self.currentConnectionRequired = [self.reachability connectionRequired];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:NULL];
self.receivedReconnectedEvent = NO;
return self;
}
eventInfo:
userData:(void *)user_p {
if (self.isThreadBlocked) {
receivedReconnectedEvent = YES;
}
}
}
- (void)run {
[super run];
session_p = NULL;
int msgsSent = 0;
[self handleErrorWithReturnCode:rc
errorString:"solClient_initialize()"];
goto notInitialized;
}
[self setLoggingLevel];
"AppReachabilityExample.m (Copyright 2014-2024 Solace Corporation. All rights reserved.)\n");
[self logCCSMPVersion];
&contextFuncInfo,
sizeof(contextFuncInfo))) !=
SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_context_create()"];
goto cleanup;
}
if ((rc = [self createAndConnectSessionWithContext:context_p
session:&session_p
messageCallback:messageReceiveCallback
eventCallback:eventCallback]) !=
[self handleErrorWithReturnCode:
rc errorString:"createAndConnectSessionWithContext:session:"
"messageCallback:eventCallback:userData:"];
goto cleanup;
}
receivedReconnectedEvent = NO;
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC]
.value cStringUsingEncoding:NSASCIIStringEncoding])) !=
if ([self isLastErrorConnectionLoss]) {
[self blockThreadDuringConnectionLoss];
if (self.requestCancel) {
goto sessionConnected;
}
} else {
[self
handleErrorWithReturnCode:rc
errorString:"solClient_session_topicSubscribe()"];
goto sessionConnected;
}
}
for (msgsSent = 0;
msgsSent < [self.parameters parameterWithId:PARAMETER_NUM_MESSAGES]
.value.integerValue &&
!self.requestCancel;
++msgsSent) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_alloc()"];
goto sessionConnected;
}
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_setDeliveryMode()"];
goto freeMessage;
}
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC].value
cStringUsingEncoding:NSASCIIStringEncoding];
msg_p, &destination,
sizeof(destination))) !=
SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_setDestination()"];
goto freeMessage;
}
msg_p, COMMON_ATTACHMENT_TEXT,
(solClient_uint32_t)strlen(COMMON_ATTACHMENT_TEXT))) !=
[self handleErrorWithReturnCode:
rc errorString:"solClient_msg_setBinaryAttachment()"];
goto freeMessage;
}
if ([self isLastErrorConnectionLoss]) {
[self blockThreadDuringConnectionLoss];
if (self.requestCancel) {
goto freeMessage;
}
} else {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_sendMsg()"];
goto freeMessage;
}
}
freeMessage:
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_free()"];
goto sessionConnected;
}
[NSThread sleepForTimeInterval:1];
}
if (self.requestCancel) {
goto sessionConnected;
}
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC]
.value cStringUsingEncoding:NSASCIIStringEncoding])) !=
if ([self isLastErrorConnectionLoss]) {
[self blockThreadDuringConnectionLoss];
if (self.requestCancel) {
goto sessionConnected;
}
} else {
[self
handleErrorWithReturnCode:rc
errorString:"solClient_session_topicSubscribe()"];
goto sessionConnected;
}
}
sessionConnected:
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_disconnect()"];
}
cleanup:
[self handleErrorWithReturnCode:rc errorString:"solClient_cleanup()"];
}
notInitialized:
[self cleanup];
}
- (BOOL)isLastErrorConnectionLoss {
return YES;
} else {
return NO;
}
}
- (void)blockThreadDuringConnectionLoss {
"[THREAD BLOCK] Thread blocked while waiting for Reachability "
"reconnection event or cancel request.\n");
self.isThreadBlocked = true;
while (!receivedReconnectedEvent && !self.requestCancel) {
[NSThread sleepForTimeInterval:1];
}
self.isThreadBlocked = false;
if (self.requestCancel) {
"[THREAD BLOCK] Received cancel request.\n");
} else {
"[THREAD BLOCK] Reachability reconnection event received.\n");
receivedReconnectedEvent = NO;
}
}
- (void)reachabilityChanged:(NSNotification *)note {
Reachability *newReachability = [note object];
NetworkStatus newNetworkStatus =
[newReachability currentReachabilityStatus];
BOOL newConnectionRequired = [newReachability connectionRequired];
if (self.running && self.exampleInterface.runningExample == self) {
if (currentNetworkStatus == NotReachable || currentConnectionRequired) {
"[REACHABILITY] Old network status: Not reachable (or "
"connection required)\n");
if (!newConnectionRequired) {
if (newNetworkStatus == ReachableViaWiFi) {
"[REACHABILITY] New network status: Reachable via "
"Wi-Fi\n");
} else if (newNetworkStatus == ReachableViaWWAN) {
"network status: "
"Reachable via WWAN\n");
}
[self handleErrorWithReturnCode:
rc errorString:"solClient_session_connect()"];
"[REACHABILITY] Could not reconnect. Cancelling...\n");
[self cancel];
} else {
"[REACHABILITY] Reconnection successful.\n");
}
} else {
"[REACHABILITY] New network status: Not reachable (or "
"connection required)\n");
}
} else if (currentNetworkStatus == ReachableViaWiFi) {
"[REACHABILITY] Old network status: Reachable via Wi-Fi\n");
if (newNetworkStatus == NotReachable || newConnectionRequired) {
"[REACHABILITY] New network status: Not reachable (or "
"connection required)\n");
"[REACHABILITY] Could not disconnect.\n");
[self handleErrorWithReturnCode:
rc errorString:"solClient_session_disconnect()"];
} else {
"[REACHABILITY] Disconnection successful.\n");
}
} else if (newNetworkStatus == ReachableViaWWAN) {
"[REACHABILITY] New network status: Reachable via WWAN\n");
}
} else if (currentNetworkStatus == ReachableViaWWAN) {
"[REACHABILITY] Old network status: Reachable via WWAN\n");
if (newNetworkStatus == NotReachable || newConnectionRequired) {
"[REACHABILITY] New network status: Not reachable (or "
"connection required)\n");
"[REACHABILITY] Could not disconnect.\n");
[self handleErrorWithReturnCode:
rc errorString:"solClient_session_disconnect()"];
} else {
"[REACHABILITY] Disconnection successful.\n");
}
} else if (newNetworkStatus == ReachableViaWiFi) {
"[REACHABILITY] New network status: Reachable via Wi-Fi\n");
"[REACHABILITY] Could not disconnect.\n");
[self handleErrorWithReturnCode:
rc errorString:"solClient_session_disconnect()"];
} else {
"[REACHABILITY] Disconnection successful.\n");
}
[self handleErrorWithReturnCode:
rc errorString:"solClient_session_connect()"];
"[REACHABILITY] Could not reconnect. Cancelling...\n");
[self cancel];
} else {
"[REACHABILITY] Reconnection successful.\n");
}
}
}
}
currentNetworkStatus = newNetworkStatus;
currentConnectionRequired = newConnectionRequired;
}
@end