PubSub+ Messaging API For C  7.29.0.6
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex/ios/intro/HelloWorldWebPubExample.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 "HelloWorldWebPubExample.h"
@implementation HelloWorldWebPubExample
- (id)initWithExampleInterface:(ExampleInterface *)exampleInterface {
self = [super initWithExampleInterface:exampleInterface];
// Set example name and description
self.name = @"HelloWorldWebPub";
self.description = @"Demonstrates the basics of web messaging session "
@"creation, connection and publishing direct messages to a topic";
// Set up example parameters
[self.parameters parameterWithId:PARAMETER_HOST].value =
@"http://192.168.168.136";
[self.parameters addParameter:PARAMETER_DESTINATION_TOPIC];
[self.parameters addParameter:PARAMETER_WEB_PROTOCOL];
[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 use of an empty function is necessary as the message receive callback
// function is mandatory for session creation.
}
- (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;
// Message variables
solClient_opaqueMsg_pt msg_p = NULL;
const char *text_p = "Hello world!";
// Ensure HTTP and WS protocols are being used
if (![[[[self.parameters parameterWithId:PARAMETER_HOST].value
substringToIndex:4] lowercaseString] isEqualToString:@"http"] &&
![[[[self.parameters parameterWithId:PARAMETER_HOST].value
substringToIndex:4] lowercaseString] isEqualToString:@"ws"]) {
"This sample supports HTTP or WS transport protocols only.");
}
// Initialize the API; this must be done prior to first usage
solClient_log(SOLCLIENT_LOG_NOTICE, "HelloWorldWebPub 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));
// Message receive callback function and the Session event function
// are both mandatory. In this sample, default functions are used.
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];
if (![[self.parameters parameterWithId:PARAMETER_WEB_PROTOCOL].value
isEqualToString:@""]) {
sessionProps[propIndex++] =
sessionProps[propIndex++] =
[[self.parameters parameterWithId:PARAMETER_WEB_PROTOCOL].value
cStringUsingEncoding:NSASCIIStringEncoding];
}
sessionProps[propIndex] = NULL;
// Create the session
solClient_session_create((char **)sessionProps, context_p, &session_p,
&sessionFuncInfo, sizeof(sessionFuncInfo));
// Connect the session
// Allocate memory for the message that is to be sent
// Set the message delivery mode
// Set the destination
destination.dest =
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC].value
cStringUsingEncoding:NSASCIIStringEncoding];
solClient_msg_setDestination(msg_p, &destination, sizeof(destination));
// Add some content to the message
msg_p, text_p, (solClient_uint32_t)strlen((char *)text_p));
// Send the message
SOLCLIENT_LOG_NOTICE, "About to send message '%s' to topic '%s'...\n",
(char *)text_p,
[[self.parameters parameterWithId:PARAMETER_DESTINATION_TOPIC].value
cStringUsingEncoding:NSASCIIStringEncoding]);
solClient_session_sendMsg(session_p, msg_p);
// Free the message
solClient_log(SOLCLIENT_LOG_NOTICE, "Message sent. Exiting.\n");
// Cleanup solclient
[self cleanup];
}
@end