ktpd_clients.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <faux/faux.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <klish/ktp_session.h>
  8. #include "private.h"
  9. static int ktpd_session_compare(const void *first, const void *second)
  10. {
  11. const ktpd_session_t *f = (const ktpd_session_t *)first;
  12. const ktpd_session_t *s = (const ktpd_session_t *)second;
  13. return (ktpd_session_fd(f) - ktpd_session_fd(s));
  14. }
  15. static int ktpd_session_kcompare(const void *key, const void *list_item)
  16. {
  17. const int *f = (const int *)key;
  18. const ktpd_session_t *s = (const ktpd_session_t *)list_item;
  19. return (*f - ktpd_session_fd(s));
  20. }
  21. ktpd_clients_t *ktpd_clients_new(void)
  22. {
  23. ktpd_clients_t *db = NULL;
  24. db = faux_zmalloc(sizeof(*db));
  25. assert(db);
  26. if (!db)
  27. return NULL;
  28. // Init
  29. db->list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  30. ktpd_session_compare, ktpd_session_kcompare,
  31. (void (*)(void *))ktpd_session_free);
  32. return db;
  33. }
  34. /** @brief Frees.
  35. */
  36. void ktpd_clients_free(ktpd_clients_t *db)
  37. {
  38. if (!db)
  39. return;
  40. faux_list_free(db->list);
  41. faux_free(db);
  42. }
  43. ktpd_session_t *ktpd_clients_find(const ktpd_clients_t *db, int fd)
  44. {
  45. assert(db);
  46. if (!db)
  47. return NULL;
  48. assert(fd >= 0);
  49. if (fd < 0)
  50. return NULL;
  51. return (ktpd_session_t *)faux_list_kfind(db->list, &fd);
  52. }
  53. ktpd_session_t *ktpd_clients_add(ktpd_clients_t *db, int fd)
  54. {
  55. ktpd_session_t *session = NULL;
  56. assert(db);
  57. if (!db)
  58. return NULL;
  59. assert(fd >= 0);
  60. if (fd < 0)
  61. return NULL;
  62. // Already exists
  63. if (ktpd_clients_find(db, fd))
  64. return NULL;
  65. session = ktpd_session_new(fd);
  66. if (!session)
  67. return NULL;
  68. if (!faux_list_add(db->list, session)) {
  69. ktpd_session_free(session);
  70. return NULL;
  71. }
  72. return session;
  73. }
  74. int ktpd_clients_del(ktpd_clients_t *db, int fd)
  75. {
  76. assert(db);
  77. if (!db)
  78. return -1;
  79. assert(fd >= 0);
  80. if (fd < 0)
  81. return -1;
  82. if (!faux_list_kdel(db->list, &fd))
  83. return -1;
  84. return 0;
  85. }
  86. faux_list_node_t *ktpd_clients_init_iter(const ktpd_clients_t *db)
  87. {
  88. assert(db);
  89. if (!db || !db->list)
  90. return NULL;
  91. return faux_list_head(db->list);
  92. }
  93. ktpd_session_t *ktpd_clients_each(faux_list_node_t **iter)
  94. {
  95. return (ktpd_session_t *)faux_list_each(iter);
  96. }
  97. void ktpd_clients_debug(ktpd_clients_t *db)
  98. {
  99. faux_list_node_t *iter = NULL;
  100. ktpd_session_t *session = NULL;
  101. assert(db);
  102. if (!db)
  103. return;
  104. iter = faux_list_head(db->list);
  105. if (!iter)
  106. return;
  107. while ((session = (ktpd_session_t *)faux_list_each(&iter))) {
  108. printf("clients: %d\n", ktpd_session_fd(session));
  109. }
  110. }