diff options
author | Timo Teras <timo.teras@iki.fi> | 2009-11-25 16:53:02 +0200 |
---|---|---|
committer | Timo Teras <timo.teras@iki.fi> | 2009-11-25 16:53:02 +0200 |
commit | 4db830052d941d9c6de281bc9a2f6ac212c59ad8 (patch) | |
tree | 3a9b5e7c78a812fc582f8844703069e5692a0c94 /test | |
parent | 2b19cc385163a43b1d559074a795a8aaab751185 (diff) | |
download | libtf-4db830052d941d9c6de281bc9a2f6ac212c59ad8.tar.bz2 libtf-4db830052d941d9c6de281bc9a2f6ac212c59ad8.tar.xz |
libtf: minor changes and new test case for network i/o
fixup the internals a bit.
Diffstat (limited to 'test')
-rw-r--r-- | test/TFbuild | 2 | ||||
-rw-r--r-- | test/httpget.c | 60 |
2 files changed, 61 insertions, 1 deletions
diff --git a/test/TFbuild b/test/TFbuild index 430e132..19384d2 100644 --- a/test/TFbuild +++ b/test/TFbuild @@ -1,3 +1,3 @@ -progs-$(TEST) += simple1 sleep read +progs-$(TEST) += simple1 sleep read httpget LIBS += $(objtree)src/libtf.a diff --git a/test/httpget.c b/test/httpget.c new file mode 100644 index 0000000..c1e37a3 --- /dev/null +++ b/test/httpget.c @@ -0,0 +1,60 @@ +#include <libtf/tf.h> + +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <arpa/inet.h> + +struct ctx { + char *hostname; +}; + +static void ping_fiber(void *ptr) +{ + struct ctx *ctx = (struct ctx*) ptr; + struct tf_sockaddr host; + struct tf_fd fd; + char buf[128]; + int bytes = 0, r = 0; + const char *req = "GET / HTTP/1.0\r\n\r\n"; + + printf("Lookup %s\n", ctx->hostname); + host.u.in.sin_family = AF_INET; + host.u.in.sin_addr.s_addr = inet_addr(ctx->hostname); + host.u.in.sin_port = htons(80); + + if (tf_socket(&fd, AF_INET, SOCK_STREAM, 0) < 0) + goto err; + + if ((r = tf_connect(&fd, &host, 10000)) < 0) + goto err_close; + + if ((r = tf_write_fully(&fd, req, strlen(req), 10000)) < 0) + goto err_close; + + while ((r = tf_read(&fd, buf, sizeof(buf), 10000)) > 0) + bytes += r; +err_close: + tf_close(&fd); +err: + printf("Host: %s, received %d bytes (r=%d, %s)\n", + ctx->hostname, bytes, -r, strerror(-r)); +} + +static void init_fiber(void *ptr) +{ + struct tf_main_ctx *ctx = (struct tf_main_ctx *) ptr; + struct ctx *c; + int i; + + for (i = 1; i < ctx->argc; i++) { + c = tf_fiber_create(ping_fiber, sizeof(struct ctx)); + c->hostname = ctx->argv[i]; + tf_fiber_put(c); + } +} + +int main(int argc, char **argv) +{ + return tf_main_args(init_fiber, argc, argv); +} |