Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cb5ad513df610be04ea0aa29904c10dfad5aa776
[simgrid.git] / doc / gtut-files / 07-timers.c
1 /* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <gras.h>
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"My little example");
10
11 /* *********************** Server *********************** */
12 typedef struct {
13    int killed;
14 } server_data_t;   
15
16 int server_kill_cb(gras_msg_cb_ctx_t ctx, void *payload) {
17   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
18   server_data_t *globals=(server_data_t*)gras_userdata_get();
19    
20   CRITICAL2("Argh, killed by %s:%d! Bye folks...",
21         gras_socket_peer_name(client), gras_socket_peer_port(client));
22   
23   globals->killed = 1;
24    
25   return 0;
26 } /* end_of_kill_callback */
27
28 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload) {
29   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
30
31   INFO2("Cool, we received the message from %s:%d.",
32         gras_socket_peer_name(client), gras_socket_peer_port(client));
33   
34   return 0;
35 } /* end_of_hello_callback */
36
37 int server(int argc, char *argv[]) {
38   gras_socket_t mysock;   /* socket on which I listen */
39   server_data_t *globals;
40   
41   gras_init(&argc,argv);
42
43   globals=gras_userdata_new(server_data_t*);
44   globals->killed=0;
45
46   gras_msgtype_declare("hello", NULL);
47   gras_msgtype_declare("kill", NULL);
48   mysock = gras_socket_server(atoi(argv[1]));
49    
50   gras_cb_register("hello",&server_hello_cb);   
51   gras_cb_register("kill",&server_kill_cb);
52
53   while (!globals->killed) {
54      gras_msg_handle(60);
55   }
56     
57   gras_exit();
58   return 0;
59 }
60
61 /* *********************** Client *********************** */
62 /* client_data */
63 typedef struct {
64    gras_socket_t toserver;
65    int done;
66 } client_data_t;
67  
68
69 void client_do_hello(void) {
70   client_data_t *globals=(client_data_t*)gras_userdata_get();
71    
72   gras_msg_send(globals->toserver,"hello", NULL);
73   INFO0("Hello sent to server");
74 } /* end_of_client_do_hello */
75
76 void client_do_stop(void) {
77   client_data_t *globals=(client_data_t*)gras_userdata_get();
78    
79   gras_msg_send(globals->toserver,"kill", NULL);
80   INFO0("Kill sent to server");
81    
82   gras_timer_cancel_repeat(0.5,client_do_hello);
83  
84   globals->done = 1;
85   INFO0("Break the client's while loop");
86 } /* end_of_client_do_stop */
87
88 int client(int argc, char *argv[]) {
89   gras_socket_t mysock;   /* socket on which I listen */
90   client_data_t *globals;
91
92   gras_init(&argc,argv);
93
94   gras_msgtype_declare("hello", NULL);
95   gras_msgtype_declare("kill", NULL);
96   mysock = gras_socket_server_range(1024, 10000, 0, 0);
97   
98   VERB1("Client ready; listening on %d", gras_socket_my_port(mysock));
99   
100   globals=gras_userdata_new(client_data_t*);
101   globals->done = 0;
102   
103   gras_os_sleep(1.5); /* sleep 1 second and half */
104   globals->toserver = gras_socket_client(argv[1], atoi(argv[2]));
105    
106   INFO0("Programming the repetitive action with a frequency of 0.5 sec");
107   gras_timer_repeat(0.5,client_do_hello);
108   
109   INFO0("Programming the delayed action in 5 secs");
110   gras_timer_delay(5,client_do_stop);
111   
112   while (!globals->done) {
113      gras_msg_handle(60);
114   }
115
116   gras_exit();
117   return 0;
118 }
119