PubSub+ Messaging API For C  7.31.0.7
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex/ios/examples/AsyncCacheRequestExample.m
/*
* Copyright 2009-2024 Solace Corporation. All rights reserved.
*/
#import "AsyncCacheRequestExample.h"
@implementation AsyncCacheRequestExample
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
// Set example name and description
self.name = @"AsyncCacheRequest";
self.description =
@"Demonstrates how to create a cache session and send an "
@"asynchronous cache request";
// Setup example parameters
[self.parameters addParameter:PARAMETER_DESTINATION_TOPIC];
[self.parameters addParameter:PARAMETER_CACHE_NAME];
return self;
}
- (void)cacheEventCallbackWithSession:
(solClient_opaqueSession_pt)opaqueSession_p
eventInfo:(solCache_eventCallbackInfo_pt)eventInfo_p
userData:(void *)user_p {
"cacheEventCallbackWithSession:eventInfo:userData: called: \n"
"\tevent: %s\n"
"\ttopic: %s\n"
"\tresponseCode: (%d) %s\n"
"\tsubCode: (%d) %s\n"
"\tcacheRequestId: %llu\n\n",
eventInfo_p->topic, eventInfo_p->rc,
eventInfo_p->subCode,
eventInfo_p->cacheRequestId);
switch (eventInfo_p->cacheEvent) {
switch (eventInfo_p->rc) {
// Non-error events are logged at the INFO level.
"event=SOLCACHE_EVENT_REQUEST_"
"COMPLETED_NOTICE,rc=SOLCLIENT_"
"OK") break;
switch (eventInfo_p->subCode) {
"received event=SOLCACHE_EVENT_REQUEST_COMPLETED_NOTICE,"
"rc=SOLCLIENT_INCOMPLETE, subCode=%s",
break;
"received event=SOLCACHE_EVENT_REQUEST_COMPLETED_NOTICE,"
"rc=SOLCLIENT_INCOMPLETE, subCode=%s",
break;
"received event=SOLCACHE_EVENT_REQUEST_COMPLETED_NOTICE,"
"rc=SOLCLIENT_INCOMPLETE, subCode=%s",
break;
default:
"received event=SOLCACHE_EVENT_REQUEST_COMPLETED_NOTICE,"
"rc=SOLCLIENT_INCOMPLETE, with unusual subcode subCode=%d",
eventInfo_p->subCode);
break;
}
break;
"received "
"event=SOLCACHE_EVENT_REQUEST_"
"COMPLETED_NOTICE,rc=SOLCLIENT_"
"FAIL") break;
default:
"with unusual rc=%d",
eventInfo_p->rc);
break;
}
break;
default:
"received unusual event=%d for cache",
eventInfo_p->cacheEvent);
break;
}
}
- (void)run {
[super run];
// Context variables
// Session variables
// Cache Session
const char *cacheProps[20];
int propIndex = 0;
solClient_uint64_t cacheRequestId = 1;
// Callback bridge
callbackBridge_t bridge;
// Initialize the API; this must be done prior to first usage
[self handleErrorWithReturnCode:rc
errorString:"solClient_initialize()"];
goto notInitialized;
}
// Set up logging level and log example and API information
[self setLoggingLevel];
SOLCLIENT_LOG_NOTICE,
"AsyncCacheRequestExample.m (Copyright 2009-2024 Solace Corporation. All rights reserved.)\n");
[self logCCSMPVersion];
// Create a context, and specify that the context thread should be created
// automatically instead of having the application create its own
// context thread.
solClient_log(SOLCLIENT_LOG_INFO, "Creating solClient context");
&contextFuncInfo, sizeof(contextFuncInfo))) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_context_create()"];
goto cleanup;
}
solClient_log(SOLCLIENT_LOG_INFO, "Creating solClient Sessions.");
if ((rc = [self
createAndConnectSessionWithContext:context_p
session:&session_p
messageCallback:messageReceivePrintMsgCallback
eventCallback:eventCallback]) !=
[self handleErrorWithReturnCode:
rc errorString:"createAndConnectSessionWithContext:session:"
"messageCallback:eventCallback:userData:"];
goto cleanup;
}
// Message published simply to ensure that there is one cached
if ((rc = [self
publishMessageWithSession:
session_p topic:(char *)
[[self.parameters
parameterWithId:PARAMETER_DESTINATION_TOPIC]
.value
cStringUsingEncoding:NSASCIIStringEncoding]
[self
handleErrorWithReturnCode:
rc errorString:"publishMessageWithSession:topic:deliveryMode:"];
goto sessionConnected;
}
propIndex = 0;
cacheProps[propIndex++] = SOLCLIENT_CACHESESSION_PROP_CACHE_NAME;
cacheProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_CACHE_NAME].value
cStringUsingEncoding:NSASCIIStringEncoding];
cacheProps[propIndex] = NULL;
(const char *const *)cacheProps, session_p, &cacheSession_p)) !=
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_createCacheSession"];
goto sessionConnected;
}
solClient_log(SOLCLIENT_LOG_NOTICE, "Sending cache request.\n\n");
bridge.example_p = (__bridge void *)self;
bridge.user_p = &session_p;
cacheSession_p,
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC]
.value cStringUsingEncoding:NSASCIIStringEncoding],
cacheRequestId, cacheEventCallback, &bridge, cacheFlags, 0)) !=
[self handleErrorWithReturnCode:
rc errorString:"solClient_cacheSession_sendCacheRequest()"];
goto cleanupCache;
}
solClient_log(SOLCLIENT_LOG_NOTICE, "Cache request sent.\n\n");
// The default timeout to wait for a response from the cache is 10 seconds.
solClient_log(SOLCLIENT_LOG_NOTICE,
"Waiting for cache response. Sleeping for 11 seconds.\n\n");
for (int i = 0; i < 11 && !self.requestCancel; i++) {
[NSThread sleepForTimeInterval:1];
}
solClient_log(SOLCLIENT_LOG_NOTICE, "Exiting.\n");
cleanupCache:
if ((rc = solClient_cacheSession_destroy(&cacheSession_p)) !=
[self handleErrorWithReturnCode:rc
errorString:"solClient_cacheSession_destroy()"];
}
sessionConnected:
// Disconnect the session
if ((rc = solClient_session_disconnect(session_p)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_disconnect()"];
}
cleanup:
// Cleanup solclient
if ((rc = solClient_cleanup()) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc errorString:"solClient_cleanup()"];
}
notInitialized:
[self cleanup];
}
@end