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, void *payload_data)
20 INFO0("Got the message");
24 int server(int argc, char *argv[])
26 gras_socket_t me = NULL, pal = NULL;
35 gras_init(&argc, argv);
37 xbt_assert0(argc == 3, "Usage: server <myport> <client>");
38 myport = atoi(argv[1]);
41 gras_msgtype_declare("hello", NULL);
42 gras_cb_register("hello", &server_cb_hello_handler);
44 INFO1("Launch server (port=%d)", myport);
46 me = gras_socket_server(myport);
48 RETHROW0("Unable to establish a server socket: %s");
50 gras_os_sleep(1); /* Wait for pal to startup */
52 pal = gras_socket_client_from_string(palstr);
54 RETHROW1("Unable to establish a socket to %s: %s", palstr);
56 INFO0("Initialization done.");
59 /* Launch handle(0) when there is no message. Timeout expected */
64 if (e.category == timeout_error) {
68 RETHROW0("Didn't got the expected timeout: %s");
71 xbt_assert0(got_expected,
72 "gras_msg_handle(0) do not lead to any timeout exception");
73 xbt_assert1(gras_os_time() - now < 0.01,
74 "gras_msg_handle(0) do not anwser immediately (%.4fsec)",
75 gras_os_time() - now);
76 INFO0("gras_msg_handle(0) works as expected (immediate timeout)");
77 /* Launch handle(0) when there is no message. Timeout expected */
83 if (e.category == timeout_error) {
87 RETHROW0("Didn't got the expected timeout: %s");
90 xbt_assert0(got_expected,
91 "gras_msg_handle(1) do not lead to any timeout exception");
92 xbt_assert1(gras_os_time() - now < 1.5,
93 "gras_msg_handle(1) needs more than 1.5 sec to answer (%.4fsec)",
94 gras_os_time() - now);
95 xbt_assert1(gras_os_time() - now >= 1.0,
96 "gras_msg_handle(1) answers in less than one second (%.4fsec)",
97 gras_os_time() - now);
98 INFO0("gras_msg_handle(1) works as expected (delayed timeout)");
101 /* Send an hello to the client to unlock it */
103 gras_msg_send(pal, "hello", NULL);
105 /* Frees the allocated resources, and shut GRAS down */
106 gras_socket_close(me);
107 gras_socket_close(pal);
112 int client(int argc, char *argv[])
114 gras_socket_t me = NULL, pal = NULL;
122 gras_init(&argc, argv);
123 xbt_assert0(argc == 3, "Usage: client <myport> <server>");
124 myport = atoi(argv[1]);
127 gras_msgtype_declare("hello", NULL);
128 gras_cb_register("hello", &server_cb_hello_handler);
130 INFO1("Launch client (port=%d)", myport);
132 me = gras_socket_server(myport);
134 RETHROW0("Unable to establish a server socket: %s");
136 gras_os_sleep(1); /* Wait for pal to startup */
138 pal = gras_socket_client_from_string(palstr);
140 RETHROW1("Unable to establish a socket to %s: %s", palstr);
142 INFO0("Initialization done.");
144 /* Launch handle(-1). Lock until message from server expected */
149 RETHROW0("No exception expected during handle(-1), but got %s");
151 INFO0("gras_msg_handle(-1) works as expected (locked)");
153 /* Frees the allocated resources, and shut GRAS down */
154 gras_socket_close(me);
155 gras_socket_close(pal);