The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups 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  DEBUGMSG3("Sender %p timeout source is: %d, interval is %d", newsender
151  , newsender->timeout_source, interval);
152  _hbsender_addlist(newsender);
153  // Avoid Martian packets - don't send the first one right away...
154  // _hbsender_sendheartbeat(newsender);
155  }
156  return newsender;
157 }
158 
159 
161 void
163 {
164  GSList* obj;
165  for (obj = _hb_senders; obj != NULL; obj=obj->next) {
166  HbSender* sender = CASTTOCLASS(HbSender, obj->data);
167  if (sendaddr->equal(sendaddr, sender->_sendaddr)) {
168  sender->unref(sender);
169  return;
170  }
171  }
172 }
173 FSTATIC void
175 {
177  //g_debug("Sending a heartbeat...");
178  self->_outmethod->sendaframeset(self->_outmethod, self->_sendaddr, heartbeat);
179  UNREF(heartbeat);
180 }
181 void
183 {
184  while (_hb_senders) {
185  HbSender* sender = CASTTOCLASS(HbSender, _hb_senders->data);
186  sender->unref(sender);
187  }
188 }
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
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 DEBUGMSG3(...)
Definition: proj_classes.h:91
#define FREECLASSOBJ(obj)
Free a C-class object.
Definition: proj_classes.h:76
void hbsender_stopallsenders(void)
Definition: hbsender.c:182
#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
Defines Heartbeat Sender interfaces.
#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:162
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)
Definition: hbsender.c:174
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
#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