[asterisk-commits] russell: branch russell/sla_rewrite r51508 - /team/russell/sla_rewrite/apps/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Jan 22 18:02:14 MST 2007


Author: russell
Date: Mon Jan 22 19:02:13 2007
New Revision: 51508

URL: http://svn.digium.com/view/asterisk?view=rev&rev=51508
Log:
- Fix the payload of the hints that are automatically created
- Create a MAX_CONFNUM and MAX_PIN to use instead of randomly using
  AST_MAX_EXTENSION for things that have nothing to do with extensions.
- Clean up build_conf() to reduce excessive nesting.
- Document the build_conf() function
- Pass the size of the dynamic_pin buffer to the find_conf() instead of just
  assuming it has a size of AST_MAX_EXTENSION.
- Add some basic stuff to the SLATrunk app.

Modified:
    team/russell/sla_rewrite/apps/app_meetme.c

Modified: team/russell/sla_rewrite/apps/app_meetme.c
URL: http://svn.digium.com/view/asterisk/team/russell/sla_rewrite/apps/app_meetme.c?view=diff&rev=51508&r1=51507&r2=51508
==============================================================================
--- team/russell/sla_rewrite/apps/app_meetme.c (original)
+++ team/russell/sla_rewrite/apps/app_meetme.c Mon Jan 22 19:02:13 2007
@@ -279,11 +279,14 @@
 static const char *slatrunk_desc =
 "  SLATrunk():\n";
 
+#define MAX_CONFNUM 80
+#define MAX_PIN     80
+
 /*! \brief The MeetMe Conference object */
 struct ast_conference {
 	ast_mutex_t playlock;                   /*!< Conference specific lock (players) */
 	ast_mutex_t listenlock;                 /*!< Conference specific lock (listeners) */
-	char confno[AST_MAX_EXTENSION];         /*!< Conference */
+	char confno[MAX_CONFNUM];               /*!< Conference */
 	struct ast_channel *chan;               /*!< Announcements channel */
 	struct ast_channel *lchan;              /*!< Listen/Record channel */
 	int fd;                                 /*!< Announcements fd */
@@ -299,8 +302,8 @@
 	pthread_attr_t attr;                    /*!< thread attribute */
 	const char *recordingfilename;          /*!< Filename to record the Conference into */
 	const char *recordingformat;            /*!< Format to record the Conference in */
-	char pin[AST_MAX_EXTENSION];            /*!< If protected by a PIN */
-	char pinadmin[AST_MAX_EXTENSION];       /*!< If protected by a admin PIN */
+	char pin[MAX_PIN];                      /*!< If protected by a PIN */
+	char pinadmin[MAX_PIN];                 /*!< If protected by a admin PIN */
 	struct ast_frame *transframe[32];
 	struct ast_frame *origframe;
 	struct ast_trans_pvt *transpath[32];
@@ -549,6 +552,19 @@
 		ast_autoservice_stop(chan);
 }
 
+/*!
+ * \brief Find or create a conference
+ *
+ * \param confno The conference name/number
+ * \param pin The regular user pin
+ * \param pinadmin The admin pin
+ * \param make Make the conf if it doesn't exist
+ * \param dynamic Mark the newly created conference as dynamic
+ * \param refcount How many references to mark on the conference
+ *
+ * \return A pointer to the conference struct, or NULL if it wasn't found and
+ *         make or dynamic were not set.
+ */
 static struct ast_conference *build_conf(char *confno, char *pin, char *pinadmin, int make, int dynamic, int refcount)
 {
 	struct ast_conference *cnf;
@@ -561,72 +577,76 @@
 			break;
 	}
 
