PubSub+ Messaging API For C  7.31.0.7
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex/ios/examples/SdtPubSubMsgIndepExample.m
/*
* Copyright 2009-2024 Solace Corporation. All rights reserved.
*/
#import "SdtPubSubMsgIndepExample.h"
@implementation SdtPubSubMsgIndepExample
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
// Set example name and description
self.name = @"SdtPubSubMsgIndep";
self.description =
@"Demonstrates how to publish SDT map messages to a topic "
@"(independent of a Solace message)";
return self;
}
- (void)run {
[super run];
// Context variables
// Session variables
// Message variables
solClient_opaqueMsg_pt msg_p = NULL;
int msgsSent = 0;
solClient_opaqueContainer_pt streamContainer = NULL;
solClient_opaqueContainer_pt userPropContainer = NULL;
char stream[1024];
char map[1024];
char messageField[32];
// 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, "SdtPubSubMsgIndepExample.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 session.");
if ((rc = [self
createAndConnectSessionWithContext:context_p
session:&session_p
messageCallback:messageReceivePrintMsgCallback
eventCallback:eventCallback]) !=
[self handleErrorWithReturnCode:
rc errorString:"createAndConnectSessionWithContext:session:"
"messageCallback:eventCallback:userData:"];
goto cleanup;
}
COMMON_MY_SAMPLE_TOPIC)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_topicSubscribe()"];
goto sessionConnected;
}
solClient_log(SOLCLIENT_LOG_INFO, "Publishing messages.\n");
// Create the message-independent Stream.
&streamContainer, stream, sizeof(stream))) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_container_createStream()"];
goto sessionConnected;
}
// Add PI to the Stream.
if ((rc = solClient_container_addDouble(streamContainer, 3.141592654,
NULL)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_container_addDouble()"];
goto freeContainer;
}
// Add a String to the Stream.
if ((rc = solClient_container_addString(streamContainer, "message",
NULL)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_container_addString()"];
goto freeContainer;
}
// Create the message independent Map.
if ((rc = solClient_container_createMap(&userPropContainer, map,
sizeof(map))) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_container_createMap()"];
goto sessionConnected;
}
// Add the largest known Mersenne Exponent to the Map.
if ((rc = solClient_container_addInt32(userPropContainer, 43112609,
"mersenne")) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_container_addInt32()"];
goto freeContainer;
}
// Allocate memory for the message to be sent.
if ((rc = solClient_msg_alloc(&msg_p)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc errorString:"solClient_msg_alloc()"];
goto freeContainer;
}
// Set the message delivery mode
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_setDeliveryMode()"];
goto freeMessage;
}
// Set the destination
destination.dest = COMMON_MY_SAMPLE_TOPIC;
msg_p, &destination, sizeof(destination))) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_setDestination()"];
goto freeMessage;
}
for (msgsSent = 0;
msgsSent < 10 &&
!self.requestCancel;
++msgsSent) {
if ((rc = solClient_container_deleteField(userPropContainer,
"message")) != SOLCLIENT_OK) {
[self
handleErrorWithReturnCode:rc
errorString:"solClient_container_deleteField()"];
goto freeMessage;
}
snprintf(messageField, 32, "message%d", (msgsSent + 1));
// Populate the Map with a string.
if ((rc = solClient_container_addString(userPropContainer, messageField,
"message")) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_container_addString()"];
goto freeMessage;
}
// Set the binary attachment that is to be the Stream.
msg_p, streamContainer)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:
rc errorString:
"solClient_msg_setBinaryAttachmentContainer()"];
goto freeMessage;
}
// Set the user property map attachment that is to be the Map.
if ((rc = solClient_msg_setUserPropertyMap(msg_p, userPropContainer)) !=
[self
handleErrorWithReturnCode:rc
errorString:"solClient_msg_setUserPropertyMap()"];
goto freeMessage;
}
// Send the message
if ((rc = solClient_session_sendMsg(session_p, msg_p)) !=
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_sendMsg()"];
goto freeMessage;
}
}
freeMessage:
if ((rc = solClient_msg_free(&msg_p)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc errorString:"solClient_msg_free()"];
goto sessionConnected;
}
freeContainer:
// Wait one second after sending messages. This will also give time
// for the final message to be received.
[NSThread sleepForTimeInterval:1];
COMMON_MY_SAMPLE_TOPIC)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_topicSubscribe()"];
goto sessionConnected;
}
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