[asterisk-commits] marquis: branch marquis/pubsub-distributed-events r214150 - /team/marquis/pub...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Aug 25 16:53:39 CDT 2009


Author: marquis
Date: Tue Aug 25 16:53:36 2009
New Revision: 214150

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=214150
Log:
More progress.  Mostly cleanup, although we no longer assume that we have to create the initial PubSub nodes.  Now we assume we can subscribe to existing nodes, and handle the 404 appropriately if it's not there.

Modified:
    team/marquis/pubsub-distributed-events/res/res_jabber.c

Modified: team/marquis/pubsub-distributed-events/res/res_jabber.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/marquis/pubsub-distributed-events/res/res_jabber.c?view=diff&rev=214150&r1=214149&r2=214150
==============================================================================
--- team/marquis/pubsub-distributed-events/res/res_jabber.c (original)
+++ team/marquis/pubsub-distributed-events/res/res_jabber.c Tue Aug 25 16:53:36 2009
@@ -244,9 +244,9 @@
 static int aji_register_approve_handler(void *data, ikspak *pak);
 static int aji_reconnect(struct aji_client *client);
 static char *aji_cli_create_collection(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
-static char *aji_cli_list_pubsub_items(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
+static char *aji_cli_list_pubsub_nodes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid);
-static int aji_receive_item_list(void *data, ikspak* pak);
+static int aji_receive_node_list(void *data, ikspak* pak);
 static void aji_init_event_distribution(struct aji_client *client);
 static iks* aji_create_pubsub_node(struct aji_client *client, const char *node_type,
 	const char *name, const char *collection_name);
@@ -259,8 +259,9 @@
 static iks* aji_pubsub_iq_create(struct aji_client *client, const char *type);
 static void aji_publish_device_state(struct aji_client *client, const char * device,
 	const char *device_state);
-static int aji_handle_publish_error(void *data, ikspak *pak);
+static int aji_handle_pubsub_error(void *data, ikspak *pak);
 static int aji_handle_pubsub_event(void *data, ikspak *pak);
+static void aji_pubsub_subscribe(struct aji_client *client, const char *node);
 /* No transports in this version */
 /*
 static int aji_create_transport(char *label, struct aji_client *client);
@@ -275,7 +276,7 @@
 	AST_CLI_DEFINE(aji_show_buddies, "Show buddy lists of our clients"),
 	AST_CLI_DEFINE(aji_test, "Shows roster, but is generally used for mog's debugging."),
 	AST_CLI_DEFINE(aji_cli_create_collection, "Creates a test PubSub node collection."),
-	AST_CLI_DEFINE(aji_cli_list_pubsub_items, "Lists Pubsub items"),
+	AST_CLI_DEFINE(aji_cli_list_pubsub_nodes, "Lists Pubsub nodes"),
 	AST_CLI_DEFINE(aji_cli_create_leafnode, "Creates a test Pubsub leaf node"),
 };
 
@@ -2502,9 +2503,7 @@
 	} else if(ast_event_get_type(ast_event) == AST_EVENT_DEVICE_STATE_CHANGE) {
 		device = ast_event_get_ie_str(ast_event, AST_EVENT_IE_DEVICE);
 		device_state = ast_devstate_str(ast_event_get_ie_uint(ast_event, AST_EVENT_IE_STATE));
-		ast_log(LOG_ERROR, "Got a DEVICE_STATE_CHANGE event for %s\n", device);
 		aji_publish_device_state(client, device, device_state);
-		ast_log(LOG_ERROR, "DEVICE_STATE is %s\n", device_state);
 	}
 }
 
@@ -2527,18 +2526,12 @@
 			aji_event_cb, "aji_devstate_subscription", client, AST_EVENT_IE_END);
 		ast_event_dump_cache(device_state_sub);
 	}
-	if(ast_test_flag(&globalflags, AJI_XEP0248)) {
-		aji_create_pubsub_collection(client, "device_state");
-		aji_create_pubsub_collection(client, "message_waiting");
-	} else {
-		aji_create_pubsub_node(client, NULL, "device_state", NULL);
-		aji_create_pubsub_node(client, NULL, "message_waiting", NULL);
-
-	}
-
+
+	aji_pubsub_subscribe(client, "device_state");
+	aji_pubsub_subscribe(client, "message_waiting");
 	iks_filter_add_rule(client->f, aji_handle_pubsub_event, client, IKS_RULE_TYPE,
 		IKS_PAK_MESSAGE, IKS_RULE_FROM, client->pubsub_node, IKS_RULE_DONE);
-	iks_filter_add_rule(client->f, aji_handle_publish_error, client, IKS_RULE_TYPE,
+	iks_filter_add_rule(client->f, aji_handle_pubsub_error, client, IKS_RULE_TYPE,
 		IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_ERROR, IKS_RULE_DONE);
 	
 }
@@ -2607,21 +2600,58 @@
 }
 
 /*!
+ * \brief Subscribe to a Pubsub node
+ * \param client the configured XMPP client we use to connect to a XMPP server
+ * \param node the name of the node to which to subscribe
+ * \return void
+ */
+static void aji_pubsub_subscribe(struct aji_client *client, const char *node)
+{
+	iks *request = aji_pubsub_iq_create(client, "set");
+	iks *pubsub, *subscribe;
+	
+	pubsub = iks_insert(request, "pubsub");
+	iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
+	subscribe = iks_insert(pubsub, "subscribe");
+	iks_insert_attrib(subscribe, "jid", client->jid->partial);
+	iks_insert_attrib(subscribe, "node", node);
+	if(ast_test_flag(&globalflags, AJI_XEP0248)) {
+		iks *options, *x, *sub_options, *sub_type, *sub_depth;
+		options = iks_insert(pubsub, "options");
+		x = iks_insert(options, "x");
+		iks_insert_attrib(x, "xmlns", "jabber:x:data");
+		iks_insert_attrib(x, "type", "submit");
+		sub_options = iks_insert(x, "field");
+		iks_insert_attrib(sub_options, "var", "FORM_TYPE");
+		iks_insert_attrib(sub_options, "type", "hidden");
+		iks_insert_cdata(iks_insert(sub_options, "value"),
+			"http://jabber.org/protocol/pubsub#subscribe_options", 51);
+		sub_type = iks_insert(x, "field");
+		iks_insert_attrib(sub_type, "var", "pubsub#subscription_type");
+		iks_insert_cdata(iks_insert(sub_type, "value"), "items", 5);
+		sub_depth = iks_insert(x, "field");
+		iks_insert_attrib(sub_type, "var", "pubsub#subscription_depth");
+		iks_insert_cdata(iks_insert(sub_depth, "value"), "all", 3);
+	}
+	ast_aji_send(client, request);
+	iks_delete(request);
+}
+
+/*!
  * \brief Publish an item to a PubSub node
  * \param client the configured XMPP client we use to connect to a XMPP server
  * \param device the name of the device whose state to publish
  * \param device_state the state to publish
  * \return void
  */
-static void aji_publish_device_state(struct aji_client *client, const char * device,
+static void aji_publish_device_state(struct aji_client *client, const char *device,
 	const char *device_state)
 {
 	iks *request = aji_pubsub_iq_create(client, "set");
 	iks *pubsub, *publish, *item, *entry, *state;
 	char eid_str[20];
 	ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
-	ast_log(LOG_ERROR, "eid_str is %s\n", eid_str);
-	
+
 	pubsub = iks_insert(request, "pubsub");
 	iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
 	publish = iks_insert(pubsub, "publish");
@@ -2659,17 +2689,18 @@
 	return request;
 }
 
-static int aji_handle_publish_error(void *data, ikspak *pak)
+static int aji_handle_pubsub_error(void *data, ikspak *pak)
 {
 	char *node_name;
-	iks *publish;
-	iks *pubsub = iks_find(pak->x, "pubsub");
+	iks *orig_request;
+	iks *orig_pubsub = iks_find(pak->x, "pubsub");
 	struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
-	if(pubsub) {
-		publish = iks_find(pubsub, "publish");
-		if(publish) {
-			node_name = iks_find_attrib(publish, "node");
-			if(iks_find(iks_find(iks_find(publish, "item"), "entry"), "state")) {
+	if(orig_pubsub) {
+		orig_request = iks_child(orig_pubsub);
+		node_name = iks_find_attrib(orig_request, "node");
+		
+		if(!strcasecmp(iks_name(orig_request), "publish")) {
+			if(iks_find(iks_find(iks_find(orig_request, "item"), "entry"), "state")) {
 				if(ast_test_flag(&globalflags, AJI_XEP0248)) {
 					aji_create_pubsub_leaf(client, "device_state", node_name);
 				} else {
@@ -2679,12 +2710,18 @@
 				iks *request = aji_pubsub_iq_create(client, "set");
 				pubsub = iks_insert(request, "pubsub");
 				iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
-				iks_insert_node(pubsub, publish);
+				iks_insert_node(pubsub, orig_request);
 				ast_aji_send(client, request);
 				iks_delete(request);
 				return IKS_FILTER_EAT;
 			}
-		} 
+		} else if(!strcasecmp(iks_name(orig_request), "subscribe")) {
+			if(ast_test_flag(&globalflags, AJI_XEP0248)) {
+				aji_create_pubsub_collection(client, node_name);
+			} else {
+				aji_create_pubsub_node(client, NULL, node_name, NULL);
+			}
+		}
 	}
 	return IKS_FILTER_EAT;
 }
@@ -2694,14 +2731,14 @@
  * \param client the configured XMPP client we use to connect to a XMPP server
  * \return void
  */
-static void aji_request_pubsub_items(struct aji_client *client)
+static void aji_request_pubsub_nodes(struct aji_client *client)
 {
 	int res = 0;
 	iks *request = aji_pubsub_iq_create(client, "get");
 	iks *query;
 	query = iks_insert(request, "query");
 	iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
-	iks_filter_add_rule(client->f, aji_receive_item_list, client, IKS_RULE_TYPE,
+	iks_filter_add_rule(client->f, aji_receive_node_list, client, IKS_RULE_TYPE,
 		IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid,
 		IKS_RULE_DONE);
 	res = ast_aji_send(client, request);
@@ -2716,29 +2753,34 @@
  * \param pak response from pubsub diso#items query
  * \return IKS_FILTER_EAT
  */
-static int aji_receive_item_list(void *data, ikspak* pak)
+static int aji_receive_node_list(void *data, ikspak* pak)
 {
 
 	struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
 	iks *item;
 	if(iks_has_children(pak->query)) {	
 		item = iks_first_tag(pak->query);
-		ast_log(LOG_WARNING, "Node name: %s\n", iks_find_attrib(item, "node"));
+		ast_log(LOG_WARNING, "Connection: %s  Node name: %s\n", client->jid->partial,
+			iks_find_attrib(item, "node"));
 		while((item = iks_next_tag(item))) {
-			ast_log(LOG_WARNING, "Node name: %s\n", iks_find_attrib(item, "node"));
+			ast_log(LOG_WARNING,
+				"Connection: %s  Node name: %s\n", client->jid->partial,
+				iks_find_attrib(item, "node"));
 		} 
 	}
 	iks_delete(item);
 	return IKS_FILTER_EAT;
 }
-		
-	
+
 
 /*!
  * \brief Method to expose PubSub node list via CLI.
+ * \param e pointer to ast_cli_entry structure
+ * \param cmd
+ * \param a pionter to ast_cli_args structure
  * \return char *
  */
-static char *aji_cli_list_pubsub_items(struct ast_cli_entry *e, int cmd, struct
+static char *aji_cli_list_pubsub_nodes(struct ast_cli_entry *e, int cmd, struct
 ast_cli_args *a)
 {
 		struct aji_client *client;
@@ -2746,10 +2788,10 @@
 
 		switch (cmd) {
 		case CLI_INIT:
-				e->command = "jabber list items";
+				e->command = "jabber list nodes";
 				e->usage =
-					"Usage: jabber list items [name]\n"
-					"       Lists root items on Pubsub server\n"
+					"Usage: jabber list nodes [name]\n"
+					"       Lists nodes on Pubsub server\n"
 					"       as configured in jabber.conf.\n";
 			return NULL;
 		case CLI_GENERATE:
@@ -2767,8 +2809,8 @@
 			return CLI_FAILURE;
 		}
 		
-		ast_cli(a->fd, "Listing pubsub items.\n");
-		aji_request_pubsub_items(client);
+		ast_cli(a->fd, "Listing pubsub nodes.\n");
+		aji_request_pubsub_nodes(client);
 		return CLI_SUCCESS;
 }
 
@@ -2778,7 +2820,6 @@
  * \param collection_name The name to use for this collection
  * \return void.
  */
-
 static void aji_create_pubsub_collection(struct aji_client *client, const char
 *collection_name)
 {




More information about the asterisk-commits mailing list