libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moEventList.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moEventList.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28 
29 
30 *******************************************************************************/
31 
32 #include "moActions.h"
33 #include "moMoldeoObject.h"
34 #include "moEventList.h"
35 
36 #include "moArray.h"
37 moDefineDynamicArray( moEventPacketArray );
38 moDefineDynamicArray( moMessages );
39 
40 //MESSAGE
42  deviceid = 0;
43  devicecode = 0;
44 
46 
47  m_MoldeoIdDest = 0;
48  m_InletIdDest = -1;
50  m_NameDest = "";
51 
52  m_MoldeoIdSrc = 0;
53  m_OutletIdSrc = -1;
55  m_NameSrc = "";
56 
57  m_Data.SetText("");
58 }
59 
60 moMessage::moMessage( MOint p_MoldeoIdDest, MOint p_MoldeoIdSrc, const moData& data ) {
61 
62  deviceid = p_MoldeoIdDest;
63  devicecode = 0;
65 
66  m_MoldeoIdDest = p_MoldeoIdDest;
67  m_MoldeoIdSrc = p_MoldeoIdSrc;
68 
69  m_Data = data;
70 
71 }
72 
73 moMessage::moMessage( MOint p_MoldeoIdDest, MOint p_InletIdDest, MOint p_MoldeoIdSrc, const moData& data ) {
74 
75  deviceid = p_MoldeoIdDest;
76  devicecode = 0;
78 
79  m_MoldeoIdDest = p_MoldeoIdDest;
80  m_InletIdDest = p_InletIdDest;
81  m_MoldeoIdSrc = p_MoldeoIdSrc;
82 
83  m_Data = data;
84 
85 }
86 
87 moMessage::moMessage( MOint p_MoldeoIdDest, MOint p_InletIdDest, moMoldeoObjectType p_TypeDest, MOint p_MoldeoIdSrc, moMoldeoObjectType p_TypeSrc, const moData& p_data ) {
88  deviceid = p_MoldeoIdDest;
89  devicecode = 0;
91 
92  m_MoldeoIdDest = p_MoldeoIdDest;
93  m_InletIdDest = p_InletIdDest;
94  m_TypeDest = p_TypeDest;
95 
96  m_MoldeoIdSrc = p_MoldeoIdSrc;
97  m_TypeSrc = p_TypeSrc;
98 
99  m_Data = p_data;
100 }
101 
102 moMessage::moMessage( MOint p_MoldeoIdDest, MOint p_InletIdDest, moMoldeoObjectType p_TypeDest, moText p_NameDest, MOint p_MoldeoIdSrc, moMoldeoObjectType p_TypeSrc, moText p_NameSrc, const moData& p_data ) {
103 
104  deviceid = p_MoldeoIdDest;
105  devicecode = 0;
107 
108  m_MoldeoIdDest = p_MoldeoIdDest;
109  m_TypeDest = p_TypeDest;
110  m_InletIdDest = p_InletIdDest;
111  m_NameDest = p_NameDest;
112 
113  m_MoldeoIdSrc = p_MoldeoIdSrc;
114  m_TypeSrc = p_TypeSrc;
115  m_NameSrc = p_NameSrc;
116 
117  m_Data = p_data;
118 }
119 
121 //skip
122 }
123 
124 
125 
126 
127 moMessage&
129 
130  deviceid = src.deviceid;
131  devicecode = src.devicecode;
136  pointer = src.pointer;
137  m_Data = src.m_Data;
140  m_NameDest = src.m_NameDest;
141  m_NameSrc = src.m_NameSrc;
143  m_TypeDest = src.m_TypeDest;
144  m_TypeSrc = src.m_TypeSrc;
145  return (*this);
146 }
147 
148 
150 
151  moText evtJSON = moEvent::ToJSON();
152  JSON = "{";
153  JSON+= "'src': '"+m_NameSrc+"',";
154  JSON+= "'out': '"+IntToStr(m_OutletIdSrc)+"',";
155  JSON+= "'dest': '"+m_NameDest+"',";
156  JSON+= "'in': '"+IntToStr(m_InletIdDest)+"',";
157  JSON+= "'event': '"+evtJSON+"'";
158  JSON+= "}";
159  return JSON;
160 }
161 
162 
163 
164 
165 
166 
167 
168 
169 
173  previous = NULL;
174  next = NULL;
175  deviceid = -1;//indefinido
176  devicecode = 0;
177  reservedvalue0 = 0;
178  reservedvalue1 = 0;
179  reservedvalue2 = 0;
180  reservedvalue3 = 0;
181 }
182 
183 moEvent::moEvent(MOint did, MOint cod, MOint val0, MOint val1, MOint val2, MOint val3, MOpointer ptr) {
184  deviceid = did;
185  devicecode = cod;
186  reservedvalue0 = val0;
187  reservedvalue1 = val1;
188  reservedvalue2 = val2;
189  reservedvalue3 = val3;
190  pointer = ptr;
191 
192  next = NULL;
193  previous = NULL;
194 }
195 
196 moEvent::moEvent(MOint did, MOint cod, MOint val0, MOpointer ptr ) {
197  deviceid = did;
198  devicecode = cod;
199  reservedvalue0 = val0;
201  pointer = ptr;
202 }
203 
205 //skip
206 }
227 
228  JSON = "{";
229  switch(deviceid) {
230  case MO_IODEVICE_CONSOLE:
231  JSON+= "'MO_IODEVICE_CONSOLE',";
232  break;
233  default:
234  JSON+= "'"+IntToStr(deviceid)+"',";
235  break;
236  };
237 
239  JSON+= "'MO_ACTION_MOLDEOAPI_EVENT_SEND',";
241  JSON+= "'MO_ACTION_MOLDEOAPI_EVENT_RECEIVE',";
242  } else JSON+= "'"+IntToStr(devicecode)+"',";
243 
244  JSON+= "'"+IntToStr(reservedvalue0)+"',";
245  JSON+= "'"+IntToStr(reservedvalue1)+"',";
246  JSON+= "'"+IntToStr(reservedvalue2)+"',";
247 
248  if (reservedvalue3==MO_MESSAGE) {
249  JSON+= "'MO_MESSAGE',";
250  } else if (reservedvalue3==MO_DATAMESSAGE) {
251  JSON+= "'MO_DATAMESSAGE',";
252  } else JSON+= "'"+IntToStr(reservedvalue3)+"',";
253 
254  if (pointer) JSON+= "'"+IntToStr((long)pointer)+"'";
255 
256  JSON+= "}";
257  return JSON;
258 }
259 
260 
261 
262 
263 
264 
265 //EVENTOS
266 
268  First = NULL;
269  Last = NULL;
270 }
271 
273  Finish();
274 }
275 
276 
277 
278 /*
279 void First() {
280  Current = First;
281 }
282 
283 void Last() {
284  Current = Last;
285 }
286 
287 void Next() {
288  if(Current!=NULL) Current = Current->next;
289 }
290 
291 void Anterior() {
292  if(Current!=NULL) Current = Current->previous;
293 }
294 
295 moEvent* Current() {
296  return Current;
297 }
298 
299 moEvent* DeleteCurrent() {
300  if(First)
301 }
302 */
303 void moEventList::Add( moMessage* p_Message ) {
304  //cout << "moEventList::Add( moMessage* p_Message )" << endl;
305  m_lock.Lock();
306  moEvent* p_Event = (moEvent*)p_Message;
307  if(First==NULL) {//lista vacia
308  First = p_Event;
309  Last = First;
310  First->previous = NULL;
311  First->next = NULL;
312  } else {//no vacia, lo ponemos al final
313  Last->next = p_Event;
314  if(Last->next != NULL) {
315  Last->next->previous = Last;
316  Last->next->next = NULL;
317  Last = Last->next;
318  }
319  }
320  m_lock.Unlock();
321 
322 }
323 
324 
325 void moEventList::Add( moEvent* p_Event ) {
326  //cout << "moEventList::Add( moEvent* p_Event )" << endl;
327  m_lock.Lock();
328  //cout << "moEventList::Add Locked" << endl;
329  //MODebug2->Message( " Event List :: added event " );
330  if(First==NULL) {//lista vacia
331  //cout << "moEventList::Add -> empty list" << endl;
332  First = p_Event;
333  Last = First;
334  First->previous = NULL;
335  First->next = NULL;
336  } else if (Last!=NULL) {//no vacia, lo ponemos al final
337  //cout << "moEventList::Add -> NOt empty -> add to last event: " << Last << endl;
338  Last->next = p_Event;
339  //cout << "moEventList::Add -> Last->next set to " << p_Event << endl;
340  if(Last->next != NULL) {
341  Last->next->previous = Last;
342  Last->next->next = NULL;
343  Last = Last->next;
344  }
345  }// else cout << "moEventList::Add -> Invalid values for First and Last values: " << "First is: " << First << " Last is: " << Last << endl;
346  //cout << "moEventList::Add Unlocking" << endl;
347  m_lock.Unlock();
348  //cout << "moEventList::Add Unlocked" << endl;
349 
350 }
351 
352 //revisado
353 void
354 moEventList::Add(MOint did, MOint cod, MOint val0, MOint val1, MOint val2, MOint val3, MOpointer ptr) {
355  //cout << "moEventList::Add(MOint did, MOint cod, MOint val0, MOint val1, MOint val2, MOint val3, MOpointer ptr)" << endl;
356  m_lock.Lock();
357  if(First==NULL) {//lista vacia
358  First = new moEvent(did,cod,val0,val1,val2,val3,ptr);
359  Last = First;
360  First->previous = NULL;
361  First->next = NULL;
362  } else {//no vacia, lo ponemos al final
363  Last->next = new moEvent(did,cod,val0,val1,val2,val3,ptr);
364  if(Last->next != NULL) {
365  Last->next->previous = Last;
366  Last->next->next = NULL;
367  Last = Last->next;
368  }
369  }
370  m_lock.Unlock();
371 }
372 
373 void
374 moEventList::Add(MOint did, MOint cod, MOint val0, MOpointer ptr ) {
375  //cout << "moEventList::Add(MOint did, MOint cod, MOint val0, MOpointer ptr )" << endl;
376  m_lock.Lock();
377 
378  if(First==NULL) {//lista vacia
379  First = new moEvent(did,cod,val0,ptr);
380  Last = First;
381  First->previous = NULL;
382  First->next = NULL;
383  } else {//no vacia, lo ponemos al final
384  Last->next = new moEvent(did,cod,val0,ptr);
385  if(Last->next != NULL) {
386  Last->next->previous = Last;
387  Last->next->next = NULL;
388  Last = Last->next;
389  }
390  }
391  m_lock.Unlock();
392 }
393 
394 
395 
396 //recontra revisado!!! anda
397 MOboolean
399 
400  m_lock.Lock();
401  //cout << "moEventList::Delete (moEvent*) ev: " << ev << " First:" << First << " Last:" << Last << endl;
402  if(ev==First) {
403  if(ev==Last) {
404  First = NULL;
405  Last = NULL;
406  } else {
407  First->next->previous = NULL;
408  First = First->next;
409  }
410  } else if(ev==Last) {
411  //el caso un solo nodo ya fue visto arriba
412  Last->previous->next = NULL;
413  Last = Last->previous;
414  } else {
415  ev->previous->next = ev->next;
416  ev->next->previous = ev->previous;
417 
418  }
419  //finalmente, lo borro al maldito
420  //cout << "moEventList::Delete (moEvent*) ev: deleting pointer!" << ev << " First:" << First << " Last:" << Last << endl;
421  delete ev;
422  ev = NULL;
423 
424  m_lock.Unlock();
425  //por si las dudas podes chequear que este bien muerto;
426  return(ev == NULL);
427 }
428 
429 MOboolean
431 
432  m_lock.Lock();
433  //cout << "moEventList::Delete (moMessage*) ev: " << ev << " First:" << First << " Last:" << Last << endl;
434  if(ev==First) {
435  if(ev==Last) {
436  //only one
437  First = NULL;
438  Last = NULL;
439  } else {
440  //at least two
441  First->next->previous = NULL;
442  First = First->next;
443  }
444  } else if(ev==Last) {
445  //el caso un solo nodo ya fue visto arriba
446  Last->previous->next = NULL;
447  Last = Last->previous;
448  } else {
449  ev->previous->next = ev->next;
450  ev->next->previous = ev->previous;
451 
452  }
453  //finalmente, lo borro al maldito
454  delete ev;
455  ev = NULL;
456 
457  m_lock.Unlock();
458  //por si las dudas podes chequear que este bien muerto;
459  return(ev == NULL);
460 }
461 
462 MOboolean
464  m_lock.Lock();
465  First = NULL;
466  Last = NULL;
467  m_lock.Unlock();
468  return true;
469 }
470 
471 MOboolean
473  m_lock.Lock();
474  while(First!=NULL) Delete(First);
475  m_lock.Unlock();
476  return true;
477 }
478 
479 
480 
481 // moEventPacket class **************************************************
482 
483 moEventPacket::moEventPacket(float p_sendInterval, int p_maxEventNum)
484 {
485  sendInterval = p_sendInterval;
486  maxEventNum = p_maxEventNum;
487 
488  buffer_events = new moEventStruct [maxEventNum];
489  pointer = (void*)buffer_events;
490  memset((void*)&empty_event,0,sizeof(moEventStruct));
491  num_events = 0;
492 
493  ClearPacket();
494 }
495 
497 {
498  if (buffer_events != NULL)
499  {
500  delete[] buffer_events;
501  buffer_events = NULL;
502  }
503 }
504 
506 {
507  for(int i=0;i<maxEventNum;i++) buffer_events[i] = empty_event;
508 
509  num_events = 0;
510  packet_full = false;
511 
512  time0 = moGetTicks() / 1000.0;
513 }
514 
516 {
517  moEventStruct new_event;
518 
519  new_event.valid = 1;
520  new_event.devicecode = e->devicecode;
521  new_event.deviceid = e->deviceid;
522  new_event.reservedvalue0 = e->reservedvalue0;
523  new_event.reservedvalue1 = e->reservedvalue1;
524  new_event.reservedvalue2 = e->reservedvalue2;
525  new_event.reservedvalue3 = e->reservedvalue3;
526 
527  if ( num_events < maxEventNum )
528  {
529  buffer_events[num_events] = new_event;
530  num_events++;
531  }
532  else
533  {
534  packet_full = true;
535  }
536 
537  return !packet_full;
538 }
539 
541 {
542  float time1 = moGetTicks() / 1000.0;
543  return (sendInterval <= time1 - time0) || packet_full;
544 }
virtual ~moEventList()
moMoldeoObjectType
Tipos de objetos en Moldeo.
Definition: moTypes.h:525
moEvent * next
Definition: moEventList.h:60
bool Unlock()
Definition: moLock.cpp:137
MOint m_InletIdDest
Definition: moEventList.h:115
void SetText(moText ptext)
Definition: moValue.cpp:158
moEvent * First
Definition: moEventList.h:145
MOint m_MoldeoIdSrc
Definition: moEventList.h:119
virtual ~moMessage()
virtual ~moEvent()
Destructor.
virtual const moText & ToJSON()
Clase Evento.
Definition: moEventList.h:56
MOint m_MoldeoIdDest
Definition: moEventList.h:114
MOint deviceid
Definition: moEventList.h:62
MOpointer pointer
Definition: moEventList.h:68
#define MO_ACTION_MOLDEOAPI_EVENT_SEND
Definition: moActions.h:46
MOboolean Delete(moEvent *ev)
bool AddEvent(moEvent *e)
#define MOboolean
Definition: moTypes.h:385
moDefineDynamicArray(moEventPacketArray)
void ClearPacket()
moText JSON
Definition: moEventList.h:79
Clase Mensaje.
Definition: moEventList.h:97
moText m_NameSrc
Definition: moEventList.h:122
moEvent * previous
Definition: moEventList.h:59
moText m_NameDest
Definition: moEventList.h:117
MOint reservedvalue0
Definition: moEventList.h:46
moMoldeoObjectType m_TypeDest
Definition: moEventList.h:116
#define MO_MESSAGE
Definition: moEventList.h:84
MOboolean Finish()
Finaliza el objeto, libera recursos.
MOint reservedvalue1
Definition: moEventList.h:47
clase de para manejar textos
Definition: moText.h:75
MOint reservedvalue1
Definition: moEventList.h:65
MOint reservedvalue2
Definition: moEventList.h:48
MOint devicecode
Definition: moEventList.h:45
void Add(moMessage *p_Message)
bool Lock()
Definition: moLock.cpp:101
#define MOint
Definition: moTypes.h:388
Estructura base de un evento.
Definition: moEventList.h:41
MOint reservedvalue0
Definition: moEventList.h:64
MOint reservedvalue3
Definition: moEventList.h:67
#define MO_DATAMESSAGE
Definition: moEventList.h:85
moMessage & operator=(const moMessage &src)
moEvent()
Constructor.
#define MO_IODEVICE_CONSOLE
virtual const moText & ToJSON()
MOint devicecode
Definition: moEventList.h:63
bool ReadyToSend()
MOint reservedvalue3
Definition: moEventList.h:49
MOboolean Init()
Inicializa el objeto.
MOint deviceid
Definition: moEventList.h:44
moEventPacket(float p_sendInterval, int p_maxEventNum)
moLock m_lock
Definition: moEventList.h:143
LIBMOLDEO_API moText0 IntToStr(int a)
Definition: moText.cpp:1070
moData m_Data
Definition: moEventList.h:112
moMoldeoObjectType m_TypeSrc
Definition: moEventList.h:121
#define MOpointer
Definition: moTypes.h:409
moEvent * Last
Definition: moEventList.h:146
#define MO_ACTION_MOLDEOAPI_EVENT_RECEIVE
Definition: moActions.h:47
MOint reservedvalue2
Definition: moEventList.h:66
MOint m_OutletIdSrc
Definition: moEventList.h:120
MOulong moGetTicks()
Devuelve en milisegundos el valor del reloj de Moldeo.
Definition: moTimer.cpp:138