The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
discovery.c
Go to the documentation of this file.
1 
24 #include <projectcommon.h>
25 #define DISCOVERY_SUBCLASS
26 #include <discovery.h>
27 #include <assert.h>
32 
33 FSTATIC char * _discovery_instancename(const Discovery* self);
36 FSTATIC gboolean _discovery_rediscover(gpointer vself);
37 FSTATIC void _discovery_ghash_destructor(gpointer gdiscovery);
38 
40 
42 FSTATIC char *
44 {
45  return self->_instancename;
46 }
47 
49 FSTATIC void
51 {
52  (void)self;
53 }
54 
56 FSTATIC guint
58 {
59  (void)self;
60  return 0;
61 }
62 static GHashTable * _discovery_timers = NULL;
63 
65 FSTATIC void
67 {
68  Discovery* self = CASTTOCLASS(Discovery, gself);
69  char * instancename = self->_instancename;
70 
71  if (self->_timerid > 0) {
72  g_source_remove(self->_timerid);
73  self->_timerid = 0;
74  }
75  if (self->_config) {
76  UNREF(self->_config);
77  }
78  if (_discovery_timers && instancename) {
79  self->_instancename = NULL; // Avoid infinite recursion...
80  g_hash_table_remove(_discovery_timers, instancename);
81  }
82  if (instancename) {
83  g_free(instancename);
84  self->_instancename = instancename = NULL;
85  }
86 
87  FREECLASSOBJ(self); self=NULL;
88 }
89 
94 FSTATIC void
95 _discovery_ghash_destructor(gpointer gdiscovery)
96 {
97  Discovery* self = CASTTOCLASS(Discovery, gdiscovery);
98  // We take steps in discovery_finalize() to avoid infinite recursion...
99  UNREF(self);
100 }
101 
104 FSTATIC gboolean
105 _discovery_rediscover(gpointer vself)
106 {
107  Discovery* self = CASTTOCLASS(Discovery, vself);
108 
109  return self->discover(self);
110 }
111 
116 Discovery*
117 discovery_new(const char * instname,
118  NetGSource* iosource,
119  ConfigContext* context,
120  gsize objsize)
121 {
122  gsize size = objsize < sizeof(Discovery) ? sizeof(Discovery) : objsize;
124  g_return_val_if_fail(ret != NULL, NULL);
126  ret->_instancename = g_strdup(instname);
130  ret->discover = NULL;
131  ret->_timerid = 0;
132  ret->_iosource = iosource;
133  ret->_config = context;
134  REF(ret->_config);
135  return ret;
136 }
137 
138 
145 void
147 {
148  gint timeout;
149  if (NULL == _discovery_timers) {
150  _discovery_timers = g_hash_table_new_full(g_str_hash, g_str_equal
152  assert(_discovery_timers != NULL);
153  }
154  self->discover(self);
155  timeout = self->discoverintervalsecs(self);
156  if (timeout > 0) {
157  self->_timerid = g_timeout_add_seconds(timeout, _discovery_rediscover, self);
158  }
159  REF(self);
160  g_hash_table_replace(_discovery_timers, self->instancename(self), self);
161 }
162 FSTATIC void
163 discovery_unregister(const char* instance)
164 {
165  if (_discovery_timers) {
166  g_hash_table_remove(_discovery_timers, instance);
167  }
168 }
169 
171 void
173 {
174  if (_discovery_timers != NULL) {
175  GHashTable* timers = _discovery_timers;
176  _discovery_timers = NULL;
177  g_hash_table_remove_all(timers);
178  g_hash_table_destroy(timers); timers = NULL;
179  }else{
180  DEBUGMSG1("Discovery timers were NULL");
181  }
182 }
183