From 422c6405c61701e222e3b7d6988b9c20e7c45e6c Mon Sep 17 00:00:00 2001 From: Marcel Poul Date: Mon, 28 May 2012 22:57:34 +0000 Subject: [PATCH] timeout as a param. in examples (default timeout still used) --- emi.canl.canl-c/examples/canl_sample_client.c | 22 +++++++++++++++------- emi.canl.canl-c/examples/canl_sample_server.c | 24 ++++++++++++++++-------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/emi.canl.canl-c/examples/canl_sample_client.c b/emi.canl.canl-c/examples/canl_sample_client.c index 7b14581..cd36d29 100644 --- a/emi.canl.canl-c/examples/canl_sample_client.c +++ b/emi.canl.canl-c/examples/canl_sample_client.c @@ -7,6 +7,8 @@ #include #define BUF_LEN 1000 +#define DEF_PORT 4321 +#define DEF_TIMEOUT 150 int main(int argc, char *argv[]) { @@ -17,18 +19,22 @@ int main(int argc, char *argv[]) int buf_len = 0; char *p_server = NULL; char *def_server = "www.linuxfoundation.org"; - int opt, port = 80; + int opt, port = DEF_PORT; struct timeval timeout; char *serv_cert = NULL; char *serv_key = NULL; char *proxy_cert = NULL; + timeout.tv_sec = DEF_TIMEOUT; + timeout.tv_usec = 0; + while ((opt = getopt(argc, argv, "hp:s:c:k:")) != -1) { switch (opt) { case 'h': fprintf(stderr, "Usage: %s [-p port] [-c certificate]" " [-k private key] [-d ca_dir] [-h] " - " [-s server] [-x proxy certificate] \n", argv[0]); + " [-s server] [-x proxy certificate] " + " [-t timeout] \n", argv[0]); exit(0); case 'p': port = atoi(optarg); @@ -45,10 +51,14 @@ int main(int argc, char *argv[]) case 'x': proxy_cert = optarg; break; + case 't': + timeout.tv_sec = atoi(optarg); + break; default: /* '?' */ fprintf(stderr, "Usage: %s [-p port] [-c certificate]" - " [-k private key] [-d ca_dir] [-h] " - " [-s server] [-x proxy certificate] \n", argv[0]); + " [-k private key] [-d ca_dir] [-h]" + " [-s server] [-x proxy certificate]" + " [-t timeout] \n", argv[0]); exit(-1); } } @@ -79,9 +89,6 @@ int main(int argc, char *argv[]) } } - timeout.tv_sec = 150; - timeout.tv_usec = 0; - err = canl_io_connect(my_ctx, my_io_h, p_server, NULL, port, NULL, 0, &timeout); if (err) { printf("[CLIENT] connection to %s cannot be established: %s\n", @@ -106,6 +113,7 @@ int main(int argc, char *argv[]) buf[err] = '\0'; printf("[CLIENT] message \"%s\" sent successfully\n", buf); } + buf[0] = '\0'; err = canl_io_read (my_ctx, my_io_h, buf, sizeof(buf)-1, &timeout); if (err > 0) { diff --git a/emi.canl.canl-c/examples/canl_sample_server.c b/emi.canl.canl-c/examples/canl_sample_server.c index f1558be..feea7ba 100644 --- a/emi.canl.canl-c/examples/canl_sample_server.c +++ b/emi.canl.canl-c/examples/canl_sample_server.c @@ -10,13 +10,15 @@ #define BUF_LEN 1000 #define BACKLOG 10 +#define DEF_PORT 4321 +#define DEF_TIMEOUT 150 int main(int argc, char *argv[]) { canl_ctx my_ctx; canl_io_handler my_io_h = NULL; int err = 0; - int opt, port = 4321; + int opt, port = DEF_PORT; char *serv_cert = NULL; char *serv_key = NULL; char *ca_dir = NULL; @@ -25,12 +27,17 @@ int main(int argc, char *argv[]) struct timeval timeout; canl_principal princ = NULL; char *name = NULL; + + timeout.tv_sec = DEF_TIMEOUT; + timeout.tv_usec = 0; + - while ((opt = getopt(argc, argv, "hp:c:k:d:")) != -1) { + while ((opt = getopt(argc, argv, "hp:c:k:d:t:")) != -1) { switch (opt) { case 'h': fprintf(stderr, "Usage: %s [-p port] [-c certificate]" - " [-k private key] [-d ca_dir] [-h] \n", argv[0]); + " [-k private key] [-d ca_dir] [-h] " + "[-t timeout] \n", argv[0]); exit(0); case 'p': port = atoi(optarg); @@ -44,9 +51,13 @@ int main(int argc, char *argv[]) case 'd': ca_dir = optarg; break; + case 't': + timeout.tv_sec = atoi(optarg); + break; default: /* '?' */ fprintf(stderr, "Usage: %s [-p port] [-c certificate]" - " [-k private key] [-d ca_dir] [-h] \n", argv[0]); + " [-k private key] [-d ca_dir] [-h] " + "[-t timeout] \n", argv[0]); exit(-1); } } @@ -138,9 +149,6 @@ int main(int argc, char *argv[]) printf("Failed to accept network connection: %s", strerror(errno)); } - timeout.tv_sec = 150; - timeout.tv_usec = 0; - /* canl_create_io_handler has to be called for my_io_h*/ /* TODO timeout in this function? and select around it*/ err = canl_io_accept(my_ctx, my_io_h, new_fd, s_addr, 0, &princ, &timeout); @@ -170,7 +178,7 @@ int main(int argc, char *argv[]) printf("[SERVER] message \"%s\" sent successfully\n", buf); } - err = canl_io_read (my_ctx, my_io_h, buf, sizeof(buf)-1, NULL); + err = canl_io_read (my_ctx, my_io_h, buf, sizeof(buf)-1, &timeout); if (err <= 0) { printf("[SERVER] Failed to receive reply from client: %s\n", canl_get_error_message(my_ctx)); -- 1.8.2.3