upubsub.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* upubsub: a minimal publish/subscribe 1 header file library for pure ANSI C. */
  2. #ifndef UPUBSUB_H_INCLUDED
  3. #define UPUBSUB_H_INCLUDED
  4. #ifndef UPUBSUB_LISTENER_AMOUNT
  5. #define UPUBSUB_LISTENER_AMOUNT 64
  6. #endif /* UPUBSUB_LISTENER_AMOUNT */
  7. #ifndef UPUBSUB_MESSAGE_SIZE
  8. #define UPUBSUB_MESSAGE_SIZE 64
  9. #endif /* UPUBSUB_MESSAGE_SIZE */
  10. #ifndef UPUBSUB_TOPIC_AMOUNT
  11. #define UPUBSUB_TOPIC_AMOUNT 32
  12. #endif /* UPUBSUB_TOPIC_AMOUNT */
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <string.h>
  16. struct upubsub_message {
  17. const char * topic;
  18. size_t size;
  19. uint8_t copy[UPUBSUB_MESSAGE_SIZE];
  20. int copy_ok;
  21. void * data;
  22. };
  23. struct upubsub_listener;
  24. struct upubsub;
  25. typedef int (upubsub_listen_func)(struct upubsub_listener *l, struct upubsub_message m);
  26. struct upubsub_listener {
  27. struct upubsub * upubsub;
  28. unsigned int id;
  29. const char * topic;
  30. void * data;
  31. upubsub_listen_func * listen;
  32. };
  33. struct upubsub_topic {
  34. const char * topic;
  35. size_t size;
  36. struct upubsub_listener * first;
  37. };
  38. struct upubsub {
  39. size_t size;
  40. unsigned int last_id;
  41. struct upubsub_listener listeners[UPUBSUB_LISTENER_AMOUNT];
  42. size_t topics_size;
  43. struct upubsub_topic topics[UPUBSUB_TOPIC_AMOUNT];
  44. };
  45. int upubsub_listener_cmp_topic_id(const void * v1, const void * v2);
  46. int upubsub_listener_cmp_topic(const void * v1, const void * v2);
  47. int upubsub_listener_cmp_id(const void * v1, const void * v2);
  48. struct upubsub_listener * upubsub_subscribe_listener(struct upubsub * u, struct upubsub_listener l);
  49. struct upubsub_listener* upubsub_subscribe(struct upubsub * u, const char * topic, void * data, upubsub_listen_func * listen);
  50. int upubsub_publish_message(struct upubsub u, struct upubsub_message m);
  51. int upubsub_publish_data(struct upubsub u, const char * topic, void * data, size_t size);
  52. int upubsub_publish_str(struct upubsub u, const char * topic, char * str);
  53. int upubsub_unsubscribe_listener(struct upubsub * u, struct upubsub_listener * l);
  54. #endif /* UPUBSUB_H_INCLUDED */
  55. #ifdef UPUBSUB_TEST
  56. #define UPUBSUB_IMPLEMENTATION
  57. #endif
  58. #ifdef UPUBSUB_IMPLEMENTATION
  59. int bsearch_low_index(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *)) {
  60. void *try;
  61. int sign;
  62. int found;
  63. int low = 0;
  64. int high = nel;
  65. while (low <= high) {
  66. int mid = low + (high - low) / 2;
  67. try = (char*)base + (mid*width);
  68. sign = cmp(key, try);
  69. if (sign <= 0) {
  70. high = mid - 1;
  71. } else {
  72. low = mid + 1;
  73. }
  74. }
  75. return low;
  76. }
  77. int bsearch_high_index(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *)) {
  78. void *try;
  79. int sign;
  80. int found;
  81. int low = 0;
  82. int high = nel;
  83. while (low <= high) {
  84. int mid = low + (high - low) / 2;
  85. try = (char*)base + (mid*width);
  86. sign = cmp(key, try);
  87. if (sign < 0) {
  88. high = mid - 1;
  89. } else {
  90. low = mid + 1;
  91. }
  92. }
  93. return high;
  94. }
  95. int upubsub_listener_cmp_topic_id(const void * v1, const void * v2) {
  96. int res;
  97. struct upubsub_listener *l1, *l2;
  98. l1 = (struct upubsub_listener *)(v1);
  99. l2 = (struct upubsub_listener *)(v2);
  100. /* Sort with null topic as last, as these ones are unused. */
  101. if ((!l1->topic) && (l2->topic)) { return 1; }
  102. if ((!l2->topic) && (l1->topic)) { return -1; }
  103. if ((!l2->topic) && (!l1->topic)) { return 0; }
  104. /* Otherwise compare topics. */
  105. res = strcmp(l1->topic, l2->topic);
  106. /* If the same sort by id to try to keep the sort stable. */
  107. if (!res) {
  108. return l1->id - l2->id;
  109. }
  110. return res;
  111. }
  112. int upubsub_topic_cmp_topic(const void * v1, const void * v2) {
  113. struct upubsub_topic *l1, *l2;
  114. l1 = (struct upubsub_topic *)(v1);
  115. l2 = (struct upubsub_topic *)(v2);
  116. /* Sort with null topic as last, as these ones are unused. */
  117. if ((!l1->topic) && (l2->topic)) { return 1; }
  118. if ((!l2->topic) && (l1->topic)) { return -1; }
  119. if ((!l2->topic) && (!l1->topic)) { return 0; }
  120. /* Otherwise compare topics. */
  121. return strcmp(l1->topic, l2->topic);
  122. }
  123. int upubsub_listener_cmp_topic(const void * v1, const void * v2) {
  124. struct upubsub_listener *l1, *l2;
  125. l1 = (struct upubsub_listener *)(v1);
  126. l2 = (struct upubsub_listener *)(v2);
  127. /* Sort with null topic as last, as these ones are unused. */
  128. if ((!l1->topic) && (l2->topic)) { return 1; }
  129. if ((!l2->topic) && (l1->topic)) { return -1; }
  130. if ((!l2->topic) && (!l1->topic)) { return 0; }
  131. /* Otherwise compare topics. */
  132. return strcmp(l1->topic, l2->topic);
  133. }
  134. int upubsub_listener_cmp_id(const void * v1, const void * v2) {
  135. struct upubsub_listener *l1, *l2;
  136. l1 = (struct upubsub_listener *)(v1);
  137. l2 = (struct upubsub_listener *)(v2);
  138. /* Sort with null topic as last, as these ones are unused. */
  139. if ((!l1->topic) && (l2->topic)) { return 1; }
  140. if ((!l2->topic) && (l1->topic)) { return -1; }
  141. if ((!l2->topic) && (!l1->topic)) { return 0; }
  142. /* Compare by id to find the exact listener. */
  143. return l1->id - l2->id;
  144. }
  145. struct upubsub_listener * upubsub_subscribe_listener(struct upubsub * u, struct upubsub_listener l) {
  146. struct upubsub_listener * res;
  147. if ( u->size >= UPUBSUB_LISTENER_AMOUNT ) {
  148. return NULL;
  149. }
  150. u->last_id++;
  151. l.id = u->last_id;
  152. l.upubsub = u;
  153. u->listeners[u->size] = l;
  154. res = u->listeners + u->size;
  155. u->size ++;
  156. qsort(u->listeners, u->size, sizeof(struct upubsub_listener), upubsub_listener_cmp_topic_id);
  157. return res;
  158. }
  159. struct upubsub_listener* upubsub_subscribe(struct upubsub * u, const char * topic, void * data, upubsub_listen_func * listen) {
  160. struct upubsub_listener l;
  161. l.topic = topic;
  162. l.data = data;
  163. l.listen = listen;
  164. return upubsub_subscribe_listener(u, l);
  165. }
  166. int upubsub_publish_message(struct upubsub u, struct upubsub_message m) {
  167. int sent = 0;
  168. size_t start, index;
  169. struct upubsub_listener * found = NULL;
  170. struct upubsub_listener key;
  171. int low, high;
  172. key.topic = m.topic;
  173. key.id = 1;
  174. low = bsearch_low_index(&key, u.listeners, u.size, sizeof(struct upubsub_listener), upubsub_listener_cmp_topic);
  175. high = bsearch_high_index(&key, u.listeners, u.size, sizeof(struct upubsub_listener), upubsub_listener_cmp_topic);
  176. if (high < low) {
  177. return sent;
  178. }
  179. for (index = low; index <= high; index ++) {
  180. found = u.listeners + index;
  181. found->listen(found, m);
  182. sent ++;
  183. }
  184. return sent;
  185. }
  186. int upubsub_publish_data(struct upubsub u, const char * topic, void * data, size_t size) {
  187. struct upubsub_message m = { 0 };
  188. m.data = data;
  189. m.size = size;
  190. m.copy_ok = 0;
  191. if (m.size < UPUBSUB_MESSAGE_SIZE) {
  192. memcpy(m.copy, data, m.size);
  193. m.copy_ok = 1;
  194. }
  195. m.topic = topic;
  196. return upubsub_publish_message(u, m);
  197. }
  198. int upubsub_publish_str(struct upubsub u, const char * topic, char * str) {
  199. return upubsub_publish_data(u, topic, str, strlen(str));
  200. }
  201. int upubsub_unsubscribe_listener(struct upubsub * u, struct upubsub_listener * l) {
  202. struct upubsub_listener * found = NULL;
  203. found = bsearch(l, u->listeners, u->size, sizeof(struct upubsub_listener), upubsub_listener_cmp_id);
  204. if (!found) {
  205. return -1;
  206. }
  207. /* Set to unused. */
  208. found->id = 0;
  209. found->topic = NULL;
  210. /* Unused will be sorted to the back. */
  211. qsort(u->listeners, u->size, sizeof(struct upubsub_listener), upubsub_listener_cmp_topic_id);
  212. u->size--;
  213. return 0;
  214. }
  215. int upubsub_unsubscribe(struct upubsub_listener * l) {
  216. return upubsub_unsubscribe_listener(l->upubsub, l);
  217. }
  218. #endif /* UPUBSUB_IMPLEMENTATION */
  219. #ifdef UPUBSUB_TEST
  220. #include <stdio.h>
  221. int listen_print_str(struct upubsub_listener *l, struct upubsub_message m) {
  222. printf("received on topic %s: data %s, %s\n", l->topic, (char*)(l->data), (char *)(m.data));
  223. return 0;
  224. }
  225. int compare_int(const void * key, const void * mem) {
  226. int * i1 = (int*)key;
  227. int * i2 = (int*)mem;
  228. return (*i1) - (*i2);
  229. }
  230. int main(void) {
  231. struct upubsub pusu = { 0 };
  232. struct upubsub_listener *l[10];
  233. int arr[] = { 7, 8 , 8, 9, 9, 9, 10, 11, 12, 20, 20, 32 };
  234. int low, high;
  235. int key = 9;
  236. low = bsearch_low_index(&key, &arr, 12, sizeof(int), compare_int);
  237. high = bsearch_high_index(&key, &arr, 12, sizeof(int), compare_int);
  238. printf("Low, high: %d -> %d...\n", low, high);
  239. if ((low > 0) && (low < 12)) {
  240. printf("Low: %d\n", arr[low]);
  241. }
  242. if ((high > 0) && (high < 12)) {
  243. printf("High: %d\n", arr[high]);
  244. }
  245. for (key = 0; key < 40; key ++) {
  246. low = bsearch_low_index(&key, &arr, 12, sizeof(int), compare_int);
  247. high = bsearch_high_index(&key, &arr, 12, sizeof(int), compare_int);
  248. printf("Key: %d; Low, high: %d -> %d:", key, low, high);
  249. if ((low >= 0) && (low < 12)) {
  250. printf(" Low: %d", arr[low]);
  251. }
  252. if ((high >= 0) && (high < 12)) {
  253. printf(" High: %d", arr[high]);
  254. }
  255. printf("\n");
  256. }
  257. printf("Subscribing...\n");
  258. l[0] = upubsub_subscribe(&pusu, "POWER" , "SYST", listen_print_str);
  259. l[1] = upubsub_subscribe(&pusu, "POWER" , "APP1", listen_print_str);
  260. l[2] = upubsub_subscribe(&pusu, "SYSTEM", "APP2", listen_print_str);
  261. l[3] = upubsub_subscribe(&pusu, "SYSTEM", "WIND", listen_print_str);
  262. printf("Publishing...\n");
  263. upubsub_publish_str(pusu, "POWER", "DOWN");
  264. upubsub_publish_str(pusu, "POWER", "UP");
  265. upubsub_publish_str(pusu, "SYSTEM", "CLICK(7,8)");
  266. upubsub_publish_str(pusu, "SYSTEM", "ROLL(9,5)");
  267. upubsub_publish_str(pusu, "NULL", "IGNORE");
  268. printf("Removing topics...\n");
  269. upubsub_unsubscribe(l[1]);
  270. upubsub_unsubscribe(l[3]);
  271. printf("Publishing...\n");
  272. upubsub_publish_str(pusu, "POWER", "DOWN 2");
  273. upubsub_publish_str(pusu, "POWER", "UP 2");
  274. upubsub_publish_str(pusu, "SYSTEM", "CLICK(7,8) 2");
  275. upubsub_publish_str(pusu, "SYSTEM", "ROLL(9,5) 2");
  276. upubsub_publish_str(pusu, "NULL", "IGNORE");
  277. return 0;
  278. }
  279. #endif /* UPUBSUB_TEST */