PubSub+ Messaging API For C  7.29.0.6
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex/ios/examples/EventMonitorExample.m
/*
* Copyright 2009-2024 Solace Corporation. All rights reserved.
*/
#import "EventMonitorExample.h"
@implementation EventMonitorExample
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
// Set example name and description
self.name = @"EventMonitor";
self.description = @"Demonstrates how to monitor appliance events using an "
@"event subscription";
// Setup example parameters
[self.parameters addParameter:PARAMETER_NUM_MESSAGES];
return self;
}
messageReceiveCallbackWithSession:
(solClient_opaqueSession_pt)opaqueSession_p
message:(solClient_opaqueMsg_pt)msg_p
userData:(void *)user_p {
void *binaryAttachmentBuffer_p;
solClient_uint32_t binaryAttachmentBufferSize;
msg_p, &destination, sizeof(destination))) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_msg_getDestination()"];
}
msg_p, &binaryAttachmentBuffer_p, &binaryAttachmentBufferSize)) !=
[self
handleErrorWithReturnCode:rc
errorString:"solClient_msg_getBinaryAttachmentPtr()"];
}
// Log event message
solClient_log(SOLCLIENT_LOG_NOTICE, "*** Event Message Received ***\n");
solClient_log(SOLCLIENT_LOG_NOTICE, "Topic:\t%s\n", destination.dest);
(char *)binaryAttachmentBuffer_p);
// Returning SOLCLIENT_CALLBACK_OK causes the API to free the memory
// used by the message. This is important to avoid leaks.
}
- (solClient_returnCode_t)triggerSecondaryConnectionWithContext:
if ((rc = [self
createAndConnectSessionWithContext:context_p
session:&session_p
messageCallback:messageReceivePrintMsgCallback
eventCallback:eventCallback]) !=
[self handleErrorWithReturnCode:
rc errorString:"createAndConnectSessionWithContext:session:"
"messageCallback:eventCallback:userData:"];
return rc;
}
COMMON_MY_SAMPLE_TOPIC)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_topicSubscribe()"];
}
if ((rc = solClient_session_disconnect(session_p)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_disconnect()"];
}
return rc;
}
- (void)run {
[super run];
// Context variables
// Session variables
// When event message publishing is enabled for a message VPN, clients can
// subscribe to a specific topic to receive the event messages.
//
// Format for Event topics:
// #LOG/<level>/<type>/<appliance hostname>/<event name>/...
char *eventTopicFormat = "#LOG/INFO/CLIENT/%s/CLIENT_CLIENT_CONNECT/>";
char eventTopic[SOLCLIENT_BUFINFO_MAX_TOPIC_SIZE + 1];
solClient_field_t routerName;
// 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];
"EventMonitorExample.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;
}
// Get the hostname of the appliance. The hostname is part of the Topic
// used to subscribe to Event Messages.
session_p, SOLCLIENT_SESSION_PEER_ROUTER_NAME, &routerName,
sizeof(routerName))) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_getCapability()"];
goto sessionConnected;
}
snprintf(eventTopic, sizeof(eventTopic), eventTopicFormat,
routerName.value.string);
eventTopic)) != SOLCLIENT_OK) {
[self handleErrorWithReturnCode:rc
errorString:"solClient_session_topicSubscribe()"];
goto sessionConnected;
}
if ((rc = [self triggerSecondaryConnectionWithContext:context_p]) !=
[self handleErrorWithReturnCode:rc
errorString:"triggerSecondaryConnection()"];
goto sessionConnected;
}
// Sleep to allow reception of event.
[NSThread sleepForTimeInterval:1];
solClient_log(SOLCLIENT_LOG_NOTICE, "Cleaning up.\n");
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