Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use new macros THROWF and RETHROWF.
[simgrid.git] / examples / gras / mmrpc / mmrpc.c
1 /* GridRPC - Fake Grid RPC thingy doing matrix multiplications (as expected)*/
2
3 /* Copyright (c) 2005, 2006, 2007, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #define GRAS_DEFINE_TYPE_EXTERN
10 #include "xbt/matrix.h"
11 #include "mmrpc.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(MatMult, "Messages specific to this example");
14
15 /* register messages which may be sent and their payload
16    (common to client and server) */
17 void mmrpc_register_messages(void)
18 {
19   gras_datadesc_type_t matrix_type, request_type;
20
21   matrix_type = gras_datadesc_matrix(gras_datadesc_by_name("double"),
22                                      NULL);
23   request_type =
24       gras_datadesc_array_fixed("s_matrix_t(double)[2]", matrix_type, 2);
25
26   gras_msgtype_declare("answer", matrix_type);
27   gras_msgtype_declare("request", request_type);
28 }
29
30 typedef xbt_matrix_t request_t[2];
31 static int server_cb_request_handler(gras_msg_cb_ctx_t ctx,
32                                      void *payload_data)
33 {
34
35   gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
36
37   /* 1. Get the payload into the data variable */
38   xbt_matrix_t *request = (xbt_matrix_t *) payload_data;
39   xbt_matrix_t result;
40
41   /* 2. Do the computation */
42   result = xbt_matrix_double_new_mult(request[0], request[1]);
43
44   /* 3. Send it back as payload of a pong message to the expeditor */
45   gras_msg_send(expeditor, "answer", &result);
46
47   /* 4. Cleanups */
48   xbt_matrix_free(request[0]);
49   xbt_matrix_free(request[1]);
50   xbt_matrix_free(result);
51   gras_socket_close(expeditor);
52
53   return 0;
54 }                               /* end_of_server_cb_request_handler */
55
56 int server(int argc, char *argv[])
57 {
58   xbt_ex_t e;
59   gras_socket_t sock = NULL;
60   int port = 4000;
61
62   /* 1. Init the GRAS infrastructure */
63   gras_init(&argc, argv);
64
65   /* 2. Get the port I should listen on from the command line, if specified */
66   if (argc == 2) {
67     port = atoi(argv[1]);
68   }
69
70   /* 3. Create my master socket */
71   XBT_INFO("Launch server (port=%d)", port);
72   TRY {
73     sock = gras_socket_server(port);
74   }
75   CATCH(e) {
76     RETHROWF("Unable to establish a server socket: %s");
77   }
78
79   /* 4. Register the known messages and payloads. */
80   mmrpc_register_messages();
81
82   /* 5. Register my callback */
83   gras_cb_register("request", &server_cb_request_handler);
84
85   /* 6. Wait up to 10 minutes for an incomming message to handle */
86   gras_msg_handle(600.0);
87
88   /* 7. Free the allocated resources, and shut GRAS down */
89   gras_socket_close(sock);
90   gras_exit();
91
92   XBT_INFO("Done.");
93   return 0;
94 }                               /* end_of_server */
95
96
97 int client(int argc, char *argv[])
98 {
99   xbt_ex_t e;
100   gras_socket_t toserver = NULL;        /* peer */
101
102   gras_socket_t from;
103   xbt_matrix_t request[2], answer;
104
105   int i, j;
106
107   const char *host = "127.0.0.1";
108   int port = 4000;
109
110   /* 1. Init the GRAS's infrastructure */
111   gras_init(&argc, argv);
112
113   /* 2. Get the server's address. The command line override defaults when specified */
114   if (argc == 3) {
115     host = argv[1];
116     port = atoi(argv[2]);
117   }
118
119   XBT_INFO("Launch client (server on %s:%d)", host, port);
120
121   /* 3. Wait for the server startup */
122   gras_os_sleep(1);
123
124   /* 4. Create a socket to speak to the server */
125   TRY {
126     toserver = gras_socket_client(host, port);
127   }
128   CATCH(e) {
129     RETHROWF("Unable to connect to the server: %s");
130   }
131   XBT_INFO("Connected to %s:%d.", host, port);
132
133
134   /* 5. Register the messages (before use) */
135   mmrpc_register_messages();
136
137   /* 6. Keep the user informed of what's going on */
138   XBT_INFO(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
139         gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
140
141   /* 7. Prepare and send the request to the server */
142
143   request[0] = xbt_matrix_double_new_id(MATSIZE, MATSIZE);
144   request[1] = xbt_matrix_double_new_rand(MATSIZE, MATSIZE);
145
146   /*
147      xbt_matrix_dump(request[0],"C:sent0",0,xbt_matrix_dump_display_double);
148      xbt_matrix_dump(request[1],"C:sent1",0,xbt_matrix_dump_display_double);
149    */
150
151   gras_msg_send(toserver, "request", &request);
152
153   xbt_matrix_free(request[0]);
154
155   XBT_INFO(">>>>>>>> Request sent to %s:%d <<<<<<<<",
156         gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
157
158   /* 8. Wait for the answer from the server, and deal with issues */
159   gras_msg_wait(6000, "answer", &from, &answer);
160
161   /*
162      xbt_matrix_dump(answer,"C:answer",0,xbt_matrix_dump_display_double);
163    */
164   for (i = 0; i < MATSIZE; i++)
165     for (j = 0; i < MATSIZE; i++)
166       xbt_assert4(xbt_matrix_get_as(answer, i, j, double) ==
167                   xbt_matrix_get_as(request[1], i, j, double),
168                   "Answer does not match expectations. Found %f at cell %d,%d instead of %f",
169                   xbt_matrix_get_as(answer, i, j, double), i, j,
170                   xbt_matrix_get_as(request[1], i, j, double));
171
172   /* 9. Keep the user informed of what's going on, again */
173   XBT_INFO(">>>>>>>> Got answer from %s:%d (values are right) <<<<<<<<",
174         gras_socket_peer_name(from), gras_socket_peer_port(from));
175
176   /* 10. Free the allocated resources, and shut GRAS down */
177   xbt_matrix_free(request[1]);
178   xbt_matrix_free(answer);
179   gras_socket_close(toserver);
180   gras_exit();
181   XBT_INFO("Done.");
182   return 0;
183 }                               /* end_of_client */