Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Lesson 4
[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   gras_socket_t toclient; /* socket used to write to the client */
15   
16   gras_init(&argc,argv);
17
18   gras_msgtype_declare("hello", NULL);
19   mysock = gras_socket_server(atoi(argv[1]));
20    
21   gras_cb_register(gras_msgtype_by_name("hello"),&server_hello_cb);   
22   gras_msg_handle(60);
23     
24   gras_exit();
25   return 0;
26 }
27
28 int client(int argc, char *argv[]) {
29   gras_socket_t mysock;   /* socket on which I listen */
30   gras_socket_t toserver; /* socket used to write to the server */
31
32   gras_init(&argc,argv);
33
34   gras_msgtype_declare("hello", NULL);
35   mysock = gras_socket_server_range(1024, 10000, 0, 0);
36   
37   fprintf(stderr,"Client ready; listening on %d\n", gras_socket_my_port(mysock));
38   
39   gras_os_sleep(1.5); /* sleep 1 second and half */
40   toserver = gras_socket_client(argv[1], atoi(argv[2]));
41   
42   gras_msg_send(toserver,gras_msgtype_by_name("hello"), NULL);
43   fprintf(stderr,"That's it, we sent the data to the server on %s\n", gras_socket_peer_name(toserver));
44
45   gras_exit();
46   return 0;
47 }