Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix make dist.
[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 {
18   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
19   server_data_t *globals = (server_data_t *) gras_userdata_get();
20
21   XBT_CRITICAL("Argh, killed by %s:%d! Bye folks...",
22             gras_socket_peer_name(client), gras_socket_peer_port(client));
23
24   globals->killed = 1;
25
26   return 0;
27 }                               /* end_of_kill_callback */
28
29 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload)
30 {
31   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
32
33   XBT_INFO("Cool, we received the message from %s:%d.",
34         gras_socket_peer_name(client), gras_socket_peer_port(client));
35
36   return 0;
37 }                               /* end_of_hello_callback */
38
39 int server(int argc, char *argv[])
40 {
41   gras_socket_t mysock;         /* socket on which I listen */
42   server_data_t *globals;
43
44   gras_init(&argc, argv);
45
46   globals = gras_userdata_new(server_data_t *);
47   globals->killed = 0;
48
49   gras_msgtype_declare("hello", NULL);
50   gras_msgtype_declare("kill", NULL);
51   mysock = gras_socket_server(atoi(argv[1]));
52
53   gras_cb_register("hello", &server_hello_cb);
54   gras_cb_register("kill", &server_kill_cb);
55
56   while (!globals->killed) {
57     gras_msg_handle(60);
58   }
59
60   gras_exit();
61   return 0;
62 }
63
64 /* *********************** Client *********************** */
65 /* client_data */
66 typedef struct {
67   gras_socket_t toserver;
68   int done;
69 } client_data_t;
70
71
72 void client_do_hello(void)
73 {
74   client_data_t *globals = (client_data_t *) gras_userdata_get();
75
76   gras_msg_send(globals->toserver, "hello", NULL);
77   XBT_INFO("Hello sent to server");
78 }                               /* end_of_client_do_hello */
79
80 void client_do_stop(void)
81 {
82   client_data_t *globals = (client_data_t *) gras_userdata_get();
83
84   gras_msg_send(globals->toserver, "kill", NULL);
85   XBT_INFO("Kill sent to server");
86
87   gras_timer_cancel_repeat(0.5, client_do_hello);
88
89   globals->done = 1;
90   XBT_INFO("Break the client's while loop");
91 }                               /* end_of_client_do_stop */
92
93 int client(int argc, char *argv[])
94 {
95   gras_socket_t mysock;         /* socket on which I listen */
96   client_data_t *globals;
97
98   gras_init(&argc, argv);
99
100   gras_msgtype_declare("hello", NULL);
101   gras_msgtype_declare("kill", NULL);
102   mysock = gras_socket_server_range(1024, 10000, 0, 0);
103
104   XBT_VERB("Client ready; listening on %d", gras_socket_my_port(mysock));
105
106   globals = gras_userdata_new(client_data_t *);
107   globals->done = 0;
108
109   gras_os_sleep(1.5);           /* sleep 1 second and half */
110   globals->toserver = gras_socket_client(argv[1], atoi(argv[2]));
111
112   XBT_INFO("Programming the repetitive action with a frequency of 0.5 sec");
113   gras_timer_repeat(0.5, client_do_hello);
114
115   XBT_INFO("Programming the delayed action in 5 secs");
116   gras_timer_delay(5, client_do_stop);
117
118   while (!globals->done) {
119     gras_msg_handle(60);
120   }
121
122   gras_exit();
123   return 0;
124 }