mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
Merge branch 'master' of supraverse.net:libpsyc
This commit is contained in:
commit
b6b4ce87e1
4 changed files with 78 additions and 33 deletions
|
@ -3,7 +3,6 @@ DEBUG = 2
|
|||
CFLAGS = -I../include -I../src -Wall -std=c99 ${OPT}
|
||||
LDFLAGS = -L../lib
|
||||
LOADLIBES = -lpsyc -lm
|
||||
LOADLIBES_NET = ${LOADLIBES}
|
||||
TARGETS = testPsyc testParser testMatch testRender testText isRoutingVar getVarType
|
||||
O = test.o
|
||||
WRAPPER =
|
||||
|
@ -13,14 +12,14 @@ NC = nc
|
|||
DIFF = diff
|
||||
|
||||
ifeq ($(shell uname),SunOS)
|
||||
LOADLIBES_NET := ${LOADLIBES_NET} -lsocket -lnsl
|
||||
LOADLIBES_NET := -lsocket -lnsl
|
||||
DIFF = gdiff
|
||||
endif
|
||||
|
||||
all: ${TARGETS}
|
||||
it: all
|
||||
|
||||
testPsyc: LOADLIBES := ${LOADLIBES_NET}
|
||||
testPsyc: LOADLIBES := ${LOADLIBES} ${LOADLIBES_NET}
|
||||
testPsyc: test.o
|
||||
|
||||
diet: WRAPPER = ${DIET}
|
||||
|
@ -67,7 +66,7 @@ pkterr:
|
|||
|
||||
srvstart:
|
||||
pkill -x testPsyc; exit 0
|
||||
./testPsyc -p ${PORT} ${srv_args} &
|
||||
./testPsyc -p ${PORT} -S ${srv_args} &
|
||||
|
||||
srvkill:
|
||||
pkill -x testPsyc
|
||||
|
|
32
test/test.c
32
test/test.c
|
@ -26,6 +26,18 @@
|
|||
// cmd line args
|
||||
extern uint8_t verbose, stats;
|
||||
|
||||
void check_range(char c, const char *s, int min, int max) {
|
||||
int n = atoi(s);
|
||||
if (n < min) {
|
||||
printf("-%c: error, should be >= %d\n", c, min);
|
||||
exit(-1);
|
||||
}
|
||||
if (max > min && n > max) {
|
||||
printf("-%c: error, should be <= %d\n", c, max);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
// get sockaddr, IPv4 or IPv6:
|
||||
void *get_in_addr (struct sockaddr *sa) {
|
||||
if (sa->sa_family == AF_INET)
|
||||
|
@ -34,10 +46,11 @@ void *get_in_addr (struct sockaddr *sa) {
|
|||
return &(((struct sockaddr_in6*)sa)->sin6_addr);
|
||||
}
|
||||
|
||||
void test_file(const char* filename, size_t recv_buf_size) {
|
||||
void test_file(const char* filename, size_t count, size_t recv_buf_size) {
|
||||
char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr]
|
||||
char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^
|
||||
size_t nbytes;
|
||||
size_t i, nbytes;
|
||||
struct timeval start, end;
|
||||
|
||||
int fd = open(filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
|
@ -47,11 +60,20 @@ void test_file(const char* filename, size_t recv_buf_size) {
|
|||
|
||||
test_init(0);
|
||||
|
||||
if (stats)
|
||||
gettimeofday(&start, NULL);
|
||||
|
||||
while ((nbytes = read(fd, (void*)recvbuf, recv_buf_size)))
|
||||
test_input(0, recvbuf, nbytes);
|
||||
for (i = 0; i < count; i++)
|
||||
test_input(0, recvbuf, nbytes);
|
||||
|
||||
if (stats) {
|
||||
gettimeofday(&end, NULL);
|
||||
printf("%ld ms\n", (end.tv_sec * 1000000 + end.tv_usec - start.tv_sec * 1000000 - start.tv_usec) / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
void test_server(const char* port, size_t recv_buf_size) {
|
||||
void test_server(const char* port, size_t count, size_t recv_buf_size) {
|
||||
char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr]
|
||||
char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^
|
||||
|
||||
|
@ -116,6 +138,8 @@ void test_server(const char* port, size_t recv_buf_size) {
|
|||
exit(3);
|
||||
}
|
||||
|
||||
printf("# Listening on TCP port %s\n", port);
|
||||
|
||||
// add the listener to the master set
|
||||
FD_SET(listener, &master);
|
||||
|
||||
|
|
|
@ -9,7 +9,8 @@
|
|||
void test_init(int i);
|
||||
int test_input(int i, char *recvbuf, size_t nbytes);
|
||||
|
||||
void test_file(const char* filename, size_t recv_buf_size);
|
||||
void test_server(const char* port, size_t recv_buf_size);
|
||||
void test_file(const char* filename, size_t count, size_t recv_buf_size);
|
||||
void test_server(const char* port, size_t count, size_t recv_buf_size);
|
||||
void check_range(char c, const char *s, int min, int max);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
|
@ -18,46 +19,64 @@
|
|||
#define ENTITY_LINES 32
|
||||
|
||||
// cmd line args
|
||||
uint8_t verbose, stats;
|
||||
uint8_t routing_only, parse_multiple, no_render, progress;
|
||||
char *filename, *port = "4440";
|
||||
size_t recv_buf_size = RECV_BUF_SIZE;
|
||||
uint8_t verbose, stats;
|
||||
uint8_t multiple, single, routing_only, no_render, quiet, progress;
|
||||
size_t count = 1, recv_buf_size = RECV_BUF_SIZE;
|
||||
|
||||
psycParseState parsers[NUM_PARSERS];
|
||||
psycPacket packets[NUM_PARSERS];
|
||||
psycModifier routing[NUM_PARSERS][ROUTING_LINES];
|
||||
psycModifier entity[NUM_PARSERS][ENTITY_LINES];
|
||||
|
||||
int contbytes, last_ret;
|
||||
int contbytes, exit_code;
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
int c;
|
||||
while ((c = getopt (argc, argv, "f:p:b:mnrsvP")) != -1) {
|
||||
while ((c = getopt (argc, argv, "f:p:b:c:nmqrsvPSh")) != -1) {
|
||||
switch (c) {
|
||||
case 'f': filename = optarg; break;
|
||||
case 'p': port = optarg;
|
||||
if (atoi(optarg) <= 0) { printf("-%c: error, should be > 0\n", c); exit(-1); }
|
||||
break;
|
||||
case 'b': recv_buf_size = atoi(optarg);
|
||||
if (atoi(optarg) <= 0) { printf("-%c: error, should be > 0\n", c); exit(-1); }
|
||||
break;
|
||||
case 'm': parse_multiple = 1; break;
|
||||
case 'p': port = optarg; check_range(c, optarg, 1, 0); break;
|
||||
case 'b': recv_buf_size = atoi(optarg); check_range(c, optarg, 1, RECV_BUF_SIZE); break;
|
||||
case 'c': count = atoi(optarg); check_range(c, optarg, 1, 0); break;
|
||||
case 'n': no_render = 1; break;
|
||||
case 'm': multiple = 1; break;
|
||||
case 'q': quiet = 1; break;
|
||||
case 'r': routing_only = 1; break;
|
||||
case 's': stats = 1; break;
|
||||
case 'v': verbose++; break;
|
||||
case 'P': progress = 1; break;
|
||||
case 'S': single = 1; break;
|
||||
case 'h':
|
||||
printf(
|
||||
"testPsyc -f <filename> [-b <read_buf_size>] [-c <count>] [-mnqrSsvP]\n"
|
||||
"testPsyc [-p <port>] [-b <recv_buf_size>] [-nqrsvP]\n"
|
||||
" -f <filename>\tInput file name\n"
|
||||
" -p <port>\t\tListen on TCP port, default is %s\n"
|
||||
" -b <buf_size>\tRead/receive buffer size, default is %d\n"
|
||||
" -c <count>\t\tParse data from file <count> times\n"
|
||||
" -m\t\t\tParse multiple packets from file\n"
|
||||
" -n\t\t\tNo rendering, only parsing\n"
|
||||
" -r\t\t\tParse routing header only\n"
|
||||
" -q\t\t\tQuiet mode, don't output rendered string\n"
|
||||
" -S\t\t\tSingle packet mode, close connection after parsing one packet\n"
|
||||
" -s\t\t\tShow statistics at the end\n"
|
||||
" -v\t\t\tVerbose, can be specified multiple times for more verbosity\n"
|
||||
" -P\t\t\tShow progress\n"
|
||||
" -h\t\t\tShow this help\n",
|
||||
port, RECV_BUF_SIZE);
|
||||
exit(0);
|
||||
case '?': exit(-1);
|
||||
default: abort();
|
||||
}
|
||||
}
|
||||
|
||||
if (filename)
|
||||
test_file(filename, recv_buf_size);
|
||||
test_file(filename, count, recv_buf_size);
|
||||
else
|
||||
test_server(port, recv_buf_size);
|
||||
test_server(port, count, recv_buf_size);
|
||||
|
||||
return last_ret;
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
static inline
|
||||
|
@ -102,7 +121,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
value.length = 0;
|
||||
|
||||
do {
|
||||
ret = last_ret = psyc_parse(&parsers[i], &oper, &name, &value);
|
||||
ret = exit_code = psyc_parse(&parsers[i], &oper, &name, &value);
|
||||
if (verbose >= 2)
|
||||
printf("# ret = %d\n", ret);
|
||||
|
||||
|
@ -145,7 +164,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
printf("# Done parsing.\n");
|
||||
else if (progress)
|
||||
r = write(1, ".", 1);
|
||||
if (!parse_multiple) // parse multiple packets?
|
||||
if ((filename && !multiple) || (!filename && single))
|
||||
ret = -1;
|
||||
|
||||
if (!no_render) {
|
||||
|
@ -160,12 +179,14 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
psyc_setPacketLength(&packets[i]);
|
||||
|
||||
if (psyc_render(&packets[i], sendbuf, SEND_BUF_SIZE) == PSYC_RENDER_SUCCESS) {
|
||||
if (filename && write(1, sendbuf, packets[i].length) == -1) {
|
||||
perror("write");
|
||||
ret = -1;
|
||||
} else if (!filename && send(i, sendbuf, packets[i].length, 0) == -1) {
|
||||
perror("send");
|
||||
ret = -1;
|
||||
if (!quiet) {
|
||||
if (filename && write(1, sendbuf, packets[i].length) == -1) {
|
||||
perror("write");
|
||||
ret = -1;
|
||||
} else if (!filename && send(i, sendbuf, packets[i].length, 0) == -1) {
|
||||
perror("send");
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
printf("# Render error");
|
||||
|
|
Loading…
Reference in a new issue