Logo AND Algorithmique Numérique Distribuée

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