Installing a callback handler

After compiling and running the CM, you must install callback handlers so the CM can detect connection requests coming in from OpenSwitch, and handle them accordingly.

In this example, the CM from the previous example is expanded to include a callback handler, which handles a dropped connection between the CM and OpenSwitch. You must call cm_set_prop to allow asynchronous callbacks; you must call cm_callback to install the callback handler.

This example shows the steps required to install the callback handler.

#include <stdio.h>
#include <string.h>
#include <cspublic.h>
#include <cm.h>

CS_STATIC CS_RETCODE CS_PUBLIC cm_lost_hdl();

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);
   }
   /* Allow asynchronous callbacks
   */
   if (cm_set_prop (cm, CM_P_ASYNC,
(CS_VOID*)&async) != CS_SUCCEED)
   {
      fprintf (stderr, "cm_callback: Unable to set async property\n");
      cm_destroy (cm);
      cm_exit (ctx);
   }
   /* Install the connection lost callback handler.
   */
   if (cm_callback (cm, CM_CB_LOST,
(CS_VOID*)cm_lost_hdl) != CS_SUCCEED)
   {
      fprintf(stderr, "cm_callback: Unable to install CM_CB_LOST handler\n");
      cm_destroy (cm);
      cm_exit (ctx);
      exit(1);
   }
   /* 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);
/* De-allocate the coordination module instance and Exit.
*/
cm_exit (ctx);

exit (0);

}
/*
* cm_lost_hdl():
*
   /* This is a coordination module handler function that is called every 
   ** time the connection is lost to OpenSwitch managed by cm. It is responsible
   ** for installing a timer callback that will attempt to reconnect to 
   ** the OpenSwitch every 10 seconds (see cm_time_connect()).
*/
CS_STATIC CS_RETCODE CS_PUBLIC cm_lost_hdl (
cm_t *cm
) {
   fprintf (stdout, "**** Connect Lost ******\n");

   if (cm_timer_add (cm, "LOST_TIMER", (CS_INT)10000,
      (cm_timer_cb*)cm_time_connect, (CS_VOID*)NULL, (CS_INT)0 ) != CS_SUCCEED)
   {
      fprintf (stderr, "cm_lost_hdl: Unable to add cm_time_connect\n");
      return CS_FAIL;
   }

   return CS_SUCCEED;
}
/*
* cm_time_connect():
*
   ** This function is installed as a timed callback from a CM_CB_LOST 
   ** callback handler. After it is installed, this function is called
   ** periodically to attempt to re-establish the coordination 
   ** module’s connection to its OpenSwitch. After the connection 
   ** is re-established, this timer removes itself from activity.
*/
CS_STATIC CS_RETCODE CS_PUBLIC cm_time_connect(
   cm_t    *cm,
   CS_CHAR *name,
   CS_VOID *data
) {
   fprintf (stdout, "cm_time_connect: Attempting re-connect...\n");

   if (cm_connect( cm, NULL, NULL, NULL ) == CS_SUCCEED)
   {
      if (cm_timer_rem( cm, name ) != CS_SUCCEED)
      {
         fprintf( stderr, "cm_time_connect: Unable to remove timer\n" );
         return CS_FAIL;
      }
   }
   return CS_SUCCEED;
}

After installing the callback handler, the CM in this example immediately detects when the OpenSwitch server goes down and starts a timer to ping the OpenSwitch server every ten seconds until the OpenSwitch server comes back online. If you want the CM to also respond to logins and failovers from the OpenSwitch server, you must create a complete CM such as the one in the next section.