diff options
-rw-r--r-- | src/Content.c | 95 | ||||
-rw-r--r-- | src/Sockets.c | 147 | ||||
-rw-r--r-- | src/Startup.c | 24 | ||||
-rw-r--r-- | src/http_Request.h | 4 | ||||
-rw-r--r-- | src/httpd.h | 1 |
5 files changed, 256 insertions, 15 deletions
diff --git a/src/Content.c b/src/Content.c index 757fcc3..2f2ffba 100644 --- a/src/Content.c +++ b/src/Content.c @@ -69,6 +69,7 @@ struct httpLink static int RegisterImages(void); +static int RedirectPage(cGH *cctkGH, httpRequest *request, void *data); static int MainPage(cGH *cctkGH, httpRequest *request, void *data); static int AboutPage(cGH *cctkGH, httpRequest *request, void *data); @@ -87,6 +88,7 @@ static int CookieTestPage(cGH *cctkGH, httpRequest *request, void *data); ********************************************************************/ void HTTP_ContentWork(cGH *GH); +int HTTP_RegisterRedirect(void); int HTTP_RegisterPages(void); int HTTPi_RegisterGroupsPages(void); int HTTPi_RegisterThornPages(void); @@ -133,6 +135,7 @@ void HTTP_ContentWork(cGH *GH) #endif } + /*@@ @routine HTTP_RegisterPages @date Wed Sep 13 23:47:43 2000 @@ -190,6 +193,29 @@ int HTTP_RegisterPages(void) } /*@@ + @routine HTTP_RegisterRedirect + @date Fri April 20 23:47:43 2001 + @author John Shalf + @desc + Redirection Page Registration. + @enddesc + @calls + @calledby + @history + + @endhistory + +@@*/ +int HTTP_RegisterRedirect(void){ + DECLARE_CCTK_PARAMETERS +#ifdef HTTP_DEBUG + printf("Registering Redirection Page"); +#endif + HTTP_RegisterPage("/index.html",RedirectPage,NULL); + RegisterImages(); +} + + /*@@ @routine HTTP_ContentLink @date Sun Sep 17 13:17:56 2000 @author Tom Goodale @@ -276,6 +302,70 @@ static int CompareStrings(const void *string1, const void *string2) /*@@ + @routine RedirectPage + @date Fri April 20 23:47:43 2001 + @author John Shalf + @desc + Redirects the webbrowser to the main server page + @enddesc + @calls + @calledby + @history + @hdate Thu Sep 14 10:54:22 2000 @hauthor John Shalf + @hdesc For clusters where its difficult to know which node is going to + have the webserver process, this opens a port on *all* distinct nodes + and redirects all web connection attempts to the root node for processing. + @endhistory + +@@*/ + +static int RedirectPage(cGH *cctkGH, httpRequest *request, void *data) +{ + + DECLARE_CCTK_PARAMETERS + + int retval; + char message[10098]; + char title[4098]; + char menu[4098]; + char *dir; + char *file; + struct httpLink *hlink; + + /* avoid compiler warning about unused parameter */ + data = data; + + /* Status message */ + strcpy(message,"HTTP/1.0 200 OK\r\n"); + + HTTP_Write(request, message, strlen(message)); + + /* Content-Type */ + strcpy(message,"Content-Type: text/html\r\n\r\n"); + + HTTP_Write(request, message, strlen(message)); + + /* Start the page */ + strcpy(message,"<HTML><HEAD><TITLE>Server Redirect</TITLE>\n"); + /* Write out the main header part */ + HTTP_Write(request, message, strlen(message)); + sprintf(message,"<META http-equiv=\"refresh\" content=\"1; URL=http://%s:%u\"></HEAD><body>\n", + HTTP_Master(),HTTP_Port()); + HTTP_Write(request,message,strlen(message)); + /* ********** Server Redirect To Master ************* */ + sprintf(message,"<p><h1>Redirect to master host=%s:%u</h1><p>\n", + HTTP_Master(),HTTP_Port()); + HTTP_Write(request,message,strlen(message)); + + HTTP_ContentFooter(cctkGH, 0, 4096, message); + retval = HTTP_Write(request, message, strlen(message)); + + /* retval = HTTP_Write(request, base_page, strlen(base_page)); */ + + return retval; +} + + /*@@ @routine MainPage @date Wed Sep 13 23:47:43 2000 @author Tom Goodale @@ -611,8 +701,6 @@ static int MainPage(cGH *cctkGH, httpRequest *request, void *data) HTTP_ContentFooter(cctkGH, 0, 4096, message); retval = HTTP_Write(request, message, strlen(message)); - /* retval = HTTP_Write(request, base_page, strlen(base_page)); */ - return retval; } @@ -1254,9 +1342,6 @@ static int ControlTerminationPage(cGH *cctkGH, httpRequest *request) HTTP_ContentFooter(cctkGH,0,4098,message); retval = HTTP_Write(request, message, strlen(message)); - - /* retval = HTTP_Write(request, base_page, strlen(base_page)); */ - return retval; } diff --git a/src/Sockets.c b/src/Sockets.c index 64fb5e0..93e8c8e 100644 --- a/src/Sockets.c +++ b/src/Sockets.c @@ -48,9 +48,16 @@ #include "httpd.h" +#include "cctk.h" +#include "cctk_DefineThorn.h" +#include "cctk_Parameters.h" +#ifdef CACTUSPUGH_PUGH +#include "CactusPUGH/PUGH/src/include/pugh.h" +#endif + static const char *rcsid = "$Header$"; -CCTK_FILEVERSION(CactusConnect_HTTPD_Sockets_c) +CCTK_FILEVERSION(CactusConnect_HTTPD_Socket_c) /******************************************************************** ********************* Local Data Types *********************** @@ -113,8 +120,9 @@ static fd_set active_fd_set; static SOCKET sock; static SOCKET minsock; static SOCKET maxsock; - +static char httpredirect=0; static httpSocket *socklist = NULL; +static char httpmaster[1024]; static unsigned long int httpport = 0; @@ -122,8 +130,89 @@ static unsigned long int httpport = 0; ********************* External Routines ********************** ********************************************************************/ +#if 0 /* this would be more efficient, but PUGH doesn't define + a datatype for 8-byte integers. So instead, + setupRedirect will use the actual hostname. */ +CCTK_INT8 Util_GetHostAddr(char *hostname){ + hostent *he; + char hostname[1025]; + int addrlen; + CCTK_INT8 addr; + + Util_GetHostName(hostname, 1024); + he=gethostbyname(hostname); + addrlen=he->h_length; + if(addrlen>sizeof(CCTK_INT8)) addrlen=CCTK_INT8; + addr=0; + memcpy(&addr,he->h_addr,addrlen); + return addr; +} +#endif /*@@ + @routine HTTP_SetupRedirect + @date Wed April 20 20:39:15 2001 + @author John Shalf + @desc + Creates a socket to listen on purely for server redirect to the root node. + @enddesc + @calls + @calledby + @history + + @endhistory + +@@*/ +int HTTP_SetupRedirect(cGH *cctkGH,int port, int queue_size,int hunt){ + /* should not hunt */ + int i; + char hostnm[1024]; + char *alladdr; + int addrlen=1024; + int nprocs=CCTK_nProcs(NULL); + int proc=CCTK_MyProc(NULL); + + puts("enter setup redirect------------"); + memset(hostnm,0,addrlen); + Util_GetHostName(hostnm, addrlen); + alladdr=(char*)malloc(addrlen * nprocs); + memset(alladdr,0,addrlen*nprocs); /* zero it */ +#ifdef CCTK_MPI + { /* push stack */ + pGH *pughGH = PUGH_pGH(cctkGH); + puts("all gather"); + if(!pughGH) puts("********would fail"); + MPI_Allgather(hostnm,addrlen,PUGH_MPI_CHAR, + alladdr,addrlen,PUGH_MPI_CHAR, + MPI_COMM_WORLD); + /* pughGH->PUGH_COMM_WORLD); fails if I use PUGH_COMM_WORLD */ + puts("collected"); + } +#endif + /* set our master */ + printf("alladdr is %s\n", alladdr); + strcpy(httpmaster,alladdr); /* the 0th element is the master hostname */ + /* so compare my addr to the list sequentially */ + for(i=0;i<nprocs;i++){ + printf("Cycle through %u\n",i); + if(!strcmp(alladdr+i*addrlen,hostnm)){ + /* we matched addresses */ + if(i<proc || i==0) break; /* I'm not the lowest order */ + /* otherwise, I am the lowest ranked processor with this + address that isn't the primary webserver. So + I should do a server redirect */ + httpredirect=1; /* this has a redirect socket */ + HTTP_SetupServer(port,queue_size,hunt); + HTTP_RegisterRedirect(); + return 1; + } + } + printf("No Redirect**************\n"); + httpredirect=0; + return 0; /* no registration performed */ +} + +/*@@ @routine HTTP_SetupServer @date Wed Sep 13 20:39:15 2000 @author Tom Goodale @@ -509,7 +598,61 @@ unsigned long int HTTP_Port(void) return httpport; } + /*@@ + @routine HTTP_Master + @date Friday April 20 11:24:40 2001 + @author John Shalf + @desc + Reports the hostname of the node the HTTP server is listening on. + The returns the hostname of the master as a string. + @enddesc + @calls + @calledby + @history + + @endhistory +@@*/ +char *HTTP_Master(void) +{ + return httpmaster; +} + + /*@@ + @routine HTTP_IsServer + @date Friday April 20 11:24:40 2001 + @author John Shalf + @desc + True if this node has a server socket open. + @enddesc + @calls + @calledby + @history + + @endhistory +@@*/ +char HTTP_IsServer(void){ + if(CCTK_MyProc(NULL)==0 || httpredirect) return 1; + else return 0; +} + /*@@ + @routine HTTP_IsRedirect + @date Friday April 20 11:24:40 2001 + @author John Shalf + @desc + True only if this node has a server open and that server + is exclusively used to redirect web traffic back to the root + node. + @enddesc + @calls + @calledby + @history + + @endhistory +@@*/ +char HTTP_IsRedirect(void){ + return httpredirect; +} /******************************************************************** ********************* Local Routines ************************* ********************************************************************/ diff --git a/src/Startup.c b/src/Startup.c index 1969dfa..a44c540 100644 --- a/src/Startup.c +++ b/src/Startup.c @@ -45,7 +45,7 @@ static void HTTP_SetupPollingThread(cGH *cctkGH); ***************** Scheduled Routine Prototypes ********************* ********************************************************************/ -int HTTP_Startup(void); +int HTTP_Startup(CCTK_ARGUMENTS); void HTTP_Work(CCTK_ARGUMENTS); int HTTP_Shutdown(void); @@ -82,28 +82,31 @@ static int thread_started = 0; @endhistory @@*/ -int HTTP_Startup(void) +int HTTP_Startup(CCTK_ARGUMENTS) { DECLARE_CCTK_PARAMETERS httpState state; - if(CCTK_MyProc(NULL) == 0) + if(CCTK_MyProc(NULL)==0) { /* Does the server provide any pages by default ? */ if(provide_pages) { HTTP_RegisterPages(); } - + HTTP_SetupServer(port, queue_length, hunt); } + else + HTTP_RegisterRedirect(); + HTTP_SetupRedirect(cctkGH,port,queue_length,hunt); HTTP_UpdateState(NULL, &state); do { - if(CCTK_MyProc(NULL) == 0) + if(HTTP_IsServer()) { HTTP_Poll(NULL, state.timeout_seconds, state.timeout_useconds); } @@ -148,7 +151,7 @@ void HTTP_Work(CCTK_ARGUMENTS) httpState state; #ifdef CCTK_PTHREADS - if(CCTK_MyProc(cctkGH) == 0 && use_pthreads && ! thread_started) + if(HTTP_IsServer() && use_pthreads && ! thread_started) { HTTP_SetupPollingThread(cctkGH); } @@ -162,7 +165,7 @@ void HTTP_Work(CCTK_ARGUMENTS) if(!use_pthreads) { #endif - if(CCTK_MyProc(cctkGH) == 0) + if(HTTP_IsServer()) { HTTP_Poll(cctkGH, state.timeout_seconds, state.timeout_useconds); } @@ -204,16 +207,21 @@ int HTTP_Shutdown(void) { DECLARE_CCTK_PARAMETERS #ifdef CCTK_PTHREADS +<<<<<<< Startup.c + /* Wait for the polling thread to exit */ + if(HTTP_IsServer() && use_pthreads && thread_started) +======= /* Set the "HTTPD::terminate" parameter to flag termination to the polling thread and wait for it to exit */ if(CCTK_MyProc(NULL) == 0 && use_pthreads && thread_started) +>>>>>>> 1.15 { CCTK_ParameterSet("terminate", CCTK_THORNSTRING, "yes"); pthread_join(polling_thread, NULL); } #endif - if(CCTK_MyProc(NULL) == 0) + if(HTTP_IsServer()) { HTTP_ShutdownServer(); } diff --git a/src/http_Request.h b/src/http_Request.h index cb2a4f5..6974e7f 100644 --- a/src/http_Request.h +++ b/src/http_Request.h @@ -79,6 +79,10 @@ int HTTP_Write(httpRequest *request, const char *buffer, size_t count); int HTTP_Read(httpRequest *request, char *buffer, size_t count); unsigned long int HTTP_Port(void); +char* HTTP_Master(void); + char HTTP_IsServer(void); + char HTTP_IsRedirect(void); + /* char HTTP_IsMaster(void); */ #ifdef __cplusplus } diff --git a/src/httpd.h b/src/httpd.h index 95485ad..ef55686 100644 --- a/src/httpd.h +++ b/src/httpd.h @@ -45,6 +45,7 @@ int HTTP_Terminate(cGH *cctkGH); /* Content stuff */ int HTTP_RegisterPages(void); +int HTTP_RegisterRedirect(void); #ifdef __cplusplus } |