Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
158fd33a6d75315cbc2dcfef5d9c1fd1042538ef
[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   }
50   CATCH_ANONYMOUS {
51     RETHROWF("Unable to establish a server socket: %s");
52   }
53   gras_os_sleep(1);             /* Wait for pal to startup */
54   TRY {
55     pal = gras_socket_client_from_string(palstr);
56   }
57   CATCH_ANONYMOUS {
58     RETHROWF("Unable to establish a socket to %s: %s", palstr);
59   }
60   XBT_INFO("Initialization done.");
61   now = gras_os_time();
62
63   /* Launch handle(0) when there is no message. Timeout expected */
64   got_expected = 0;
65   TRY {
66     gras_msg_handle(0);
67   } CATCH(e) {
68     if (e.category == timeout_error) {
69       got_expected = 1;
70       xbt_ex_free(e);
71     } else {
72       RETHROWF("Didn't got the expected timeout: %s");
73     }
74   }
75   xbt_assert(got_expected,
76               "gras_msg_handle(0) do not lead to any timeout exception");
77   xbt_assert(gras_os_time() - now < 0.01,
78               "gras_msg_handle(0) do not anwser immediately (%.4fsec)",
79               gras_os_time() - now);
80   XBT_INFO("gras_msg_handle(0) works as expected (immediate timeout)");
81   /* Launch handle(0) when there is no message. Timeout expected */
82   got_expected = 0;
83   TRY {
84     gras_msg_handle(1);
85   }
86   CATCH(e) {
87     if (e.category == timeout_error) {
88       got_expected = 1;
89       xbt_ex_free(e);
90     } else {
91       RETHROWF("Didn't got the expected timeout: %s");
92     }
93   }
94   xbt_assert(got_expected,
95               "gras_msg_handle(1) do not lead to any timeout exception");
96   xbt_assert(gras_os_time() - now < 1.5,
97               "gras_msg_handle(1) needs more than 1.5 sec to answer (%.4fsec)",
98               gras_os_time() - now);
99   xbt_assert(gras_os_time() - now >= 1.0,
100               "gras_msg_handle(1) answers in less than one second (%.4fsec)",
101               gras_os_time() - now);
102   XBT_INFO("gras_msg_handle(1) works as expected (delayed timeout)");
103   gras_os_sleep(3);
104
105   /* Send an hello to the client to unlock it */
106   XBT_INFO("Unlock pal");
107   gras_msg_send(pal, "hello", NULL);
108
109   /* Frees the allocated resources, and shut GRAS down */
110   gras_socket_close(me);
111   gras_socket_close(pal);
112   gras_exit();
113   return 0;
114 }
115
116 int client(int argc, char *argv[])
117 {
118   gras_socket_t me = NULL, pal = NULL;
119   int myport;
120   char *palstr;
121
122   gras_init(&argc, argv);
123   xbt_assert(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   XBT_INFO("Launch client (port=%d)", myport);
131   TRY {
132     me = gras_socket_server(myport);
133   }
134   CATCH_ANONYMOUS {
135     RETHROWF("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   }
141   CATCH_ANONYMOUS {
142     RETHROWF("Unable to establish a socket to %s: %s", palstr);
143   }
144   XBT_INFO("Initialization done.");
145
146   /* Launch handle(-1). Lock until message from server expected */
147   TRY {
148     gras_msg_handle(-1);
149   }
150   CATCH_ANONYMOUS {
151     RETHROWF("No exception expected during handle(-1), but got %s");
152   }
153   XBT_INFO("gras_msg_handle(-1) works as expected (locked)");
154
155   /* Frees the allocated resources, and shut GRAS down */
156   gras_socket_close(me);
157   gras_socket_close(pal);
158   gras_exit();
159   return 0;
160 }