Logo AND Algorithmique Numérique Distribuée

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