Thu Sep 7 01:03:03 2017

Asterisk developer's documentation


res_ais.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007 - 2008, 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 common code between the uses of the different AIS
00028  * services.
00029  *
00030  * \note This module is still considered experimental, as it exposes the
00031  * internal binary format of events between Asterisk servers over a network.
00032  * However, this format is still subject to change between 1.6.X releases.
00033  */
00034 
00035 /*** MODULEINFO
00036    <depend>ais</depend>
00037    <support_level>extended</support_level>
00038  ***/
00039 
00040 #include "asterisk.h"
00041 
00042 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 359053 $");
00043 
00044 #include <stdlib.h>
00045 #include <stdio.h>
00046 #include <string.h>
00047 #include <unistd.h>
00048 #include <errno.h>
00049 #include <signal.h>
00050 #include <pthread.h>
00051 
00052 #include "ais/ais.h"
00053 
00054 #include "asterisk/module.h"
00055 #include "asterisk/options.h"
00056 #include "asterisk/logger.h"
00057 #include "asterisk/channel.h"
00058 #include "asterisk/utils.h"
00059 #include "asterisk/cli.h"
00060 
00061 static struct {
00062    pthread_t id;
00063    int alert_pipe[2];
00064    unsigned int stop:1;
00065 } dispatch_thread = {
00066    .id = AST_PTHREADT_NULL,
00067    .alert_pipe = { -1, -1 },
00068 };
00069 
00070 SaVersionT ais_version = { 'B', 1, 1 };
00071 
00072 static const struct ais_error {
00073    SaAisErrorT error;
00074    const char *desc;
00075 } ais_errors[] = {
00076         { SA_AIS_OK, "OK" },
00077         { SA_AIS_ERR_LIBRARY, "Library Error" },
00078         { SA_AIS_ERR_VERSION, "Version Not Compatible" },
00079         { SA_AIS_ERR_INIT, "Callback Not Registered" },
00080         { SA_AIS_ERR_TIMEOUT, "Timeout" },
00081         { SA_AIS_ERR_TRY_AGAIN , "Try Again" },
00082         { SA_AIS_ERR_INVALID_PARAM, "Invalid Parameter" },
00083         { SA_AIS_ERR_NO_MEMORY, "No Memory" },
00084         { SA_AIS_ERR_BAD_HANDLE, "Invalid Handle" },
00085         { SA_AIS_ERR_BUSY, "Resource Already In Use" },
00086         { SA_AIS_ERR_ACCESS, "Access Denied" },
00087         { SA_AIS_ERR_NOT_EXIST, "Does Not Exist" },
00088         { SA_AIS_ERR_NAME_TOO_LONG, "Name Too Long" },
00089         { SA_AIS_ERR_EXIST, "Already Exists" },
00090         { SA_AIS_ERR_NO_SPACE, "Buffer Too Small" },
00091         { SA_AIS_ERR_INTERRUPT, "Request Interrupted" },
00092         { SA_AIS_ERR_NAME_NOT_FOUND, "Name Not Found" },
00093         { SA_AIS_ERR_NO_RESOURCES, "Not Enough Resources" },
00094         { SA_AIS_ERR_NOT_SUPPORTED, "Requested Function Not Supported" },
00095         { SA_AIS_ERR_BAD_OPERATION, "Operation Not Allowed" },
00096         { SA_AIS_ERR_FAILED_OPERATION, "Operation Failed" },
00097         { SA_AIS_ERR_MESSAGE_ERROR, "Communication Error" },
00098         { SA_AIS_ERR_QUEUE_FULL, "Destination Queue Full" },
00099         { SA_AIS_ERR_QUEUE_NOT_AVAILABLE, "Destination Queue Not Available" },
00100         { SA_AIS_ERR_BAD_FLAGS, "Invalid Flags" },
00101         { SA_AIS_ERR_TOO_BIG, "Value Too Large" },
00102         { SA_AIS_ERR_NO_SECTIONS, "No More Sections to Initialize" },
00103 };
00104 
00105 const char *ais_err2str(SaAisErrorT error)
00106 {
00107    int x;
00108 
00109    for (x = 0; x < ARRAY_LEN(ais_errors); x++) {
00110       if (ais_errors[x].error == error)
00111          return ais_errors[x].desc;
00112    }
00113 
00114    return "Unknown";
00115 }
00116 
00117 static void *dispatch_thread_handler(void *data)
00118 {
00119    SaSelectionObjectT clm_fd, evt_fd;
00120    int res;
00121    struct pollfd pfd[3] = {
00122       { .events = POLLIN, },
00123       { .events = POLLIN, },
00124       { .events = POLLIN, },
00125    };
00126    SaAisErrorT ais_res;
00127 
00128    ais_res = saClmSelectionObjectGet(clm_handle, &clm_fd);
00129    if (ais_res != SA_AIS_OK) {
00130       ast_log(LOG_ERROR, "Failed to retrieve select fd for CLM service.  "
00131          "This module will not operate.\n");
00132       return NULL;
00133    }
00134 
00135    ais_res = saEvtSelectionObjectGet(evt_handle, &evt_fd);
00136    if (ais_res != SA_AIS_OK) {
00137       ast_log(LOG_ERROR, "Failed to retrieve select fd for EVT service.  "
00138          "This module will not operate.\n");
00139       return NULL;
00140    }
00141 
00142    pfd[0].fd = clm_fd;
00143    pfd[1].fd = evt_fd;
00144    pfd[2].fd = dispatch_thread.alert_pipe[0];
00145 
00146    while (!dispatch_thread.stop) {
00147       pfd[0].revents = 0;
00148       pfd[1].revents = 0;
00149       pfd[2].revents = 0;
00150 
00151       res = ast_poll(pfd, ARRAY_LEN(pfd), -1);
00152       if (res == -1 && errno != EINTR && errno != EAGAIN) {
00153          ast_log(LOG_ERROR, "Select error (%s) dispatch thread going away now, "
00154             "and the module will no longer operate.\n", strerror(errno));
00155          break;
00156       }
00157 
00158       if (pfd[0].revents & POLLIN) {
00159          saClmDispatch(clm_handle,   SA_DISPATCH_ALL);
00160       }
00161       if (pfd[1].revents & POLLIN) {
00162          saEvtDispatch(evt_handle,   SA_DISPATCH_ALL);
00163       }
00164       if (pfd[2].revents & POLLIN) {
00165          enum ast_ais_cmd cmd;
00166          ast_debug(1, "Got a command in the poll() loop\n");
00167          if (read(dispatch_thread.alert_pipe[0], &cmd, sizeof(cmd)) != -1) {
00168             switch (cmd) {
00169             case AST_AIS_CMD_MEMBERSHIP_CHANGED:
00170                ast_ais_evt_membership_changed();
00171                break;
00172             case AST_AIS_CMD_EXIT:
00173                break;
00174             }
00175          }
00176       }
00177    }
00178 
00179    return NULL;
00180 }
00181 
00182 int ast_ais_cmd(enum ast_ais_cmd cmd)
00183 {
00184    int res;
00185 
00186    res = write(dispatch_thread.alert_pipe[1], (char *) &cmd, sizeof(cmd));
00187 
00188    ast_debug(1, "AIS cmd: %d, res: %d\n", cmd, res);
00189 
00190    return res;
00191 }
00192 
00193 static int load_module(void)
00194 {
00195    if (pipe(dispatch_thread.alert_pipe) == -1) {
00196       ast_log(LOG_ERROR, "Failed to create alert pipe: %s (%d)\n",
00197             strerror(errno), errno);
00198       goto return_error;
00199    }
00200 
00201    if (ast_ais_clm_load_module())
00202       goto clm_failed;
00203 
00204    if (ast_ais_evt_load_module())
00205       goto evt_failed;
00206 
00207    if (ast_pthread_create_background(&dispatch_thread.id, NULL, 
00208       dispatch_thread_handler, NULL)) {
00209       ast_log(LOG_ERROR, "Error starting AIS dispatch thread.\n");
00210       goto dispatch_failed;
00211    }
00212 
00213    return AST_MODULE_LOAD_SUCCESS;
00214 
00215 dispatch_failed:
00216    ast_ais_evt_unload_module();
00217 evt_failed:
00218    ast_ais_clm_unload_module();
00219 clm_failed:
00220    close(dispatch_thread.alert_pipe[0]);
00221    close(dispatch_thread.alert_pipe[1]);
00222 return_error:
00223    return AST_MODULE_LOAD_DECLINE;
00224 }
00225 
00226 static int unload_module(void)
00227 {
00228    ast_ais_clm_unload_module();
00229    ast_ais_evt_unload_module();
00230 
00231    if (dispatch_thread.id != AST_PTHREADT_NULL) {
00232       dispatch_thread.stop = 1;
00233       if (ast_ais_cmd(AST_AIS_CMD_EXIT) == -1) {
00234          ast_log(LOG_ERROR, "Failed to write to pipe: %s (%d)\n",
00235                strerror(errno), errno);
00236       }
00237       pthread_join(dispatch_thread.id, NULL);
00238    }
00239 
00240    if (dispatch_thread.alert_pipe[0] != -1) {
00241       close(dispatch_thread.alert_pipe[0]);
00242       dispatch_thread.alert_pipe[0] = -1;
00243    }
00244 
00245    if (dispatch_thread.alert_pipe[1] != -1) {
00246       close(dispatch_thread.alert_pipe[1]);
00247       dispatch_thread.alert_pipe[1] = -1;
00248    }
00249 
00250    return 0;
00251 }
00252 
00253 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SAForum AIS");

Generated on 7 Sep 2017 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1