/src/directory-client.c

https://github.com/schneider42/ubd · C · 181 lines · 145 code · 33 blank · 3 comment · 9 complexity · e3ceec95d49cb799589efb089622f700 MD5 · raw file

  1. #include "directory-server.h"
  2. #include <glib.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <gio/gio.h>
  8. #include <syslog.h>
  9. #include <json/json.h>
  10. #include "groups.h"
  11. #include "debug.h"
  12. #include "bus.h"
  13. #include "classes.h"
  14. #include "net_multicast.h"
  15. #include "config.h"
  16. #include "nodes.h"
  17. static gboolean dirclient_tick(gpointer data);
  18. static GHashTable *services;
  19. static GSocketAddress *sa;
  20. static GSocket *dirclientsocket;
  21. void dirclient_init(void)
  22. {
  23. g_timeout_add_seconds(15,dirclient_tick,NULL);
  24. //services = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
  25. services = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
  26. dirclientsocket = multicast_createSocket("directoryserver", 2323, &sa);
  27. if( socket != NULL){
  28. syslog(LOG_DEBUG,"dirclient_init: socket open");
  29. }else{
  30. syslog(LOG_WARNING,
  31. "directory-server.c: warning: could not create socket");
  32. }
  33. }
  34. void dirclient_addService(struct node *n, guint classid)
  35. {
  36. guint class = n->classes[classid];
  37. struct socketdata *sd = &(n->tcpsockets[classid]);
  38. gchar *addr = g_inet_address_to_string(n->netadr);
  39. json_object *cmd = json_object_new_string("update-service");
  40. json_object *service_type = json_object_new_string(classes_getClassName(class));
  41. json_object *url = json_object_new_string(addr);
  42. json_object *id = json_object_new_string(n->id);
  43. json_object *name = json_object_new_string(n->name);
  44. json_object *port = json_object_new_int(classes_getServicePort(class));
  45. json_object *udpproto = json_object_new_boolean(1);
  46. json_object *tcpproto = json_object_new_boolean(1);
  47. json_object *multicast = json_object_new_boolean(0);
  48. json_object *json = json_object_new_object();
  49. json_object_object_add(json,"cmd", cmd);
  50. json_object_object_add(json,"name", name);
  51. json_object_object_add(json,"id", id);
  52. json_object_object_add(json,"url", url);
  53. json_object_object_add(json,"port", port);
  54. json_object_object_add(json,"service-type", service_type);
  55. json_object_object_add(json,"tcp", tcpproto);
  56. json_object_object_add(json,"udp", udpproto);
  57. json_object_object_add(json,"multicast", multicast);
  58. const char *jsons = json_object_to_json_string(json);
  59. syslog(LOG_DEBUG,"sending json: %s", jsons);
  60. g_socket_send_to(dirclientsocket, sa, jsons, strlen(jsons), NULL, NULL);
  61. g_hash_table_insert(services, sd, g_strdup(jsons));
  62. json_object_put(json);
  63. g_free(addr);
  64. }
  65. void dirclient_removeService(struct node *n, guint classid)
  66. {
  67. guint class = n->classes[classid];
  68. struct socketdata *sd = &(n->tcpsockets[classid]);
  69. json_object *cmd = json_object_new_string("delete-service");
  70. json_object *service_type = json_object_new_string(classes_getClassName(class));
  71. json_object *id = json_object_new_string(n->id);
  72. json_object *json = json_object_new_object();
  73. json_object_object_add(json,"cmd", cmd);
  74. json_object_object_add(json,"id", id);
  75. json_object_object_add(json,"service-type", service_type);
  76. const char *jsons = json_object_to_json_string(json);
  77. syslog(LOG_DEBUG,"sending json: %s", jsons);
  78. g_socket_send_to(dirclientsocket, sa, jsons, strlen(jsons), NULL, NULL);
  79. json_object_put(json);
  80. g_hash_table_remove(services, sd);
  81. }
  82. void dirclient_registerServices(struct node *n)
  83. {
  84. guint i;
  85. for(i=0; i<sizeof(n->classes); i++){
  86. if( n->classes[i] != 0 ){
  87. syslog(LOG_DEBUG,"adding service %d\n", n->classes[i]);
  88. dirclient_addService(n, i);
  89. }
  90. }
  91. }
  92. void dirclient_removeServices(struct node *n)
  93. {
  94. guint i;
  95. for(i=0; i<sizeof(n->classes); i++){
  96. if( n->classes[i] != 0 ){
  97. syslog(LOG_DEBUG,"removing service %d\n", n->classes[i]);
  98. dirclient_removeService(n, i);
  99. }
  100. }
  101. }
  102. void dirclient_registerMulticastGroup(struct multicastgroup *g)
  103. {
  104. printf("dirclient_registerMulticastGroup()\n");
  105. char *address = g_inet_address_to_string(
  106. g_inet_socket_address_get_address(
  107. (GInetSocketAddress*)g->sa));
  108. guint class = g->class;
  109. json_object *cmd = json_object_new_string("update-service");
  110. json_object *service_type = json_object_new_string(classes_getClassName(class));
  111. json_object *url = json_object_new_string(address);
  112. json_object *id = json_object_new_string(g->name);
  113. json_object *name = json_object_new_string(g->hostname);
  114. json_object *port = json_object_new_int(classes_getServicePort(class));
  115. json_object *udpproto = json_object_new_boolean(1);
  116. json_object *tcpproto = json_object_new_boolean(0);
  117. json_object *multicast = json_object_new_boolean(1);
  118. json_object *json = json_object_new_object();
  119. json_object_object_add(json,"cmd", cmd);
  120. json_object_object_add(json,"name", name);
  121. json_object_object_add(json,"id", id);
  122. json_object_object_add(json,"url", url);
  123. json_object_object_add(json,"port", port);
  124. json_object_object_add(json,"service-type", service_type);
  125. json_object_object_add(json,"tcp", tcpproto);
  126. json_object_object_add(json,"udp", udpproto);
  127. json_object_object_add(json,"multicast", multicast);
  128. const char *jsons = json_object_to_json_string(json);
  129. syslog(LOG_DEBUG,"sending json: %s", jsons);
  130. g_socket_send_to(dirclientsocket, sa, jsons, strlen(jsons), NULL, NULL);
  131. g_hash_table_insert(services, g, g_strdup(jsons));
  132. json_object_put(json);
  133. g_free(address);
  134. }
  135. static gboolean dirclient_tick(gpointer data)
  136. {
  137. data = NULL;
  138. GHashTableIter iter;
  139. struct socketdata *id;
  140. char *service;
  141. g_hash_table_iter_init (&iter, services);
  142. //syslog(LOG_DEBUG,"dirserver_tick: decrement");
  143. while( g_hash_table_iter_next (&iter, (void**)&id, (void**)&service) ){
  144. //syslog(LOG_DEBUG,"dirserver_tick: decrementing %s", id);
  145. g_socket_send_to(dirclientsocket, sa, service, strlen(service), NULL, NULL);
  146. }
  147. return TRUE;
  148. }