Logo AND Algorithmique Numérique Distribuée

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