3 /* timer: repetitive and delayed actions */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 int client(int argc,char *argv[]); /* Placed here to not bother doxygen inclusion */
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
16 #define REPEAT_INTERVAL 1.0
17 #define DELAY_INTERVAL 2.0
24 static void repetitive_action(void) {
25 my_globals *globals=(my_globals*)gras_userdata_get();
27 /* Stop if nothing to do yet */
28 if (globals->still_to_do <= 0) {
29 INFO0("Repetitive_action has nothing to do yet");
33 if (globals->still_to_do == 1) {
34 /* Unregister myself if I'm done */
35 gras_timer_cancel_repeat(REPEAT_INTERVAL,repetitive_action);
38 INFO1("repetitive_action decrementing globals->still_to_do. New value: %d",
39 globals->still_to_do-1);
41 globals->still_to_do--; /* should be the last line of the action since value=0 stops the program */
42 } /* end_of_repetitive_action */
44 static void delayed_action(void) {
45 my_globals *globals=(my_globals*)gras_userdata_get();
47 INFO1("delayed_action setting globals->still_to_do to %d",LOOP_COUNT);
49 globals->still_to_do = LOOP_COUNT;
50 } /* end_of_delayed_action */
52 int client(int argc,char *argv[]) {
55 gras_init(&argc,argv);
56 globals=gras_userdata_new(my_globals);
57 globals->still_to_do = -1;
59 INFO1("Programming the repetitive_action with a frequency of %f sec", REPEAT_INTERVAL);
60 gras_timer_repeat(REPEAT_INTERVAL,repetitive_action);
62 INFO1("Programming the delayed_action for after %f sec", DELAY_INTERVAL);
63 gras_timer_delay(REPEAT_INTERVAL,delayed_action);
66 gras_os_sleep(DELAY_INTERVAL / 2.0);
68 INFO0("Canceling the delayed_action.");
69 gras_timer_cancel_delay(REPEAT_INTERVAL,delayed_action);
71 INFO1("Re-programming the delayed_action for after %f sec",DELAY_INTERVAL);
72 gras_timer_delay(REPEAT_INTERVAL,delayed_action);
74 while (globals->still_to_do == -1 || /* Before delayed action runs */
75 globals->still_to_do > 0 /* after delayed_action, and not enough repetitive_action */) {
77 DEBUG1("Prepare to handle messages for 5 sec (still_to_do=%d)", globals->still_to_do);