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(gconstpointer 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 }