PubSub+ Messaging API For C  7.29.0.6
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex/ios/examples/SempGetOverMbExample.m
/*
* Copyright 2009-2024 Solace Corporation. All rights reserved.
*/
#import "SempGetOverMbExample.h"
@implementation SempGetOverMbExample
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
// Set example name and description
self.name = @"SempGetOverMb";
self.description =
@"Demonstrates how to perform a SEMP request over the message backbone";
// Setup example parameters
[self.parameters addParameter:PARAMETER_SEMP_VERSION];
return self;
}
- (void)sempRequestRequestAndReplyWithSession:
sempVersion:(const char *)sempVersion {
solClient_field_t routerName;
char sempTopic[SOLCLIENT_BUFINFO_MAX_TOPIC_SIZE + 1];
char sempRequest[512];
void *sempReplyBuf_p;
solClient_uint32_t sempReplyBufSize;
solClient_uint32_t ix;
snprintf(sempRequest, sizeof(sempRequest),
"<rpc "
"semp-version=\"%s\"><show><"
"client><name>*</name></client></"
"show></rpc>",
sempVersion);
// Allocate a message for requests.
if ((rc = solClient_msg_alloc(&msg_p)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc errorString:"solClient_msg_alloc()"];
return;
}
// Get the hostname of the appliance. The hostname is part of the Topic
// used to perform SEMP requests.
session_p, SOLCLIENT_SESSION_PEER_ROUTER_NAME, &routerName,
sizeof(routerName))) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_getCapability()"];
goto freeMsg;
}
// Construct the SEMP topic.
snprintf(sempTopic, sizeof(sempTopic), COMMON_SEMP_TOPIC_FORMAT,
routerName.value.string);
// Set SEMP topic as the destination for the message.
destination.dest = sempTopic;
msg_p, &destination, sizeof(destination))) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_setDestination()"];
goto freeMsg;
}
// Set the binary attachment of the message to the SEMP request.
msg_p, (void *)sempRequest,
(solClient_uint32_t)strlen(sempRequest))) != SOLCLIENT_OK) {
[self
handleErrorWithReturnCode:rc
errorString:"solClient_msg_setBinaryAttachmentPtr()"];
goto freeMsg;
}
// Send a blocking request.
solClient_log(SOLCLIENT_LOG_NOTICE, "REQUEST: %s\n", sempRequest);
solClient_log(SOLCLIENT_LOG_NOTICE, "REQUEST ADDRESS: %s\n", sempTopic);
if ((rc = solClient_session_sendRequest(session_p, msg_p, &replyMsg_p,
5000)) == SOLCLIENT_OK) {
// The reply is in the binary attachment, so a pointer is returned.
replyMsg_p, &sempReplyBuf_p, &sempReplyBufSize)) !=
[self handleErrorWithReturnCode:
rc errorString:"solClient_msg_getBinaryAttachmentPtr()"];
goto freeReplyMsg;
}
// The SEMP reply is XML, but not in a string format. It need to be
// printed to the screen character by character.
if (sempReplyBufSize >= 5000) {
"SEMP reply exceeds buffer length and will be truncated.");
}
char buffer[5000];
for (ix = 0; ix < 4999; ix++) {
buffer[ix] = ((char *)sempReplyBuf_p)[ix];
}
buffer[ix] = (char)0;
solClient_log(SOLCLIENT_LOG_NOTICE, "%s\n\n", buffer);
freeReplyMsg:
// Done with the reply message, so free it.
if ((rc = solClient_msg_free(&replyMsg_p)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_free()"];
goto freeMsg;
}
} else {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_sendRequest()"];
goto freeMsg;
}
freeMsg:
// Finally, free the request message.
if ((rc = solClient_msg_free(&msg_p)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc errorString:"solClient_msg_free()"];
}
}
- (void)run {
[super run];
char sempVersion[256];
// Context variables
// Session variables
// If the user specified the semp version string, override the default
int sempVersionLength = (int)strlen(
[[self.parameters parameterWithId:PARAMETER_SEMP_VERSION].value
cStringUsingEncoding:NSASCIIStringEncoding]);
for (int i = 0; i < sempVersionLength && i < 255; i++) {
sempVersion[i] =
[[self.parameters parameterWithId:PARAMETER_SEMP_VERSION].value
cStringUsingEncoding:NSASCIIStringEncoding][i];
}
sempVersion[sempVersionLength] = (char)0;
// 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(SOLCLIENT_LOG_NOTICE, "SempGetOverMbExample.m (Copyright 2009-2024 Solace Corporation. All rights reserved.)\n");
[self logCCSMPVersion];
// Create a context, and specify that the context thread 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:messageReceiveCallback
eventCallback:eventCallback]) !=
[self handleErrorWithReturnCode:
rc errorString:"createAndConnectSessionWithContext:session:"
"messageCallback:eventCallback:userData:"];
goto cleanup;
}
// A SEMP request is sent, and the reply is output to the screen.
[self sempRequestRequestAndReplyWithSession:session_p
sempVersion:sempVersion];
// 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