The Assimilation Project  based on Assimilation version 0.5.1427809010
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
hbsender.c
Go to the documentation of this file.
1 
23 #include <memory.h>
24 #include <glib.h>
25 #include <frameset.h>
26 #include <frame.h>
27 #include <hbsender.h>
30 FSTATIC void _hbsender_notify_function(gpointer data);
32 FSTATIC void _hbsender_ref(HbSender * self);
33 FSTATIC void _hbsender_unref(HbSender * self);
37 FSTATIC gboolean _hbsender_gsourcefunc(gpointer);
38 
40 
45 
46 static GSList* _hb_senders = NULL;
47 static gint _hb_sender_count = 0;
48 
49 #define ONESEC 1000000
50 
52 FSTATIC void
54 {
55  _hb_senders = g_slist_prepend(_hb_senders, self);
56  _hb_sender_count += 1;
57 }
58 
60 FSTATIC void
62 {
63  if (g_slist_find(_hb_senders, self) != NULL) {
64  _hb_senders = g_slist_remove(_hb_senders, self);
65  _hb_sender_count -= 1;
66  return;
67  }
68  g_warn_if_reached();
69 }
70 
72 FSTATIC gboolean
73 _hbsender_gsourcefunc(gpointer gself)
74 {
75  HbSender* self = CASTTOCLASS(HbSender, gself);
77  return TRUE;
78 }
79 
80 
82 FSTATIC void
84 {
85  self->_refcount += 1;
86 }
87 
89 FSTATIC void
91 {
92  g_return_if_fail(self->_refcount > 0);
93  self->_refcount -= 1;
94  if (self->_refcount == 0) {
95  _hbsender_dellist(self);
96  self->_finalize(self);
97  self = NULL;
98  }
99 }
100 // Callback function from the GSource world - notifying us when we're getting shut down from their end
101 FSTATIC void
103 {
104  HbSender* self = CASTTOCLASS(HbSender, data);
105  self->timeout_source = 0;
106 }
107 
109 FSTATIC void
111 {
112  if (self->_sendaddr) {
113  UNREF(self->_sendaddr);
114  }
115  if (self->timeout_source != 0) {
116  g_source_remove(self->timeout_source);
117  self->timeout_source = 0;
118  }
119  memset(self, 0x00, sizeof(*self));
120  FREECLASSOBJ(self);
121 }
122 
123 
126 HbSender*
128  NetGSource* outmethod,
129  guint interval,
130  gsize objsize)
131 {
132  HbSender * newsender;
134  if (objsize < sizeof(HbSender)) {
135  objsize = sizeof(HbSender);
136  }
137  newsender = MALLOCCLASS(HbSender, objsize);
138  if (newsender != NULL) {
139  newsender->_sendaddr = sendaddr;
140  REF(sendaddr);
141  newsender->_refcount = 1;
142  newsender->ref = _hbsender_ref;
143  newsender->_outmethod = outmethod;
144  newsender->unref = _hbsender_unref;
145  newsender->_finalize = _hbsender_finalize;
146  newsender->_expected_interval = interval;
147  newsender->timeout_source = g_timeout_add_seconds_full
148  (G_PRIORITY_HIGH, interval, _hbsender_gsourcefunc
149  , newsender, _hbsender_notify_function);
150  if (DEBUG) {
151  char * addrstr = sendaddr->baseclass.toString(&sendaddr->baseclass);
152  g_debug("%s.%d: Start sending heartbeats to %s at interval "FMT_64BIT"d"
153  , __FUNCTION__, __LINE__, addrstr, newsender->_expected_interval);
154  FREE(addrstr);
155  }
156  _hbsender_addlist(newsender);
157  _hbsender_sendheartbeat(newsender);
158  }
159  return newsender;
160 }
161 
162 
164 void
166 {
167  GSList* obj;
168  if (DEBUG) {
169  char * addrstr = sendaddr->baseclass.toString(sendaddr);
170  g_debug("%s.%d: Stop sending heartbeats to %s"
171  , __FUNCTION__, __LINE__, addrstr);
172  FREE(addrstr);
173  }
174  for (obj = _hb_senders; obj != NULL; obj=obj->next) {
175  HbSender* sender = CASTTOCLASS(HbSender, obj->data);
176  if (sendaddr->equal(sendaddr, sender->_sendaddr)) {
177  // Send one last heartbeat - avoid unnecessary death reports
178  _hbsender_sendheartbeat(sender);
179  sender->unref(sender);
180  return;
181  }
182  }
183 }
185 FSTATIC void
187 {
189  //g_debug("Sending a heartbeat...");
190  if (DEBUG >= 4) {
191  char * addrstr = self->_sendaddr->baseclass.toString(self->_sendaddr);
192  g_debug("%s.%d: Sending heartbeat to %s at interval "FMT_64BIT"d"
193  , __FUNCTION__, __LINE__, addrstr, self->_expected_interval);
194  FREE(addrstr);
195  }
196  self->_outmethod->sendaframeset(self->_outmethod, self->_sendaddr, heartbeat);
197  UNREF(heartbeat);
198 }
200 void
202 {
203  while (_hb_senders) {
204  HbSender* sender = CASTTOCLASS(HbSender, _hb_senders->data);
205  sender->unref(sender);
206  }
207 }
FSTATIC gboolean _hbsender_gsourcefunc(gpointer)
A GSourceFunc to be used with g_timeout_add_seconds()
Definition: hbsender.c:73
#define MALLOCCLASS(Cclass, size)
Allocate memory for an object (which might be further subclassed) - and register it with our C-Class ...
Definition: proj_classes.h:61
This is the base HbSender class. object - which sends heartbeats to particular listeners.
Definition: hbsender.h:39
struct _HbSender HbSender
Definition: hbsender.h:32
void(* unref)(HbSender *)
Decrement reference count.
Definition: hbsender.h:41
guint64 _expected_interval
How often to expect heartbeats.
Definition: hbsender.h:43
#define FRAMESETTYPE_HEARTBEAT
A heartbeat packet.
Definition: framesettypes.h:32
void(* _finalize)(HbSender *)
HbSender destructor.
Definition: hbsender.h:42
NetAddr * _sendaddr
What address are we sending to?
Definition: hbsender.h:45
HbSender * hbsender_new(NetAddr *sendaddr, NetGSource *outmethod, guint interval, gsize objsize)
Construct a new HbSender - setting up timeout data structures for it.
Definition: hbsender.c:127
Implements minimal client-oriented Frame and Frameset capabilities.
FSTATIC void _hbsender_ref(HbSender *self)
Increment the reference count by one.
Definition: hbsender.c:83
AssimObj baseclass
Definition: netaddr.h:44
FSTATIC void _hbsender_notify_function(gpointer data)
Definition: hbsender.c:102
FrameSet * frameset_new(guint16 frameset_type)
Construct a new frameset of the given type.
Definition: frameset.c:110
FSTATIC void _hbsender_unref(HbSender *self)
Decrement the reference count by one - possibly freeing up the object.
Definition: hbsender.c:90
#define FSTATIC
Definition: projectcommon.h:31
#define FREECLASSOBJ(obj)
Free a C-class object.
Definition: proj_classes.h:76
void hbsender_stopallsenders(void)
Stop sending heartbeats to anyone...
Definition: hbsender.c:201
#define BINDDEBUG(Cclass)
BINDDEBUG is for telling the class system where the debug variable for this class is - put it in the ...
Definition: proj_classes.h:82
FSTATIC void _hbsender_finalize(HbSender *self)
Finalize an HbSender.
Definition: hbsender.c:110
#define __FUNCTION__
#define DEBUG
Definition: proj_classes.h:85
Defines Heartbeat Sender interfaces.
#define FMT_64BIT
Format designator for a 64 bit integer.
Definition: projectcommon.h:32
#define FREE(m)
Our interface to free.
Definition: projectcommon.h:29
#define REF(obj)
Definition: assimobj.h:39
Implements basic Frame class.
void hbsender_stopsend(NetAddr *sendaddr)
Stop sending heartbeats to a particular address.
Definition: hbsender.c:165
NetGSource * _outmethod
How to send out heartbeats.
Definition: hbsender.h:44
gboolean(* equal)(const NetAddr *, const NetAddr *)
Compare NetAddrs.
Definition: netaddr.h:53
The NetAddr class class represents a general network address - whether IP, MAC, or any other type of ...
Definition: netaddr.h:43
guint timeout_source
timeout source id
Definition: hbsender.h:47
FSTATIC void _hbsender_sendheartbeat(HbSender *self)
Send out a heartbeat.
Definition: hbsender.c:186
int _refcount
Current reference count.
Definition: hbsender.h:46
FrameSet class - used for collecting Frames when not on the wire, and for marshalling/demarshalling t...
Definition: frameset.h:46
gchar *(* toString)(gconstpointer)
Produce malloc-ed string representation.
Definition: assimobj.h:58
#define DEBUGDECLARATIONS
Definition: proj_classes.h:79
FSTATIC void _hbsender_addlist(HbSender *self)
Add an HbSender to our global list of HbSenders.
Definition: hbsender.c:53
void(* ref)(HbSender *)
Increment reference count.
Definition: hbsender.h:40
FSTATIC void _hbsender_dellist(HbSender *self)
Remove an HbSender from our global list of HbSenders.
Definition: hbsender.c:61
#define CASTTOCLASS(Cclass, obj)
Safely cast 'obj' to C-class 'class' - verifying that it was registerd as being of type class ...
Definition: proj_classes.h:66
#define UNREF(obj)
Definition: assimobj.h:35
The NetGSource class objects integrate NetIO class objects into the g_main_loop paradigm.
Definition: netgsource.h:43