async library for manda (adaptive backend manager protocol)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

217 lines
6.5 KiB

  1. #ifndef _LIBMANDA_LIBMANDA_H
  2. #define _LIBMANDA_LIBMANDA_H
  3. #include "libmanda-config.h"
  4. #include <glib.h>
  5. #include <sys/socket.h>
  6. /* idlist */
  7. typedef struct manda_IDList manda_IDList;
  8. typedef enum {
  9. MANDA_FD_READ = 1,
  10. MANDA_FD_WRITE = 2
  11. } manda_async_events;
  12. typedef struct manda_async_ctrl manda_async_ctrl;
  13. typedef struct manda_fd_watcher manda_fd_watcher;
  14. typedef struct manda_timeout manda_timeout;
  15. typedef struct manda_server_callbacks manda_server_callbacks;
  16. typedef struct manda_server_connection manda_server_connection;
  17. typedef struct manda_server_backend_use manda_server_backend_use;
  18. typedef struct manda_server_backend manda_server_backend;
  19. typedef struct manda_server manda_server;
  20. typedef struct manda_client_backend_callbacks manda_client_backend_callbacks;
  21. typedef struct manda_client_backend manda_client_backend;
  22. typedef struct manda_client manda_client;
  23. typedef struct manda_connection manda_connection;
  24. typedef void (*manda_fd_watcher_cb)(manda_fd_watcher *watcher);
  25. typedef void (*manda_new_fd_watcher)(gpointer srv, manda_fd_watcher *watcher);
  26. typedef void (*manda_update_fd_watcher)(gpointer srv, manda_fd_watcher *watcher);
  27. typedef void (*manda_destroy_fd_watcher)(gpointer srv, manda_fd_watcher *watcher);
  28. typedef void (*manda_timeout_cb)(manda_timeout *timeout);
  29. typedef void (*manda_new_timeout)(gpointer srv, manda_timeout *timeout);
  30. typedef void (*manda_start_timeout)(gpointer srv, manda_timeout *timeout);
  31. typedef void (*manda_destroy_timeout)(gpointer srv, manda_timeout *timeout);
  32. typedef double (*manda_get_time)(gpointer srv);
  33. struct manda_async_ctrl {
  34. manda_new_fd_watcher new_fd_watcher;
  35. manda_update_fd_watcher update_fd_watcher;
  36. manda_destroy_fd_watcher destroy_fd_watcher;
  37. manda_new_timeout new_timeout;
  38. manda_start_timeout start_timeout; /* one shot */
  39. manda_destroy_timeout destroy_timeout;
  40. manda_get_time get_time;
  41. };
  42. struct manda_fd_watcher {
  43. gpointer data; /* application data */
  44. manda_fd_watcher_cb callback;
  45. int events; /* bitmask of manda_async_events; "update_fd_watcher" needs to check this */
  46. int fd; /* filedescriptor; doesn't get changed after "new_fd_watcher" */
  47. /* private from here */
  48. gpointer priv;
  49. };
  50. struct manda_timeout {
  51. gpointer data; /* application data */
  52. manda_timeout_cb callback;
  53. double timeout; /* absolute timestamp; check in start_timeout */
  54. /* private from here */
  55. gpointer priv;
  56. };
  57. /* Server API */
  58. typedef void (*manda_server_new_connection_cb)(gpointer srv, manda_server_connection *con);
  59. typedef void (*manda_server_closed_connection_cb)(gpointer srv, manda_server_connection *con);
  60. typedef void (*manda_server_bind_backend_cb)(gpointer srv, manda_server_connection *con, GString *name, guint16 reqid);
  61. typedef void (*manda_server_update_backend_cb)(gpointer srv, manda_server_backend *backend, guint ndx);
  62. typedef void (*manda_server_release_backend_cb)(gpointer srv, manda_server_backend *backend, guint old_ndx, manda_server_backend_use *old_use);
  63. struct manda_server_callbacks {
  64. manda_server_new_connection_cb new_connection;
  65. manda_server_closed_connection_cb closed_connection;
  66. manda_server_bind_backend_cb bind_backend;
  67. manda_server_update_backend_cb update_backend;
  68. manda_server_release_backend_cb release_backend;
  69. };
  70. struct manda_server_connection {
  71. gpointer data; /* application data */
  72. manda_server *srv;
  73. /* private from here */
  74. gint refcount;
  75. manda_connection *con;
  76. GPtrArray *backends; /* manda_server_backend_use */
  77. manda_IDList *idlist;
  78. };
  79. struct manda_server_backend_use {
  80. manda_server_connection *con;
  81. guint32 backend_id;
  82. guint32 last_load, last_workers;
  83. /* private from here */
  84. manda_server_backend *backend;
  85. guint ndx;
  86. };
  87. struct manda_server_backend {
  88. gpointer data; /* application data */
  89. GPtrArray *usage; /* array of manda_server_backend_use */
  90. guint32 sum_last_load;
  91. GString *addr;
  92. /* private from here */
  93. gint refcount;
  94. };
  95. struct manda_server {
  96. gint refcount;
  97. gpointer data; /* application data */
  98. GPtrArray *connections;
  99. /* private from here */
  100. const manda_async_ctrl *ctrl;
  101. const manda_server_callbacks *callbacks;
  102. GPtrArray *sockets;
  103. };
  104. manda_server* manda_server_new(gpointer srv, const manda_async_ctrl *ctrl, const manda_server_callbacks *callbacks);
  105. void manda_server_acquire(manda_server *s);
  106. void manda_server_release(manda_server *s);
  107. /* close everything */
  108. void manda_server_close(manda_server *s);
  109. void manda_server_add_socket(manda_server *s, int fd, gpointer data);
  110. void manda_server_con_close(manda_server_connection *con);
  111. manda_server_backend *manda_server_backend_new(gpointer data, GString *addr);
  112. void manda_server_backend_acquire(manda_server_backend *backend);
  113. void manda_server_backend_release(manda_server_backend *backend);
  114. gboolean manda_server_return_backend(manda_server_connection *con, gint16 reqid, manda_server_backend *backend);
  115. void manda_server_return_backend_fail(manda_server_connection *con, gint16 reqid, GString *errmsg);
  116. void manda_server_drop_backend(manda_server_backend *backend); /* tell all users that the backend is gone */
  117. /* Client API */
  118. typedef void (*manda_client_return_backend)(gpointer srv, manda_client_backend *backend);
  119. /* backend will be free()d after the callback, don't keep the pointer; reason is NULL if the connection was closed */
  120. typedef void (*manda_client_lost_backend)(gpointer srv, manda_client_backend *backend, const GString *reason);
  121. struct manda_client_backend_callbacks {
  122. manda_client_return_backend return_backend;
  123. manda_client_lost_backend lost_backend;
  124. };
  125. struct manda_client_backend {
  126. gpointer data; /* application data */
  127. manda_client *client;
  128. GString *addr;
  129. /* private from here */
  130. const manda_client_backend_callbacks *callbacks;
  131. guint32 id;
  132. };
  133. struct manda_client { /* private */
  134. gint refcount;
  135. gpointer data; /* application data */
  136. gboolean closed;
  137. const manda_async_ctrl *ctrl;
  138. struct sockaddr *addr;
  139. socklen_t addrlen;
  140. manda_connection *con;
  141. GPtrArray *backends; /* manda_client_backend* */
  142. /* establish connection */
  143. double last_connect_ts;
  144. int sock_fd;
  145. manda_fd_watcher sock_watcher;
  146. };
  147. manda_client* manda_client_new(gpointer srv, const manda_async_ctrl *ctrl, struct sockaddr *addr, socklen_t addrlen);
  148. void manda_client_close(manda_client *c);
  149. void manda_client_acquire(manda_client *c);
  150. void manda_client_release(manda_client *c);
  151. manda_client_backend* manda_client_bind_backend(manda_client *c, GString *name, gpointer data, const manda_client_backend_callbacks *callbacks);
  152. void manda_client_release_backend(manda_client_backend *backend);
  153. void manda_client_update_backend(manda_client_backend *backend, guint32 load, guint32 workers);
  154. #endif