-	if (!cnf && (make || dynamic)) {
-		/* Make a new one */
-		if ((cnf = ast_calloc(1, sizeof(*cnf)))) {
-			ast_mutex_init(&cnf->playlock);
-			ast_mutex_init(&cnf->listenlock);
-			ast_copy_string(cnf->confno, confno, sizeof(cnf->confno));
-			ast_copy_string(cnf->pin, pin, sizeof(cnf->pin));
-			ast_copy_string(cnf->pinadmin, pinadmin, sizeof(cnf->pinadmin));
-			cnf->refcount = 0;
-			cnf->markedusers = 0;
-			cnf->chan = ast_request("zap", AST_FORMAT_SLINEAR, "pseudo", NULL);
-			if (cnf->chan) {
-				ast_set_read_format(cnf->chan, AST_FORMAT_SLINEAR);
-				ast_set_write_format(cnf->chan, AST_FORMAT_SLINEAR);
-				cnf->fd = cnf->chan->fds[0];	/* for use by conf_play() */
-			} else {
-				ast_log(LOG_WARNING, "Unable to open pseudo channel - trying device\n");
-				cnf->fd = open("/dev/zap/pseudo", O_RDWR);
-				if (cnf->fd < 0) {
-					ast_log(LOG_WARNING, "Unable to open pseudo device\n");
-					free(cnf);
-					cnf = NULL;
-					goto cnfout;
-				}
-			}
-			memset(&ztc, 0, sizeof(ztc));
-			/* Setup a new zap conference */
-			ztc.chan = 0;
-			ztc.confno = -1;
-			ztc.confmode = ZT_CONF_CONFANN | ZT_CONF_CONFANNMON;
-			if (ioctl(cnf->fd, ZT_SETCONF, &ztc)) {
-				ast_log(LOG_WARNING, "Error setting conference\n");
-				if (cnf->chan)
-					ast_hangup(cnf->chan);
-				else
-					close(cnf->fd);
-				free(cnf);
-				cnf = NULL;
-				goto cnfout;
-			}
-			cnf->lchan = ast_request("zap", AST_FORMAT_SLINEAR, "pseudo", NULL);
-			if (cnf->lchan) {
-				ast_set_read_format(cnf->lchan, AST_FORMAT_SLINEAR);
-				ast_set_write_format(cnf->lchan, AST_FORMAT_SLINEAR);
-				ztc.chan = 0;
-				ztc.confmode = ZT_CONF_CONFANN | ZT_CONF_CONFANNMON;
-				if (ioctl(cnf->lchan->fds[0], ZT_SETCONF, &ztc)) {
-					ast_log(LOG_WARNING, "Error setting conference\n");
-					ast_hangup(cnf->lchan);
-					cnf->lchan = NULL;
-				}
-			}
-			/* Fill the conference struct */
-			cnf->start = time(NULL);
-			cnf->zapconf = ztc.confno;
-			cnf->isdynamic = dynamic ? 1 : 0;
-			if (option_verbose > 2)
-				ast_verbose(VERBOSE_PREFIX_3 "Created MeetMe conference %d for conference '%s'\n", cnf->zapconf, cnf->confno);
-			AST_LIST_INSERT_HEAD(&confs, cnf, list);
-		} 
-	}
- cnfout:
-	if (cnf){ 
+	if (cnf || (!make && !dynamic))
+		goto cnfout;
+
+	/* Make a new one */
+	if (!(cnf = ast_calloc(1, sizeof(*cnf))))
+		goto cnfout;
+
+	ast_mutex_init(&cnf->playlock);
+	ast_mutex_init(&cnf->listenlock);
+	ast_copy_string(cnf->confno, confno, sizeof(cnf->confno));
+	ast_copy_string(cnf->pin, pin, sizeof(cnf->pin));
+	ast_copy_string(cnf->pinadmin, pinadmin, sizeof(cnf->pinadmin));
+	cnf->refcount = 0;
+	cnf->markedusers = 0;
+	cnf->chan = ast_request("zap", AST_FORMAT_SLINEAR, "pseudo", NULL);
+	if (cnf->chan) {
+		ast_set_read_format(cnf->chan, AST_FORMAT_SLINEAR);
+		ast_set_write_format(cnf->chan, AST_FORMAT_SLINEAR);
+		cnf->fd = cnf->chan->fds[0];	/* for use by conf_play() */
+	} else {
+		ast_log(LOG_WARNING, "Unable to open pseudo channel - trying device\n");
+		cnf->fd = open("/dev/zap/pseudo", O_RDWR);
+		if (cnf->fd < 0) {
+			ast_log(LOG_WARNING, "Unable to open pseudo device\n");
+			free(cnf);
+			cnf = NULL;
+			goto cnfout;
+		}
+	}
+	memset(&ztc, 0, sizeof(ztc));
+	/* Setup a new zap conference */
+	ztc.chan = 0;
+	ztc.confno = -1;
+	ztc.confmode = ZT_CONF_CONFANN | ZT_CONF_CONFANNMON;
+	if (ioctl(cnf->fd, ZT_SETCONF, &ztc)) {
+		ast_log(LOG_WARNING, "Error setting conference\n");
+		if (cnf->chan)
+			ast_hangup(cnf->chan);
+		else
+			close(cnf->fd);
+		free(cnf);
+		cnf = NULL;
+		goto cnfout;
+	}
+	cnf->lchan = ast_request("zap", AST_FORMAT_SLINEAR, "pseudo", NULL);
+	if (cnf->lchan) {
+		ast_set_read_format(cnf->lchan, AST_FORMAT_SLINEAR);
+		ast_set_write_format(cnf->lchan, AST_FORMAT_SLINEAR);
+		ztc.chan = 0;
+		ztc.confmode = ZT_CONF_CONFANN | ZT_CONF_CONFANNMON;
+		if (ioctl(cnf->lchan->fds[0], ZT_SETCONF, &ztc)) {
+			ast_log(LOG_WARNING, "Error setting conference\n");
+			ast_hangup(cnf->lchan);
+			cnf->lchan = NULL;
+		}
+	}
+	/* Fill the conference struct */
+	cnf->start = time(NULL);
+	cnf->zapconf = ztc.confno;
+	cnf->isdynamic = dynamic ? 1 : 0;
+	if (option_verbose > 2)
+		ast_verbose(VERBOSE_PREFIX_3 "Created MeetMe conference %d for conference '%s'\n", cnf->zapconf, cnf->confno);
+	AST_LIST_INSERT_HEAD(&confs, cnf, list);
+	
+cnfout:
+	if (cnf)
 		cnf->refcount += refcount;
-	}
+
 	AST_LIST_UNLOCK(&confs);
