1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

testServer modularizing

This commit is contained in:
Gabor Adam Toth 2011-05-14 18:21:00 +02:00
parent bafa18f7b7
commit 5b73646508
5 changed files with 58 additions and 44 deletions

2
.gitignore vendored
View file

@ -9,7 +9,7 @@ src/match
test/testMatch test/testMatch
test/testParser test/testParser
test/testRender test/testRender
test/testServer test/testServerPsyc
test/testText test/testText
test/isRoutingVar test/isRoutingVar
test/getVarType test/getVarType

View file

@ -4,7 +4,8 @@ CFLAGS = -I../include -I../src -Wall -std=c99 ${OPT}
LDFLAGS = -L../lib LDFLAGS = -L../lib
LOADLIBES = -lpsyc -lm LOADLIBES = -lpsyc -lm
LOADLIBES_NET = ${LOADLIBES} LOADLIBES_NET = ${LOADLIBES}
TARGETS = testServer testParser testMatch testRender testText isRoutingVar getVarType TARGETS = testServerPsyc testParser testMatch testRender testText isRoutingVar getVarType
O = testServer.o
WRAPPER = WRAPPER =
DIET = diet DIET = diet
PORT = 4440 PORT = 4440
@ -19,7 +20,8 @@ endif
all: ${TARGETS} all: ${TARGETS}
it: all it: all
testServer: LOADLIBES := ${LOADLIBES_NET} testServerPsyc: LOADLIBES := ${LOADLIBES_NET}
testServerPsyc: testServer.o
diet: WRAPPER = ${DIET} diet: WRAPPER = ${DIET}
diet: all diet: all
@ -29,7 +31,7 @@ debug: CFLAGS := $(subst ${OPT},-O0,${CFLAGS})
debug: all debug: all
clean: clean:
rm -f ${TARGETS} rm -f ${TARGETS} $O
test: ${TARGETS} test: ${TARGETS}
./testRender ./testRender
@ -47,12 +49,12 @@ nettest: nettestfull nettestsplit
nettestrun: srvstart pkt srvkill nettestrun: srvstart pkt srvkill
nettestfull: nettestfull:
${MAKE} nettestrun; x=$$?; pkill -x testServer; exit $$x ${MAKE} nettestrun; x=$$?; pkill -x testServerPsyc; exit $$x
${MAKE} nettestrun srv_args=r; x=$$?; pkill -x testServer; exit $$x ${MAKE} nettestrun srv_args=r; x=$$?; pkill -x testServerPsyc; exit $$x
split_max = 10 split_max = 10
nettestsplit: nettestsplit:
x=0; for n in `seq 1 ${split_max}`; do ${MAKE} nettestrun srv_recvbuf=$$n && ${MAKE} nettestrun srv_args=r srv_recvbuf=$$n || break; done; x=$$?; pkill -x testServer; exit $$x x=0; for n in `seq 1 ${split_max}`; do ${MAKE} nettestrun srv_recvbuf=$$n && ${MAKE} nettestrun srv_args=r srv_recvbuf=$$n || break; done; x=$$?; pkill -x testServerPsyc; exit $$x
pkt: pkt:
x=0; for f in packets/[0-9]*; do echo ">> $$f"; cat $$f | nc localhost ${PORT} | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x x=0; for f in packets/[0-9]*; do echo ">> $$f"; cat $$f | nc localhost ${PORT} | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
@ -64,8 +66,8 @@ pkterr:
for f in packets/err-*; do echo ">> $$f"; cat $$f | nc localhost ${PORT}; done for f in packets/err-*; do echo ">> $$f"; cat $$f | nc localhost ${PORT}; done
srvstart: srvstart:
pkill -x testServer; exit 0 pkill -x testServerPsyc; exit 0
./testServer ${PORT} -${srv_args} ${srv_recvbuf} & ./testServerPsyc ${PORT} -${srv_args} ${srv_recvbuf} &
srvkill: srvkill:
pkill -x testServer pkill -x testServerPsyc

View file

