1
0

hloop_test.c 2.7 KB

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