[asterisk-commits] branch oej/test-this-branch r10685 - /team/oej/test-this-branch/pbx/pbx_config.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Feb 21 13:18:28 MST 2006


Author: oej
Date: Tue Feb 21 14:18:25 2006
New Revision: 10685

URL: http://svn.digium.com/view/asterisk?rev=10685&view=rev
Log:
Adding manager events for dialplan changes

Modified:
    team/oej/test-this-branch/pbx/pbx_config.c

Modified: team/oej/test-this-branch/pbx/pbx_config.c
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/pbx/pbx_config.c?rev=10685&r1=10684&r2=10685&view=diff
==============================================================================
--- team/oej/test-this-branch/pbx/pbx_config.c (original)
+++ team/oej/test-this-branch/pbx/pbx_config.c Tue Feb 21 14:18:25 2006
@@ -41,6 +41,14 @@
 #include "asterisk/logger.h"
 #include "asterisk/cli.h"
 #include "asterisk/callerid.h"
+#include "asterisk/manager.h"
+
+enum dialplanreloadreasons {
+	PBX_MODULE_LOAD,
+	PBX_CLI_RELOAD,
+	PBX_MODULE_RELOAD,
+	PBX_MANAGER_RELOAD,
+};
 
 #ifdef __AST_DEBUG_MALLOC
 static void FREE(void *ptr)
@@ -1421,13 +1429,31 @@
 	return RESULT_SUCCESS;
 }
 
-static int pbx_load_module(void);
+static int pbx_load_module(enum dialplanreloadreasons reason);
 
 static int handle_reload_extensions(int fd, int argc, char *argv[])
 {
-	if (argc!=2) return RESULT_SHOWUSAGE;
-	pbx_load_module();
+	if (argc!=2)
+		return RESULT_SHOWUSAGE;
+	pbx_load_module(PBX_CLI_RELOAD);
 	return RESULT_SUCCESS;
+}
+
+static char mandescr_extreload[] = 
+  "Description: Reload dial plan. ExtensionsReload event will\n"
+  "be sent after reload reporting number of loaded extensions,\n"
+  "contexts and switches.\n\n"
+  "Variables: (Names marked with * are required)\n"
+  "     ActionID: ActionID to be returned in response\n";
+
+/*!	\brief Reload dial plan from manager API
+	\ingroup Group_AMI
+*/
+static int action_extreload(struct mansession *s, struct message *m)
+{     
+	pbx_load_module(PBX_MANAGER_RELOAD);
+	astman_send_ack(s, m, "Reload initiated");
+	return 0;
 }
 
 static char *complete_context_remove_ignorepat(const char *line, const char *word,
@@ -1604,10 +1630,12 @@
 	ast_cli_unregister(&context_add_ignorepat_cli);
 	ast_cli_unregister(&reload_extensions_cli);
 	ast_context_destroy(NULL, registrar);
+	ast_manager_unregister("ExtReload");
 	return 0;
 }
 
