1 /* $Id: mmrpc.c 3399 2007-04-11 19:34:43Z cherierm $ */
3 /* msg_handle - ensures the semantic of gras_msg_handle(i) for i<0,=0 or >0 */
5 /* Copyright (c) 2007 Martin Quinson. All rights reserved. */
6 /* Thanks to Flavien Vernier for reporting an issue around this */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
13 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Messages specific to this test");
15 int server (int argc,char *argv[]);
16 int client (int argc,char *argv[]);
18 static int server_cb_hello_handler(gras_msg_cb_ctx_t ctx,
20 INFO0("Got the message");
24 int server (int argc,char *argv[]) {
25 gras_socket_t me=NULL,pal=NULL;
34 gras_init(&argc,argv);
36 xbt_assert0(argc == 3,"Usage: server <myport> <client>");
40 gras_msgtype_declare("hello", NULL);
41 gras_cb_register("hello",&server_cb_hello_handler);
43 INFO1("Launch server (port=%d)", myport);
45 me = gras_socket_server(myport);
47 RETHROW0("Unable to establish a server socket: %s");
49 gras_os_sleep(1); /* Wait for pal to startup */
51 pal = gras_socket_client_from_string(palstr);
53 RETHROW1("Unable to establish a socket to %s: %s",palstr);
55 INFO0("Initialization done.");
58 /* Launch handle(0) when there is no message. Timeout expected */
63 if (e.category == timeout_error) {
67 RETHROW0("Didn't got the expected timeout: %s");
70 xbt_assert0(got_expected,"gras_msg_handle(0) do not lead to any timeout exception");
71 xbt_assert1(gras_os_time() - now < 0.01,
72 "gras_msg_handle(0) do not anwser immediately (%.4fsec)",
73 gras_os_time() - now);
74 INFO0("gras_msg_handle(0) works as expected (immediate timeout)");
75 /* Launch handle(0) when there is no message. Timeout expected */
80 if (e.category == timeout_error) {
84 RETHROW0("Didn't got the expected timeout: %s");
87 xbt_assert0(got_expected,"gras_msg_handle(1) do not lead to any timeout exception");
88 xbt_assert1(gras_os_time() - now < 1.5,
89 "gras_msg_handle(1) needs more than 1.5 sec to answer (%.4fsec)",
90 gras_os_time() - now);
91 xbt_assert1(gras_os_time() - now >= 1.0,
92 "gras_msg_handle(1) answers in less than one second (%.4fsec)",
93 gras_os_time() - now);
94 INFO0("gras_msg_handle(1) works as expected (delayed timeout)");
97 /* Send an hello to the client to unlock it */
99 gras_msg_send(pal, "hello", NULL);
101 /* Frees the allocated resources, and shut GRAS down */
102 gras_socket_close(me);
103 gras_socket_close(pal);
108 int client(int argc,char *argv[]) {
109 gras_socket_t me=NULL,pal=NULL;
117 gras_init(&argc,argv);
118 xbt_assert0(argc == 3,"Usage: client <myport> <server>");
119 myport=atoi(argv[1]);
122 gras_msgtype_declare("hello", NULL);
123 gras_cb_register("hello",&server_cb_hello_handler);
125 INFO1("Launch client (port=%d)", myport);
127 me = gras_socket_server(myport);
129 RETHROW0("Unable to establish a server socket: %s");
131 gras_os_sleep(1); /* Wait for pal to startup */
133 pal = gras_socket_client_from_string(palstr);
135 RETHROW1("Unable to establish a socket to %s: %s",palstr);
137 INFO0("Initialization done.");
139 /* Launch handle(-1). Lock until message from server expected */
144 RETHROW0("No exception expected during handle(-1), but got %s");
146 INFO0("gras_msg_handle(-1) works as expected (locked)");
148 /* Frees the allocated resources, and shut GRAS down */
149 gras_socket_close(me);
150 gras_socket_close(pal);