Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4b5806a5973ad426d6fa9fd94ed69736fdf1a8e5
[simgrid.git] / examples / gras / mmrpc / mmrpc_client.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 int client(int argc, char *argv[])
15 {
16   xbt_ex_t e;
17   gras_socket_t toserver = NULL;        /* peer */
18   int connected = 0;
19
20   gras_socket_t from;
21   xbt_matrix_t request[2], answer;
22
23   int i, j;
24
25   const char *host = "127.0.0.1";
26   int port = 4000;
27
28   /* 1. Init the GRAS's infrastructure */
29   gras_init(&argc, argv);
30
31   /* 2. Get the server's address. The command line override defaults when specified */
32   if (argc == 3) {
33     host = argv[1];
34     port = atoi(argv[2]);
35   }
36
37   INFO2("Launch client (server on %s:%d)", host, port);
38
39   /* 3. Create a socket to speak to the server */
40   while (!connected) {
41     TRY {
42       toserver = gras_socket_client(host, port);
43       connected = 1;
44     }
45     CATCH(e) {
46       if (e.category != system_error)
47         RETHROW0("Unable to connect to the server: %s");
48       xbt_ex_free(e);
49       gras_os_sleep(0.05);
50     }
51   }
52   INFO2("Connected to %s:%d.", host, port);
53
54
55   /* 4. Register the messages (before use) */
56   mmrpc_register_messages();
57
58   /* 5. Keep the user informed of what's going on */
59   INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
60         gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
61
62   /* 6. Prepare and send the request to the server */
63
64   request[0] = xbt_matrix_double_new_id(MATSIZE, MATSIZE);
65   request[1] = xbt_matrix_double_new_rand(MATSIZE, MATSIZE);
66
67   /*
68      xbt_matrix_dump(request[0],"C:sent0",0,xbt_matrix_dump_display_double);
69      xbt_matrix_dump(request[1],"C:sent1",0,xbt_matrix_dump_display_double);
70    */
71
72   gras_msg_send(toserver, "request", &request);
73
74   xbt_matrix_free(request[0]);
75
76   INFO2(">>>>>>>> Request sent to %s:%d <<<<<<<<",
77         gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
78
79   /* 7. Wait for the answer from the server, and deal with issues */
80   gras_msg_wait(6000, "answer", &from, &answer);
81
82   /*
83      xbt_matrix_dump(answer,"C:answer",0,xbt_matrix_dump_display_double);
84    */
85   for (i = 0; i < MATSIZE; i++)
86     for (j = 0; i < MATSIZE; i++)
87       xbt_assert4(xbt_matrix_get_as(answer, i, j, double) ==
88                   xbt_matrix_get_as(request[1], i, j, double),
89                   "Answer does not match expectations. Found %f at cell %d,%d instead of %f",
90                   xbt_matrix_get_as(answer, i, j, double), i, j,
91                   xbt_matrix_get_as(request[1], i, j, double));
92
93   /* 8. Keep the user informed of what's going on, again */
94   INFO2(">>>>>>>> Got answer from %s:%d (values are right) <<<<<<<<",
95         gras_socket_peer_name(from), gras_socket_peer_port(from));
96
97   /* 9. Free the allocated resources, and shut GRAS down */
98   xbt_matrix_free(request[1]);
99   xbt_matrix_free(answer);
100   gras_socket_close(toserver);
101   gras_exit();
102   return 0;
103 }                               /* end_of_client */