PubSub+ Messaging API For C  7.31.0.7
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex/ios/examples/SdtPubSubMsgDepExample.m
/*
* Copyright 2009-2024 Solace Corporation. All rights reserved.
*/
#import "SdtPubSubMsgDepExample.h"
@implementation SdtPubSubMsgDepExample
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
// Set example name and description
self.name = @"SdtPubSubMsgDep";
self.description =
@"Demonstrates how to publish SDT map messages to a topic "
@"(dependent on 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 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];
"SdtPubSubMsgDepExample.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");
// 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 sessionConnected;
}
// 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;
}
// Create the binary attachment Stream.
msg_p, &streamContainer, 1024)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:
"solClient_msg_createBinaryAttachmentStream()"];
goto freeMessage;
}
// Add PI to the Stream.
if ((rc = solClient_container_addDouble(streamContainer, 3.141592654,
NULL)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_container_addDouble()"];
goto freeMessage;
}
// Add a String to the Stream.
if ((rc = solClient_container_addString(streamContainer, "message",
NULL)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_container_addString()"];
goto freeMessage;
}
// Create a User Property Map; only a MAP may be created in User Properties.
if ((rc = solClient_msg_createUserPropertyMap(msg_p, &userPropContainer,
1024)) != SOLCLIENT_OK) {
[self
handleErrorWithReturnCode:rc
errorString:"solClient_msg_createUserPropertyMap()"];
goto freeMessage;
}
// 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 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;
}
// 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;
}
// Wait one second after sending messages. This also gives 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