Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1749ef21339ec4062509815078c05b5ff1cca3a3
[simgrid.git] / doc / gtut-files / 4-callback.c
1 #include <gras.h>
2
3 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload) {
4   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
5
6   fprintf(stderr,"Cool, we received the message from %s:%d.\n",
7           gras_socket_peer_name(client), gras_socket_peer_port(client));
8   
9   return 1;
10 } /* end_of_callback */
11
12 int server(int argc, char *argv[]) {
13   gras_socket_t mysock;   /* socket on which I listen */
14   
15   gras_init(&argc,argv);
16
17   gras_msgtype_declare("hello", NULL);
18   mysock = gras_socket_server(atoi(argv[1]));
19    
20   gras_cb_register(gras_msgtype_by_name("hello"),&server_hello_cb);   
21   gras_msg_handle(60);
22     
23   gras_exit();
24   return 0;
25 }
26
27 int client(int argc, char *argv[]) {
28   gras_socket_t mysock;   /* socket on which I listen */
29   gras_socket_t toserver; /* socket used to write to the server */
30
31   gras_init(&argc,argv);
32
33   gras_msgtype_declare("hello", NULL);
34   mysock = gras_socket_server_range(1024, 10000, 0, 0);
35   
36   fprintf(stderr,"Client ready; listening on %d\n", gras_socket_my_port(mysock));
37   
38   gras_os_sleep(1.5); /* sleep 1 second and half */
39   toserver = gras_socket_client(argv[1], atoi(argv[2]));
40   
41   gras_msg_send(toserver,gras_msgtype_by_name("hello"), NULL);
42   fprintf(stderr,"That's it, we sent the data to the server on %s\n", gras_socket_peer_name(toserver));
43
44   gras_exit();
45   return 0;
46 }