You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by jf...@apache.org on 2009/04/14 15:01:54 UTC

svn commit: r764762 - /httpd/httpd/trunk/modules/cluster/mod_heartmonitor.c

Author: jfclere
Date: Tue Apr 14 13:01:54 2009
New Revision: 764762

URL: http://svn.apache.org/viewvc?rev=764762&view=rev
Log:
Add a logic to use an handler and HTTP.

Modified:
    httpd/httpd/trunk/modules/cluster/mod_heartmonitor.c

Modified: httpd/httpd/trunk/modules/cluster/mod_heartmonitor.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/cluster/mod_heartmonitor.c?rev=764762&r1=764761&r2=764762&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/cluster/mod_heartmonitor.c (original)
+++ httpd/httpd/trunk/modules/cluster/mod_heartmonitor.c Tue Apr 14 13:01:54 2009
@@ -17,6 +17,7 @@
 #include "httpd.h"
 #include "http_config.h"
 #include "http_log.h"
+#include "http_core.h"
 #include "apr_strings.h"
 #include "apr_hash.h"
 #include "apr_time.h"
@@ -108,8 +109,6 @@
         return rv;
     }
 
-    ctx->servers = apr_hash_make(ctx->p);
-
     return APR_SUCCESS;
 }
 
@@ -240,30 +239,12 @@
     return s;
 }
 
-#define MAX_MSG_LEN (1000)
-static apr_status_t hm_recv(hm_ctx_t *ctx, apr_pool_t *p)
+/* Process a message receive from a backend node */
+static void hm_processmsg(hm_ctx_t *ctx, apr_pool_t *p,
+                                  apr_sockaddr_t *from, char *buf, int len)
 {
-    char buf[MAX_MSG_LEN + 1];
-    apr_sockaddr_t from;
-    apr_size_t len = MAX_MSG_LEN;
-    apr_status_t rv;
     apr_table_t *tbl;
 
-    from.pool = p;
-
-    rv = apr_socket_recvfrom(&from, ctx->sock, 0, buf, &len);
-
-    if (APR_STATUS_IS_EAGAIN(rv)) {
-        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ctx->s,
-                     "Heartmonitor: would block");
-        return APR_SUCCESS;
-    }
-    else if (rv) {
-        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ctx->s,
-                     "Heartmonitor: recvfrom failed");
-        return rv;
-    }
-
     buf[len] = '\0';
 
     tbl = apr_table_make(p, 10);
@@ -276,11 +257,11 @@
         char *ip;
         hm_server_t *s;
         /* TODO: REMOVE ME BEFORE PRODUCTION (????) */
-        ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, ctx->s,
-                     "Heartmonitor: %pI busy=%s ready=%s", &from,
+        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ctx->s,
+                     "Heartmonitor: %pI busy=%s ready=%s", from,
                      apr_table_get(tbl, "busy"), apr_table_get(tbl, "ready"));
 
-        apr_sockaddr_ip_get(&ip, &from);
+        apr_sockaddr_ip_get(&ip, from);
 
         s = hm_get_server(ctx, ip);
 
@@ -289,11 +270,38 @@
         s->seen = apr_time_now();
     }
     else {
+        ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ctx->s,
+                     "Heartmonitor: malformed message from %pI",
+                     from);
+    }
+
+}
+/* Read message from multicast socket */
+#define MAX_MSG_LEN (1000)
+static apr_status_t hm_recv(hm_ctx_t *ctx, apr_pool_t *p)
+{
+    char buf[MAX_MSG_LEN + 1];
+    apr_sockaddr_t from;
+    apr_size_t len = MAX_MSG_LEN;
+    apr_status_t rv;
+
+    from.pool = p;
+
+    rv = apr_socket_recvfrom(&from, ctx->sock, 0, buf, &len);
+
+    if (APR_STATUS_IS_EAGAIN(rv)) {
         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ctx->s,
-                     "Heartmonitor: malformed multicast message from %pI",
-                     &from);
+                     "Heartmonitor: would block");
+        return APR_SUCCESS;
+    }
+    else if (rv) {
+        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ctx->s,
+                     "Heartmonitor: recvfrom failed");
+        return rv;
     }
 
+    hm_processmsg(ctx, p, &from, buf, len);
+
     return rv;
 }
 
@@ -383,7 +391,6 @@
     if (!ctx->active) {
         return OK;
     }
-    apr_pool_create(&ctx->p, p);
     rv = ap_watchdog_get_instance(&ctx->watchdog,
                                   HM_WATHCHDOG_NAME,
                                   0, 1, p);
@@ -410,9 +417,38 @@
     return OK;
 }
 
+static int hm_handler(request_rec *r)
+{
+    apr_bucket_brigade *input_brigade;
+    apr_size_t len=MAX_MSG_LEN;
+    char *buf;
+    apr_status_t status;
+    hm_ctx_t *ctx = ap_get_module_config(r->server->module_config,
+                                         &heartmonitor_module);
+
+    if (strcmp(r->handler, "hearthbeat")) {
+        return DECLINED;
+    }
+    if (r->method_number != M_POST) {
+        return HTTP_METHOD_NOT_ALLOWED;
+    }
+    buf = apr_pcalloc(r->pool, MAX_MSG_LEN);
+    input_brigade = apr_brigade_create(r->connection->pool, r->connection->bucket_alloc);
+    status = ap_get_brigade(r->input_filters, input_brigade, AP_MODE_READBYTES, APR_BLOCK_READ, MAX_MSG_LEN);
+    if (status != APR_SUCCESS) {
+        return HTTP_INTERNAL_SERVER_ERROR;
+    }
+    apr_brigade_flatten(input_brigade, buf, &len);
+    hm_processmsg(ctx, r->pool, r->connection->remote_addr, buf, len);
+    return HTTP_OK;
+}
+
 static void hm_register_hooks(apr_pool_t *p)
 {
+    static const char * const aszSucc[]={ "mod_proxy.c", NULL };
     ap_hook_post_config(hm_post_config, NULL, NULL, APR_HOOK_MIDDLE);
+
+    ap_hook_handler(hm_handler, NULL, aszSucc, APR_HOOK_FIRST);
 }
 
 static void *hm_create_config(apr_pool_t *p, server_rec *s)
@@ -425,6 +461,9 @@
      */
     ctx->interval = apr_time_from_sec(HM_UPDATE_SEC);
     ctx->s = s;
+    apr_pool_create(&ctx->p, p);
+    ctx->servers = apr_hash_make(ctx->p);
+
     return ctx;
 }