From 42d96dc3ca3d28d3a76f54fb11da49b64cd8660b Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 31 Mar 2011 00:21:15 -0700 Subject: [PATCH] Add ping-pong test - not yet working on unix --- .gitignore | 1 + Makefile | 5 ++ ol-unix.c | 2 +- test/echo.c | 3 +- test/test-ping-pong.c | 118 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 test/test-ping-pong.c diff --git a/.gitignore b/.gitignore index 25639a40..8ed05b54 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ ev/autom4te.cache /Release/ test/echo-demo +test/test-ping-pong diff --git a/Makefile b/Makefile index 70a88db0..4b1a66fb 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,11 @@ +all: test/echo-demo test/test-ping-pong + test/echo-demo: test/echo-demo.c test/echo.o ol.a $(CC) -ansi -g -o test/echo-demo test/echo-demo.c test/echo.o ol.a -lm +test/test-ping-pong: test/test-ping-pong.c test/echo.o ol.a + $(CC) -ansi -g -o test/test-ping-pong test/test-ping-pong.c test/echo.o ol.a -lm + ol.a: ol-unix.o ev/ev.o $(AR) rcs ol.a ol-unix.o ev/ev.o diff --git a/ol-unix.c b/ol-unix.c index 79023919..1dc83cba 100644 --- a/ol-unix.c +++ b/ol-unix.c @@ -63,6 +63,7 @@ ol_handle* ol_tcp_handle_new(ol_close_cb close_cb, void* data) { return NULL; } + handle->type = OL_TCP; handle->close_cb = close_cb; handle->data = data; @@ -316,7 +317,6 @@ void ol_tcp_io(EV_P_ ev_io* watcher, int revents) { void ol_tcp_connect(ol_handle* handle, ol_req* req) { assert(handle->_.fd >= 0); assert(req); - assert(req->type == OL_CONNECT); int error; int errorsize = sizeof(int); diff --git a/test/echo.c b/test/echo.c index c3be192a..bd18c874 100644 --- a/test/echo.c +++ b/test/echo.c @@ -8,7 +8,7 @@ typedef struct { - ol_handle *handle; + ol_handle* handle; ol_req req; ol_buf buf; char read_buffer[BUFSIZE]; @@ -103,4 +103,3 @@ int echo_start(int port) { return 0; } - diff --git a/test/test-ping-pong.c b/test/test-ping-pong.c new file mode 100644 index 00000000..cddfdec8 --- /dev/null +++ b/test/test-ping-pong.c @@ -0,0 +1,118 @@ +#include "../ol.h" +#include "echo.h" +#include +#include + +static int completed_pingers = 0; +static ol_req connect_req; + +/* 64 bytes is enough for a pinger */ +#define BUFSIZE 64 + +static char* PING = "PING\n"; + +typedef struct { + int pongs; + int state; + ol_handle* handle; + ol_req req; + ol_buf buf; + char read_buffer[BUFSIZE]; +} pinger; + + +void pinger_on_close(ol_handle* handle, ol_err err) { + assert(!err); + pinger* p = handle->data; + assert(1000 == p->pongs); + free(p); + ol_free(handle); + completed_pingers++; +} + + +void pinger_after_read(ol_req* req, size_t nread, ol_err err) { + int i, r; + + if (!err) { + return; + } + + if (nread == 0) { + ol_close(req->handle); + return; + } + + pinger *p = req->data; + + /* Now we count the pings */ + for (i = 0; i < nread; i++) { + assert(p->buf.base[i] == PING[p->state]); + /* 5 = strlen(PING) */ + p->state = (p->state + 1) % 5; + if (p->state == 0) { + p->pongs++; + if (p->pongs < 1000) { + r = ol_write2(p->handle, PING); + assert(!r); + } else { + ol_close(p->handle); + } + } + } +} + + +void pinger_try_read(pinger* pinger) { + pinger->buf.len = BUFSIZE; + pinger->req.cb = pinger_after_read; + ol_read(pinger->handle, &pinger->req, &pinger->buf, 1); +} + + +void pinger_on_connect(ol_handle* handle, ol_err err) { + int r; + + pinger *p = calloc(sizeof(pinger), 1); + p->handle = handle; + p->buf.base = p->read_buffer; + p->buf.len = BUFSIZE; + p->req.data = p; + + handle->data = p; + + pinger_try_read(p); + + r = ol_write2(handle, PING); + if (r < 0) { + /* error */ + assert(0); + } +} + + +int pinger_connect(int port) { + /* Try to connec to the server and do 1000 ping-pongs. */ + ol_handle* handle = ol_tcp_handle_new(pinger_on_close, NULL); + struct sockaddr_in addr = ol_ip4_addr("127.0.0.1", port); + return ol_connect(handle, &connect_req, (struct sockaddr*)&addr); +} + + +int main(int argc, char** argv) { + ol_init(); + + if (echo_start(8000)) { + return 1; + } + + if (pinger_connect(8000)) { + return 2; + } + + ol_run(); + + assert(completed_pingers == 1); + + return 0; +}