On Thu, Jul 21, 2011 at 12:04 PM, Adam Stokes <astokes(a)fedoraproject.org> wrote:
- Test for matahari/sysconfig MATAHARI_{HOST,PORT}
if not available, set to localhost.
- Something other than localhost attempt to resolve
servername, if fails fallback to localhost.
- Localhost fails, attempt DNS SRV lookup.
Signed-off-by: Adam Stokes <astokes(a)fedoraproject.org>
---
matahari.init.in | 4 ++
src/include/matahari/mh_agent.h | 3 ++
src/lib/mh_agent.cpp | 65 +++++++++++++++++++++++++++------------
3 files changed, 52 insertions(+), 20 deletions(-)
diff --git a/matahari.init.in b/matahari.init.in
index 9c6a541..5dbffce 100644
--- a/matahari.init.in
+++ b/matahari.init.in
@@ -30,6 +30,10 @@ test -f @sysconfdir@/sysconfig/matahari && .
@sysconfdir@/sysconfig/matahari
start() {
echo -n $"Starting the $SERVICE daemon: "
+ if [ ! -z $MATAHARI_BROKER ]; then
+ MATAHARI_BROKER="127.0.0.1"
+ MATAHARI_PORT=49000
+ fi
Not quite.
Leave it unset and let parse_config decide what to do next.
Otherwise you can't tell the difference between unset (in which case
we try various defaults), and set but unavailable (in which case we
wait).
This would allow you to keep using most of the HAVE_RESOLV_A logic.
daemon $PROCESS --broker $MATAHARI_BROKER --port $MATAHARI_PORT
$MATAHARI_AGENT_ARGS --daemon
RETVAL=$?
echo
diff --git a/src/include/matahari/mh_agent.h b/src/include/matahari/mh_agent.h
index 5b6c8a3..275e090 100644
--- a/src/include/matahari/mh_agent.h
+++ b/src/include/matahari/mh_agent.h
@@ -47,6 +47,9 @@ int required_argument = 1;
#define MH_NOT_IMPLEMENTED "Not implemented"
+#ifndef NS_MAXDNAME
+# define NS_MAXDNAME 1025
+#endif
using namespace qpid::management;
using namespace std;
diff --git a/src/lib/mh_agent.cpp b/src/lib/mh_agent.cpp
index d3183d7..4ef1054 100644
--- a/src/lib/mh_agent.cpp
+++ b/src/lib/mh_agent.cpp
@@ -42,8 +42,13 @@ int use_stderr = 0;
#include "matahari/mh_agent.h"
extern "C" {
+#include <sys/types.h>
#include "matahari/logging.h"
#include "matahari/dnssrv.h"
+#ifndef WIN32
+#include <sys/socket.h>
+#include <netdb.h>
+#endif
}
using namespace qpid::management;
@@ -188,7 +193,7 @@ mh_parse_options(const char *proc_name, int argc, char **argv,
qpid::types::Vari
std::stringstream url;
int arg;
- int serverport = MATAHARI_PORT;
+ int serverport = MATAHARI_PORT;
char *servername = NULL;
const char *protocol = NULL;
@@ -254,6 +259,8 @@ mh_parse_options(const char *proc_name, int argc, char **argv,
qpid::types::Vari
int opt_string_len = 0;
char opt_string[2 * DIMOF(matahari_options)];
struct option *long_opts = (struct option *)calloc(1, sizeof(struct option));
+ struct addrinfo hints, *res;
+ int rc;
/* Force more local-only processing */
mh_add_option('h', no_argument, "help", NULL, NULL, NULL);
@@ -298,6 +305,14 @@ mh_parse_options(const char *proc_name, int argc, char **argv,
qpid::types::Vari
break;
case 'b':
servername = strdup(optarg);
+#ifndef WIN32
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_family = AF_UNSPEC;
+ if ((rc = getaddrinfo(servername, NULL, &hints, &res)) != 0) {
+ free(servername);
+ servername = strdup("127.0.0.1");
+ }
+#endif
break;
case 'N':
ssl_cert_name = optarg;
@@ -354,24 +369,6 @@ mh_parse_options(const char *proc_name, int argc, char **argv,
qpid::types::Vari
protocol = "tcp";
}
- /*
- * Based on servername do a SRV lookup for best possible
- * server providing this service.
- */
- if (servername) {
-#ifdef HAVE_RESOLV_A
- int ret;
- char query[NS_MAXDNAME];
- char target[NS_MAXDNAME];
- g_snprintf(query, sizeof(query), "_matahari._tcp.%s", servername);
- ret = mh_srv_lookup(query, target, sizeof(target));
- if (ret == 0) {
- servername = strdup(target);
- }
-#endif
- } else {
- servername = strdup(MATAHARI_BROKER);
- }
url << "amqp:" << protocol << ":" <<
servername << ":" << serverport ;
@@ -422,8 +419,17 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
string url = mh_parse_options(proc_name, argc, argv, options);
I think I'd prefer to see the signature of this function change
instead of parsing the url.
- cout << options << endl;
+ /* Parse servername out of url for fallback use */
+ vector<string>url_array;
+ string token;
+ istringstream iss(url);
+ while (getline(iss, token, ':')) {
+ url_array.push_back(token);
+ }
+
+ string servername = *(url_array.rbegin() + 1);
+ string serverport = *(url_array.rbegin());
/* Re-initialize logging now that we've completed option processing */
mh_log_init(proc_name, mh_log_level, mh_log_level > LOG_INFO);
@@ -435,6 +441,25 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
_amqp_connection = qpid::messaging::Connection(url, options);
_amqp_connection.open();
+ while (!_amqp_connection.isOpen()) {
+ std::stringstream url;
+ url << "amqp:tcp:";
+ int ret;
+ char query[NS_MAXDNAME];
+ char target[NS_MAXDNAME];
+ g_snprintf(query, sizeof(query), "_matahari._tcp.%s",
servername.c_str());
+ ret = mh_srv_lookup(query, target, sizeof(target));
+ if (ret == 0) {
+ url << target << ":" << MATAHARI_PORT;
+ _amqp_connection = qpid::messaging::Connection(url.str(), options);
+ _amqp_connection.open();
+ } else {
+ url << servername.c_str() << ":" <<
serverport.c_str();
+ _amqp_connection = qpid::messaging::Connection(url.str(), options);
+ _amqp_connection.open();
+ }
+ g_usleep(1000);
+ }
_agent_session = qmf::AgentSession(_amqp_connection);
_agent_session.setVendor("matahariproject.org");
--
1.7.6
_______________________________________________
Matahari mailing list
Matahari(a)lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/matahari