Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Second commit for cmake in an other directory.
[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 Martin Quinson. All rights reserved.                  */
6 /* Thanks to Flavien Vernier for reporting an issue around this             */
7
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. */
10
11 #include "gras.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Messages specific to this test");
14
15 int server(int argc, char *argv[]);
16 int client(int argc, char *argv[]);
17
18 static int server_cb_hello_handler(gras_msg_cb_ctx_t ctx, void *payload_data)
19 {
20   INFO0("Got the message");
21   return 0;
22 }
23
24 int server(int argc, char *argv[])
25 {
26   gras_socket_t me = NULL, pal = NULL;
27   int myport;
28   char *palstr;
29
30   xbt_ex_t e;
31   int got_expected;
32   double now;
33
34
35   gras_init(&argc, argv);
36
37   xbt_assert0(argc == 3, "Usage: server <myport> <client>");
38   myport = atoi(argv[1]);
39   palstr = argv[2];
40
41   gras_msgtype_declare("hello", NULL);
42   gras_cb_register("hello", &server_cb_hello_handler);
43
44   INFO1("Launch server (port=%d)", myport);
45   TRY {
46     me = gras_socket_server(myport);
47   } CATCH(e) {
48     RETHROW0("Unable to establish a server socket: %s");
49   }
50   gras_os_sleep(1);             /* Wait for pal to startup */
51   TRY {
52     pal = gras_socket_client_from_string(palstr);
53   } CATCH(e) {
54     RETHROW1("Unable to establish a socket to %s: %s", palstr);
55   }
56   INFO0("Initialization done.");
57   now = gras_os_time();
58
59   /* Launch handle(0) when there is no message. Timeout expected */
60   got_expected = 0;
61   TRY {
62     gras_msg_handle(0);
63   } CATCH(e) {
64     if (e.category == timeout_error) {
65       got_expected = 1;
66       xbt_ex_free(e);
67     } else {
68       RETHROW0("Didn't got the expected timeout: %s");
69     }
70   }
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 */
78   got_expected = 0;
79   TRY {
80     gras_msg_handle(1);
81   }
82   CATCH(e) {
83     if (e.category == timeout_error) {
84       got_expected = 1;
85       xbt_ex_free(e);
86     } else {
87       RETHROW0("Didn't got the expected timeout: %s");
88     }
89   }
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)");
99   gras_os_sleep(3);
100
101   /* Send an hello to the client to unlock it */
102   INFO0("Unlock pal");
103   gras_msg_send(pal, "hello", NULL);
104
105   /* Frees the allocated resources, and shut GRAS down */
106   gras_socket_close(me);
107   gras_socket_close(pal);
108   gras_exit();
109   return 0;
110 }
111
112 int client(int argc, char *argv[])
113 {
114   gras_socket_t me = NULL, pal = NULL;
115   int myport;
116   char *palstr;
117
118   xbt_ex_t e;
119   int got_expected;
120
121
122   gras_init(&argc, argv);
123   xbt_assert0(argc == 3, "Usage: client <myport> <server>");
124   myport = atoi(argv[1]);
125   palstr = argv[2];
126
127   gras_msgtype_declare("hello", NULL);
128   gras_cb_register("hello", &server_cb_hello_handler);
129
130   INFO1("Launch client (port=%d)", myport);
131   TRY {
132     me = gras_socket_server(myport);
133   } CATCH(e) {
134     RETHROW0("Unable to establish a server socket: %s");
135   }
136   gras_os_sleep(1);             /* Wait for pal to startup */
137   TRY {
138     pal = gras_socket_client_from_string(palstr);
139   } CATCH(e) {
140     RETHROW1("Unable to establish a socket to %s: %s", palstr);
141   }
142   INFO0("Initialization done.");
143
144   /* Launch handle(-1). Lock until message from server expected */
145   got_expected = 0;
146   TRY {
147     gras_msg_handle(-1);
148   } CATCH(e) {
149     RETHROW0("No exception expected during handle(-1), but got %s");
150   }
151   INFO0("gras_msg_handle(-1) works as expected (locked)");
152
153   /* Frees the allocated resources, and shut GRAS down */
154   gras_socket_close(me);
155   gras_socket_close(pal);
156   gras_exit();
157   return 0;
158 }