@ -19,7 +19,9 @@
#include <netdb.h> #include <netdb.h>
#include "testServer.h" #include "testServer.h"
#include "testServerPsyc.c"
// cmd line args
extern uint8_t verbose, stats;
// get sockaddr, IPv4 or IPv6: // get sockaddr, IPv4 or IPv6:
void *get_in_addr (struct sockaddr *sa) { void *get_in_addr (struct sockaddr *sa) {
@ -29,21 +31,9 @@ void *get_in_addr (struct sockaddr *sa) {
return &(((struct sockaddr_in6*)sa)->sin6_addr); return &(((struct sockaddr_in6*)sa)->sin6_addr);
} }
int main (int argc, char **argv) { void test_server(const char* port, size_t recv_buf_size) {
char *port = argc > 1 ? argv[1] : "4440"; char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr]
char *opts = argc > 2 ? argv[2] : NULL; char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^
char *v, *w;
verbose = opts && (v = memchr(opts, (int)'v', strlen(opts))) ?
v - opts > 0 && (w = memchr(v+1, (int)'v', strlen(opts) - (v - opts))) ?
w - v > 0 && memchr(w+1, (int)'v', strlen(opts) - (w - opts)) ? 3 : 2 : 1 : 0;
routing_only = opts && memchr(opts, (int)'r', strlen(opts));
parse_multiple = opts && memchr(opts, (int)'m', strlen(opts));
no_render = opts && memchr(opts, (int)'n', strlen(opts));
progress = opts && memchr(opts, (int)'p', strlen(opts));
stats = opts && memchr(opts, (int)'s', strlen(opts));
size_t recv_buf_size = argc > 3 ? atoi(argv[3]) : 0;
if (recv_buf_size <= 0)
recv_buf_size = RECV_BUF_SIZE;
fd_set master; // master file descriptor list fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select() fd_set read_fds; // temp file descriptor list for select()
@ -53,6 +43,7 @@ int main (int argc, char **argv) {
int newfd; // newly accept()ed socket descriptor int newfd; // newly accept()ed socket descriptor
struct sockaddr_storage remoteaddr; // client address struct sockaddr_storage remoteaddr; // client address
socklen_t addrlen; socklen_t addrlen;
size_t nbytes;
char remoteIP[INET6_ADDRSTRLEN]; char remoteIP[INET6_ADDRSTRLEN];
@ -62,6 +53,9 @@ int main (int argc, char **argv) {
struct addrinfo hints, *ai, *p; struct addrinfo hints, *ai, *p;
struct timeval start[NUM_PARSERS], end[NUM_PARSERS]; struct timeval start[NUM_PARSERS], end[NUM_PARSERS];
if (recv_buf_size <= 0)
recv_buf_size = RECV_BUF_SIZE;
FD_ZERO(&master); // clear the master and temp sets FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds); FD_ZERO(&read_fds);
@ -169,10 +163,7 @@ int main (int argc, char **argv) {
if (verbose >= 3) if (verbose >= 3)
printf("> [%.*s]", (int)nbytes, recvbuf); printf("> [%.*s]", (int)nbytes, recvbuf);
ret = test_input(i); ret = test_input(i, recvbuf, nbytes);
if (progress)
write(1, " ", 1);
if (stats) if (stats)
gettimeofday(&end[i], NULL); gettimeofday(&end[i], NULL);
@ -188,6 +179,4 @@ int main (int argc, char **argv) {
} // END got new incoming connection } // END got new incoming connection
} // END looping through file descriptors } // END looping through file descriptors
} // END for(;;)--and you thought it would never end! } // END for(;;)--and you thought it would never end!
return 0;
} }

View file

@ -6,16 +6,8 @@
# define SEND_BUF_SIZE 8 * 1024 # define SEND_BUF_SIZE 8 * 1024
# define NUM_PARSERS 100 # define NUM_PARSERS 100
char sendbuf[SEND_BUF_SIZE];
char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr]
char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^
// parse buf: ^^^^^^
// cmd line args
uint8_t verbose, routing_only, parse_multiple, no_render, progress, stats;
size_t nbytes;
void test_init(int i); void test_init(int i);
int test_input(int i); int test_input(int i, char *recvbuf, size_t nbytes);
void test_server(const char* port, size_t recv_buf_size);
#endif #endif

View file

