These basic steps allow you to build a minimal CM library program that establishes a connection to OpenSwitch:
Allocate a context structure using cm_init.
Create a CM instance using cm_create.
Establish connection between the CM and Open Switch using cm_connect.
Start the CM using cm_run.
Destroy the CM instance using cm_destroy.
Deallocate the context structure using cm_exit.
For details about the routines used to build a CM, see Chapter 3, “Coordination Module Routines and Registered Procedures.”
The following example program shows the steps required to create a minimal CM.
#include <stdio.h>
#include <string.h>
#include <cspublic.h>
#include <cm.h>
int
main (
CS_INT argc,
CS_CHAR *argv[]
) {
char *username = "switch_coord";
char *password = "switch_coord";
char *server = "SWITCH1";
cm_ctx_t *ctx;
cm_t *cm;
/* Initialize and allocate a coordination module context structure **for this executable. */ if (cm_init(&ctx) != CS_SUCCEED) { fprintf (stderr, "cm_init: Failed.\n"); exit (1); }
/* Create a coordination module instance to manage an OpenSwitch server.
*/
if (cm_create(ctx, &cm) != CS_SUCCEED)
{
fprintf (stderr, "cm_create: Failed.\n");
cm_exit (ctx);
}
/* Establish a connection between the coordination module and a single
**OpenSwitch server.
*/
if (cm_connect(cm, server, username, password)
!= CS_SUCCEED)
{
fprintf (stderr, "cm_connect: Unable to connect to server\n" );
cm_destroy (cm);
cm_exit (ctx);
exit (1);
}
/* Start the coordination module.
*/
if (cm_run(ctx) != CS_SUCCEED)
{
fprintf (stderr, "cm_run: Failed.\n");
}
/* Destroy the coordination module instance.
*/
cm_destroy (cm);
/* Deallocate the coordination module instance and Exit.
*/
cm_exit (ctx);
exit (0);
}