Permalink
Browse files

Add ping-pong test - not yet working on unix

  • Loading branch information...
1 parent 5304a18 commit 42d96dc3ca3d28d3a76f54fb11da49b64cd8660b @ry ry committed Mar 31, 2011
Showing with 126 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +5 −0 Makefile
  3. +1 −1 ol-unix.c
  4. +1 −2 test/echo.c
  5. +118 −0 test/test-ping-pong.c
View
@@ -23,3 +23,4 @@ ev/autom4te.cache
/Release/
test/echo-demo
+test/test-ping-pong
View
@@ -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
View
@@ -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);
View
@@ -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;
}
-
View
@@ -0,0 +1,118 @@
+#include "../ol.h"
+#include "echo.h"
+#include <assert.h>
+#include <stdlib.h>
+
+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;
+}

0 comments on commit 42d96dc

Please sign in to comment.