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 {
174  SeqnoFrame* ret;
175  guint16 length = get_generic_tlv_len(tlvstart, pktend);
176  guint16 tlvtype = get_generic_tlv_type(tlvstart, pktend);
177  const guint8* valpos = get_generic_tlv_value(tlvstart, pktend);
178 
179  g_return_val_if_fail(length == (sizeof(guint64)+sizeof(guint16)+sizeof(guint32)), NULL);
180 
181  ret = seqnoframe_new(tlvtype, 0);
182  ret->_sessionid = tlv_get_guint32(valpos, pktend);
183  ret->setreqid(ret, tlv_get_guint64(valpos+sizeof(guint32), pktend));
184  ret->setqid(ret, tlv_get_guint16(valpos+sizeof(guint32)+sizeof(guint64), pktend));
185  return CASTTOCLASS(Frame, ret);
186 }
187 
189 FSTATIC gboolean
191  gconstpointer tlvptr,
192  gconstpointer pktend)
193 {
194  (void)tlvptr;
195  (void)pktend;
196  return self->length == (sizeof(guint32)+sizeof(guint64)+sizeof(guint16));
197 }
198 
199 
201 SeqnoFrame*
202 seqnoframe_new(guint16 frametype,
203  int objsize)
204 {
205  Frame* frame;
206  SeqnoFrame* sframe;
207 
208  if (objsize < ((gssize)sizeof(SeqnoFrame))) {
209  objsize = sizeof(SeqnoFrame);
210  }
211 
212  frame = frame_new(frametype, objsize);
213  sframe = NEWSUBCLASS(SeqnoFrame, frame);
214  if (_sessionId == 0) {
216  }
217  sframe->getreqid = _seqnoframe_getreqid;
218  sframe->setreqid = _seqnoframe_setreqid;
219  sframe->getqid = _seqnoframe_getqid;
220  sframe->setqid = _seqnoframe_setqid;
222  sframe->equal = _seqnoframe_equal;
223  sframe->compare = _seqnoframe_compare;
224 
225  // Base class member functions
229  sframe->baseclass.value = NULL;
230  sframe->baseclass.setvalue = NULL;
231  sframe->baseclass.valuefinalize = NULL;
232  sframe->baseclass.length = sizeof(guint32)+sizeof(guint64)+sizeof(guint16);
233 
234  // Subclass (i.e., SeqnoFrame) data
235  sframe->_sessionid = _sessionId;
236  sframe->_reqid = 0;
237  sframe->_qid = 0;
238  return sframe;
239 }
241 SeqnoFrame*
242 seqnoframe_new_init(guint16 frametype,
243  guint64 reqid,
244  guint16 qid)
245 {
246  SeqnoFrame* ret = seqnoframe_new(frametype, 0);
247  if (ret) {
248  ret->setreqid(ret, reqid);
249  ret->setqid(ret, qid);
250  }
251  return ret;
252 }
253 
254 FSTATIC char *
255 _seqnoframe_toString(gconstpointer vself)
256 {
257  const SeqnoFrame* self = CASTTOCONSTCLASS(SeqnoFrame, vself);
258  return g_strdup_printf("SeqnoFrame(type=%d, (%d,%d,"FMT_64BIT"d))", self->baseclass.type, self->_sessionid
259  , self->_qid, self->_reqid);
260 }