PubSub+ Messaging API For C  7.31.0.7
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Intro/HelloWorldSub.c
/*
* Copyright 2012-2024 Solace Corporation. All rights reserved.
*
* http://www.solace.com
*
* 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.
*
* HelloWorldSub
*
* This sample shows the basics of creating session, connecting a session,
* and receiving a direct message from a topic. This is meant to be a very
* basic example for demonstration purposes.
*/
#include "os.h"
/* Message Count */
static int msgCount = 0;
/*****************************************************************************
* messageReceiveCallback
*
* The message callback is invoked for each Direct message received by
* the Session. In this sample, the message is printed to the screen.
*****************************************************************************/
messageReceiveCallback ( solClient_opaqueSession_pt opaqueSession_p, solClient_opaqueMsg_pt msg_p, void *user_p )
{
printf ( "Received message:\n" );
solClient_msg_dump ( msg_p, NULL, 0 );
printf ( "\n" );
msgCount++;
}
/*****************************************************************************
* eventCallback
*
* The event callback function is mandatory for session creation.
*****************************************************************************/
void
eventCallback ( solClient_opaqueSession_pt opaqueSession_p,
solClient_session_eventCallbackInfo_pt eventInfo_p, void *user_p )
{
}
/*****************************************************************************
* main
*
* The entry point to the application.
*****************************************************************************/
int
main ( int argc, char *argv[] )
{
/* Context */
/* Session */
/* Session Properties */
const char *sessionProps[20];
int propIndex = 0;
if ( argc < 5 ) {
printf ( "Usage: HelloWorldSub <msg_backbone_ip:port> <vpn> <client-username> <topic>\n" );
return -1;
}
/*************************************************************************
* Initialize the API (and setup logging level)
*************************************************************************/
/* solClient needs to be initialized before any other API calls. */
printf ( "HelloWorldSub initializing...\n" );
/*************************************************************************
* Create a Context
*************************************************************************/
/*
* 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 ) );
/*************************************************************************
* Create and connect a Session
*************************************************************************/
/* Configure the Session function information. */
sessionFuncInfo.rxMsgInfo.callback_p = messageReceiveCallback;
sessionFuncInfo.rxMsgInfo.user_p = NULL;
sessionFuncInfo.eventInfo.callback_p = eventCallback;
sessionFuncInfo.eventInfo.user_p = NULL;
/* Configure the Session properties. */
propIndex = 0;
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_HOST;
sessionProps[propIndex++] = argv[1];
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_VPN_NAME;
sessionProps[propIndex++] = argv[2];
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_USERNAME;
sessionProps[propIndex++] = argv[3];
sessionProps[propIndex] = NULL;
/* Create the Session. */
solClient_session_create ( ( char ** ) sessionProps,
context_p,
&session_p, &sessionFuncInfo, sizeof ( sessionFuncInfo ) );
/* Connect the Session. */
printf ( "Connected.\n" );
/*************************************************************************
* Subscribe
*************************************************************************/
argv[4] );
/*************************************************************************
* Wait for message
*************************************************************************/
printf ( "Waiting for message......\n" );
fflush ( stdout );
while ( msgCount < 1 ) {
SLEEP ( 1 );
}
printf ( "Exiting.\n" );
/*************************************************************************
* Unsubscribe
*************************************************************************/
argv[4] );
/*************************************************************************
* Cleanup
*************************************************************************/
/* Cleanup solClient. */
return 0;
}