Mon Sep 20 2010 00:22:29

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: 145121 $");
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 
00050 static void clm_node_get_cb(SaInvocationT invocation, 
00051    const SaClmClusterNodeT *cluster_node, SaAisErrorT error);
00052 static void clm_track_cb(const SaClmClusterNotificationBufferT *notif_buffer,
00053    SaUint32T num_members, SaAisErrorT error);
00054 
00055 static const SaClmCallbacksT clm_callbacks = {
00056    .saClmClusterNodeGetCallback = clm_node_get_cb,
00057    .saClmClusterTrackCallback   = clm_track_cb,
00058 };
00059 
00060 static void clm_node_get_cb(SaInvocationT invocation, 
00061    const SaClmClusterNodeT *cluster_node, SaAisErrorT error)
00062 {
00063 
00064 }
00065 
00066 static void clm_track_cb(const SaClmClusterNotificationBufferT *notif_buffer,
00067    SaUint32T num_members, SaAisErrorT error)
00068 {
00069 
00070 }
00071 
00072 static char *ais_clm_show_members(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00073 {
00074    int i;
00075    SaClmClusterNotificationBufferT buf;
00076    SaClmClusterNotificationT notif[64];
00077    SaAisErrorT ais_res;
00078 
00079    switch (cmd) {
00080    case CLI_INIT:
00081       e->command = "ais show clm members";
00082       e->usage =
00083          "Usage: ais show clm members\n"
00084          "       List members of the cluster using the CLM (Cluster Membership) service.\n";
00085       return NULL;
00086 
00087    case CLI_GENERATE:
00088       return NULL;   /* no completion */
00089    }
00090 
00091    if (a->argc != e->args)
00092       return CLI_SHOWUSAGE;
00093 
00094    buf.notification = notif;
00095    buf.numberOfItems = ARRAY_LEN(notif);
00096 
00097    ais_res = saClmClusterTrack(clm_handle, SA_TRACK_CURRENT, &buf);
00098    if (ais_res != SA_AIS_OK) {
00099       ast_cli(a->fd, "Error retrieving current cluster members.\n");
00100       return CLI_FAILURE;
00101    }
00102 
00103    ast_cli(a->fd, "\n"
00104                "=============================================================\n"
00105                "=== Cluster Members =========================================\n"
00106                "=============================================================\n"
00107                "===\n");
00108 
00109    for (i = 0; i < buf.numberOfItems; i++) {
00110       SaClmClusterNodeT *node = &buf.notification[i].clusterNode;
00111 
00112       ast_cli(a->fd, "=== ---------------------------------------------------------\n"
00113                      "=== Node Name: %s\n"
00114                      "=== ==> ID: 0x%x\n"
00115                      "=== ==> Address: %s\n"
00116                      "=== ==> Member: %s\n",
00117                      (char *) node->nodeName.value, (int) node->nodeId, 
00118                      (char *) node->nodeAddress.value,
00119                      node->member ? "Yes" : "No");
00120 
00121       ast_cli(a->fd, "=== ---------------------------------------------------------\n"
00122                      "===\n");
00123    }
00124 
00125    ast_cli(a->fd, "=============================================================\n"
00126                   "\n");
00127 
00128    return CLI_SUCCESS;
00129 }
00130 
00131 static struct ast_cli_entry ais_cli[] = {
00132    AST_CLI_DEFINE(ais_clm_show_members, "List current members of the cluster"),
00133 };
00134 
00135 int ast_ais_clm_load_module(void)
00136 {
00137    SaAisErrorT ais_res;
00138 
00139    ais_res = saClmInitialize(&clm_handle, &clm_callbacks, &ais_version);
00140    if (ais_res != SA_AIS_OK) {
00141       ast_log(LOG_ERROR, "Could not initialize cluster membership service: %s\n",
00142          ais_err2str(ais_res));
00143       return -1;
00144    }
00145 
00146    ast_cli_register_multiple(ais_cli, ARRAY_LEN(ais_cli));
00147 
00148    return 0;
00149 }
00150 
00151 int ast_ais_clm_unload_module(void)
00152 {
00153    SaAisErrorT ais_res;
00154 
00155    ast_cli_unregister_multiple(ais_cli, ARRAY_LEN(ais_cli));
00156 
00157    ais_res = saClmFinalize(clm_handle);
00158    if (ais_res != SA_AIS_OK) {
00159       ast_log(LOG_ERROR, "Problem stopping cluster membership service: %s\n", 
00160          ais_err2str(ais_res));
00161       return -1;
00162    }
00163 
00164    return 0;
00165 }