The Assimilation Project  based on Assimilation version 0.5.1441048221
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
seqnoframe.c
Go to the documentation of this file.
1 
23 #include <string.h>
24 #include <projectcommon.h>
25 #include <frameset.h>
26 #include <seqnoframe.h>
27 #include <frametypes.h>
28 #include <generic_tlv_min.h>
29 #include <tlvhelper.h>
30 
31 FSTATIC void _seqnoframe_setreqid(SeqnoFrame * self, guint64 value);
33 FSTATIC void _seqnoframe_setqid(SeqnoFrame * self, guint16 value);
34 FSTATIC guint16 _seqnoframe_getqid(SeqnoFrame * self);
35 FSTATIC void _seqnoframe_updatedata(Frame*, gpointer, gconstpointer, FrameSet*);
36 FSTATIC gboolean _seqnoframe_isvalid(const Frame*, gconstpointer, gconstpointer);
37 FSTATIC gboolean _seqnoframe_equal(SeqnoFrame * self, SeqnoFrame* rhs);
41 FSTATIC char * _seqnoframe_toString(gconstpointer vself);
42 
43 static guint32 _sessionId = 0;
44 
62 
67 
83 FSTATIC void
85 {
86 # define FIVESECONDS 5000000
87  guint64 now = g_get_real_time();
88  now /= FIVESECONDS;
90  _sessionId = (guint32)now;
91 }
92 
94 FSTATIC void
95 _seqnoframe_setreqid(SeqnoFrame * self, guint64 value)
96 {
97  self->_reqid = value;
98 }
99 
101 FSTATIC void
102 _seqnoframe_setqid(SeqnoFrame * self, guint16 value)
103 {
104  self->_qid = value;
105 }
106 
108 FSTATIC guint64
110 {
111  return self->_reqid;
112 }
114 FSTATIC guint16
116 {
117  return self->_qid;
118 }
120 FSTATIC guint32
122 {
123  return self->_sessionid;
124 }
125 
127 FSTATIC gboolean
129 {
130  if (self->_qid != rhs->_qid) {
131  return FALSE;
132  }
133  return self->compare(self, rhs) == 0;
134 }
136 FSTATIC int
138 {
139  if (self->getsessionid(self) == self->getsessionid(rhs)) {
140  if (self->getreqid(self) < rhs->getreqid(rhs)) {
141  return -1;
142  }
143  if (self->getreqid(self) > rhs->getreqid(rhs)) {
144  return 1;
145  }
146  return 0;
147  }
148 
149  if (self->getsessionid(self) < rhs->getsessionid(rhs)) {
150  return -1;
151  }
152  return 1;
153 }
154 
155 
156 
158 FSTATIC void
160  gpointer tlvptr,
161  gconstpointer pktend,
162  FrameSet* fs)
163 {
164  SeqnoFrame* self = CASTTOCLASS(SeqnoFrame, fself);
165  // NOTE - this gets rid of the "const" coming out of get_generic_tlv_value...
167  guint8* pktpos = get_generic_tlv_nonconst_value(tlvptr, pktend);
168  (void)fs;
169  g_return_if_fail(NULL != pktpos);
170 
171  tlv_set_guint32(pktpos, self->_sessionid, pktend);
172  tlv_set_guint64(pktpos+sizeof(guint32), self->_reqid, pktend);
173  tlv_set_guint16(pktpos+sizeof(guint32)+sizeof(guint64),self->_qid, pktend);
174 }
176 Frame*
177 seqnoframe_tlvconstructor(gpointer tlvstart,
178  gconstpointer pktend,
179  gpointer* ignorednewpkt,
180  gpointer* ignoredpktend)
181 {
182  SeqnoFrame* ret;
183  guint16 length = get_generic_tlv_len(tlvstart, pktend);
184  guint16 tlvtype = get_generic_tlv_type(tlvstart, pktend);
185  const guint8* valpos = get_generic_tlv_value(tlvstart, pktend);
186 
187  (void)ignorednewpkt; (void)ignoredpktend;
188  g_return_val_if_fail(length == (sizeof(guint64)+sizeof(guint16)+sizeof(guint32)), NULL);
189 
190  ret = seqnoframe_new(tlvtype, 0);
191  ret->_sessionid = tlv_get_guint32(valpos, pktend);
192  ret->setreqid(ret, tlv_get_guint64(valpos+sizeof(guint32), pktend));
193  ret->setqid(ret, tlv_get_guint16(valpos+sizeof(guint32)+sizeof(guint64), pktend));
194  return CASTTOCLASS(Frame, ret);
195 }
196 
198 FSTATIC gboolean
200  gconstpointer tlvptr,
201  gconstpointer pktend)
202 {
203  (void)tlvptr;
204  (void)pktend;
205  return self->length == (sizeof(guint32)+sizeof(guint64)+sizeof(guint16));
206 }
207 
208 
210 SeqnoFrame*
211 seqnoframe_new(guint16 frametype,
212  int objsize)
213 {
214  Frame* frame;
215  SeqnoFrame* sframe;
216 
217  if (objsize < ((gssize)sizeof(SeqnoFrame))) {
218  objsize = sizeof(SeqnoFrame);
219  }
220 
221  frame = frame_new(frametype, objsize);
222  sframe = NEWSUBCLASS(SeqnoFrame, frame);
223  if (_sessionId == 0) {
225  }
226  sframe->getreqid = _seqnoframe_getreqid;
227  sframe->setreqid = _seqnoframe_setreqid;
228  sframe->getqid = _seqnoframe_getqid;
229  sframe->setqid = _seqnoframe_setqid;
231  sframe->equal = _seqnoframe_equal;
232  sframe->compare = _seqnoframe_compare;
233 
234  // Base class member functions
238  sframe->baseclass.value = NULL;
239  sframe->baseclass.setvalue = NULL;
240  sframe->baseclass.valuefinalize = NULL;
241  sframe->baseclass.length = sizeof(guint32)+sizeof(guint64)+sizeof(guint16);
242 
243  // Subclass (i.e., SeqnoFrame) data
244  sframe->_sessionid = _sessionId;
245  sframe->_reqid = 0;
246  sframe->_qid = 0;
247  return sframe;
248 }
250 SeqnoFrame*
251 seqnoframe_new_init(guint16 frametype,
252  guint64 reqid,
253  guint16 qid)
254 {
255  SeqnoFrame* ret = seqnoframe_new(frametype, 0);
256  if (ret) {
257  ret->setreqid(ret, reqid);
258  ret->setqid(ret, qid);
259  }
260  return ret;
261 }
262 
263 FSTATIC char *
264 _seqnoframe_toString(gconstpointer vself)
265 {
266  const SeqnoFrame* self = CASTTOCONSTCLASS(SeqnoFrame, vself);
267  return g_strdup_printf("SeqnoFrame(type=%d, (%d,%d,"FMT_64BIT"d))", self->baseclass.type, self->_sessionid
268  , self->_qid, self->_reqid);
269 }
guint32 _sessionid
value of this session id
Definition: seqnoframe.h:53
guint32 length
Frame Length.
Definition: frame.h:46
#define FIVESECONDS
int(* equal)(SeqnoFrame *self, SeqnoFrame *rhs)
Equal compare two SeqnoFrames including qid.
Definition: seqnoframe.h:50
SeqnoFrame * seqnoframe_new(guint16 frametype, int objsize)
Construct new SeqnoFrame object.
Definition: seqnoframe.c:211
GDestroyNotify valuefinalize
method for finalizing value
Definition: frame.h:57
SeqnoFrame * seqnoframe_new_init(guint16 frametype, guint64 reqid, guint16 qid)
Construct a fully-initialized SeqnoFrame object.
Definition: seqnoframe.c:251
AssimObj baseclass
Base object class for our Class system.
Definition: frame.h:44
Implements minimal client-oriented Frame and Frameset capabilities.
guint64 tlv_get_guint64(const void *vitem, const void *bufend)
Retrieve an unsigned 64 bit integer from the given location with error checking and without caring ab...
Definition: tlvhelper.c:108
This is the base Frame class object (in-memory TLV (type, length, value)) for every general component...
Definition: frame.h:43
WINEXPORT gint64 g_get_real_time(void)
HAVE_G_GET_MONOTONIC_TIME.
#define FSTATIC
Definition: projectcommon.h:31
guint16 _qid
value of this queue id
Definition: seqnoframe.h:54
gpointer value
Frame Value (pointer)
Definition: frame.h:47
FSTATIC void _seqnoframe_setreqid(SeqnoFrame *self, guint64 value)
Set the request id value associated with a SeqnoFrame.
Definition: seqnoframe.c:95
FSTATIC void _seqnoframe_updatedata(Frame *, gpointer, gconstpointer, FrameSet *)
Update packet data from the frame.
Definition: seqnoframe.c:159
void tlv_set_guint16(void *vitem, guint16 item, const void *bufend)
Set an unsigned 16 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:67
guint16 get_generic_tlv_type(gconstpointer tlv_vp, gconstpointer pktend)
Return the 'Type' of the given TLV TLV entry (first two bytes)
FSTATIC guint64 _seqnoframe_getreqid(SeqnoFrame *self)
Get the request id associated with a SeqnoFrame.
Definition: seqnoframe.c:109
guint64 _reqid
value of this request id
Definition: seqnoframe.h:52
FSTATIC guint32 _seqnoframe_getsessionid(SeqnoFrame *self)
Get the session id associated with a SeqnoFrame.
Definition: seqnoframe.c:121
FSTATIC gboolean _seqnoframe_isvalid(const Frame *, gconstpointer, gconstpointer)
Return TRUE if this sequence number is valid - if it's the right size.
Definition: seqnoframe.c:199
guint32 tlv_get_guint32(const void *vitem, const void *bufend)
Retrieve an unsigned 32 bit integer from the given location with error checking and without caring ab...
Definition: tlvhelper.c:81
struct _SeqnoFrame SeqnoFrame
Definition: seqnoframe.h:35
Frame * frame_new(guint16 frame_type, gsize framesize)
Construct a new frame - allowing for "derived" frame types...
Definition: frame.c:125
gboolean(* isvalid)(const Frame *self, gconstpointer tlvptr, gconstpointer pktend)
TRUE if TLV data looks valid...
Definition: frame.h:50
#define FMT_64BIT
Format designator for a 64 bit integer.
Definition: projectcommon.h:32
guint16(* getqid)(SeqnoFrame *self)
get value of queue id
Definition: seqnoframe.h:47
Frame * seqnoframe_tlvconstructor(gpointer tlvstart, gconstpointer pktend, gpointer *ignorednewpkt, gpointer *ignoredpktend)
Construct Frame (SeqnoFrame) object from marshalled packet data.
Definition: seqnoframe.c:177
void(* setqid)(SeqnoFrame *self, guint16 value)
set the queue id
Definition: seqnoframe.h:48
#define CASTTOCONSTCLASS(Cclass, obj)
Safely cast 'obj' to const C-class 'class' - verifying that it was registered as being of type class ...
Definition: proj_classes.h:71
guint32 get_generic_tlv_len(gconstpointer tlv_vp, gconstpointer pktend)
Return the 'Length' of the given generic TLV entry (first 3 bytes after type) Note: return result is ...
Project common header file.
Header file defining the data layouts for our Frames.
TLV helper interfaces definitions.
FSTATIC void _seqnoframe_setqid(SeqnoFrame *self, guint16 value)
Set the Queue id value associated with a SeqnoFrame.
Definition: seqnoframe.c:102
guint64(* getreqid)(SeqnoFrame *self)
get value of request id
Definition: seqnoframe.h:45
Frame baseclass
base Frame class object
Definition: seqnoframe.h:44
void tlv_set_guint32(void *vitem, guint32 item, const void *bufend)
Set an unsigned 32 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:93
Provides definitions for using our generic TLV capabilities.
gconstpointer get_generic_tlv_value(gconstpointer tlv_vp, gconstpointer pktend)
Return a const pointer to the 'Value' of the given generic TLV entry.
FSTATIC void _seqnoframe_initsessionid(void)
Initialize our session ID to something monotonically increasing.
Definition: seqnoframe.c:84
FSTATIC guint16 _seqnoframe_getqid(SeqnoFrame *self)
Get the queue id associated with a SeqnoFrame.
Definition: seqnoframe.c:115
void(* updatedata)(Frame *self, gpointer tlvptr, gconstpointer pktend, FrameSet *fs)
Update packet data.
Definition: frame.h:49
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
gpointer get_generic_tlv_nonconst_value(gpointer tlv_vp, gconstpointer pktend)
Return a non-const (mutable) pointer to the 'Value' of the given generic TLV entry.
FSTATIC gboolean _seqnoframe_equal(SeqnoFrame *self, SeqnoFrame *rhs)
Compare two SeqnoFrames for equality including the queue id.
Definition: seqnoframe.c:128
Implements the SeqnoFrame class.
void(* setreqid)(SeqnoFrame *self, guint64 value)
set the request id
Definition: seqnoframe.h:46
void(* setvalue)(Frame *self, gpointer value, guint32 length, GDestroyNotify valfinal)
member function for setting value
Definition: frame.h:52
guint16 tlv_get_guint16(const void *vitem, const void *bufend)
Retrieve an unsigned 16 bit integer from the given location with error checking and without caring ab...
Definition: tlvhelper.c:55
void tlv_set_guint64(void *vitem, guint64 item, const void *bufend)
Set an unsigned 64 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:120
#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
FSTATIC int _seqnoframe_compare(SeqnoFrame *self, SeqnoFrame *rhs)
Compare two SeqnoFrames - ignoring the queue id.
Definition: seqnoframe.c:137
#define NEWSUBCLASS(Cclass, obj)
Definition: proj_classes.h:67
This is an SeqnoFrame class TLV (type, length, value) frame.
Definition: seqnoframe.h:43
int(* compare)(SeqnoFrame *self, SeqnoFrame *rhs)
Compare two SeqnoFrames: -1, 0, +1.
Definition: seqnoframe.h:51
FSTATIC char * _seqnoframe_toString(gconstpointer vself)
Definition: seqnoframe.c:264
guint32(* getsessionid)(SeqnoFrame *self)
get value of session id
Definition: seqnoframe.h:49