Skip to content
Snippets Groups Projects
Commit 34aeba8e authored by rswindell's avatar rswindell
Browse files

Changed SERVICE_OPT_STANDALONE to SERVICE_OPT_STATIC to describe non-dynamic

services.
Created new services.terminated property (set to true when terminating services
before recycle or shutdown).
Fixed a few bugs related to static (standalone) services.
parent 3ee64255
No related branches found
No related tags found
No related merge requests found
...@@ -88,6 +88,8 @@ typedef struct { ...@@ -88,6 +88,8 @@ typedef struct {
/* These are run-time state and stat vars */ /* These are run-time state and stat vars */
DWORD clients; DWORD clients;
SOCKET socket; SOCKET socket;
BOOL running;
BOOL terminated;
} service_t; } service_t;
typedef struct { typedef struct {
...@@ -264,7 +266,10 @@ js_log(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -264,7 +266,10 @@ js_log(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
strcat(str," "); strcat(str," ");
} }
lprintf("%04d %s %s",client->socket,client->service->protocol,str); if(service==NULL)
lprintf("%04d %s",client->socket,str);
else
lprintf("%04d %s %s",client->socket,client->service->protocol,str);
*rval = JSVAL_VOID; *rval = JSVAL_VOID;
return(JS_TRUE); return(JS_TRUE);
...@@ -452,10 +457,55 @@ js_ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report) ...@@ -452,10 +457,55 @@ js_ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
lprintf("%04d %s !JavaScript %s%s%s: %s",sock,prot,warning,file,line,message); lprintf("%04d %s !JavaScript %s%s%s: %s",sock,prot,warning,file,line,message);
} }
/* Server Object Properites */
enum {
SERVER_PROP_TERMINATED
};
static JSBool js_server_get(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
jsint tiny;
service_client_t* client;
if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
return(JS_FALSE);
tiny = JSVAL_TO_INT(id);
switch(tiny) {
case SERVER_PROP_TERMINATED:
#if 0
lprintf("%s client->service->terminated=%d"
,client->service->protocol, client->service->terminated);
#endif
*vp = BOOLEAN_TO_JSVAL(client->service->terminated);
break;
}
return(JS_TRUE);
}
#define SERVER_PROP_FLAGS JSPROP_ENUMERATE|JSPROP_READONLY
static struct JSPropertySpec js_server_properties[] = {
/* name ,tinyid ,flags, getter, setter */
{ "terminated" ,SERVER_PROP_TERMINATED ,SERVER_PROP_FLAGS, NULL,NULL},
{0}
};
static JSClass js_server_class = { static JSClass js_server_class = {
"Server",0, "Server" /* name */
JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub, ,0 /* flags */
JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub ,JS_PropertyStub /* addProperty */
,JS_PropertyStub /* delProperty */
,js_server_get /* getProperty */
,JS_PropertyStub /* setProperty */
,JS_EnumerateStub /* enumerate */
,JS_ResolveStub /* resolve */
,JS_ConvertStub /* convert */
,JS_FinalizeStub /* finalize */
}; };
static JSContext* static JSContext*
...@@ -512,7 +562,10 @@ js_initcx(JSRuntime* js_runtime, SOCKET sock, service_client_t* service_client, ...@@ -512,7 +562,10 @@ js_initcx(JSRuntime* js_runtime, SOCKET sock, service_client_t* service_client,
,NULL,0))==NULL) ,NULL,0))==NULL)
break; break;
if(service_client->client==NULL) /* standalone service */ if(!JS_DefineProperties(js_cx, server, js_server_properties))
break;
if(service_client->client==NULL) /* static service */
if(js_CreateSocketObject(js_cx, server, "socket", service_client->socket)==NULL) if(js_CreateSocketObject(js_cx, server, "socket", service_client->socket)==NULL)
break; break;
...@@ -772,11 +825,12 @@ static void js_service_thread(void* arg) ...@@ -772,11 +825,12 @@ static void js_service_thread(void* arg)
close_socket(socket); close_socket(socket);
} }
static void js_standalone_service_thread(void* arg) static void js_static_service_thread(void* arg)
{ {
char spath[MAX_PATH+1]; char spath[MAX_PATH+1];
service_t* service; service_t* service;
service_client_t service_client; service_client_t service_client;
SOCKET socket;
/* JavaScript-specific */ /* JavaScript-specific */
JSObject* js_glob; JSObject* js_glob;
JSScript* js_script; JSScript* js_script;
...@@ -788,7 +842,10 @@ static void js_standalone_service_thread(void* arg) ...@@ -788,7 +842,10 @@ static void js_standalone_service_thread(void* arg)
// Copy service_client arg // Copy service_client arg
service=(service_t*)arg; service=(service_t*)arg;
lprintf("%04d %s JavaScript standalone service thread started", service->socket, service->protocol); service->running=TRUE;
socket = service->socket;
lprintf("%04d %s static JavaScript service thread started", service->socket, service->protocol);
thread_up(TRUE /* setuid */); thread_up(TRUE /* setuid */);
...@@ -826,23 +883,29 @@ static void js_standalone_service_thread(void* arg) ...@@ -826,23 +883,29 @@ static void js_standalone_service_thread(void* arg)
JS_DestroyRuntime(js_runtime); JS_DestroyRuntime(js_runtime);
thread_down(); thread_down();
lprintf("%04d %s JavaScript standalone service thread terminated" lprintf("%04d %s static JavaScript service thread terminated"
,service->socket, service->protocol); ,socket, service->protocol);
close_socket(service->socket); close_socket(service->socket);
service->socket=INVALID_SOCKET; service->socket=INVALID_SOCKET;
service->running=FALSE;
} }
static void native_standalone_service_thread(void* arg) static void native_static_service_thread(void* arg)
{ {
char cmd[MAX_PATH]; char cmd[MAX_PATH];
char fullcmd[MAX_PATH*2]; char fullcmd[MAX_PATH*2];
SOCKET socket;
SOCKET socket_dup; SOCKET socket_dup;
service_t* service; service_t* service;
service = (service_t*)arg; service = (service_t*)arg;
lprintf("%04d %s standalone service thread started", service->socket, service->protocol); service->running=TRUE;
socket = service->socket;
lprintf("%04d %s static service thread started", socket, service->protocol);
thread_up(TRUE /* setuid */); thread_up(TRUE /* setuid */);
...@@ -855,7 +918,7 @@ static void native_standalone_service_thread(void* arg) ...@@ -855,7 +918,7 @@ static void native_standalone_service_thread(void* arg)
TRUE, // Inheritable TRUE, // Inheritable
DUPLICATE_SAME_ACCESS)) { DUPLICATE_SAME_ACCESS)) {
lprintf("%04d !%s ERROR %d duplicating socket descriptor" lprintf("%04d !%s ERROR %d duplicating socket descriptor"
,service->socket,service->protocol,GetLastError()); ,socket,service->protocol,GetLastError());
close_socket(service->socket); close_socket(service->socket);
service->socket=INVALID_SOCKET; service->socket=INVALID_SOCKET;
thread_down(); thread_down();
...@@ -875,12 +938,14 @@ static void native_standalone_service_thread(void* arg) ...@@ -875,12 +938,14 @@ static void native_standalone_service_thread(void* arg)
system(fullcmd); system(fullcmd);
thread_down(); thread_down();
lprintf("%04d %s standalone service thread terminated" lprintf("%04d %s static service thread terminated"
,socket, service->protocol); ,socket, service->protocol);
close_socket(service->socket); close_socket(service->socket);
service->socket=INVALID_SOCKET; service->socket=INVALID_SOCKET;
closesocket(socket_dup); /* close duplicate handle */ closesocket(socket_dup); /* close duplicate handle */
service->running=FALSE;
} }
static void native_service_thread(void* arg) static void native_service_thread(void* arg)
...@@ -1124,6 +1189,7 @@ void DLLCALL services_thread(void* arg) ...@@ -1124,6 +1189,7 @@ void DLLCALL services_thread(void* arg)
int result; int result;
int optval; int optval;
ulong total_clients; ulong total_clients;
ulong total_running;
time_t t; time_t t;
time_t initialized=0; time_t initialized=0;
fd_set socket_set; fd_set socket_set;
...@@ -1305,18 +1371,18 @@ void DLLCALL services_thread(void* arg) ...@@ -1305,18 +1371,18 @@ void DLLCALL services_thread(void* arg)
return; return;
} }
/* Setup standalone service threads */ /* Setup static service threads */
for(i=0;i<(int)services;i++) { for(i=0;i<(int)services;i++) {
if(!(service[i].options&SERVICE_OPT_STANDALONE)) if(!(service[i].options&SERVICE_OPT_STATIC))
continue; continue;
/* start thread here */ /* start thread here */
SAFECOPY(cmd,service[i].cmd); SAFECOPY(cmd,service[i].cmd);
strlwr(cmd); strlwr(cmd);
if(strstr(cmd,".js")) /* JavaScript */ if(strstr(cmd,".js")) /* JavaScript */
_beginthread(js_standalone_service_thread, 0, &service[i]); _beginthread(js_static_service_thread, 0, &service[i]);
else /* Native */ else /* Native */
_beginthread(native_standalone_service_thread, 0, &service[i]); _beginthread(native_static_service_thread, 0, &service[i]);
} }
/* signal caller that we've started up successfully */ /* signal caller that we've started up successfully */
...@@ -1361,7 +1427,7 @@ void DLLCALL services_thread(void* arg) ...@@ -1361,7 +1427,7 @@ void DLLCALL services_thread(void* arg)
FD_ZERO(&socket_set); FD_ZERO(&socket_set);
high_socket=0; high_socket=0;
for(i=0;i<(int)services;i++) { for(i=0;i<(int)services;i++) {
if(service[i].options&SERVICE_OPT_STANDALONE) if(service[i].options&SERVICE_OPT_STATIC)
continue; continue;
if(service[i].socket==INVALID_SOCKET) if(service[i].socket==INVALID_SOCKET)
continue; continue;
...@@ -1549,13 +1615,15 @@ void DLLCALL services_thread(void* arg) ...@@ -1549,13 +1615,15 @@ void DLLCALL services_thread(void* arg)
} }
/* Close Service Sockets */ /* Close Service Sockets */
lprintf("0000 Closing service sockets");
for(i=0;i<(int)services;i++) { for(i=0;i<(int)services;i++) {
service[i].terminated=TRUE;
if(service[i].socket!=INVALID_SOCKET) if(service[i].socket!=INVALID_SOCKET)
close_socket(service[i].socket); close_socket(service[i].socket);
service[i].socket=INVALID_SOCKET; service[i].socket=INVALID_SOCKET;
} }
/* Wait for Service Threads to terminate */ /* Wait for Dynamic Service Threads to terminate */
total_clients=0; total_clients=0;
for(i=0;i<(int)services;i++) for(i=0;i<(int)services;i++)
total_clients+=service[i].clients; total_clients+=service[i].clients;
...@@ -1572,6 +1640,23 @@ void DLLCALL services_thread(void* arg) ...@@ -1572,6 +1640,23 @@ void DLLCALL services_thread(void* arg)
lprintf("0000 Done waiting"); lprintf("0000 Done waiting");
} }
/* Wait for Static Service Threads to terminate */
total_running=0;
for(i=0;i<(int)services;i++)
total_running+=service[i].running;
if(total_running) {
lprintf("0000 Waiting for %d static services to terminate",total_running);
while(1) {
total_running=0;
for(i=0;i<(int)services;i++)
total_running+=service[i].running;
if(!total_running)
break;
mswait(500);
}
lprintf("0000 Done waiting");
}
/* Free Service Data */ /* Free Service Data */
services=0; services=0;
free(service); free(service);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment