PubSub+ Messaging API For C  7.31.0.7
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex/ios/intro/HelloWorldSubExample.m
/*
* This source is distributed under the terms and conditions
* of any contract or contracts between Solace and you or
* your company. If there are no contracts in place use of
* this source is not authorized. No support is provided and
* no distribution, sharing with others or re-use of this
* source is authorized unless specifically stated in the
* contracts referred to above.
*
* Copyright 2012-2024 Solace Corporation. All rights reserved.
*/
#import "HelloWorldSubExample.h"
@implementation HelloWorldSubExample
@synthesize msgCount;
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
// Set example name and description
self.name = @"HelloWorldSub";
self.description = @"Demonstrates the basics of session creation, "
@"connection and receiving direct messages from a topic";
// Setup example parameters
[self.parameters addParameter:PARAMETER_DESTINATION_TOPIC];
[self.parameters removeParameter:PARAMETER_PASSWORD];
[self.parameters removeParameter:PARAMETER_COMPRESSION];
[self.parameters removeParameter:PARAMETER_LOGGING_LEVEL];
return self;
}
messageReceiveCallbackWithSession:
(solClient_opaqueSession_pt)opaqueSession_p
message:(solClient_opaqueMsg_pt)msg_p
userData:(void *)user_p {
// The message callback is invoked for each direct message received by the
// session. In this sample, the message is printed to the screen.
solClient_log(SOLCLIENT_LOG_NOTICE, "Received message:\n");
char buffer[5000];
solClient_msg_dump(msg_p, buffer, 5000);
self.msgCount++;
}
- (void)eventCallbackWithSession:(solClient_opaqueSession_pt)opaqueSession_p
eventInfo:
userData:(void *)user_p {
// The use of an empty function is necessary as the event callback function
// is mandatory for session creation.
}
- (void)run {
[super run];
// Context variables
// Session variables
// Session properties
const char *sessionProps[20];
int propIndex = 0;
self.msgCount = 0;
// Initialize the API; this must be done prior to first usage
solClient_log(SOLCLIENT_LOG_NOTICE, "HelloWorldSub initializing...\n");
// Create a context, and specify that the context thread be created
// automatically instead of having the application create its own
// context thread.
&context_p, &contextFuncInfo,
sizeof(contextFuncInfo));
// Configure the Session function information.
sessionFuncInfo.rxMsgInfo.callback_p = messageReceiveCallback;
sessionFuncInfo.rxMsgInfo.user_p = self.nullBridge_p;
sessionFuncInfo.eventInfo.callback_p = eventCallback;
sessionFuncInfo.eventInfo.user_p = self.nullBridge_p;
// Configure the session's properties
propIndex = 0;
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_HOST;
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_HOST].value
cStringUsingEncoding:NSASCIIStringEncoding];
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_VPN_NAME;
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_VPN].value
cStringUsingEncoding:NSASCIIStringEncoding];
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_USERNAME;
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_USERNAME].value
cStringUsingEncoding:NSASCIIStringEncoding];
sessionProps[propIndex] = NULL;
// Create the session
solClient_session_create((char **)sessionProps, context_p, &session_p,
&sessionFuncInfo, sizeof(sessionFuncInfo));
// Connect the session
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC].value
cStringUsingEncoding:NSASCIIStringEncoding]);
solClient_log(SOLCLIENT_LOG_NOTICE, "Waiting for message......\n");
while (self.msgCount < 1 &&
!self.requestCancel) {
[NSThread sleepForTimeInterval:1];
}
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC].value
cStringUsingEncoding:NSASCIIStringEncoding]);
// Cleanup solclient
[self cleanup];
}
@end