hloop_test.c 3.2 KB

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