Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Massive mv to use cmake as the default compilation infrastructure.
[simgrid.git] / examples / gras / mmrpc / mmrpc_server.c
1 /* $Id$ */
2
3 /* GridRPC - Fake Grid RPC thingy doing matrix multiplications (as expected)*/
4
5 /* Copyright (c) 2005 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #define GRAS_DEFINE_TYPE_EXTERN
11 #include "xbt/matrix.h"
12 #include "mmrpc.h"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(MatMult);
15
16
17 typedef xbt_matrix_t request_t[2];
18 static int server_cb_request_handler(gras_msg_cb_ctx_t ctx,
19                                      void *payload_data)
20 {
21
22   gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
23
24   /* 1. Get the payload into the data variable */
25   xbt_matrix_t *request = (xbt_matrix_t *) payload_data;
26   xbt_matrix_t result;
27
28   /* 2. Do the computation */
29   result = xbt_matrix_double_new_mult(request[0], request[1]);
30
31   /* 3. Send it back as payload of a pong message to the expeditor */
32   gras_msg_send(expeditor, "answer", &result);
33
34   /* 4. Cleanups */
35   xbt_matrix_free(request[0]);
36   xbt_matrix_free(request[1]);
37   xbt_matrix_free(result);
38
39   return 0;
40 }                               /* end_of_server_cb_request_handler */
41
42 int server(int argc, char *argv[])
43 {
44   xbt_ex_t e;
45   gras_socket_t sock = NULL;
46   int port = 4000;
47
48   /* 1. Init the GRAS infrastructure */
49   gras_init(&argc, argv);
50
51   /* 2. Get the port I should listen on from the command line, if specified */
52   if (argc == 2) {
53     port = atoi(argv[1]);
54   }
55
56   /* 3. Create my master socket */
57   INFO1("Launch server (port=%d)", port);
58   TRY {
59     sock = gras_socket_server(port);
60   }
61   CATCH(e) {
62     RETHROW0("Unable to establish a server socket: %s");
63   }
64
65   /* 4. Register the known messages and payloads. */
66   mmrpc_register_messages();
67
68   /* 5. Register my callback */
69   gras_cb_register("request", &server_cb_request_handler);
70
71   /* 6. Wait up to 10 minutes for an incomming message to handle */
72   gras_msg_handle(600.0);
73
74   /* 7. Free the allocated resources, and shut GRAS down */
75   gras_socket_close(sock);
76   gras_exit();
77
78   return 0;
79 }                               /* end_of_server */