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, 2008, 2009, 2010. The SimGrid Team.
6 * All rights reserved. */
7 /* Thanks to Flavien Vernier for reporting an issue around this */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Messages specific to this test");
16 int server(int argc, char *argv[]);
17 int client(int argc, char *argv[]);
19 static int server_cb_hello_handler(gras_msg_cb_ctx_t ctx,
22 XBT_INFO("Got the message");
26 int server(int argc, char *argv[])
28 gras_socket_t me = NULL, pal = NULL;
37 gras_init(&argc, argv);
39 xbt_assert(argc == 3, "Usage: server <myport> <client>");
40 myport = atoi(argv[1]);
43 gras_msgtype_declare("hello", NULL);
44 gras_cb_register("hello", &server_cb_hello_handler);
46 XBT_INFO("Launch server (port=%d)", myport);
48 me = gras_socket_server(myport);
51 RETHROWF("Unable to establish a server socket: %s");
53 gras_os_sleep(1); /* Wait for pal to startup */
55 pal = gras_socket_client_from_string(palstr);
58 RETHROWF("Unable to establish a socket to %s: %s", palstr);
60 XBT_INFO("Initialization done.");
63 /* Launch handle(0) when there is no message. Timeout expected */
69 if (e.category == timeout_error) {
73 RETHROWF("Didn't got the expected timeout: %s");
76 xbt_assert(got_expected,
77 "gras_msg_handle(0) do not lead to any timeout exception");
78 xbt_assert(gras_os_time() - now < 0.01,
79 "gras_msg_handle(0) do not anwser immediately (%.4fsec)",
80 gras_os_time() - now);
81 XBT_INFO("gras_msg_handle(0) works as expected (immediate timeout)");
82 /* Launch handle(0) when there is no message. Timeout expected */
88 if (e.category == timeout_error) {
92 RETHROWF("Didn't got the expected timeout: %s");
95 xbt_assert(got_expected,
96 "gras_msg_handle(1) do not lead to any timeout exception");
97 xbt_assert(gras_os_time() - now < 1.5,
98 "gras_msg_handle(1) needs more than 1.5 sec to answer (%.4fsec)",
99 gras_os_time() - now);
100 xbt_assert(gras_os_time() - now >= 1.0,
101 "gras_msg_handle(1) answers in less than one second (%.4fsec)",
102 gras_os_time() - now);
103 XBT_INFO("gras_msg_handle(1) works as expected (delayed timeout)");
106 /* Send an hello to the client to unlock it */
107 XBT_INFO("Unlock pal");
108 gras_msg_send(pal, "hello", NULL);
110 /* Frees the allocated resources, and shut GRAS down */
111 gras_socket_close(me);
112 gras_socket_close(pal);
117 int client(int argc, char *argv[])
119 gras_socket_t me = NULL, pal = NULL;
123 gras_init(&argc, argv);
124 xbt_assert(argc == 3, "Usage: client <myport> <server>");
125 myport = atoi(argv[1]);
128 gras_msgtype_declare("hello", NULL);
129 gras_cb_register("hello", &server_cb_hello_handler);
131 XBT_INFO("Launch client (port=%d)", myport);
133 me = gras_socket_server(myport);
136 RETHROWF("Unable to establish a server socket: %s");
138 gras_os_sleep(1); /* Wait for pal to startup */
140 pal = gras_socket_client_from_string(palstr);
143 RETHROWF("Unable to establish a socket to %s: %s", palstr);
145 XBT_INFO("Initialization done.");
147 /* Launch handle(-1). Lock until message from server expected */
152 RETHROWF("No exception expected during handle(-1), but got %s");
154 XBT_INFO("gras_msg_handle(-1) works as expected (locked)");
156 /* Frees the allocated resources, and shut GRAS down */
157 gras_socket_close(me);
158 gras_socket_close(pal);