Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move initialization somewhere else because people that do not use MSG_launch_applicat...
[simgrid.git] / doc / gtut-files / 04-callback.c
1 #include <stdio.h>
2 #include <gras.h>
3
4 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload) {
5   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
6
7   fprintf(stderr,"Cool, we received the message from %s:%d.\n",
8           gras_socket_peer_name(client), gras_socket_peer_port(client));
9   
10   return 0;
11 } /* end_of_callback */
12
13 int server(int argc, char *argv[]) {
14   gras_socket_t mysock;   /* socket on which I listen */
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("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,"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 }