+
 	return cnf;
 }
 
@@ -1889,7 +1909,7 @@
 }
 
 static struct ast_conference *find_conf_realtime(struct ast_channel *chan, char *confno, int make, int dynamic,
-						 char *dynamic_pin, int refcount, struct ast_flags *confflags)
+						 char *dynamic_pin, size_t pin_buf_len, int refcount, struct ast_flags *confflags)
 {
 	struct ast_variable *var;
 	struct ast_conference *cnf;
@@ -1946,7 +1966,7 @@
 
 
 static struct ast_conference *find_conf(struct ast_channel *chan, char *confno, int make, int dynamic,
-					char *dynamic_pin, int refcount, struct ast_flags *confflags)
+					char *dynamic_pin, size_t pin_buf_len, int refcount, struct ast_flags *confflags)
 {
 	struct ast_config *cfg;
 	struct ast_variable *var;
@@ -1976,7 +1996,7 @@
 			if (dynamic_pin) {
 				if (dynamic_pin[0] == 'q') {
 					/* Query the user to enter a PIN */
-					if (ast_app_getdata(chan, "conf-getpin", dynamic_pin, AST_MAX_EXTENSION - 1, 0) < 0)
+					if (ast_app_getdata(chan, "conf-getpin", dynamic_pin, pin_buf_len - 1, 0) < 0)
 						return NULL;
 				}
 				cnf = build_conf(confno, dynamic_pin, "", make, dynamic, refcount);
@@ -2066,7 +2086,7 @@
 
 	AST_STANDARD_APP_ARGS(args, localdata);
 	
-	conf = find_conf(chan, args.confno, 0, 0, NULL, 0, NULL);
+	conf = find_conf(chan, args.confno, 0, 0, NULL, 0, 0, NULL);
 
 	if (conf)
 		count = conf->users;
@@ -2092,7 +2112,7 @@
 {
 	int res=-1;
 	struct ast_module_user *u;
-	char confno[AST_MAX_EXTENSION] = "";
+	char confno[MAX_CONFNUM] = "";
 	int allowretry = 0;
 	int retrycnt = 0;
 	struct ast_conference *cnf;
@@ -2100,7 +2120,7 @@
 	int dynamic = 0;
 	int empty = 0, empty_no_pin = 0;
 	int always_prompt = 0;
-	char *notdata, *info, the_pin[AST_MAX_EXTENSION] = "";
+	char *notdata, *info, the_pin[MAX_PIN] = "";
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(confno);
 		AST_APP_ARG(options);
@@ -2254,9 +2274,12 @@
 		}
 		if (!ast_strlen_zero(confno)) {
 			/* Check the validity of the conference */
-			cnf = find_conf(chan, confno, 1, dynamic, the_pin, 1, &confflags);
-			if (!cnf)
-				cnf = find_conf_realtime(chan, confno, 1, dynamic, the_pin, 1, &confflags);
+			cnf = find_conf(chan, confno, 1, dynamic, the_pin, 
+				sizeof(the_pin), 1, &confflags);
+			if (!cnf) {
+				cnf = find_conf_realtime(chan, confno, 1, dynamic, 
+					the_pin, sizeof(the_pin), 1, &confflags);
+			}
 
 			if (!cnf) {
 				res = ast_streamfile(chan, "conf-invalid", chan->language);
@@ -2270,7 +2293,7 @@
 				     !ast_test_flag(&confflags, CONFFLAG_ADMIN)) ||
 				    (!ast_strlen_zero(cnf->pinadmin) &&
 				     ast_test_flag(&confflags, CONFFLAG_ADMIN))) {
-					char pin[AST_MAX_EXTENSION]="";
+					char pin[MAX_PIN] = "";
 					int j;
 
 					/* Allow the pin to be retried up to 3 times */
@@ -2710,6 +2733,25 @@
 
 static int slatrunk_exec(struct ast_channel *chan, void *data)
 {
+	const char *trunk_name = data;
+	char conf_name[MAX_CONFNUM];
+	struct ast_conference *conf;
+	int res;
+	struct ast_flags conf_flags = { 0 };
+
+	snprintf(conf_name, sizeof(conf_name), "SLA-%s", trunk_name);
+
+	conf = build_conf(conf_name, NULL, NULL, 
+		1 /* make it! */, 1 /* dynamic */, 1 /* refcount++ */); 
+	if (!conf) {
+		pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");
+		return 0;
+	}
+
+	/* Set flags for the conf */
+
+	res = conf_run(chan, conf, conf_flags.flags, NULL);
+
 	return 0;
 }
 
@@ -2735,10 +2777,12 @@
 		AST_RWLIST_RDLOCK(&sla_trunks);
 		AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
 			char exten[AST_MAX_EXTENSION];
+			char hint[AST_MAX_APP];
 			snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);
+			snprintf(hint, sizeof(hint), "SLA:%s", exten);
 			ast_context_remove_extension(station->autocontext, exten, 
 				1, sla_registrar);
-			ast_context_remove_extension(station->autocontext, exten, 
+			ast_context_remove_extension(station->autocontext, hint, 
 				PRIORITY_HINT, sla_registrar);
 		}
 		AST_RWLIST_UNLOCK(&sla_trunks);
@@ -2891,7 +2935,9 @@
 		AST_RWLIST_RDLOCK(&sla_trunks);
 		AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
 			char exten[AST_MAX_EXTENSION];
+			char hint[AST_MAX_APP];
 			snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);
+			snprintf(hint, sizeof(hint), "SLA:%s", exten);
 			/* Extension for this line button 
 			 * exten => station1_line1,1,SLAStation(station1_line1) */
 			if (ast_add_extension2(context, 0 /* don't replace */, exten, 1,
@@ -2904,7 +2950,7 @@
 			/* Hint for this line button 
 			 * exten => station1_line1,hint,SLA:station1_line1 */
 			if (ast_add_extension2(context, 0 /* don't replace */, exten, PRIORITY_HINT,
-				NULL, NULL, slastation_app, ast_strdup(exten), ast_free, sla_registrar)) {
+				NULL, NULL, hint, NULL, NULL, sla_registrar)) {
 				ast_log(LOG_ERROR, "Failed to automatically create hint "
 					"for trunk '%s'!\n", station->name);
 				destroy_station(station);



More information about the asterisk-commits mailing list