Debugging (mostly AppClient)
This commit is contained in:
parent
e6e79e3675
commit
129dd7af42
|
@ -10,6 +10,7 @@
|
|||
#ifdef DEBUG
|
||||
# include <stdio.h>
|
||||
#endif
|
||||
#include <netdb.h>
|
||||
|
||||
#include "string.h"
|
||||
#include "appinterface.h"
|
||||
|
@ -36,32 +37,11 @@ struct _AppClient
|
|||
/* private */
|
||||
static int _appclient_timeout(AppClient * appclient)
|
||||
{
|
||||
static int cnt = 0;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s%d%s", "appclient_timeout(): ", cnt, "\n");
|
||||
fprintf(stderr, "%s%d%s", "appclient_timeout()\n");
|
||||
#endif
|
||||
return cnt++ > 1000 || appclient->fd > -1; /* FIXME use a constant */
|
||||
}
|
||||
|
||||
|
||||
static int _appclient_read(int fd, AppClient * ac)
|
||||
{
|
||||
ssize_t len;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s%d%s", "appclient_read(", fd, ")\n");
|
||||
#endif
|
||||
if((len = sizeof(ac->buf_read) - ac->buf_read_cnt) <= 0
|
||||
|| (len = read(fd, &ac->buf_read[ac->buf_read_cnt],
|
||||
len)) <= 0)
|
||||
{
|
||||
/* FIXME */
|
||||
return 1;
|
||||
}
|
||||
ac->buf_read_cnt+=len;
|
||||
/* FIXME */
|
||||
return ac->buf_read_cnt < sizeof(ac->buf_read) ? 0 : 1; /* FIXME */
|
||||
close(appclient->fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
@ -72,11 +52,12 @@ static int _appclient_read_int(int fd, AppClient * ac)
|
|||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s%d%s", "appclient_read_int(", fd, ")\n");
|
||||
#endif
|
||||
if((len = (sizeof(ac->buf_read) - ac->buf_read_cnt)) <= 0
|
||||
if((len = (sizeof(ac->buf_read) - ac->buf_read_cnt)) < 0
|
||||
|| (len = read(fd, &ac->buf_read[ac->buf_read_cnt],
|
||||
len)) <= 0)
|
||||
{
|
||||
/* FIXME */
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
ac->buf_read_cnt+=len;
|
||||
|
@ -86,6 +67,8 @@ static int _appclient_read_int(int fd, AppClient * ac)
|
|||
memmove(ac->buf_read, &ac->buf_read[sizeof(int)],
|
||||
ac->buf_read_cnt-sizeof(int));
|
||||
ac->buf_read_cnt-=sizeof(int);
|
||||
event_unregister_timeout(ac->event,
|
||||
(EventTimeoutFunc)_appclient_timeout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -98,19 +81,14 @@ static int _appclient_write(int fd, AppClient * ac)
|
|||
fprintf(stderr, "%s%d%s", "appclient_write(", fd, ")\n");
|
||||
#endif
|
||||
len = ac->buf_write_cnt;
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "writing %d\n", len);
|
||||
#endif
|
||||
if((len = write(fd, ac->buf_write, len)) <= 0)
|
||||
{
|
||||
/* FIXME */
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
memmove(ac->buf_write, &ac->buf_write[len], len);
|
||||
ac->buf_write_cnt-=len;
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "written %d, %d left\n", len, ac->buf_write_cnt);
|
||||
#endif
|
||||
if(ac->buf_write_cnt > 0)
|
||||
return 0;
|
||||
event_register_io_read(ac->event, fd,
|
||||
|
@ -156,6 +134,7 @@ AppClient * appclient_new_event(char * app, Event * event)
|
|||
appclient->fd = -1;
|
||||
appclient->buf_read_cnt = 0;
|
||||
appclient->buf_write_cnt = 0;
|
||||
appclient->res = -1;
|
||||
if(_new_connect(appclient, app) != 0)
|
||||
{
|
||||
free(appclient);
|
||||
|
@ -164,6 +143,7 @@ AppClient * appclient_new_event(char * app, Event * event)
|
|||
return appclient;
|
||||
}
|
||||
|
||||
static uint32_t _connect_addr(char * service);
|
||||
static int _new_connect(AppClient * appclient, char * app)
|
||||
{
|
||||
struct sockaddr_in sa;
|
||||
|
@ -172,8 +152,8 @@ static int _new_connect(AppClient * appclient, char * app)
|
|||
if((appclient->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
||||
return 1;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_port = htons(4242); /* FIXME */
|
||||
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
sa.sin_port = htons(appinterface_port(appclient->interface));
|
||||
sa.sin_addr.s_addr = _connect_addr("Session");
|
||||
if(connect(appclient->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0)
|
||||
return 1;
|
||||
if((port = appclient_call(appclient, "port", 1, app)) == -1)
|
||||
|
@ -194,19 +174,36 @@ static int _new_connect(AppClient * appclient, char * app)
|
|||
return 1;
|
||||
if((appclient->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
||||
return 1;
|
||||
sa.sin_addr.s_addr = _connect_addr(app);
|
||||
sa.sin_port = htons(port);
|
||||
if(connect(appclient->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0)
|
||||
return 1;
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "HERE %p\n", appclient->interface);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t _connect_addr(char * service)
|
||||
{
|
||||
char prefix[] = "APPSERVER_";
|
||||
int len = sizeof(prefix);
|
||||
char * env;
|
||||
char * appserver;
|
||||
struct hostent * he;
|
||||
|
||||
if((env = malloc(len + string_length(service) + 1)) == NULL)
|
||||
return htonl(INADDR_LOOPBACK);
|
||||
sprintf(env, "%s%s", prefix, service);
|
||||
appserver = getenv(env);
|
||||
free(env);
|
||||
if(appserver == NULL || (he = gethostbyname(appserver)) == NULL)
|
||||
return htonl(INADDR_LOOPBACK);
|
||||
return *((uint32_t*)(he->h_addr));
|
||||
}
|
||||
|
||||
|
||||
/* appclient_delete */
|
||||
void appclient_delete(AppClient * appclient)
|
||||
{
|
||||
appinterface_delete(appclient->interface);
|
||||
free(appclient);
|
||||
}
|
||||
|
||||
|
@ -218,7 +215,8 @@ int appclient_call(AppClient * ac, char * function, int args_cnt, ...)
|
|||
int i;
|
||||
void ** args = NULL;
|
||||
void ** p;
|
||||
struct timeval tv = { 0, 10 };
|
||||
struct timeval tv = { 0, 1000000 };
|
||||
Event * eventtmp;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s%p%s", "appclient_call(), interface ", ac->interface,
|
||||
|
@ -227,20 +225,24 @@ int appclient_call(AppClient * ac, char * function, int args_cnt, ...)
|
|||
va_start(arg, args_cnt);
|
||||
for(i = 0; i < args_cnt; i++) /* FIXME */
|
||||
{
|
||||
if((p = realloc(args, i * sizeof(void*))) == NULL)
|
||||
if((p = realloc(args, (i+1) * sizeof(void*))) == NULL)
|
||||
break;
|
||||
args = p;
|
||||
args[i] = va_arg(arg, void *);
|
||||
}
|
||||
va_end(arg);
|
||||
if(i != args_cnt)
|
||||
return -1;
|
||||
if((i = appinterface_call(ac->interface, function,
|
||||
if(i != args_cnt || (i = appinterface_call(ac->interface, function,
|
||||
&ac->buf_write[ac->buf_write_cnt],
|
||||
sizeof(ac->buf_write) - ac->buf_write_cnt,
|
||||
args)) <= 0)
|
||||
{
|
||||
free(args);
|
||||
return -1;
|
||||
}
|
||||
free(args);
|
||||
ac->buf_write_cnt+=i;
|
||||
eventtmp = ac->event;
|
||||
ac->event = event_new();
|
||||
event_register_timeout(ac->event, tv,
|
||||
(EventTimeoutFunc)_appclient_timeout, ac);
|
||||
event_register_io_write(ac->event, ac->fd,
|
||||
|
@ -249,5 +251,7 @@ int appclient_call(AppClient * ac, char * function, int args_cnt, ...)
|
|||
fprintf(stderr, "%s", "AppClient looping in wait for answer()\n");
|
||||
#endif
|
||||
event_loop(ac->event);
|
||||
event_delete(ac->event);
|
||||
ac->event = eventtmp;
|
||||
return ac->res;
|
||||
}
|
||||
|
|
85
src/event.c
85
src/event.c
|
@ -101,15 +101,18 @@ int event_loop(Event * event)
|
|||
_loop_timeout(event);
|
||||
_loop_io(event, event->reads, &rfds);
|
||||
_loop_io(event, event->writes, &wfds);
|
||||
timeout = event->timeout.tv_sec == LONG_MAX
|
||||
&& event->timeout.tv_usec == LONG_MAX
|
||||
? NULL : &event->timeout;
|
||||
if(event->timeout.tv_sec == LONG_MAX
|
||||
&& event->timeout.tv_usec == LONG_MAX)
|
||||
timeout = NULL;
|
||||
else
|
||||
timeout = &event->timeout;
|
||||
rfds = event->rfds;
|
||||
wfds = event->wfds;
|
||||
}
|
||||
if(ret != -1)
|
||||
return 0;
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s", "event_loop(): ");
|
||||
perror("select");
|
||||
sleep(1);
|
||||
#endif
|
||||
|
@ -123,7 +126,7 @@ static void _loop_timeout(Event * event)
|
|||
EventTimeout * et;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s", "_loop_timeout()\n");
|
||||
fprintf(stderr, "%s", "_loop_timeout()\n");
|
||||
#endif
|
||||
if(gettimeofday(&now, NULL) != 0)
|
||||
#ifdef DEBUG
|
||||
|
@ -182,9 +185,8 @@ static void _loop_timeout(Event * event)
|
|||
i++;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s%ld%s", "loop event.tv_sec=",
|
||||
event->timeout.tv_sec, "\n");
|
||||
fprintf(stderr, "%s%ld%s", "loop event.tv_usec=",
|
||||
fprintf(stderr, "%s%ld%s%ld%s", "_loop_timeout() tv_sec=",
|
||||
event->timeout.tv_sec, ", tv_usec=",
|
||||
event->timeout.tv_usec, "\n");
|
||||
#endif
|
||||
}
|
||||
|
@ -195,13 +197,12 @@ static void _loop_io(Event * event, eventioArray * eios, fd_set * fds)
|
|||
EventIO * eio;
|
||||
int fd;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s", "_loop_io()\n");
|
||||
#endif
|
||||
while(i < array_count(eios))
|
||||
{
|
||||
array_get(eios, i, &eio);
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s%d%s%p%s", "_loop_io(): i=", i,
|
||||
", eio=", eio, "\n");
|
||||
#endif
|
||||
if((fd = eio->fd) <= event->fdmax && FD_ISSET(fd, fds)
|
||||
&& eio->func(fd, eio->data) != 0)
|
||||
{
|
||||
|
@ -246,9 +247,6 @@ int event_register_io_write(Event * event, int fd, EventIOFunc func,
|
|||
{
|
||||
EventIO * eventio;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s", "event_register_io_write()\n");
|
||||
#endif
|
||||
if((eventio = malloc(sizeof(EventIO))) == NULL)
|
||||
return 1;
|
||||
eventio->fd = fd;
|
||||
|
@ -272,7 +270,8 @@ int event_register_timeout(Event * event, struct timeval timeout,
|
|||
return 1;
|
||||
if((eventtimeout = malloc(sizeof(EventTimeout))) == NULL)
|
||||
return 1;
|
||||
eventtimeout->initial = timeout;
|
||||
eventtimeout->initial.tv_sec = timeout.tv_sec;
|
||||
eventtimeout->initial.tv_usec = timeout.tv_usec;
|
||||
eventtimeout->timeout.tv_sec = now.tv_sec + timeout.tv_sec;
|
||||
eventtimeout->timeout.tv_usec = now.tv_usec + timeout.tv_usec;
|
||||
eventtimeout->func = func;
|
||||
|
@ -282,6 +281,12 @@ int event_register_timeout(Event * event, struct timeval timeout,
|
|||
|| (event->timeout.tv_sec == timeout.tv_sec
|
||||
&& event->timeout.tv_usec > timeout.tv_usec))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s%ld%s%ld%s",
|
||||
"event_register_timeout() tv_sec=",
|
||||
timeout.tv_sec, ", tv_usec=", timeout.tv_usec,
|
||||
"\n");
|
||||
#endif
|
||||
event->timeout.tv_sec = timeout.tv_sec;
|
||||
event->timeout.tv_usec = timeout.tv_usec;
|
||||
}
|
||||
|
@ -330,3 +335,53 @@ static int _unregister_io(eventioArray * eios, fd_set * fds, int fd)
|
|||
}
|
||||
return fdmax;
|
||||
}
|
||||
|
||||
|
||||
/* event_unregister_timeout */
|
||||
int event_unregister_timeout(Event * event, EventTimeoutFunc func)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
EventTimeout * et;
|
||||
struct timeval now;
|
||||
|
||||
fprintf(stderr, "%s", "event_unregister_timeout()\n");
|
||||
while(i < array_count(event->timeouts))
|
||||
{
|
||||
array_get(event->timeouts, i, &et);
|
||||
if(et->func != func)
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
array_remove_pos(event->timeouts, i);
|
||||
free(et);
|
||||
}
|
||||
if(gettimeofday(&now, NULL) != 0)
|
||||
return 1;
|
||||
event->timeout.tv_sec = LONG_MAX;
|
||||
event->timeout.tv_usec = LONG_MAX;
|
||||
for(i = 0; i < array_count(event->timeouts); i++)
|
||||
{
|
||||
array_get(event->timeouts, i, &et);
|
||||
if(et->timeout.tv_sec < event->timeout.tv_sec
|
||||
|| (et->timeout.tv_sec == event->timeout.tv_sec
|
||||
&& et->timeout.tv_usec
|
||||
< event->timeout.tv_usec))
|
||||
{
|
||||
if((event->timeout.tv_sec = et->timeout.tv_sec
|
||||
- now.tv_sec) < 0)
|
||||
{
|
||||
event->timeout.tv_sec = 0;
|
||||
event->timeout.tv_usec = 0;
|
||||
break;
|
||||
}
|
||||
event->timeout.tv_usec = et->timeout.tv_usec
|
||||
- now.tv_usec;
|
||||
if(event->timeout.tv_usec >= 0)
|
||||
continue;
|
||||
event->timeout.tv_sec = max(0, event->timeout.tv_sec-1);
|
||||
event->timeout.tv_usec = -event->timeout.tv_usec;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user