[Asterisk-code-review] main/pbx: Move switch routines to pbx switch.c. (asterisk[13])

Corey Farrell asteriskteam at digium.com
Tue Jan 5 11:32:21 CST 2016


Corey Farrell has uploaded a new change for review.

  https://gerrit.asterisk.org/1914

Change subject: main/pbx: Move switch routines to pbx_switch.c.
......................................................................

main/pbx: Move switch routines to pbx_switch.c.

This is the fifth patch in a series meant to reduce the bulk of pbx.c.
This moves ast_switch functions to their own source.

Change-Id: Ic2592a18a5c4d8a3c2dcf9786c9a6f650a8c628e
---
M include/asterisk/_private.h
M main/asterisk.c
M main/pbx.c
M main/pbx_private.h
A main/pbx_switch.c
5 files changed, 143 insertions(+), 80 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/14/1914/1

diff --git a/include/asterisk/_private.h b/include/asterisk/_private.h
index 70e14dc..edf8fff 100644
--- a/include/asterisk/_private.h
+++ b/include/asterisk/_private.h
@@ -20,6 +20,7 @@
 int load_pbx_builtins(void);	/*!< Provided by pbx_builtins.c */
 int load_pbx_functions_cli(void);	/*!< Provided by pbx_functions.c */
 int load_pbx_variables(void);	/*!< Provided by pbx_variables.c */
+int load_pbx_switch(void);		/*!< Provided by pbx_switch.c */
 int init_logger(void);			/*!< Provided by logger.c */
 void close_logger(void);		/*!< Provided by logger.c */
 void logger_queue_start(void);		/*!< Provided by logger.c */
diff --git a/main/asterisk.c b/main/asterisk.c
index bc74eca..31cb90c 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -4658,6 +4658,11 @@
 		exit(1);
 	}
 
+	if (load_pbx_switch()) {
+		printf("Failed: load_pbx_switch\n%s", term_quit());
+		exit(1);
+	}
+
 	if (ast_local_init()) {
 		printf("Failed: ast_local_init\n%s", term_quit());
 		exit(1);
diff --git a/main/pbx.c b/main/pbx.c
index 9cc0cc8..3034ccd 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -767,8 +767,6 @@
  */
 static AST_RWLIST_HEAD_STATIC(apps, ast_app);
 
-static AST_RWLIST_HEAD_STATIC(switches, ast_switch);
-
 static int stateid = 1;
 /*!
  * \note When holding this container's lock, do _not_ do
@@ -1022,20 +1020,6 @@
 	AST_RWLIST_UNLOCK(&apps);
 
 	return ret;
-}
-
-static struct ast_switch *pbx_findswitch(const char *sw)
-{
-	struct ast_switch *asw;
-
-	AST_RWLIST_RDLOCK(&switches);
-	AST_RWLIST_TRAVERSE(&switches, asw, list) {
-		if (!strcasecmp(asw->name, sw))
-			break;
-	}
-	AST_RWLIST_UNLOCK(&switches);
-
-	return asw;
 }
 
 static inline int include_valid(struct ast_include *i)
@@ -5408,35 +5392,6 @@
 }
 
 /*
- * Append to the list. We don't have a tail pointer because we need
- * to scan the list anyways to check for duplicates during insertion.
- */
-int ast_register_switch(struct ast_switch *sw)
-{
-	struct ast_switch *tmp;
-
-	AST_RWLIST_WRLOCK(&switches);
-	AST_RWLIST_TRAVERSE(&switches, tmp, list) {
-		if (!strcasecmp(tmp->name, sw->name)) {
-			AST_RWLIST_UNLOCK(&switches);
-			ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
-			return -1;
-		}
-	}
-	AST_RWLIST_INSERT_TAIL(&switches, sw, list);
-	AST_RWLIST_UNLOCK(&switches);
-
-	return 0;
-}
-
-void ast_unregister_switch(struct ast_switch *sw)
-{
-	AST_RWLIST_WRLOCK(&switches);
-	AST_RWLIST_REMOVE(&switches, sw, list);
-	AST_RWLIST_UNLOCK(&switches);
-}
-
-/*
  * Help for CLI commands ...
  */
 
@@ -5720,40 +5675,6 @@
 		ast_cli(a->fd, "No hints matching extension %s\n", a->argv[3]);
 	else
 		ast_cli(a->fd, "%d hint%s matching extension %s\n", num, (num!=1 ? "s":""), a->argv[3]);
-	return CLI_SUCCESS;
-}
-
-
-/*! \brief  handle_show_switches: CLI support for listing registered dial plan switches */
-static char *handle_show_switches(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
-	struct ast_switch *sw;
-
-	switch (cmd) {
-	case CLI_INIT:
-		e->command = "core show switches";
-		e->usage =
-			"Usage: core show switches\n"
-			"       List registered switches\n";
-		return NULL;
-	case CLI_GENERATE:
-		return NULL;
-	}
-
-	AST_RWLIST_RDLOCK(&switches);
-
-	if (AST_RWLIST_EMPTY(&switches)) {
-		AST_RWLIST_UNLOCK(&switches);
-		ast_cli(a->fd, "There are no registered alternative switches\n");
-		return CLI_SUCCESS;
-	}
-
-	ast_cli(a->fd, "\n    -= Registered Asterisk Alternative Switches =-\n");
-	AST_RWLIST_TRAVERSE(&switches, sw, list)
-		ast_cli(a->fd, "%s: %s\n", sw->name, sw->description);
-
-	AST_RWLIST_UNLOCK(&switches);
-
 	return CLI_SUCCESS;
 }
 
