Creating a minimal coordination module

These basic steps allow you to build a minimal CM library program that establishes a connection to OpenSwitch:

  1. Allocate a context structure using cm_init.

  2. Create a CM instance using cm_create.

  3. Establish connection between the CM and Open Switch using cm_connect.

  4. Start the CM using cm_run.

  5. Destroy the CM instance using cm_destroy.

  6. 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);
}