Bladeren bron

rabbitmq: add 'url' new module parameter

If set to something else than the default value, it will overwrite the values
of all individual connection parameters.

Updated doc and README.
Stefan Mititelu 8 jaren geleden
bovenliggende
commit
49f8582baa

+ 70 - 52
src/modules/rabbitmq/README

@@ -25,14 +25,15 @@ Stefan-Cristian Mititelu
 
         3. Parameters
 
-              3.1. username (string)
-              3.2. password (string)
-              3.3. host (string)
-              3.4. vhost (string)
-              3.5. port (int)
-              3.6. timeout_sec (int)
-              3.7. timeout_usec (int)
-              3.8. direct_reply_to (int)
+              3.1. url (string)
+              3.2. username (string)
+              3.3. password (string)
+              3.4. host (string)
+              3.5. vhost (string)
+              3.6. port (int)
+              3.7. timeout_sec (int)
+              3.8. timeout_usec (int)
+              3.9. direct_reply_to (int)
 
         4. Functions
 
@@ -44,16 +45,17 @@ Stefan-Cristian Mititelu
 
    List of Examples
 
-   1.1. Set the “username” parameter
-   1.2. Set the “password” parameter
-   1.3. Set the “host” parameter
-   1.4. Set the “vhost” parameter
-   1.5. Set the “port” parameter
-   1.6. Set the “timeout_sec” parameter
-   1.7. Set the “timeout_usec” parameter
-   1.8. Set the “direct_reply_to” parameter
-   1.9. rabbitmq_publish usage
-   1.10. rabbitmq_publish_consume usage
+   1.1. Set the “url” parameter
+   1.2. Set the “username” parameter
+   1.3. Set the “password” parameter
+   1.4. Set the “host” parameter
+   1.5. Set the “vhost” parameter
+   1.6. Set the “port” parameter
+   1.7. Set the “timeout_sec” parameter
+   1.8. Set the “timeout_usec” parameter
+   1.9. Set the “direct_reply_to” parameter
+   1.10. rabbitmq_publish usage
+   1.11. rabbitmq_publish_consume usage
 
 Chapter 1. Admin Guide
 
@@ -67,14 +69,15 @@ Chapter 1. Admin Guide
 
    3. Parameters
 
-        3.1. username (string)
-        3.2. password (string)
-        3.3. host (string)
-        3.4. vhost (string)
-        3.5. port (int)
-        3.6. timeout_sec (int)
-        3.7. timeout_usec (int)
-        3.8. direct_reply_to (int)
+        3.1. url (string)
+        3.2. username (string)
+        3.3. password (string)
+        3.4. host (string)
+        3.5. vhost (string)
+        3.6. port (int)
+        3.7. timeout_sec (int)
+        3.8. timeout_usec (int)
+        3.9. direct_reply_to (int)
 
    4. Functions
 
@@ -114,71 +117,86 @@ Chapter 1. Admin Guide
 
 3. Parameters
 
-   3.1. username (string)
-   3.2. password (string)
-   3.3. host (string)
-   3.4. vhost (string)
-   3.5. port (int)
-   3.6. timeout_sec (int)
-   3.7. timeout_usec (int)
-   3.8. direct_reply_to (int)
+   3.1. url (string)
+   3.2. username (string)
+   3.3. password (string)
+   3.4. host (string)
+   3.5. vhost (string)
+   3.6. port (int)
+   3.7. timeout_sec (int)
+   3.8. timeout_usec (int)
+   3.9. direct_reply_to (int)
 
