hloop_test.c 2.8 KB

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