-static int pbx_load_module(void)
+/*! \brief Load PBX with updated dialplan */
+static int pbx_load_module(enum dialplanreloadreasons reason)
 {
 	struct ast_config *cfg;
 	struct ast_variable *v;
@@ -1617,6 +1645,8 @@
 	char *label;
 	char realvalue[256];
 	int lastpri = -2;
+	char *textreason = "";
+	int context_count = 0, extension_count = 0, switch_count = 0;
 
 	cfg = ast_config_load(config);
 	if (cfg) {
@@ -1640,137 +1670,141 @@
 				cxt = ast_category_browse(cfg, cxt);
 				continue;
 			}
-			if ((con=ast_context_create(&local_contexts,cxt, registrar))) {
-				v = ast_variable_browse(cfg, cxt);
-				while(v) {
-					if (!strcasecmp(v->name, "exten")) {
-						char *stringp=NULL;
-						int ipri = -2;
-						char realext[256]="";
-						char *plus, *firstp, *firstc;
-						tc = strdup(v->value);
-						if(tc!=NULL){
-							stringp=tc;
-							ext = strsep(&stringp, ",");
-							if (!ext)
-								ext="";
-							pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1);
-							cidmatch = strchr(realext, '/');
-							if (cidmatch) {
-								*cidmatch = '\0';
-								cidmatch++;
-								ast_shrink_phone_number(cidmatch);
-							}
-							pri = strsep(&stringp, ",");
-							if (!pri)
-								pri="";
-							label = strchr(pri, '(');
-							if (label) {
-								*label = '\0';
-								label++;
-								end = strchr(label, ')');
-								if (end)
-									*end = '\0';
-								else
-									ast_log(LOG_WARNING, "Label missing trailing ')' at line %d\n", v->lineno);
-							}
-							plus = strchr(pri, '+');
-							if (plus) {
-								*plus = '\0';
-								plus++;
-							}
-							if (!strcmp(pri,"hint"))
-								ipri=PRIORITY_HINT;
-							else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
-								if (lastpri > -2)
-									ipri = lastpri + 1;
-								else
-									ast_log(LOG_WARNING, "Can't use 'next' priority on the first entry!\n");
-							} else if (!strcmp(pri, "same") || !strcmp(pri, "s")) {
-								if (lastpri > -2)
-									ipri = lastpri;
-								else
-									ast_log(LOG_WARNING, "Can't use 'same' priority on the first entry!\n");
-							} else  {
-								if (sscanf(pri, "%d", &ipri) != 1) {
-									if ((ipri = ast_findlabel_extension2(NULL, con, realext, pri, cidmatch)) < 1) {
-										ast_log(LOG_WARNING, "Invalid priority/label '%s' at line %d\n", pri, v->lineno);
-										ipri = 0;
-									}
+			if (!(con=ast_context_create(&local_contexts,cxt, registrar))) {
+				ast_log(LOG_ERROR, "Failed to create context %s\n", cxt);
+				continue;
+			}
+			v = ast_variable_browse(cfg, cxt);
+			while(v) {
+				if (!strcasecmp(v->name, "exten")) {
+					char *stringp=NULL;
+					int ipri = -2;
+					char realext[256]="";
+					char *plus, *firstp, *firstc;
+
+					tc = strdup(v->value);
+					if(tc!=NULL){
+						stringp=tc;
+						ext = strsep(&stringp, ",");
+						if (!ext)
+							ext="";
+						pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1);
+						cidmatch = strchr(realext, '/');
+						if (cidmatch) {
+							*cidmatch = '\0';
+							cidmatch++;
+							ast_shrink_phone_number(cidmatch);
+						}
+						pri = strsep(&stringp, ",");
+						if (!pri)
+							pri="";
+						label = strchr(pri, '(');
+						if (label) {
+							*label = '\0';
+							label++;
+							end = strchr(label, ')');
+							if (end)
+								*end = '\0';
+							else
+								ast_log(LOG_WARNING, "Label missing trailing ')' at line %d\n", v->lineno);
+						}
+						plus = strchr(pri, '+');
+						if (plus) {
+							*plus = '\0';
+							plus++;
+						}
+						if (!strcmp(pri,"hint"))
+							ipri=PRIORITY_HINT;
+						else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
+							if (lastpri > -2)
+								ipri = lastpri + 1;
+							else
+								ast_log(LOG_WARNING, "Can't use 'next' priority on the first entry!\n");
+						} else if (!strcmp(pri, "same") || !strcmp(pri, "s")) {
+							if (lastpri > -2)
+								ipri = lastpri;
+							else
+								ast_log(LOG_WARNING, "Can't use 'same' priority on the first entry!\n");
+						} else  {
+							if (sscanf(pri, "%d", &ipri) != 1) {
+								if ((ipri = ast_findlabel_extension2(NULL, con, realext, pri, cidmatch)) < 1) {
+									ast_log(LOG_WARNING, "Invalid priority/label '%s' at line %d\n", pri, v->lineno);
+									ipri = 0;
 								}
 							}
-							appl = stringp;
-							if (!appl)
-								appl="";
-							/* Find the first occurrence of either '(' or ',' */
-							firstc = strchr(appl, ',');
-							firstp = strchr(appl, '(');
-							if (firstc && ((!firstp) || (firstc < firstp))) {
-								/* comma found, no parenthesis */
-								/* or both found, but comma found first */
-								appl = strsep(&stringp, ",");
-								data = stringp;
-							} else if ((!firstc) && (!firstp)) {
-								/* Neither found */
-								data = "";
+						}
+						appl = stringp;
+						if (!appl)
+							appl="";
+						/* Find the first occurrence of either '(' or ',' */
+						firstc = strchr(appl, ',');
+						firstp = strchr(appl, '(');
+						if (firstc && ((!firstp) || (firstc < firstp))) {
+							/* comma found, no parenthesis */
+							/* or both found, but comma found first */
+							appl = strsep(&stringp, ",");
+							data = stringp;
+						} else if ((!firstc) && (!firstp)) {
+							/* Neither found */
+							data = "";
+						} else {
+							/* Final remaining case is parenthesis found first */
+							appl = strsep(&stringp, "(");
+							data = stringp;
+							end = strrchr(data, ')');
+							if ((end = strrchr(data, ')'))) {
+								*end = '\0';
 							} else {
-								/* Final remaining case is parenthesis found first */
-								appl = strsep(&stringp, "(");
-								data = stringp;
-								end = strrchr(data, ')');
-								if ((end = strrchr(data, ')'))) {
-									*end = '\0';
-								} else {
-									ast_log(LOG_WARNING, "No closing parenthesis found? '%s(%s'\n", appl, data);
-								}
-								ast_process_quotes_and_slashes(data, ',', '|');
+								ast_log(LOG_WARNING, "No closing parenthesis found? '%s(%s'\n", appl, data);
 							}
-
-							if (!data)
-								data="";
-							while(*appl && (*appl < 33)) appl++;
-							if (ipri) {
-								if (plus)
-									ipri += atoi(plus);
-								lastpri = ipri;
-								if (!ast_opt_dont_warn) {
-									if (!strcmp(realext, "_."))
-										ast_log(LOG_WARNING, "The use of '_.' for an extension is strongly discouraged and can have unexpected behavior.  Please use '_X.' instead at line %d\n", v->lineno);
-								}
-								if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, strdup(data), FREE, registrar)) {
-									ast_log(LOG_WARNING, "Unable to register extension at line %d\n", v->lineno);
-								}
+							ast_process_quotes_and_slashes(data, ',', '|');
+						}
+
+						if (!data)
+							data="";
+						while(*appl && (*appl < 33))
+							appl++;
+						if (ipri) {
+							if (plus)
+								ipri += atoi(plus);
+							lastpri = ipri;
+							if (!ast_opt_dont_warn) {
+								if (!strcmp(realext, "_."))
+									ast_log(LOG_WARNING, "The use of '_.' for an extension is strongly discouraged and can have unexpected behavior.  Please use '_X.' instead at line %d\n", v->lineno);
 							}
-							free(tc);
-						} else fprintf(stderr,"Error strdup returned NULL in %s\n",__PRETTY_FUNCTION__);
-					} else if(!strcasecmp(v->name, "include")) {
-						memset(realvalue, 0, sizeof(realvalue));
+							if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, strdup(data), FREE, registrar)) {
+								ast_log(LOG_WARNING, "Unable to register extension at line %d\n", v->lineno);
+							}
+						}
+						free(tc);
+					} else fprintf(stderr,"Error strdup returned NULL in %s\n",__PRETTY_FUNCTION__);
+				} else if(!strcasecmp(v->name, "include")) {
+					memset(realvalue, 0, sizeof(realvalue));
+					pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
+					if (ast_context_add_include2(con, realvalue, registrar))
+						ast_log(LOG_WARNING, "Unable to include context '%s' in context '%s'\n", v->value, cxt);
+				} else if(!strcasecmp(v->name, "ignorepat")) {
+					memset(realvalue, 0, sizeof(realvalue));
+					pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
+					if (ast_context_add_ignorepat2(con, realvalue, registrar))
+						ast_log(LOG_WARNING, "Unable to include ignorepat '%s' in context '%s'\n", v->value, cxt);
+				} else if (!strcasecmp(v->name, "switch") || !strcasecmp(v->name, "lswitch") || !strcasecmp(v->name, "eswitch")) {
+					char *stringp=NULL;
+					memset(realvalue, 0, sizeof(realvalue));
+					if (!strcasecmp(v->name, "switch"))
 						pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
-						if (ast_context_add_include2(con, realvalue, registrar))
-							ast_log(LOG_WARNING, "Unable to include context '%s' in context '%s'\n", v->value, cxt);
-					} else if(!strcasecmp(v->name, "ignorepat")) {
-						memset(realvalue, 0, sizeof(realvalue));
-						pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
-						if (ast_context_add_ignorepat2(con, realvalue, registrar))
-							ast_log(LOG_WARNING, "Unable to include ignorepat '%s' in context '%s'\n", v->value, cxt);
-					} else if (!strcasecmp(v->name, "switch") || !strcasecmp(v->name, "lswitch") || !strcasecmp(v->name, "eswitch")) {
-						char *stringp=NULL;
-						memset(realvalue, 0, sizeof(realvalue));
-						if (!strcasecmp(v->name, "switch"))
-							pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
-						else
-							strncpy(realvalue, v->value, sizeof(realvalue) - 1);
-						tc = realvalue;
-						stringp=tc;
-						appl = strsep(&stringp, "/");
-						data = strsep(&stringp, "");
-						if (!data)
-							data = "";
-						if (ast_context_add_switch2(con, appl, data, !strcasecmp(v->name, "eswitch"), registrar))
-							ast_log(LOG_WARNING, "Unable to include switch '%s' in context '%s'\n", v->value, cxt);
-					}
-					v = v->next;
+					else
+						strncpy(realvalue, v->value, sizeof(realvalue) - 1);
+					tc = realvalue;
+					stringp=tc;
+					appl = strsep(&stringp, "/");
+					data = strsep(&stringp, "");
+					if (!data)
+						data = "";
+					if (ast_context_add_switch2(con, appl, data, !strcasecmp(v->name, "eswitch"), registrar))
+						ast_log(LOG_WARNING, "Unable to include switch '%s' in context '%s'\n", v->value, cxt);
 				}
