[asterisk-commits] nadi: branch nadi/trunk-cm r44548 - in /team/nadi/trunk-cm: include/asterisk/...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri Oct 6 02:27:52 MST 2006


Author: nadi
Date: Fri Oct  6 04:27:50 2006
New Revision: 44548

URL: http://svn.digium.com/view/asterisk?rev=44548&view=rev
Log:
adding a general way of channel selection.

Added:
    team/nadi/trunk-cm/include/asterisk/csel.h   (with props)
    team/nadi/trunk-cm/res/res_csel.c   (with props)

Added: team/nadi/trunk-cm/include/asterisk/csel.h
URL: http://svn.digium.com/view/asterisk/team/nadi/trunk-cm/include/asterisk/csel.h?rev=44548&view=auto
==============================================================================
--- team/nadi/trunk-cm/include/asterisk/csel.h (added)
+++ team/nadi/trunk-cm/include/asterisk/csel.h Fri Oct  6 04:27:50 2006
@@ -1,0 +1,53 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2006, Nadi Sarrar
+ *
+ * Nadi Sarrar <nadi at beronet.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 csel.h
+ * \brief Abstract Channel Selection
+ * \author Nadi Sarrar <nadi at beronet.com>
+ */
+
+#ifndef _ASTERISK_CSEL_H
+#define _ASTERISK_CSEL_H
+
+struct csel;
+
+typedef void * (*occupy_func) (void *priv);
+typedef void   (*free_func)   (void *priv);
+
+
+struct csel * csel_create   (const char *method,
+							 const char *params,
+							 occupy_func occupy,
+							 free_func   free);
+
+void          csel_destroy  (struct csel *cs);
+
+int           csel_add      (struct csel *cs,
+							 void        *priv);
+
+void *        csel_get_next (struct csel *cs);
+
+void          csel_call     (struct csel *cs,
+							 void        *priv,
+							 const char  *uid);
+
+void          csel_hangup   (struct csel *cs,
+							 void        *priv,
+							 const char  *uid);
+
+#endif

