Mon Jun 18 2012 03:55:31

Asterisk developer's documentation


clm.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007, Digium, Inc.
00005  *
00006  * Russell Bryant <russell@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!
00020  * \file
00021  * \author Russell Bryant <russell@digium.com>
00022  *
00023  * \brief Usage of the SAForum AIS (Application Interface Specification)
00024  *
00025  * \arg http://www.openais.org/
00026  *
00027  * This file contains the code specific to the use of the CLM
00028  * (Cluster Membership) Service.
00029  */
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 359053 $");
00034 
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <string.h>
00038 #include <unistd.h>
00039 #include <errno.h>
00040 
00041 #include "ais.h"
00042 
00043 #include "asterisk/module.h"
00044 #include "asterisk/utils.h"
00045 #include "asterisk/cli.h"
00046 #include "asterisk/logger.h"
00047 
00048 SaClmHandleT clm_handle;
00049 static SaAisErrorT clm_init_res;
00050 
00051 static void clm_node_get_cb(SaInvocationT invocation,
00052    const SaClmClusterNodeT *cluster_node, SaAisErrorT error);
00053 static void clm_track_cb(const SaClmClusterNotificationBufferT *notif_buffer,
00054    SaUint32T num_members, SaAisErrorT error);
00055 
00056 static const SaClmCallbacksT clm_callbacks = {
00057    .saClmClusterNodeGetCallback = clm_node_get_cb,
00058    .saClmClusterTrackCallback   = clm_track_cb,
00059 };
00060 
00061 static void clm_node_get_cb(SaInvocationT invocation,
00062    const SaClmClusterNodeT *cluster_node, SaAisErrorT error)
00063 {
00064 
00065 }
00066 
00067 static void clm_track_cb(const SaClmClusterNotificationBufferT *notif_buffer,
00068    SaUint32T num_members, SaAisErrorT error)
00069 {
00070    unsigned int i;
00071    unsigned int node_joined = 0;
00072 
00073    ast_debug(1, "Cluster membership changed. Number of members: %u\n", num_members);
00074 
00075    for (i = 0; i < notif_buffer->numberOfItems; i++) {
00076       SaClmClusterNotificationT *notif = notif_buffer->notification + i;
00077 
00078       if (notif->clusterChange == SA_CLM_NODE_JOINED) {
00079          node_joined = 1;
00080          break;
00081       }
00082    }
00083 
00084    if (node_joined) {
00085       ast_debug(1, "A node has joined the cluster, dumping event cache.\n");
00086       ast_ais_cmd(AST_AIS_CMD_MEMBERSHIP_CHANGED);
00087    }
00088 }
00089 
00090 static char *ais_clm_show_members(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00091 {
00092    int i;
00093    SaClmClusterNotificationBufferT buf;
00094    SaClmClusterNotificationT notif[64];
00095    SaAisErrorT ais_res;
00096 
00097    switch (cmd) {
00098    case CLI_INIT:
00099       e->command = "ais clm show members";
00100       e->usage =
00101          "Usage: ais clm show members\n"
00102          "       List members of the cluster using the CLM (Cluster Membership) service.\n";
00103       return NULL;
00104 
00105    case CLI_GENERATE:
00106       return NULL;   /* no completion */
00107    }
00108 
00109    if (a->argc != e->args)
00110       return CLI_SHOWUSAGE;
00111 
00112    buf.notification = notif;
00113    buf.numberOfItems = ARRAY_LEN(notif);
00114 
00115    ais_res = saClmClusterTrack(clm_handle, SA_TRACK_CURRENT, &buf);
00116    if (ais_res != SA_AIS_OK) {
00117       ast_cli(a->fd, "Error retrieving current cluster members.\n");
00118       return CLI_FAILURE;
00119    }
00120 
00121    ast_cli(a->fd, "\n"
00122                "=============================================================\n"
00123                "=== Cluster Members =========================================\n"
00124                "=============================================================\n"
00125                "===\n");
00126 
00127    for (i = 0; i < buf.numberOfItems; i++) {
00128       SaClmClusterNodeT *node = &buf.notification[i].clusterNode;
00129 
00130       ast_cli(a->fd, "=== ---------------------------------------------------------\n"
00131                      "=== Node Name: %s\n"
00132                      "=== ==> ID: 0x%x\n"
00133                      "=== ==> Address: %s\n"
00134                      "=== ==> Member: %s\n",
00135                      (char *) node->nodeName.value, (int) node->nodeId,
00136                      (char *) node->nodeAddress.value,
00137                      node->member ? "Yes" : "No");
00138 
00139       ast_cli(a->fd, "=== ---------------------------------------------------------\n"
00140                      "===\n");
00141    }
00142 
00143    ast_cli(a->fd, "=============================================================\n"
00144                   "\n");
00145 
00146    return CLI_SUCCESS;
00147 }
00148 
00149 static struct ast_cli_entry ais_cli[] = {
00150    AST_CLI_DEFINE(ais_clm_show_members, "List current members of the cluster"),
00151 };
00152 
00153 int ast_ais_clm_load_module(void)
00154 {
00155    SaAisErrorT ais_res;
00156 
00157    clm_init_res = saClmInitialize(&clm_handle, &clm_callbacks, &ais_version);
00158    if (clm_init_res != SA_AIS_OK) {
00159       ast_log(LOG_ERROR, "Could not initialize cluster membership service: %s\n",
00160          ais_err2str(clm_init_res));
00161       return -1;
00162    }
00163 
00164    ais_res = saClmClusterTrack(clm_handle, SA_TRACK_CHANGES, NULL);
00165    if (ais_res != SA_AIS_OK) {
00166       ast_log(LOG_ERROR, "Error starting tracking of cluster membership changes.\n");
00167    }
00168 
00169    ast_cli_register_multiple(ais_cli, ARRAY_LEN(ais_cli));
00170 
00171    return 0;
00172 }
00173 
00174 int ast_ais_clm_unload_module(void)
00175 {
00176    SaAisErrorT ais_res;
00177 
00178    if (clm_init_res != SA_AIS_OK) {
00179       return 0;
00180    }
00181 
00182    ast_cli_unregister_multiple(ais_cli, ARRAY_LEN(ais_cli));
00183 
00184    ais_res = saClmFinalize(clm_handle);
00185    if (ais_res != SA_AIS_OK) {
00186       ast_log(LOG_ERROR, "Problem stopping cluster membership service: %s\n",
00187          ais_err2str(ais_res));
00188       return -1;
00189    }
00190 
00191    return 0;
00192 }