[asterisk-commits] kpfleming: branch kpfleming/SRV-priority-handling r87071 - /team/kpfleming/SR...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Oct 25 18:13:27 CDT 2007


Author: kpfleming
Date: Thu Oct 25 18:13:27 2007
New Revision: 87071

URL: http://svn.digium.com/view/asterisk?view=rev&rev=87071
Log:
initial code (not yet complete and not at all working) for selecting records by weight

Modified:
    team/kpfleming/SRV-priority-handling/main/srv.c

Modified: team/kpfleming/SRV-priority-handling/main/srv.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/SRV-priority-handling/main/srv.c?view=diff&rev=87071&r1=87070&r2=87071
==============================================================================
--- team/kpfleming/SRV-priority-handling/main/srv.c (original)
+++ team/kpfleming/SRV-priority-handling/main/srv.c Thu Oct 25 18:13:27 2007
@@ -144,6 +144,50 @@
 	return 0;
 }
 
+/* Do the bizarre SRV record weight-handling algorithm
+   involving sorting and random number generation...
+
+   See RFC 2782 if you want know why this code does this
+*/
+static void process_weights(struct srv_context *context)
+{
+	struct srv_entry *current;
+	struct srv_entries newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
+
+	while (AST_LIST_FIRST(&context->entries)) {
+		unsigned int random_weight;
+		unsigned int weight_sum;
+		unsigned short cur_priority = AST_LIST_FIRST(&context->entries)->priority;
+		struct srv_entries temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
+		weight_sum = 0;
+
+		AST_LIST_TRAVERSE_SAFE_BEGIN(&context->entries, current, list) {
+			if (current->priority != cur_priority)
+				break;
+
+			AST_LIST_REMOVE_CURRENT(&context->entries, list);
+			AST_LIST_INSERT_TAIL(&temp_list, current, list);
+		}
+		AST_LIST_TRAVERSE_SAFE_END;
+
+		while (AST_LIST_FIRST(&temp_list)) {
+			weight_sum = 0;
+			AST_LIST_TRAVERSE(&temp_list, current, list)
+				current->weight_sum = weight_sum += current->weight;
+
+			/* if all the remaining entries have weight == 0,
+			   then just append them to the result list and quit */
+			if (weight_sum == 0) {
+				AST_LIST_APPEND_LIST(&newlist, &temp_list, list);
+				break;
+			}
+
+			random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / (RAND_MAX + 1.0)));
+		}
+
+	}
+}
+
 int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
 {
 	struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE };
@@ -154,6 +198,9 @@
 		return -1;
 
 	ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
+
+	if (context.have_weights)
+		process_weights(&context);
 
 	if (chan)
 		ret |= ast_autoservice_stop(chan);




More information about the asterisk-commits mailing list