Propchange: team/nadi/trunk-cm/include/asterisk/csel.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/nadi/trunk-cm/include/asterisk/csel.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/nadi/trunk-cm/include/asterisk/csel.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/nadi/trunk-cm/res/res_csel.c
URL: http://svn.digium.com/view/asterisk/team/nadi/trunk-cm/res/res_csel.c?rev=44548&view=auto
==============================================================================
--- team/nadi/trunk-cm/res/res_csel.c (added)
+++ team/nadi/trunk-cm/res/res_csel.c Fri Oct  6 04:27:50 2006
@@ -1,0 +1,231 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2006, Nadi Sarrar
+ *
+ * Nadi Sarrar <nadi at beronet.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 res_csel.c
+ *
+ * \brief Abstract Channel Selection
+ *
+ * \author Nadi Sarrar <nadi at beronet.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/csel.h"
+#include "asterisk/module.h"
+#include "asterisk/logger.h"
+#include "asterisk/lock.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+struct csel {
+	ast_mutex_t     lock;
+	
+	occupy_func     occupy;
+	free_func       free;
+
+	struct method * m;
+	void          * data;
+};
+
+struct method {
+	char          * name;
+	int          (* init) (struct csel *cs, const char *params);
+	void         (* destroy) (struct csel *cs);
+	int          (* add) (struct csel *cs, void *priv);
+	void *       (* get_next) (struct csel *cs);
+	void         (* call) (struct csel *cs, void *priv, const void *uid);
+	void         (* hangup) (struct csel *cs, void *priv, const void *uid);
+};
+
+#define LOCK(cs)	ast_mutex_lock(&(cs)->lock)
+#define UNLOCK(cs)	ast_mutex_unlock(&(cs)->lock)
+
+/* RANDOM: begin */
+#include "asterisk/utils.h"
+
+struct rand_data {
+	int size;
+	void **arr;
+};
+
+static int rand_init (struct csel *cs, const char *params)
+{
+	cs->data = calloc(1, sizeof(struct rand_data));
+	return cs->data ? 0 : 1;
+}
+
+static void rand_destroy (struct csel *cs)
+{
+	struct rand_data *data = cs->data;
+	int i = 0;
+
+	if (data) {
+		if (data->arr) {
+			if (cs->free)
+				for (; i < data->size; ++i)
+					cs->free(data->arr[i]);
+			free(data->arr);
+		}
+		free(data);
+	}
+}
+
+static int rand_add (struct csel *cs, void *priv)
+{
+	struct rand_data *data = cs->data;
+
+	++data->size;
+	data->arr = realloc(data->arr, data->size * sizeof(void *));
+	data->arr[data->size - 1] = priv;
+
+	return 0;
+}
+
+static void * rand_get_next (struct csel *cs)
+{
+	struct rand_data *data = cs->data;
+	void *p;
+	int i = data->size,
+		pos;
+
+	for (; i > 0 ; --i) {
+		pos = random() % i;
+		if ((p = cs->occupy(data->arr[pos])))
+			return p;
+		p = data->arr[i - 1];
+		data->arr[i - 1] = data->arr[pos];
+		data->arr[pos] = p;
+	}
+	return NULL;
+}
+/* RANDOM: end */
+
+static struct method methods[] = {
+	{ "random", rand_init, rand_destroy, rand_add, rand_get_next, 0, 0 },
+};
+
+struct csel * csel_create (const char *method,
+						   const char *params,
+						   occupy_func occupy,
+						   free_func   free)
+{
+	struct csel *cs;
+	int i = 0;
+
+	for (; i < (sizeof(methods) / sizeof(struct method)); ++i) {
+		if (!strcasecmp(methods[i].name, method))
+			break;
+	}
+
+	if (i == (sizeof(methods) / sizeof(struct method))) {
+		ast_log(LOG_WARNING, "csel: unknown method: %s, falling back to: %s\n", method, methods[0].name);
+		i = 0;
+	}
+
+	cs = calloc(1, sizeof(struct csel));
+
+	cs->occupy = occupy;
+	cs->m = &methods[i];
+
+	if (cs->m->init(cs, params)) {
+		free(cs);
+		return NULL;
+	}
+	
+	ast_mutex_init(&cs->lock);
+
+	ast_module_ref(ast_module_info->self);
+
+	return cs;
+}
+
+void csel_destroy (struct csel *cs)
+{
+	ast_mutex_destroy(&cs->lock);
+	cs->m->destroy(cs);
+	free(cs);
+	
+	ast_module_unref(ast_module_info->self);
+}
+
+int csel_add (struct csel *cs,
+			  void *priv)
+{
+	int retval;
+	
+	LOCK(cs);
+	retval = cs->m->add(cs, priv);
+	UNLOCK(cs);
+
+	return retval;
+}
+
+void * csel_get_next (struct csel *cs)
+{
+	void *retval;
+	
+	LOCK(cs);
+	retval = cs->m->get_next(cs);
+	UNLOCK(cs);
+
+	return retval;
+}
+
+void csel_call (struct csel *cs,
+				void *priv,
+				const char *uid)
+{
+	LOCK(cs);
+	if (cs->m->call)
+		cs->m->call(cs, priv, uid);
+	UNLOCK(cs);
+}
+
+void csel_hangup (struct csel *cs,
+				  void *priv,
+				  const char *uid)
+{
+	LOCK(cs);
+	if (cs->m->hangup)
+		cs->m->hangup(cs, priv, uid);
+	UNLOCK(cs);
+}
+
+static int load_module (void)
+{
+	return 0;
+}
+
+static int unload_module (void)
+{
+	return 0;
+}
+
+static int reload (void)
+{
+	return -1;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Channel Selection Resource",
+				.load = load_module,
+				.unload = unload_module,
+				.reload = reload,
+			   );

Propchange: team/nadi/trunk-cm/res/res_csel.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/nadi/trunk-cm/res/res_csel.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/nadi/trunk-cm/res/res_csel.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain



More information about the asterisk-commits mailing list