+				v = v->next;
 			}
 			cxt = ast_category_browse(cfg, cxt);
 		}
@@ -1782,13 +1816,27 @@
 		ast_context_verify_includes(con);
 
 	pbx_set_autofallthrough(autofallthrough_config);
+	
+	switch (reason) {
+	case PBX_MODULE_LOAD:	textreason = "PBX_LOAD_MODULE  - PBX startup (load module)";
+			break;
+	case PBX_CLI_RELOAD:	textreason = "PBX_CLI_RELOAD - CLI reload command";
+			break;
+	case PBX_MODULE_RELOAD:	textreason = "PBX_MODULE_RELOAD - Module reload";
+			break;
+	case PBX_MANAGER_RELOAD:	textreason = "PBX_MANAGER_RELOAD - Manager forced dialplan reload";
+			break;
+	};
+	manager_event(EVENT_FLAG_SYSTEM, "ExtensionsReload", "Module: Extensions\r\nReloadReason: %s\r\nContext_Count: %d\r\nExtension_Count: %d\r\nSwitch_Count: %d\r\nRegistrar: %s\r\n", textreason, context_count, extension_count, switch_count, registrar);
+	ast_log(LOG_EVENT, "PBX Dial plan loaded. Contexts: %d Extensions: %d Switches: %d Reason: %s\n", context_count, extension_count, switch_count, textreason);
 
 	return 0;
 }
 
 int load_module(void)
 {
-	if (pbx_load_module()) return -1;
+	if (pbx_load_module(PBX_MODULE_LOAD)) 
+		return -1;
  
 	ast_cli_register(&context_remove_extension_cli);
 	ast_cli_register(&context_dont_include_cli);
@@ -1799,6 +1847,7 @@
 	ast_cli_register(&context_add_ignorepat_cli);
 	ast_cli_register(&context_remove_ignorepat_cli);
 	ast_cli_register(&reload_extensions_cli);
+	ast_manager_register2("ExtReload", EVENT_FLAG_SYSTEM, action_extreload, "Reload Asterisk dial plan", mandescr_extreload);
 
 	return 0;
 }
@@ -1808,7 +1857,7 @@
 	ast_context_destroy(NULL, registrar);
 	if (clearglobalvars_config)
 		pbx_builtin_clear_globals();
-	pbx_load_module();
+	pbx_load_module(PBX_MODULE_RELOAD);
 	return 0;
 }
 



More information about the asterisk-commits mailing list