@@ -6599,7 +6520,6 @@
 	AST_CLI_DEFINE(handle_eat_memory, "Eats all available memory"),
 #endif
 	AST_CLI_DEFINE(handle_show_applications, "Shows registered dialplan applications"),
-	AST_CLI_DEFINE(handle_show_switches, "Show alternative switches"),
 	AST_CLI_DEFINE(handle_show_hints, "Show dialplan hints"),
 	AST_CLI_DEFINE(handle_show_hint, "Show dialplan hint"),
 #ifdef AST_DEVMODE
diff --git a/main/pbx_private.h b/main/pbx_private.h
index e171179..87abefa 100644
--- a/main/pbx_private.h
+++ b/main/pbx_private.h
@@ -32,6 +32,10 @@
 int indicate_congestion(struct ast_channel *, const char *);
 int indicate_busy(struct ast_channel *, const char *);
 
+/*! pbx_switch.c functions needed by pbx.c */
+struct ast_switch *pbx_findswitch(const char *sw);
+
+
 #define VAR_BUF_SIZE 4096
 
 #endif /* _PBX_PRIVATE_H */
diff --git a/main/pbx_switch.c b/main/pbx_switch.c
new file mode 100644
index 0000000..4ac7f8c
--- /dev/null
+++ b/main/pbx_switch.c
@@ -0,0 +1,133 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, CFWare, LLC
+ *
+ * Corey Farrell <git at cfware.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief PBX switch routines.
+ *
+ * \author Corey Farrell <git at cfware.com>
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/_private.h"
+#include "asterisk/cli.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/pbx.h"
+#include "pbx_private.h"
+
+static AST_RWLIST_HEAD_STATIC(switches, ast_switch);
+
+struct ast_switch *pbx_findswitch(const char *sw)
+{
+	struct ast_switch *asw;
+
+	AST_RWLIST_RDLOCK(&switches);
+	AST_RWLIST_TRAVERSE(&switches, asw, list) {
+		if (!strcasecmp(asw->name, sw))
+			break;
+	}
+	AST_RWLIST_UNLOCK(&switches);
+
+	return asw;
+}
+
+/*
+ * Append to the list. We don't have a tail pointer because we need
+ * to scan the list anyways to check for duplicates during insertion.
+ */
+int ast_register_switch(struct ast_switch *sw)
+{
+	struct ast_switch *tmp;
+
+	AST_RWLIST_WRLOCK(&switches);
+	AST_RWLIST_TRAVERSE(&switches, tmp, list) {
+		if (!strcasecmp(tmp->name, sw->name)) {
+			AST_RWLIST_UNLOCK(&switches);
+			ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
+			return -1;
+		}
+	}
+	AST_RWLIST_INSERT_TAIL(&switches, sw, list);
+	AST_RWLIST_UNLOCK(&switches);
+
+	return 0;
+}
+
+void ast_unregister_switch(struct ast_switch *sw)
+{
+	AST_RWLIST_WRLOCK(&switches);
+	AST_RWLIST_REMOVE(&switches, sw, list);
+	AST_RWLIST_UNLOCK(&switches);
+}
+
+/*! \brief  handle_show_switches: CLI support for listing registered dial plan switches */
+static char *handle_show_switches(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct ast_switch *sw;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "core show switches";
+		e->usage =
+			"Usage: core show switches\n"
+			"       List registered switches\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	AST_RWLIST_RDLOCK(&switches);
+
+	if (AST_RWLIST_EMPTY(&switches)) {
+		AST_RWLIST_UNLOCK(&switches);
+		ast_cli(a->fd, "There are no registered alternative switches\n");
+		return CLI_SUCCESS;
+	}
+
+	ast_cli(a->fd, "\n    -= Registered Asterisk Alternative Switches =-\n");
+	AST_RWLIST_TRAVERSE(&switches, sw, list)
+		ast_cli(a->fd, "%s: %s\n", sw->name, sw->description);
+
+	AST_RWLIST_UNLOCK(&switches);
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry sw_cli[] = {
+	AST_CLI_DEFINE(handle_show_switches, "Show alternative switches"),
+};
+
+static void unload_pbx_switch(void)
+{
+	ast_cli_unregister_multiple(sw_cli, ARRAY_LEN(sw_cli));
+}
+
+int load_pbx_switch(void)
+{
+	ast_cli_register_multiple(sw_cli, ARRAY_LEN(sw_cli));
+	ast_register_cleanup(unload_pbx_switch);
+
+	return 0;
+}

-- 
To view, visit https://gerrit.asterisk.org/1914
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic2592a18a5c4d8a3c2dcf9786c9a6f650a8c628e
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Corey Farrell <git at cfware.com>



More information about the asterisk-code-review mailing list