Logo AND Algorithmique Numérique Distribuée

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