hloop_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "hloop.h"
  2. #include "hbase.h"
  3. #include "hlog.h"
  4. #include "nlog.h"
  5. void mylogger(int loglevel, const char* buf, int len) {
  6. if (loglevel >= LOG_LEVEL_ERROR) {
  7. stderr_logger(loglevel, buf, len);
  8. }
  9. if (loglevel >= LOG_LEVEL_INFO) {
  10. file_logger(loglevel, buf, len);
  11. }
  12. network_logger(loglevel, buf, len);
  13. }
  14. void on_idle(hidle_t* idle) {
  15. printf("on_idle: event_id=%llu\tpriority=%d\tuserdata=%ld\n",
  16. LLU(hevent_id(idle)), hevent_priority(idle), (long)(intptr_t)(hevent_userdata(idle)));
  17. }
  18. void on_timer(htimer_t* timer) {
  19. hloop_t* loop = hevent_loop(timer);
  20. printf("on_timer: event_id=%llu\tpriority=%d\tuserdata=%ld\ttime=%llus\thrtime=%lluus\n",
  21. LLU(hevent_id(timer)), hevent_priority(timer), (long)(intptr_t)(hevent_userdata(timer)),
  22. LLU(hloop_now(loop)), LLU(hloop_now_hrtime(loop)));
  23. }
  24. void cron_hourly(htimer_t* timer) {
  25. time_t tt;
  26. time(&tt);
  27. printf("cron_hourly: %s\n", ctime(&tt));
  28. }
  29. void timer_write_log(htimer_t* timer) {
  30. static int cnt = 0;
  31. hlogd("[%d] Do you recv me?", ++cnt);
  32. hlogi("[%d] Do you recv me?", ++cnt);
  33. hloge("[%d] Do you recv me?", ++cnt);
  34. }
  35. void on_stdin(hio_t* io, void* buf, int readbytes) {
  36. printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  37. printf("> %s\n", (char*)buf);
  38. if (strncmp((char*)buf, "quit", 4) == 0) {
  39. hloop_stop(hevent_loop(io));
  40. }
  41. }
  42. void on_custom_events(hevent_t* ev) {
  43. printf("on_custom_events event_type=%d userdata=%ld\n", (int)ev->event_type, (long)ev->userdata);
  44. }
  45. int main() {
  46. // memcheck atexit
  47. HV_MEMCHECK;
  48. hloop_t* loop = hloop_new(0);
  49. // test idle and priority
  50. for (int i = HEVENT_LOWEST_PRIORITY; i <= HEVENT_HIGHEST_PRIORITY; ++i) {
  51. hidle_t* idle = hidle_add(loop, on_idle, 10);
  52. hevent_set_priority(idle, i);
  53. }
  54. // test timer timeout
  55. for (int i = 1; i <= 10; ++i) {
  56. htimer_t* timer = htimer_add(loop, on_timer, i*1000, 3);
  57. hevent_set_userdata(timer, (void*)(intptr_t)i);
  58. }
  59. // test timer period
  60. int minute = time(NULL)%3600/60;
  61. htimer_add_period(loop, cron_hourly, minute+1, -1, -1, -1, -1, INFINITE);
  62. // test network_logger
  63. htimer_add(loop, timer_write_log, 1000, INFINITE);
  64. logger_set_handler(hlog, mylogger);
  65. hlog_set_file("loop.log");
  66. logger_enable_color(hlog, 1);
  67. nlog_listen(loop, DEFAULT_LOG_PORT);
  68. // test nonblock stdin
  69. printf("input 'quit' to quit loop\n");
  70. char buf[64];
  71. hread(loop, 0, buf, sizeof(buf), on_stdin);
  72. // test custom_events
  73. for (int i = 0; i < 10; ++i) {
  74. hevent_t ev;
  75. ev.event_type = (hevent_type_e)(HEVENT_TYPE_CUSTOM + i);
  76. ev.cb = on_custom_events;
  77. ev.userdata = (void*)(long)i;
  78. hloop_post_event(loop, &ev);
  79. }
  80. hloop_run(loop);
  81. hloop_free(&loop);
  82. return 0;
  83. }