1
0

Timeout_test.c.disabled 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * You may redistribute this program and/or modify it under the terms of
  3. * the GNU General Public License as published by the Free Software Foundation,
  4. * either version 3 of the License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <event2/event.h>
  16. #include "memory/MemAllocator.h"
  17. #include "memory/BufferAllocator.h"
  18. #include "util/Timeout.h"
  19. /**
  20. * This is slow so it should only be run manually.
  21. */
  22. struct Context
  23. {
  24. struct Timeout* timeout;
  25. uint32_t count;
  26. };
  27. void callback(void* context)
  28. {
  29. printf("Timeout was called!\n");
  30. *((uint32_t*) context) = 1;
  31. }
  32. void intervalCallback(void* vcontext)
  33. {
  34. struct Context* context = (struct Context*) vcontext;
  35. context->count++;
  36. if (context->count > 2) {
  37. Timeout_resetTimeout(context->timeout, 5000);
  38. }
  39. if (context->count > 3) {
  40. Timeout_clearTimeout(context->timeout);
  41. }
  42. printf("Interval was called!\n");
  43. }
  44. int main()
  45. {
  46. uint8_t buffer[1024];
  47. struct MemAllocator* allocator = BufferAllocator_new(buffer, 1024);
  48. struct Context timeoutContext = {NULL, 0};
  49. struct Context intervalContext = {NULL, 0};
  50. struct event_base* base = event_base_new();
  51. timeoutContext.timeout =
  52. Timeout_setTimeout(callback, &timeoutContext, 2000, base, allocator);
  53. intervalContext.timeout =
  54. Timeout_setInterval(intervalCallback, &intervalContext, 500, base, allocator);
  55. event_base_loop(base, 0);
  56. }