Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
edab230f207589a4bc632d850bc31864d54306b8
[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 "mmrpc.h"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(MatMult);
14
15 int client(int argc,char *argv[]) {
16   xbt_ex_t e; 
17   gras_socket_t toserver=NULL; /* peer */
18
19   gras_socket_t from;
20   matrix_t request[2], answer;
21
22   int i,j;
23
24   const char *host = "127.0.0.1";
25         int   port = 4000;
26
27   /* 1. Init the GRAS's infrastructure */
28   gras_init(&argc, argv);
29    
30   /* 2. Get the server's address. The command line override defaults when specified */
31   if (argc == 3) {
32     host=argv[1];
33     port=atoi(argv[2]);
34   } 
35
36   INFO2("Launch client (server on %s:%d)",host,port);
37    
38   /* 3. Wait for the server startup */
39   gras_os_sleep(1);
40    
41   /* 4. Create a socket to speak to the server */
42   TRY {
43     toserver=gras_socket_client(host,port);
44   } CATCH(e) {
45     RETHROW0("Unable to connect to the server: %s");
46   }
47   INFO2("Connected to %s:%d.",host,port);    
48
49
50   /* 5. Register the messages (before use) */
51   mmrpc_register_messages();
52
53   /* 6. Keep the user informed of what's going on */
54   INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
55         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
56
57   /* 7. Prepare and send the request to the server */
58
59   request[0].rows=request[0].cols=request[1].rows=request[1].cols=MATSIZE;
60
61   request[0].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
62   request[1].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
63
64   for (i=0; i<MATSIZE; i++) {
65     request[0].ctn[i*MATSIZE+i] = 1;
66     for (j=0; j<MATSIZE; j++)
67       request[1].ctn[i*MATSIZE+j] = i*MATSIZE+j;
68   }
69   /*  mat_dump(&request[0],"C:sent0");*/
70   /*  mat_dump(&request[1],"C:sent1");*/
71
72   gras_msg_send(toserver, gras_msgtype_by_name("request"), &request);
73
74   free(request[0].ctn);
75   free(request[1].ctn);
76
77   INFO2(">>>>>>>> Request sent to %s:%d <<<<<<<<",
78         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
79
80   /* 8. Wait for the answer from the server, and deal with issues */
81   gras_msg_wait(6000,gras_msgtype_by_name("answer"),&from,&answer);
82
83   /*  mat_dump(&answer,"C:answer");*/
84   for (i=0; i<MATSIZE*MATSIZE; i++) 
85     xbt_assert(answer.ctn[i]==i);
86
87   /* 9. Keep the user informed of what's going on, again */
88   INFO2(">>>>>>>> Got answer from %s:%d <<<<<<<<", 
89         gras_socket_peer_name(from),gras_socket_peer_port(from));
90
91   /* 10. Free the allocated resources, and shut GRAS down */
92   free(answer.ctn);
93   gras_socket_close(toserver);
94   gras_exit();
95   INFO0("Done.");
96   return 0;
97 } /* end_of_client */