libuv/test/test-ping-pong.c

177 lines
4.0 KiB
C
Raw Normal View History

/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
2011-04-07 09:02:54 +00:00
#include "../oio.h"
2011-04-19 02:26:32 +00:00
#include "task.h"
#include <stdlib.h>
2011-04-04 23:43:17 +00:00
#include <stdio.h>
2011-04-26 00:26:07 +00:00
#include <string.h> /* strlen */
static int completed_pingers = 0;
2011-04-07 02:51:59 +00:00
#define NUM_PINGS 1000
2011-04-04 23:43:17 +00:00
/* 64 bytes is enough for a pinger */
2011-04-07 02:51:59 +00:00
#define BUFSIZE 10240
static char PING[] = "PING\n";
typedef struct {
int pongs;
int state;
2011-04-07 09:02:54 +00:00
oio_handle handle;
oio_req connect_req;
oio_req read_req;
char read_buffer[BUFSIZE];
2011-04-07 02:51:59 +00:00
} pinger_t;
void pinger_try_read(pinger_t* pinger);
void pinger_on_close(oio_handle* handle, int status) {
2011-04-07 02:51:59 +00:00
pinger_t* pinger = (pinger_t*)handle->data;
2011-03-31 22:09:06 +00:00
ASSERT(status == 0);
ASSERT(NUM_PINGS == pinger->pongs);
2011-04-07 02:51:59 +00:00
free(pinger);
completed_pingers++;
}
2011-04-07 02:51:59 +00:00
void pinger_after_write(oio_req *req, int status) {
ASSERT(status == 0);
2011-04-07 02:51:59 +00:00
free(req);
}
2011-04-16 21:04:45 +00:00
static void pinger_write_ping(pinger_t* pinger) {
2011-04-07 09:02:54 +00:00
oio_req *req;
2011-04-19 15:01:45 +00:00
oio_buf buf;
buf.base = (char*)&PING;
buf.len = strlen(PING);
2011-04-15 17:32:28 +00:00
2011-04-07 09:02:54 +00:00
req = (oio_req*)malloc(sizeof(*req));
oio_req_init(req, &pinger->handle, pinger_after_write);
2011-04-19 15:01:45 +00:00
if (oio_write(req, &buf, 1)) {
FATAL("oio_write failed");
2011-04-16 21:04:45 +00:00
}
puts("PING");
2011-04-07 02:51:59 +00:00
}
2011-04-16 21:04:45 +00:00
2011-05-03 00:35:11 +00:00
static void pinger_read_cb(oio_handle* handle, int nread, oio_buf buf) {
2011-03-31 22:09:06 +00:00
unsigned int i;
2011-04-07 02:51:59 +00:00
pinger_t* pinger;
2011-05-03 00:35:11 +00:00
pinger = (pinger_t*)handle->data;
2011-05-03 00:35:11 +00:00
if (nread < 0) {
ASSERT(oio_last_error().code == OIO_EOF);
2011-04-16 21:04:45 +00:00
puts("got EOF");
2011-05-03 00:35:11 +00:00
if (buf.base) {
free(buf.base);
}
2011-04-07 09:02:54 +00:00
oio_close(&pinger->handle);
2011-05-03 00:35:11 +00:00
return;
}
/* Now we count the pings */
for (i = 0; i < nread; i++) {
2011-05-03 00:55:51 +00:00
ASSERT(buf.base[i] == PING[pinger->state]);
2011-04-07 02:51:59 +00:00
pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
if (pinger->state == 0) {
2011-04-16 21:04:45 +00:00
printf("PONG %d\n", pinger->pongs);
2011-04-07 02:51:59 +00:00
pinger->pongs++;
if (pinger->pongs < NUM_PINGS) {
pinger_write_ping(pinger);
} else {
2011-04-07 09:02:54 +00:00
oio_close(&pinger->handle);
2011-03-31 22:09:06 +00:00
return;
}
}
}
}
void pinger_on_connect(oio_req *req, int status) {
2011-04-07 02:51:59 +00:00
pinger_t *pinger = (pinger_t*)req->handle->data;
ASSERT(status == 0);
2011-04-04 23:43:17 +00:00
2011-04-07 02:51:59 +00:00
pinger_write_ping(pinger);
2011-05-03 00:35:11 +00:00
oio_read_start(req->handle, pinger_read_cb);
2011-04-07 02:51:59 +00:00
}
2011-04-16 21:04:45 +00:00
void pinger_new() {
int r;
2011-04-13 23:35:39 +00:00
struct sockaddr_in server_addr = oio_ip4_addr("127.0.0.1", TEST_PORT);
2011-04-07 02:51:59 +00:00
pinger_t *pinger;
2011-04-07 02:51:59 +00:00
pinger = (pinger_t*)malloc(sizeof(*pinger));
pinger->state = 0;
pinger->pongs = 0;
2011-04-04 23:43:17 +00:00
/* Try to connec to the server and do NUM_PINGS ping-pongs. */
2011-04-18 20:01:50 +00:00
r = oio_tcp_init(&pinger->handle, pinger_on_close, (void*)pinger);
ASSERT(!r);
2011-04-07 02:51:59 +00:00
/* We are never doing multiple reads/connects at a time anyway. */
/* so these handles can be pre-initialized. */
2011-04-07 09:02:54 +00:00
oio_req_init(&pinger->connect_req, &pinger->handle, pinger_on_connect);
2011-03-31 22:09:06 +00:00
2011-04-16 21:04:45 +00:00
r = oio_connect(&pinger->connect_req, (struct sockaddr*)&server_addr);
ASSERT(!r);
}
2011-05-03 00:35:11 +00:00
static oio_buf alloc_cb(oio_handle* handle, size_t size) {
oio_buf buf;
buf.base = (char*)malloc(size);
buf.len = size;
return buf;
}
2011-04-04 23:43:17 +00:00
TEST_IMPL(ping_pong) {
2011-05-03 00:35:11 +00:00
oio_init(alloc_cb);
2011-04-16 21:04:45 +00:00
pinger_new();
2011-04-07 09:02:54 +00:00
oio_run();
ASSERT(completed_pingers == 1);
return 0;
}