#import "AppTransitionsExample.h"
@implementation AppTransitionsExample
@synthesize session_p;
@synthesize suspended;
@synthesize requestSuspend;
@synthesize requestResume;
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
self.name = @"AppTransitions";
self.description =
@"Demonstrates how to respond to changes in application state";
[self.parameters addParameter:PARAMETER_NUM_MESSAGES];
[self.parameters addParameter:PARAMETER_DESTINATION_TOPIC];
[self.parameters parameterWithId:PARAMETER_NUM_MESSAGES].value = @"100";
suspended = NO;
requestSuspend = NO;
requestResume = NO;
return self;
}
- (void)run {
[super run];
int msgsSent = 0;
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
if (self.requestCancel) {
goto notInitialized;
}
} else {
[self handleErrorWithReturnCode:rc
errorString:"solClient_initialize()"];
goto notInitialized;
}
}
[self setLoggingLevel];
"AppTransitionsExample.m (Copyright 2014-2024 Solace Corporation. All rights reserved.)\n");
[self logCCSMPVersion];
&contextFuncInfo,
sizeof(contextFuncInfo))) !=
SOLCLIENT_OK) {
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
if (self.requestCancel) {
goto cleanup;
}
} else {
[self handleErrorWithReturnCode:rc
errorString:"solClient_context_create()"];
goto cleanup;
}
}
while ((rc = [self createAndConnectSessionWithContext:context_p
session:&session_p
messageCallback:messageReceiveCallback
eventCallback:eventCallback]) !=
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
if (self.requestCancel) {
goto cleanup;
}
} else {
[self handleErrorWithReturnCode:rc
errorString:"createAndConnectSessionWithContext"
":session:messageCallback:"
"eventCallback:userData:"];
goto cleanup;
}
}
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC]
.value cStringUsingEncoding:NSASCIIStringEncoding])) !=
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
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) {
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
if (self.requestCancel) {
goto sessionConnected;
}
} else {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_alloc()"];
goto sessionConnected;
}
}
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
if (self.requestCancel) {
goto freeMessage;
}
} else {
[self handleErrorWithReturnCode:
rc errorString:"solClient_msg_setDeliveryMode()"];
goto freeMessage;
}
}
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC].value
cStringUsingEncoding:NSASCIIStringEncoding];
sizeof(destination))) !=
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
if (self.requestCancel) {
goto freeMessage;
}
} else {
[self
handleErrorWithReturnCode:rc
errorString:"solClient_msg_setDestination()"];
goto freeMessage;
}
}
msg_p, COMMON_ATTACHMENT_TEXT,
(solClient_uint32_t)strlen(COMMON_ATTACHMENT_TEXT))) !=
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
if (self.requestCancel) {
goto freeMessage;
}
} else {
[self handleErrorWithReturnCode:
rc errorString:"solClient_msg_setBinaryAttachment()"];
goto freeMessage;
}
}
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
if (self.requestCancel) {
goto freeMessage;
}
} else {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_sendMsg()"];
goto freeMessage;
}
}
freeMessage:
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
if (self.requestCancel) {
goto sessionConnected;
}
} else {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_free()"];
goto sessionConnected;
}
}
[NSThread sleepForTimeInterval:1];
}
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC]
.value cStringUsingEncoding:NSASCIIStringEncoding])) !=
if (self.requestSuspend) {
[self blockThreadDuringSuspension];
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];
}
- (void)suspend {
if (self.running && !self.requestSuspend) {
"[SUSPEND] Suspending example...\n");
self.requestSuspend = YES;
"[SUSPEND] Failed to disconnect session!\n");
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_disconnect()"];
} else {
"[SUSPEND] Session disconnected.\n");
}
}
}
- (void)resume {
if (self.suspended && !self.requestResume) {
"[RESUME] Failed to reconnect session!");
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_connect()"];
"[RESUME] Could not resume example from suspension! "
"Cancelling...\n");
[self cancel];
} else {
"[RESUME] Session reconnected.\n");
self.requestResume = YES;
}
}
}
- (void)blockThreadDuringSuspension {
self.suspended = YES;
"[THREAD BLOCK] Thread blocked while waiting for resume or cancel "
"request.\n");
while (!self.requestResume && !self.requestCancel) {
[NSThread sleepForTimeInterval:1];
}
if (self.requestResume) {
"[THREAD BLOCK] Got resume request.\n");
self.requestResume = NO;
} else {
"[THREAD BLOCK] Got cancel request.\n");
}
self.requestSuspend = NO;
self.suspended = NO;
}
@end