Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix warnings about clobbered variables in gras/mmrpc example.
[simgrid.git] / examples / gras / mmrpc / mmrpc_server.c
1 /* GridRPC - Fake Grid RPC thingy doing matrix multiplications (as expected)*/
2
3 /* Copyright (c) 2006, 2007, 2009, 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_EXTERNAL_DEFAULT_CATEGORY(MatMult);
14
15
16 typedef xbt_matrix_t request_t[2];
17 static int server_cb_request_handler(gras_msg_cb_ctx_t ctx,
18                                      void *payload_data)
19 {
20
21   gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
22
23   /* 1. Get the payload into the data variable */
24   xbt_matrix_t *request = (xbt_matrix_t *) payload_data;
25   xbt_matrix_t result;
26
27   /* 2. Do the computation */
28   result = xbt_matrix_double_new_mult(request[0], request[1]);
29
30   /* 3. Send it back as payload of a pong message to the expeditor */
31   gras_msg_send(expeditor, "answer", &result);
32
33   /* 4. Cleanups */
34   xbt_matrix_free(request[0]);
35   xbt_matrix_free(request[1]);
36   xbt_matrix_free(result);
37
38   return 0;
39 }                               /* end_of_server_cb_request_handler */
40
41 static gras_socket_t try_gras_socket_server(int port)
42 {
43   volatile gras_socket_t sock = NULL;
44   TRY {
45     sock = gras_socket_server(port);
46   }
47   CATCH_ANONYMOUS {
48     RETHROWF("Unable to establish a server socket: %s");
49   }
50   return sock;
51 }
52
53 int server(int argc, char *argv[])
54 {
55   gras_socket_t sock = NULL;
56   int port = 4002;
57
58   /* 1. Init the GRAS infrastructure */
59   gras_init(&argc, argv);
60
61   /* 2. Get the port I should listen on from the command line, if specified */
62   if (argc == 2) {
63     port = atoi(argv[1]);
64   }
65
66   /* 3. Register the known messages and payloads. */
67   mmrpc_register_messages();
68
69   /* 4. Register my callback */
70   gras_cb_register("request", &server_cb_request_handler);
71
72   /* 5. Create my master socket */
73   XBT_INFO("Launch server (port=%d)", port);
74   sock = try_gras_socket_server(port);
75
76   /* 6. Wait up to 10 minutes for an incomming message to handle */
77   gras_msg_handle(600.0);
78
79   /* 7. Free the allocated resources, and shut GRAS down */
80   gras_socket_close(sock);
81   gras_exit();
82
83   return 0;
84 }                               /* end_of_server */