The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups 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 
77 FSTATIC void
79 {
80 # define FIVESECONDS 5000000
81  guint64 now = g_get_real_time();
82  now /= FIVESECONDS;
84  _sessionId = (guint32)now;
85 }
86 
88 FSTATIC void
89 _seqnoframe_setreqid(SeqnoFrame * self, guint64 value)
90 {
91  self->_reqid = value;
92 }
93 
95 FSTATIC void
96 _seqnoframe_setqid(SeqnoFrame * self, guint16 value)
97 {
98  self->_qid = value;
99 }
100 
102 FSTATIC guint64
104 {
105  return self->_reqid;
106 }
108 FSTATIC guint16
110 {
111  return self->_qid;
112 }
114 FSTATIC guint32
116 {
117  return self->_sessionid;
118 }
119 
121 FSTATIC gboolean
123 {
124  if (self->_qid != rhs->_qid) {
125  return FALSE;
126  }
127  return self->compare(self, rhs) == 0;
128 }
130 FSTATIC int
132 {
133  if (self->getsessionid(self) == self->getsessionid(rhs)) {
134  if (self->getreqid(self) < rhs->getreqid(rhs)) {
135  return -1;
136  }
137  if (self->getreqid(self) > rhs->getreqid(rhs)) {
138  return 1;
139  }
140  return 0;
141  }
142 
143  if (self->getsessionid(self) < rhs->getsessionid(rhs)) {
144  return -1;
145  }
146  return 1;
147 }
148 
149 
150 
152 FSTATIC void
154  gpointer tlvptr,
155  gconstpointer pktend,
156  FrameSet* fs)
157 {
158  SeqnoFrame* self = CASTTOCLASS(SeqnoFrame, fself);
159  // NOTE - this gets rid of the "const" coming out of get_generic_tlv_value...
161  guint8* pktpos = get_generic_tlv_nonconst_value(tlvptr, pktend);
162  (void)fs;
163  g_return_if_fail(NULL != pktpos);
164 
165  tlv_set_guint32(pktpos, self->_sessionid, pktend);
166  tlv_set_guint64(pktpos+sizeof(guint32), self->_reqid, pktend);
167  tlv_set_guint16(pktpos+sizeof(guint32)+sizeof(guint64),self->_qid, pktend);
168 }
170 Frame*
171 seqnoframe_tlvconstructor(gpointer tlvstart,
172  gconstpointer pktend,
173  gpointer* ignorednewpkt,
174  gpointer* ignoredpktend)
175 {
176  SeqnoFrame* ret;
177  guint16 length = get_generic_tlv_len(tlvstart, pktend);
178  guint16 tlvtype = get_generic_tlv_type(tlvstart, pktend);
179  const guint8* valpos = get_generic_tlv_value(tlvstart, pktend);
180 
181  (void)ignorednewpkt; (void)ignoredpktend;
182  g_return_val_if_fail(length == (sizeof(guint64)+sizeof(guint16)+sizeof(guint32)), NULL);
183 
184  ret = seqnoframe_new(tlvtype, 0);
185  ret->_sessionid = tlv_get_guint32(valpos, pktend);
186  ret->setreqid(ret, tlv_get_guint64(valpos+sizeof(guint32), pktend));
187  ret->setqid(ret, tlv_get_guint16(valpos+sizeof(guint32)+sizeof(guint64), pktend));
188  return CASTTOCLASS(Frame, ret);
189 }
190 
192 FSTATIC gboolean
194  gconstpointer tlvptr,
195  gconstpointer pktend)
196 {
197  (void)tlvptr;
198  (void)pktend;
199  return self->length == (sizeof(guint32)+sizeof(guint64)+sizeof(guint16));
200 }
201 
202 
204 SeqnoFrame*
205 seqnoframe_new(guint16 frametype,
206  int objsize)
207 {
208  Frame* frame;
209  SeqnoFrame* sframe;
210 
211  if (objsize < ((gssize)sizeof(SeqnoFrame))) {
212  objsize = sizeof(SeqnoFrame);
213  }
214 
215  frame = frame_new(frametype, objsize);
216  sframe = NEWSUBCLASS(SeqnoFrame, frame);
217  if (_sessionId == 0) {
219  }
220  sframe->getreqid = _seqnoframe_getreqid;
221  sframe->setreqid = _seqnoframe_setreqid;
222  sframe->getqid = _seqnoframe_getqid;
223  sframe->setqid = _seqnoframe_setqid;
225  sframe->equal = _seqnoframe_equal;
226  sframe->compare = _seqnoframe_compare;
227 
228  // Base class member functions
232  sframe->baseclass.value = NULL;
233  sframe->baseclass.setvalue = NULL;
234  sframe->baseclass.valuefinalize = NULL;
235  sframe->baseclass.length = sizeof(guint32)+sizeof(guint64)+sizeof(guint16);
236 
237  // Subclass (i.e., SeqnoFrame) data
238  sframe->_sessionid = _sessionId;
239  sframe->_reqid = 0;
240  sframe->_qid = 0;
241  return sframe;
242 }
244 SeqnoFrame*
245 seqnoframe_new_init(guint16 frametype,
246  guint64 reqid,
247  guint16 qid)
248 {
249  SeqnoFrame* ret = seqnoframe_new(frametype, 0);
250  if (ret) {
251  ret->setreqid(ret, reqid);
252  ret->setqid(ret, qid);
253  }
254  return ret;
255 }
256 
257 FSTATIC char *
258 _seqnoframe_toString(gconstpointer vself)
259 {
260  const SeqnoFrame* self = CASTTOCONSTCLASS(SeqnoFrame, vself);
261  return g_strdup_printf("SeqnoFrame(type=%d, (%d,%d,"FMT_64BIT"d))", self->baseclass.type, self->_sessionid
262  , self->_qid, self->_reqid);
263 }
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:205
GDestroyNotify valuefinalize
method for finalizing value
Definition: frame.h:57
SeqnoFrame * seqnoframe_new_init(guint16 frametype, guint64 reqid, guint16 qid)
Construct a fully-iniitialized SeqnoFrame object.
Definition: seqnoframe.c:245
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:107
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:89
FSTATIC void _seqnoframe_updatedata(Frame *, gpointer, gconstpointer, FrameSet *)
Update packet data from the frame.
Definition: seqnoframe.c:153
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:68
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:103
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:115
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:193
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:124
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:171
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)
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:96
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:78
FSTATIC guint16 _seqnoframe_getqid(SeqnoFrame *self)
Get the queue id associated with a SeqnoFrame.
Definition: seqnoframe.c:109
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:122
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:53
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:119
#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:131
#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:258
guint32(* getsessionid)(SeqnoFrame *self)
get value of session id
Definition: seqnoframe.h:49