@ -10,17 +10,23 @@
#include <psyc/syntax.h> #include <psyc/syntax.h>
#include "testServer.h" #include "testServer.h"
//#include "testServer.c"
// max size for routing & entity header // max size for routing & entity header
#define ROUTING_LINES 16 #define ROUTING_LINES 16
#define ENTITY_LINES 32 #define ENTITY_LINES 32
// cmd line args
uint8_t verbose, stats;
psycParseState parsers[NUM_PARSERS]; psycParseState parsers[NUM_PARSERS];
psycPacket packets[NUM_PARSERS]; psycPacket packets[NUM_PARSERS];
psycModifier routing[NUM_PARSERS][ROUTING_LINES]; psycModifier routing[NUM_PARSERS][ROUTING_LINES];
psycModifier entity[NUM_PARSERS][ENTITY_LINES]; psycModifier entity[NUM_PARSERS][ENTITY_LINES];
psycModifier *mod = NULL; psycModifier *mod = NULL;
uint8_t routing_only, parse_multiple, no_render, progress;
int ret, retl, contbytes = 0; int ret, retl, contbytes = 0;
char oper; char oper;
psycString name, value, elem; psycString name, value, elem;
@ -28,6 +34,25 @@ psycString *pname = NULL, *pvalue = NULL;
psycParseListState listState; psycParseListState listState;
size_t len; size_t len;
int main (int argc, char **argv) {
char *port = argc > 1 ? argv[1] : "4440";
char *opts = argc > 2 ? argv[2] : NULL;
char *v, *w;
verbose = opts && (v = memchr(opts, (int)'v', strlen(opts))) ?
v - opts > 0 && (w = memchr(v+1, (int)'v', strlen(opts) - (v - opts))) ?
w - v > 0 && memchr(w+1, (int)'v', strlen(opts) - (w - opts)) ? 3 : 2 : 1 : 0;
stats = opts && memchr(opts, (int)'s', strlen(opts));
routing_only = opts && memchr(opts, (int)'r', strlen(opts));
parse_multiple = opts && memchr(opts, (int)'m', strlen(opts));
no_render = opts && memchr(opts, (int)'n', strlen(opts));
progress = opts && memchr(opts, (int)'p', strlen(opts));
size_t recv_buf_size = argc > 3 ? atoi(argv[3]) : 0;
test_server(port, recv_buf_size);
return 0;
}
static inline static inline
void resetString (psycString *s, uint8_t freeptr) { void resetString (psycString *s, uint8_t freeptr) {
if (freeptr && s->length) if (freeptr && s->length)
@ -51,10 +76,12 @@ void test_init (int i) {
packets[i].entity.modifiers = entity[i]; packets[i].entity.modifiers = entity[i];
} }
int test_input (int i) { int test_input (int i, char *recvbuf, size_t nbytes) {
// we got some data from a client // we got some data from a client
int j; int j;
char *parsebuf = recvbuf - contbytes; char *parsebuf = recvbuf - contbytes;
char sendbuf[SEND_BUF_SIZE];
psyc_setParseBuffer2(&parsers[i], parsebuf, contbytes + nbytes); psyc_setParseBuffer2(&parsers[i], parsebuf, contbytes + nbytes);
contbytes = 0; contbytes = 0;
oper = 0; oper = 0;
@ -164,7 +191,7 @@ int test_input (int i) {
contbytes = psyc_getParseRemainingLength(&parsers[i]); contbytes = psyc_getParseRemainingLength(&parsers[i]);
if (contbytes > 0) { // copy end of parsebuf before start of recvbuf if (contbytes > 0) { // copy end of parsebuf before start of recvbuf
assert(recvbuf - contbytes >= buf); // make sure it's still in the buffer assert(contbytes <= CONT_BUF_SIZE); // make sure it's still in the buffer
memmove(recvbuf - contbytes, psyc_getParseRemainingBuffer(&parsers[i]), contbytes); memmove(recvbuf - contbytes, psyc_getParseRemainingBuffer(&parsers[i]), contbytes);
} }
ret = 0; ret = 0;
@ -271,5 +298,9 @@ int test_input (int i) {
} }
} }
while (ret > 0); while (ret > 0);
if (progress)
write(1, " ", 1);
return ret; return ret;
} }