#import "TopicDispatchExample.h"
@implementation TopicDispatchExample
@synthesize solMsgRxCount;
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
self.name = @"TopicDispatch";
self.description = @"Demonstrates the use of topic dispatch to direct "
@"received messages into specialized received data paths";
return self;
}
messageReceiveCallbackWithSession:
userData:(void *)user_p {
int index = ((__bridge NSNumber *)user_p).intValue;
int oldValue = ((NSNumber *)[solMsgRxCount objectAtIndex:index]).intValue;
solMsgRxCount[index] = [NSNumber numberWithInt:oldValue + 1];
}
- (void)run {
[super run];
const char *sessionProps[40];
int propIndex;
solMsgRxCount = [[NSMutableArray alloc] init];
for (int i = 0; i < 4; i++) {
solMsgRxCount[i] = [NSNumber numberWithInt:0];
}
callbackBridge_t bridges[4];
for (int i = 0; i < 4; i++) {
bridges[i].example_p = (__bridge void *)self;
bridges[i].user_p = (__bridge void *)[[NSNumber alloc] initWithInt:i];
}
[self handleErrorWithReturnCode:rc
errorString:"solClient_initialize()"];
goto notInitialized;
}
[self setLoggingLevel];
[self logCCSMPVersion];
&contextFuncInfo,
sizeof(contextFuncInfo))) !=
SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_context_create()"];
goto cleanup;
}
propIndex = 0;
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_USERNAME].value
cStringUsingEncoding:NSASCIIStringEncoding];
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_PASSWORD].value
cStringUsingEncoding:NSASCIIStringEncoding];
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_HOST].value
cStringUsingEncoding:NSASCIIStringEncoding];
sessionProps[propIndex++] = "3";
sessionProps[propIndex++] = "3";
sessionProps[propIndex++] =
([self.parameters parameterWithId:PARAMETER_COMPRESSION]
.value.boolValue)
? "9"
: "0";
if ([[self.parameters parameterWithId:PARAMETER_VPN].value
cStringUsingEncoding:NSASCIIStringEncoding][0]) {
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_VPN].value
cStringUsingEncoding:NSASCIIStringEncoding];
}
sessionProps[propIndex] = NULL;
(char **)sessionProps, context_p, &session_p, &sessionFuncInfo,
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_create()"];
goto cleanup;
}
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_connect()"];
goto cleanup;
}
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_topicSubscribe()"];
goto sessionConnected;
}
dispatchInfo.
user_p = &bridges[1];
[self
handleErrorWithReturnCode:rc
errorString:
"solClient_session_topicSubscribeWithDispatch()"];
goto sessionConnected;
}
dispatchInfo.
user_p = &bridges[2];
[self
handleErrorWithReturnCode:rc
errorString:
"solClient_session_topicSubscribeWithDispatch()"];
goto sessionConnected;
}
dispatchInfo.
user_p = &bridges[3];
[self
handleErrorWithReturnCode:rc
errorString:
"solClient_session_topicSubscribeWithDispatch()"];
goto sessionConnected;
}
[self publishMessageWithSession:session_p
topic:"a/c"
deliveryMode:SOLCLIENT_DELIVERY_MODE_DIRECT];
[self publishMessageWithSession:session_p
topic:"a/b"
deliveryMode:SOLCLIENT_DELIVERY_MODE_DIRECT];
[self publishMessageWithSession:session_p
topic:"c/d"
deliveryMode:SOLCLIENT_DELIVERY_MODE_DIRECT];
[self publishMessageWithSession:session_p
topic:"c/e"
deliveryMode:SOLCLIENT_DELIVERY_MODE_DIRECT];
[NSThread sleepForTimeInterval:1];
if (((NSNumber *)solMsgRxCount[0]).intValue == 1) {
"Received exactly one message on session callback as expected.\n");
} else {
"Received %d message(s) on session callback - 1 expected.\n",
solMsgRxCount[0]);
}
if (((NSNumber *)solMsgRxCount[1]).intValue == 1) {
"Received exactly one message on dispatch callback for topic 'a/b' "
"as expected.\n");
} else {
"Received %d message(s) on dispatch "
"callback for topic 'a/b' - 1 expected.\n",
solMsgRxCount[1]);
}
if (((NSNumber *)solMsgRxCount[2]).intValue == 2) {
"Received exactly two messages on dispatch callback for topic "
"'c/>' as expected.\n");
} else {
"Received %d message(s) on dispatch "
"callback for topic 'c/>' - 2 expected.\n",
solMsgRxCount[2]);
}
if (((NSNumber *)solMsgRxCount[3]).intValue == 1) {
"Received exactly one message on dispatch callback for topic 'c/d' "
"as expected.\n");
} else {
"Received %d message(s) on dispatch "
"callback for topic 'c/d' - 1 expected.\n",
solMsgRxCount[3]);
}
sessionConnected:
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_disconnect()"];
}
cleanup:
[self handleErrorWithReturnCode:rc errorString:"solClient_cleanup()"];
}
notInitialized:
[self cleanup];
}
@end