tst_timers.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2014 Oswald Buddenhagen <ossi@users.sf.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * As a special exception, mbsync may be linked with the OpenSSL library,
  19. * despite that library's more restrictive license.
  20. */
  21. #include "common.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. /* Just to satisfy the references in util.c */
  26. int DFlags;
  27. const char *Home;
  28. struct tst {
  29. int id;
  30. int first, other, morph_at, morph_to;
  31. time_t start;
  32. wakeup_t timer;
  33. wakeup_t morph_timer;
  34. };
  35. static void
  36. timer_start( struct tst *timer, int to )
  37. {
  38. printf( "starting timer %d, should expire after %d\n", timer->id, to );
  39. time( &timer->start );
  40. conf_wakeup( &timer->timer, to );
  41. }
  42. static void
  43. timed_out( void *aux )
  44. {
  45. struct tst *timer = (struct tst *)aux;
  46. printf( "timer %d expired after %d, repeat %d\n",
  47. timer->id, (int)(time( 0 ) - timer->start), timer->other );
  48. if (timer->other >= 0) {
  49. timer_start( timer, timer->other );
  50. } else {
  51. wipe_wakeup( &timer->timer );
  52. wipe_wakeup( &timer->morph_timer );
  53. free( timer );
  54. }
  55. }
  56. static void
  57. morph_timed_out( void *aux )
  58. {
  59. struct tst *timer = (struct tst *)aux;
  60. printf( "morphing timer %d after %d\n",
  61. timer->id, (int)(time( 0 ) - timer->start) );
  62. timer_start( timer, timer->morph_to );
  63. }
  64. static int nextid;
  65. int
  66. main( int argc, char **argv )
  67. {
  68. int i;
  69. for (i = 1; i < argc; i++) {
  70. char *val = argv[i];
  71. struct tst *timer = nfmalloc( sizeof(*timer) );
  72. init_wakeup( &timer->timer, timed_out, timer );
  73. init_wakeup( &timer->morph_timer, morph_timed_out, timer );
  74. timer->id = ++nextid;
  75. timer->first = strtol( val, &val, 0 );
  76. if (*val == '@') {
  77. timer->other = timer->first;
  78. timer->first = strtol( ++val, &val, 0 );
  79. } else {
  80. timer->other = -1;
  81. }
  82. if (*val == ':') {
  83. timer->morph_to = strtol( ++val, &val, 0 );
  84. if (*val != '@')
  85. goto fail;
  86. timer->morph_at = strtol( ++val, &val, 0 );
  87. } else {
  88. timer->morph_at = -1;
  89. }
  90. if (*val) {
  91. fail:
  92. fprintf( stderr, "Fatal: syntax error in %s, use <timeout>[@<delay>][:<newtimeout>@<delay>]\n", argv[i] );
  93. return 1;
  94. }
  95. timer_start( timer, timer->first );
  96. if (timer->morph_at >= 0) {
  97. printf( "timer %d, should morph after %d\n", timer->id, timer->morph_at );
  98. conf_wakeup( &timer->morph_timer, timer->morph_at );
  99. }
  100. }
  101. main_loop();
  102. return 0;
  103. }