1
/* msg_handle - ensures the semantic of gras_msg_handle(i) for i<0,=0 or >0 */
3
/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4
* All rights reserved. */
5
/* Thanks to Flavien Vernier for reporting an issue around this */
7
/* This program is free software; you can redistribute it and/or modify it
8
* under the terms of the license (GNU LGPL) which comes with this package. */
12
XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Messages specific to this test");
14
int server(int argc, char *argv[]);
15
int client(int argc, char *argv[]);
17
static int server_cb_hello_handler(gras_msg_cb_ctx_t ctx,
20
XBT_INFO("Got the message");
24
int server(int argc, char *argv[])
26
volatile xbt_socket_t me = NULL, pal = NULL;
31
_XBT_GNUC_UNUSED int got_expected;
32
_XBT_GNUC_UNUSED double now;
35
gras_init(&argc, argv);
37
xbt_assert(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
XBT_INFO("Launch server (port=%d)", myport);
46
me = gras_socket_server(myport);
49
RETHROWF("Unable to establish a server socket: %s");
51
gras_os_sleep(1); /* Wait for pal to startup */
53
pal = gras_socket_client_from_string(palstr);
56
RETHROWF("Unable to establish a socket to %s: %s", palstr);
58
XBT_INFO("Initialization done.");
61
/* Launch handle(0) when there is no message. Timeout expected */
67
if (e.category == timeout_error) {
71
RETHROWF("Didn't got the expected timeout: %s");
74
xbt_assert(got_expected,
75
"gras_msg_handle(0) do not lead to any timeout exception");
76
xbt_assert(gras_os_time() - now < 0.01,
77
"gras_msg_handle(0) do not anwser immediately (%.4fsec)",
78
gras_os_time() - now);
79
XBT_INFO("gras_msg_handle(0) works as expected (immediate timeout)");
80
/* Launch handle(0) when there is no message. Timeout expected */
86
if (e.category == timeout_error) {
90
RETHROWF("Didn't got the expected timeout: %s");
93
xbt_assert(got_expected,
94
"gras_msg_handle(1) do not lead to any timeout exception");
95
xbt_assert(gras_os_time() - now < 1.5,
96
"gras_msg_handle(1) needs more than 1.5 sec to answer (%.4fsec)",
97
gras_os_time() - now);
98
xbt_assert(gras_os_time() - now >= 1.0,
99
"gras_msg_handle(1) answers in less than one second (%.4fsec)",
100
gras_os_time() - now);
101
XBT_INFO("gras_msg_handle(1) works as expected (delayed timeout)");
104
/* Send an hello to the client to unlock it */
105
XBT_INFO("Unlock pal");
106
gras_msg_send(pal, "hello", NULL);
108
/* Frees the allocated resources, and shut GRAS down */
109
gras_socket_close(me);
110
gras_socket_close(pal);
115
int client(int argc, char *argv[])
117
volatile xbt_socket_t me = NULL, pal = NULL;
121
gras_init(&argc, argv);
122
xbt_assert(argc == 3, "Usage: client <myport> <server>");
123
myport = atoi(argv[1]);
126
gras_msgtype_declare("hello", NULL);
127
gras_cb_register("hello", &server_cb_hello_handler);
129
XBT_INFO("Launch client (port=%d)", myport);
131
me = gras_socket_server(myport);
134
RETHROWF("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);
141
RETHROWF("Unable to establish a socket to %s: %s", palstr);
143
XBT_INFO("Initialization done.");
145
/* Launch handle(-1). Lock until message from server expected */
150
RETHROWF("No exception expected during handle(-1), but got %s");
152
XBT_INFO("gras_msg_handle(-1) works as expected (locked)");
154
/* Frees the allocated resources, and shut GRAS down */
155
gras_socket_close(me);
156
gras_socket_close(pal);