Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Generate files correctely with gras_stub_generator.
[simgrid.git] / teshsuite / gras / msg_handle / msg_handle.c
1 /* $Id: mmrpc.c 3399 2007-04-11 19:34:43Z cherierm $ */
2
3 /* msg_handle - ensures the semantic of gras_msg_handle(i) for i<0,=0 or >0 */
4
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             */
8
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. */
11
12 #include "gras.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Messages specific to this test");
15
16 int server(int argc, char *argv[]);
17 int client(int argc, char *argv[]);
18
19 static int server_cb_hello_handler(gras_msg_cb_ctx_t ctx, void *payload_data)
20 {
21   INFO0("Got the message");
22   return 0;
23 }
24
25 int server(int argc, char *argv[])
26 {
27   gras_socket_t me = NULL, pal = NULL;
28   int myport;
29   char *palstr;
30
31   xbt_ex_t e;
32   int got_expected;
33   double now;
34
35
36   gras_init(&argc, argv);
37
38   xbt_assert0(argc == 3, "Usage: server <myport> <client>");
39   myport = atoi(argv[1]);
40   palstr = argv[2];
41
42   gras_msgtype_declare("hello", NULL);
43   gras_cb_register("hello", &server_cb_hello_handler);
44
45   INFO1("Launch server (port=%d)", myport);
46   TRY {
47     me = gras_socket_server(myport);
48   } CATCH(e) {
49     RETHROW0("Unable to establish a server socket: %s");
50   }
51   gras_os_sleep(1);             /* Wait for pal to startup */
52   TRY {
53     pal = gras_socket_client_from_string(palstr);
54   } CATCH(e) {
55     RETHROW1("Unable to establish a socket to %s: %s", palstr);
56   }
57   INFO0("Initialization done.");
58   now = gras_os_time();
59
60   /* Launch handle(0) when there is no message. Timeout expected */
61   got_expected = 0;
62   TRY {
63     gras_msg_handle(0);
64   } CATCH(e) {
65     if (e.category == timeout_error) {
66       got_expected = 1;
67       xbt_ex_free(e);
68     } else {
69       RETHROW0("Didn't got the expected timeout: %s");
70     }
71   }
72   xbt_assert0(got_expected,
73               "gras_msg_handle(0) do not lead to any timeout exception");
74   xbt_assert1(gras_os_time() - now < 0.01,
75               "gras_msg_handle(0) do not anwser immediately (%.4fsec)",
76               gras_os_time() - now);
77   INFO0("gras_msg_handle(0) works as expected (immediate timeout)");
78   /* Launch handle(0) when there is no message. Timeout expected */
79   got_expected = 0;
80   TRY {
81     gras_msg_handle(1);
82   }
83   CATCH(e) {
84     if (e.category == timeout_error) {
85       got_expected = 1;
86       xbt_ex_free(e);
87     } else {
88       RETHROW0("Didn't got the expected timeout: %s");
89     }
90   }
91   xbt_assert0(got_expected,
92               "gras_msg_handle(1) do not lead to any timeout exception");
93   xbt_assert1(gras_os_time() - now < 1.5,
94               "gras_msg_handle(1) needs more than 1.5 sec to answer (%.4fsec)",
95               gras_os_time() - now);
96   xbt_assert1(gras_os_time() - now >= 1.0,
97               "gras_msg_handle(1) answers in less than one second (%.4fsec)",
98               gras_os_time() - now);
99   INFO0("gras_msg_handle(1) works as expected (delayed timeout)");
100   gras_os_sleep(3);
101
102   /* Send an hello to the client to unlock it */
103   INFO0("Unlock pal");
104   gras_msg_send(pal, "hello", NULL);
105
106   /* Frees the allocated resources, and shut GRAS down */
107   gras_socket_close(me);
108   gras_socket_close(pal);
109   gras_exit();
110   return 0;
111 }
112
113 int client(int argc, char *argv[])
114 {
115   gras_socket_t me = NULL, pal = NULL;
116   int myport;
117   char *palstr;
118
119   xbt_ex_t e;
120   int got_expected;
121
122
123   gras_init(&argc, argv);
124   xbt_assert0(argc == 3, "Usage: client <myport> <server>");
125   myport = atoi(argv[1]);
126   palstr = argv[2];
127
128   gras_msgtype_declare("hello", NULL);
129   gras_cb_register("hello", &server_cb_hello_handler);
130
131   INFO1("Launch client (port=%d)", myport);
132   TRY {
133     me = gras_socket_server(myport);
134   } CATCH(e) {
135     RETHROW0("Unable to establish a server socket: %s");
136   }
137   gras_os_sleep(1);             /* Wait for pal to startup */
138   TRY {
139     pal = gras_socket_client_from_string(palstr);
140   } CATCH(e) {
141     RETHROW1("Unable to establish a socket to %s: %s", palstr);
142   }
143   INFO0("Initialization done.");
144
145   /* Launch handle(-1). Lock until message from server expected */
146   got_expected = 0;
147   TRY {
148     gras_msg_handle(-1);
149   } CATCH(e) {
150     RETHROW0("No exception expected during handle(-1), but got %s");
151   }
152   INFO0("gras_msg_handle(-1) works as expected (locked)");
153
154   /* Frees the allocated resources, and shut GRAS down */
155   gras_socket_close(me);
156   gras_socket_close(pal);
157   gras_exit();
158   return 0;
159 }