Bläddra i källkod

dmq: Add an RPC interface to query the nodes in the cluster

Alex Hermann 11 år sedan
förälder
incheckning
e990a74e44
1 ändrade filer med 38 tillägg och 0 borttagningar
  1. 38 0
      modules/dmq/dmq.c

+ 38 - 0
modules/dmq/dmq.c

@@ -40,6 +40,7 @@
 #include "../../lib/kmi/mi.h"
 #include "../../hashes.h"
 #include "../../mod_fix.h"
+#include "../../rpc_lookup.h"
 
 #include "dmq.h"
 #include "dmq_funcs.h"
@@ -112,6 +113,8 @@ static mi_export_t mi_cmds[] = {
 	{0, 0, 0, 0, 0}
 };
 
+static rpc_export_t rpc_methods[];
+
 /** module exports */
 struct module_exports exports = {
 	"dmq",				/* module name */
@@ -195,6 +198,11 @@ static int mod_init(void)
 		return -1;
 	}
 
+	if (rpc_register_array(rpc_methods)!=0) {
+		LM_ERR("failed to register RPC commands\n");
+		return -1;
+	}
+
 	/* register worker processes - add one because of the ping process */
 	register_procs(num_workers);
 	
@@ -325,3 +333,33 @@ static int bcast_dmq_fixup(void** param, int param_no)
         return fixup_spve_null(param, 1);
 }
 
+static void dmq_rpc_list_nodes(rpc_t *rpc, void *c)
+{
+	void *h;
+	dmq_node_t* cur = node_list->nodes;
+
+	while(cur) {
+		if (rpc->add(c, "{", &h) < 0) goto error;
+		if (rpc->struct_add(h, "SSddd",
+			"host", &cur->uri.host,
+			"port", &cur->uri.port,
+			"status", cur->status,
+			"last_notification", cur->last_notification,
+			"local", cur->local) < 0) goto error;
+		cur = cur->next;
+	}
+	return;
+error:
+	LM_ERR("Failed to add item to RPC response\n");
+	return;
+
+}
+
+static const char *dmq_rpc_list_nodes_doc[2] = {
+	"Print all nodes", 0
+};
+
+static rpc_export_t rpc_methods[] = {
+	{"dmq.list_nodes", dmq_rpc_list_nodes, dmq_rpc_list_nodes_doc, RET_ARRAY},
+	{0, 0, 0, 0}
+};