-3.1. username (string)
+3.1. url (string)
+
+   The amqp connection url. If set to something else than the default
+   value, it will overwrite the values of all connection parameters below
+   (e.g. username, password, host, vhost, port)
+
+   Default value is “amqp://guest:guest@localhost:5672/%2F”.
+
+   Example 1.1. Set the “url” parameter
+...
+modparam("rabbitmq", "url", "amqp://kamailio:kamailio@localhost:5672/%2Fkamailio
+")
+...
+
+3.2. username (string)
 
    The user name of amqp connection.
 
    Default value is “"guest"”.
 
-   Example 1.1. Set the “username” parameter
+   Example 1.2. Set the “username” parameter
 ...
 modparam("rabbitmq", "username", "user")
 ...
 
-3.2. password (string)
+3.3. password (string)
 
    The password of amqp connection.
 
    Default value is “"guest"”.
 
-   Example 1.2. Set the “password” parameter
+   Example 1.3. Set the “password” parameter
 ...
 modparam("rabbitmq", "password", "pass")
 ...
 
-3.3. host (string)
+3.4. host (string)
 
    The host of amqp connection.
 
    Default value is “"localhost"”.
 
-   Example 1.3. Set the “host” parameter
+   Example 1.4. Set the “host” parameter
 ...
 modparam("rabbitmq", "host", "127.0.0.1")
 ...
 
-3.4. vhost (string)
+3.5. vhost (string)
 
    The vhost of the amqp connection.
 
    Default value is “"/"”.
 
-   Example 1.4. Set the “vhost” parameter
+   Example 1.5. Set the “vhost” parameter
 ...
 modparam("rabbitmq", "vhost", "/vhost")
 ...
 
-3.5. port (int)
+3.6. port (int)
 
    The port of the amqp connection.
 
    Default value is “5672”.
 
-   Example 1.5. Set the “port” parameter
+   Example 1.6. Set the “port” parameter
 ...
 modparam("rabbitmq", "port", 5672)
 ...
 
-3.6. timeout_sec (int)
+3.7. timeout_sec (int)
 
    The timeout in seconds. The timeout_sec + timeout_usec combination
    gives the time to wait for an amqp reply, when
@@ -186,12 +204,12 @@ modparam("rabbitmq", "port", 5672)
 
    Default value is “1”.
 
-   Example 1.6. Set the “timeout_sec” parameter
+   Example 1.7. Set the “timeout_sec” parameter
 ...
 modparam("rabbitmq", "timeout_sec", 1)
 ...
 
-3.7. timeout_usec (int)
+3.8. timeout_usec (int)
 
    The timeout in micro seconds. The timeout_sec + timeout_usec
    combination gives the time to wait for an amqp reply, when
@@ -199,12 +217,12 @@ modparam("rabbitmq", "timeout_sec", 1)
 
    Default value is “0”.
 
-   Example 1.7. Set the “timeout_usec” parameter
+   Example 1.8. Set the “timeout_usec” parameter
 ...
 modparam("rabbitmq", "timeout_usec", 0)
 ...
 
-3.8. direct_reply_to (int)
+3.9. direct_reply_to (int)
 
    Setting this parameter to 1, enables rabbitmq direct reply-to feature.
    More info about this, can be found at
@@ -212,7 +230,7 @@ modparam("rabbitmq", "timeout_usec", 0)
 
    Default value is “0”.
 
-   Example 1.8. Set the “direct_reply_to” parameter
+   Example 1.9. Set the “direct_reply_to” parameter
 ...
 modparam("rabbitmq", "direct_reply_to", 1)
 ...
@@ -236,7 +254,7 @@ modparam("rabbitmq", "direct_reply_to", 1)
 
    This function can be used from REQUEST_ROUTE.
 
-   Example 1.9. rabbitmq_publish usage
+   Example 1.10. rabbitmq_publish usage
 rabbitmq_publish("exchange", "routing_key", "application/json", "$avp(json_reque
 st)");
 
@@ -256,6 +274,6 @@ messagebody, reply)
 
    This function can be used from REQUEST_ROUTE.
 
-   Example 1.10. rabbitmq_publish_consume usage
+   Example 1.11. rabbitmq_publish_consume usage
 rabbitmq_publish_consume("exchange", "routing_key", "application/json", "$avp(js
 on_request)", "$avp(json_reply)");

+ 23 - 0
src/modules/rabbitmq/doc/rabbitmq_admin.xml

@@ -71,6 +71,29 @@
 	<section>
 		<title>Parameters</title>
 
+		<section id="rabbitmq.p.url">
+			<title><varname>url</varname> (string)</title>
+			<para>
+				The amqp connection url.
+				If set to something else than the default value, it will overwrite the values of all connection parameters below (e.g. username, password, host, vhost, port)
+			</para>
+
+			<para>
+				<emphasis>
+					Default value is <quote>amqp://guest:guest@localhost:5672/%2F</quote>.
+				</emphasis>
+			</para>
+
+			<example>
+				<title>Set the <quote>url</quote> parameter</title>
+				<programlisting format="linespecific">
+...
+modparam("rabbitmq", "url", "amqp://kamailio:kamailio@localhost:5672/%2Fkamailio")
+...
+				</programlisting>
+			</example>
+		</section>
+
 		<section id="rabbitmq.p.username">
 			<title><varname>username</varname> (string)</title>
 			<para>

+ 17 - 0
src/modules/rabbitmq/rabbitmq.c

@@ -70,6 +70,8 @@ amqp_socket_t *amqp_sock = NULL;
 amqp_connection_state_t conn = NULL;
 
 /* module parameters */
+struct amqp_connection_info amqp_info;
+char *amqp_url = RABBITMQ_DEFAULT_AMQP_URL;
 char *amqp_username = "guest";
 char *amqp_password = "guest";
 char *amqp_host = "localhost";
@@ -125,6 +127,7 @@ static cmd_export_t cmds[] = {
 
 /* module parameters */
 static param_export_t params[] = {
+	{"url", PARAM_STRING, &amqp_url},
 	{"username", PARAM_STRING, &amqp_username},
 	{"password", PARAM_STRING, &amqp_password},
 	{"host", PARAM_STRING, &amqp_host},
@@ -152,6 +155,20 @@ struct module_exports exports = {
 /* module init */
 static int mod_init(void)
 {
+	if (strcmp(amqp_url, RABBITMQ_DEFAULT_AMQP_URL)) {
+		if (amqp_parse_url(amqp_url, &amqp_info) == AMQP_STATUS_BAD_URL) {
+			LM_ERR("FAIL parsing url: '%s'\n", amqp_url);
+			return -1;
+		} else {
+			LM_INFO("SUCCESS parsing url: '%s'\n", amqp_url);
+			amqp_username = amqp_info.user;
+			amqp_password = amqp_info.password;
+			amqp_host = amqp_info.host;
+			amqp_vhost = amqp_info.vhost;
+			amqp_port = amqp_info.port;
+		}
+	}
+
 	return 0;
 }
 

+ 3 - 1
src/modules/rabbitmq/rabbitmq.h

@@ -38,7 +38,9 @@
  */
 
 #ifndef _RABBITMQ_H_
-#define _RTPENGINE_H_H
+#define _RABBITMQ_H_
+
+#define RABBITMQ_DEFAULT_AMQP_URL	"amqp://guest:guest@localhost:5672/%2F"
 
 typedef enum {
 	RABBITMQ_OK=1,