Source

Coin / src / scxml / ScXMLEventTarget.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/**************************************************************************\
 *
 *  This file is part of the Coin 3D visualization library.
 *  Copyright (C) by Kongsberg Oil & Gas Technologies.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  ("GPL") version 2 as published by the Free Software Foundation.
 *  See the file LICENSE.GPL at the root directory of this source
 *  distribution for additional information about the GNU GPL.
 *
 *  For using Coin with software that can not be combined with the GNU
 *  GPL, and for taking advantage of the additional benefits of our
 *  support services, please contact Kongsberg Oil & Gas Technologies
 *  about acquiring a Coin Professional Edition License.
 *
 *  See http://www.coin3d.org/ for more information.
 *
 *  Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
 *  http://www.sim.no/  sales@sim.no  coin-support@coin3d.org
 *
\**************************************************************************/

#include <Inventor/scxml/ScXMLEventTarget.h>

/*!
  \class ScXMLEventTarget ScXMLEventTarget.h Inventor/scxml/ScXMLEventTarget.h
  \brief base class for event targets for inter-system event communication

  This class manages inter-communication between SCXML systems and systems
  intended for communicating with such systems.  It also manages the external
  event queue for such systems (systems may also have internal event queues).

  \ingroup scxml
  \since Coin 3.1
*/

#include <cassert>
#include <map>
#include <list>
#include <algorithm>

#include <boost/scoped_ptr.hpp>

#include <Inventor/SbName.h>
#include <Inventor/C/threads/mutex.h>
#include <Inventor/sensors/SoAlarmSensor.h>
#include <Inventor/scxml/ScXMLEvent.h>
#include <Inventor/scxml/ScXMLEventElt.h>
#include <Inventor/scxml/ScXMLSendElt.h>
#include <Inventor/scxml/ScXMLStateMachine.h>
#include <Inventor/errors/SoDebugError.h>
#include "scxml/ScXMLCommonP.h"
#include "scxml/ScXMLP.h"

// *************************************************************************

class DelayEventData {
  // FIXME: these objects should per spec die with the invoking eventtarget.
public:
  DelayEventData(void)
  : targettype(NULL), targetname(NULL), delay(0.0f), alarm(NULL), event(NULL)
  {
  }
  ~DelayEventData(void)
  {
    delete [] this->targettype; this->targettype = NULL;
    delete [] this->targetname; this->targetname = NULL;
    if (event) { delete this->event; this->event = NULL; }
  }

  char * targettype;
  char * targetname;
  float delay;
  boost::scoped_ptr<SoAlarmSensor> alarm;
  const ScXMLEvent * event;
};

// *************************************************************************

typedef std::map<const char *, ScXMLEventTarget *> SessionMap;
typedef std::pair<const char *, ScXMLEventTarget *> SessionMapEntry;

typedef std::map<const char *, SessionMap *> TargetMap;
typedef std::pair<const char *, SessionMap *> TargetMapEntry;

typedef std::map<const char *, TargetMap *> TargetTypeMap;
typedef std::pair<const char *, TargetMap *> TargetTypeMapEntry;

class ScXMLEventTarget::PImpl {
public:
  PImpl(void) : targetsessionid(NULL) { }
  ~PImpl(void)
  {
    delete [] this->targetsessionid; this->targetsessionid = NULL;
    // FIX?ME: clean out queues
  }

  static TargetTypeMap * targettypes;

  std::list<const ScXMLEvent *> externaleventqueue;
  std::list<const ScXMLEvent *> internaleventqueue;
  std::list<const ScXMLEvent *> delayeventqueue;
  std::list<ScXMLEventTarget *> targetqueue;

  char * targetsessionid; // needed in destructor - can't fetch virtually

  static void delay_event_alarm_cb(void * userdata, SoSensor * sensor);
  static SbList<ScXMLEventTarget *> targets;

};

void
ScXMLEventTarget::PImpl::delay_event_alarm_cb(void * userdata, SoSensor * COIN_UNUSED_ARG(sensor))
{
  assert(userdata);
  DelayEventData * eventdata = static_cast<DelayEventData *>(userdata);
  assert(eventdata->targettype);
  assert(eventdata->targetname);
  assert(eventdata->event);

  ScXMLEventTarget * target =
    ScXMLEventTarget::getEventTarget(eventdata->targettype,
                                     eventdata->targetname);

  if (unlikely(!target)) {
    SoDebugError::post("delay_event_alarm_cb",
                       "delay-event %s: no target found",
                       eventdata->event->getEventName().getString());
  }
  else {
    target->queueEvent(eventdata->event);
    target->processEventQueue();
  }

  delete eventdata;
}

#define PRIVATE(obj) ((obj)->pimpl)

TargetTypeMap * ScXMLEventTarget::PImpl::targettypes = NULL;
SbList<ScXMLEventTarget *> ScXMLEventTarget::PImpl::targets;

SCXML_OBJECT_ABSTRACT_SOURCE(ScXMLEventTarget);

void
ScXMLEventTarget::initClass(void)
{
  SCXML_OBJECT_INIT_ABSTRACT_CLASS(ScXMLEventTarget, ScXMLObject, "ScXMLObject");

  ScXMLP::lock();
  assert(ScXMLEventTarget::PImpl::targettypes == NULL);
  ScXMLEventTarget::PImpl::targettypes = new TargetTypeMap;
  ScXMLP::unlock();
}

void
ScXMLEventTarget::cleanClass(void)
{
  int n;
  while ( (n = ScXMLEventTarget::PImpl::targets.getLength())>0) {
    ScXMLEventTarget * target = ScXMLEventTarget::PImpl::targets[n-1];
    unregisterEventTarget(target,NULL);
  }
  
  ScXMLP::lock();
  assert(ScXMLEventTarget::PImpl::targettypes != NULL);
  delete ScXMLEventTarget::PImpl::targettypes;
  ScXMLEventTarget::PImpl::targettypes = NULL;
  ScXMLP::unlock();
  ScXMLEventTarget::classTypeId = SoType::badType();
}


/*!
*/
void
ScXMLEventTarget::registerEventTarget(ScXMLEventTarget * target, const char * sessionid)
{
  const SbName targettypename(target->getEventTargetType());
  const SbName targetnamename(target->getEventTargetName());
  SbName targetsessionid(SbName::empty());
  if (sessionid) {
    targetsessionid = SbName(sessionid);
  }

  assert(ScXMLEventTarget::PImpl::targettypes);
  ScXMLP::lock();
  TargetTypeMap::iterator typeit = ScXMLEventTarget::PImpl::targettypes->find(targettypename.getString());
  TargetMap * targetmap = NULL;
  if (typeit != ScXMLEventTarget::PImpl::targettypes->end()) {
    targetmap = typeit->second;
  } else {
    targetmap = new TargetMap;
    TargetTypeMapEntry entry(targettypename.getString(), targetmap);
    ScXMLEventTarget::PImpl::targettypes->insert(entry);
  }

  TargetMap::iterator targetit = targetmap->find(targetnamename.getString());
  SessionMap * sessionmap = NULL;
  if (targetit != targetmap->end()) {
    sessionmap = targetit->second;
  } else {
    sessionmap = new SessionMap;
    TargetMapEntry entry(targetnamename.getString(), sessionmap);
    targetmap->insert(entry);
  }

  SessionMap::iterator sessionit = sessionmap->find(targetsessionid.getString());
  if (sessionit != sessionmap->end()) {
    if (target == sessionit->second) {
      // already registered
      SoDebugError::post("ScXMLEventTarget::registerEventTarget",
                         "type/name/session touple already registered for this target");
    } else {
      SoDebugError::post("ScXMLEventTarget::registerEventTarget",
                         "type/name/session touple already registered for another target");
    }
  } else {
    SessionMapEntry entry(targetsessionid.getString(), target);
    sessionmap->insert(entry);
  }

  ScXMLEventTarget::PImpl::targets.append(target);

  ScXMLP::unlock();
}

/*!
*/
void
ScXMLEventTarget::unregisterEventTarget(ScXMLEventTarget * target, const char * sessionid)
{
  // FIXME This was done at the end of the function, but we have multiple exit points so target was sometimes
  // not removed causing an endless loop.
  ScXMLEventTarget::PImpl::targets.removeItem(target);

  const SbName targettypename(target->getEventTargetType());
  const SbName targetnamename(target->getEventTargetName());
  SbName targetsessionid(SbName::empty());
  if (sessionid) {
    targetsessionid = SbName(sessionid);
  }

  assert(ScXMLEventTarget::PImpl::targettypes);
  ScXMLP::lock();
  TargetTypeMap::iterator typeit = ScXMLEventTarget::PImpl::targettypes->find(targettypename.getString());
  if (unlikely(typeit == ScXMLEventTarget::PImpl::targettypes->end())) {
    SoDebugError::post("ScXMLEventTarget::unregisterEventTarget",
                       "given target type ('%s') not registered",
                       targettypename.getString());
    ScXMLP::unlock();
    return;
  }
  TargetMap * targetmap = typeit->second;

  TargetMap::iterator targetit = targetmap->find(targetnamename.getString());
  if unlikely (targetit == targetmap->end()) {
    SoDebugError::post("ScXMLEventTarget::unregisterEventTarget",
                       "given target name not registered");
    ScXMLP::unlock();
    return;
  }

  SessionMap * sessionmap = targetit->second;

  SessionMap::iterator sessionit = sessionmap->find(targetsessionid.getString());
  if unlikely (sessionit == sessionmap->end()) {
      SoDebugError::post("ScXMLEventTarget::unregisterEventTarget",
                         "given target session not registered");
      ScXMLP::unlock();
      return;
  }

  if (target == sessionit->second) {
    sessionmap->erase(sessionit);
    if (sessionmap->empty()) {
      targetmap->erase(targetit);
      if (targetmap->empty()) {
        ScXMLEventTarget::PImpl::targettypes->erase(typeit);
        delete targetmap;
      }
      delete sessionmap;
    }
  }
  else {
    SoDebugError::post("ScXMLEventTarget::unregisterEventTarget",
                       "given target session is registered, but to a different target");
    ScXMLP::unlock();
    return;
  }
  ScXMLP::unlock();
}

/*!
*/
ScXMLEventTarget *
ScXMLEventTarget::getEventTarget(const char * targettype, const char * targetname, const char * sessionid)
{
  const SbName targettypename(targettype);
  const SbName targetnamename(targetname);
  SbName targetsessionid(SbName::empty());
  if (sessionid != NULL) {
    targetsessionid = SbName(sessionid);
  }

  assert(ScXMLEventTarget::PImpl::targettypes);
  ScXMLP::lock();
  TargetTypeMap::iterator typeit = ScXMLEventTarget::PImpl::targettypes->find(targettypename.getString());
  if unlikely (typeit == ScXMLEventTarget::PImpl::targettypes->end()) {
    // SoDebugError::post("ScXMLEventTarget::getEventTarget",
    //                    "given target type ('%s') not registered 1",
    //                    targettypename.getString());
    ScXMLP::unlock();
    return NULL;
  }
  TargetMap * targetmap = typeit->second;

  TargetMap::iterator targetit = targetmap->find(targetnamename.getString());
  if unlikely (targetit == targetmap->end()) {
    // SoDebugError::post("ScXMLEventTarget::getEventTarget",
    //                    "given target (type '%s' '%s') not registered",
    //                    targettypename.getString(), targetnamename.getString());
    ScXMLP::unlock();
    return NULL;
  }
  SessionMap * sessionmap = targetit->second;

  SessionMap::iterator sessionit = sessionmap->find(targetsessionid.getString());
  if unlikely (targetit == targetmap->end()) {
    // SoDebugError::post("ScXMLEventTarget::getEventTarget",
    //                    "given target (type '%s' '%s') with session '%s' not registered",
    //                    targettypename.getString(), targetnamename.getString(), targetsessionid.getString());
    ScXMLP::unlock();
    return NULL;
  }

  ScXMLEventTarget * target = sessionit->second;
  ScXMLP::unlock();
  return target;
}


/*!
*/
ScXMLEventTarget::ScXMLEventTarget(void)
: targetname(NULL),
  targettype(NULL),
  currentevent(NULL),
  isprocessingqueue(FALSE)
{
}

/*!
*/
ScXMLEventTarget::~ScXMLEventTarget(void)
{
  if (this->targetname && this->targettype) {
    ScXMLEventTarget::unregisterEventTarget(this, PRIVATE(this)->targetsessionid);
  }
  delete [] this->targetname;
  this->targetname = NULL;
  delete [] this->targettype;
  this->targettype = NULL;
}

/*!
*/
void
ScXMLEventTarget::setEventTargetType(const char * targettypestr)
{
  if (this->targetname && this->targettype) {
    ScXMLEventTarget::unregisterEventTarget(this, PRIVATE(this)->targetsessionid);
  }

  delete [] this->targettype;
  this->targettype = NULL;

  if (targettypestr) {
    this->targettype = new char [strlen(targettypestr) + 1];
    assert(this->targettype);
    strcpy(this->targettype, targettypestr);

    if (this->targetname && this->targettype) {
      if (this->isOfType(ScXMLStateMachine::getClassTypeId())) {
        SbName sessionid = static_cast<ScXMLStateMachine *>(this)->getSessionId();
        delete [] PRIVATE(this)->targetsessionid;
        PRIVATE(this)->targetsessionid = new char [strlen(sessionid.getString()) + 1];
        strcpy(PRIVATE(this)->targetsessionid, sessionid.getString());
      }
      ScXMLEventTarget::registerEventTarget(this, PRIVATE(this)->targetsessionid);
    }
  }
}

/*!
  \fn const char * ScXMLEventTarget::getEventTargetType(void) const
*/

/*!
*/
void
ScXMLEventTarget::setEventTargetName(const char * targetnamestr)
{
  if (this->targetname && this->targettype) {
    ScXMLEventTarget::unregisterEventTarget(this, PRIVATE(this)->targetsessionid);
  }

  delete [] this->targetname;
  this->targetname = NULL;

  if (targetnamestr) {
    this->targetname = new char [strlen(targetnamestr) + 1];
    assert(this->targetname);
    strcpy(this->targetname, targetnamestr);

    if (this->targetname && this->targettype) {
      if (this->isOfType(ScXMLStateMachine::getClassTypeId())) {
        SbName sessionid = static_cast<ScXMLStateMachine *>(this)->getSessionId();
        delete [] PRIVATE(this)->targetsessionid;
        PRIVATE(this)->targetsessionid = new char [strlen(sessionid.getString()) + 1];
        strcpy(PRIVATE(this)->targetsessionid, sessionid.getString());
      }
      ScXMLEventTarget::registerEventTarget(this, PRIVATE(this)->targetsessionid);
    }
  }
}

/*!
  \fn const char * ScXMLEventTarget::getEventTargetName(void) const
*/


/*!
  Queues an ScXMLEvent event.
  The event is copied, and can be deleted as soon as the function return.
*/
void
ScXMLEventTarget::queueEvent(const ScXMLEvent * event)
{
  ScXMLP::lock();
  PRIVATE(this)->externaleventqueue.push_back(event->clone());
  ScXMLP::unlock();
}

/*!
  Queues an ScXMLEvent event that has the given \a eventid as its event name.
*/
void
ScXMLEventTarget::queueEvent(const SbName & eventid)
{
  ScXMLEvent * event = new ScXMLEvent;
  event->setEventName(eventid);
  ScXMLP::lock();
  PRIVATE(this)->externaleventqueue.push_back(event);
  ScXMLP::unlock();
}

/*!
*/
void
ScXMLEventTarget::queueInternalEvent(const SbName & eventid)
{
  ScXMLEvent * event = new ScXMLEvent;
  event->setEventName(eventid);
  ScXMLP::lock();
  PRIVATE(this)->internaleventqueue.push_back(event);
  ScXMLP::unlock();
}

/*!
*/
void
ScXMLEventTarget::queueInternalEvent(const ScXMLEvent * event)
{
  ScXMLP::lock();
  PRIVATE(this)->internaleventqueue.push_back(event->clone());
  ScXMLP::unlock();
}

/*!
*/
SbBool
ScXMLEventTarget::processEventQueue(void)
{
  if (this->isprocessingqueue) {
    // to avoid recursive event-processing - everything must go through the event
    // queues in a strict serialized fashion...
    return FALSE;
  }

  this->isprocessingqueue = TRUE;

  SbBool eventsuccess = FALSE;
  SbBool communicating = TRUE;
  while (communicating) {
    const ScXMLEvent * event = NULL;
    while ((event = this->getNextEvent())) {
      if (this->processOneEvent(event)) {
        eventsuccess = TRUE;
      }
      delete event;
    }

    // have we communicated with other event targets, and need to roll
    // their event queue as well?
    if (PRIVATE(this)->targetqueue.empty()) {
      communicating = FALSE;
    } else {
      while (!PRIVATE(this)->targetqueue.empty()) {
        // FIXME: process external event targets we've communicated with,
        ScXMLP::lock();
        ScXMLEventTarget * target = PRIVATE(this)->targetqueue.front();
        PRIVATE(this)->targetqueue.pop_front();
        ScXMLP::unlock();
        target->processEventQueue();
      }
    }
  }

  this->isprocessingqueue = FALSE;
  return eventsuccess;
}

/*!
*/
const ScXMLEvent *
ScXMLEventTarget::getNextEvent(void)
{
  const ScXMLEvent * nextevent = NULL;
  // internal events have first priority
  nextevent = this->getNextInternalEvent();
  if (!nextevent) {
    nextevent = this->getNextExternalEvent();
  }
  return nextevent;
}

/*!
*/
const ScXMLEvent *
ScXMLEventTarget::getNextInternalEvent(void)
{
  ScXMLP::lock();
  const ScXMLEvent * nextevent = NULL;
  if (!PRIVATE(this)->internaleventqueue.empty()) {
    nextevent = PRIVATE(this)->internaleventqueue.front();
    PRIVATE(this)->internaleventqueue.pop_front();
  }
  ScXMLP::unlock();
  return nextevent;
}

/*!
*/
const ScXMLEvent *
ScXMLEventTarget::getNextExternalEvent(void)
{
  ScXMLP::lock();
  const ScXMLEvent * nextevent = NULL;
  if (!PRIVATE(this)->externaleventqueue.empty()) {
    nextevent = PRIVATE(this)->externaleventqueue.front();
    PRIVATE(this)->externaleventqueue.pop_front();
  }
  ScXMLP::unlock();
  return nextevent;
}

/*!
  This function processes one event.  The base class implementation does nothing.
*/
SbBool
ScXMLEventTarget::processOneEvent(const ScXMLEvent * COIN_UNUSED_ARG(event))
{
  return FALSE;
}

/*!
  Sets a pointer for the event that is 'current' during event processing.
  This is an internal method, and the updating of the current event should
  be handled automatically.
*/
void
ScXMLEventTarget::setCurrentEvent(const ScXMLEvent * event)
{
  this->currentevent = event;
}

/*!
  \fn const ScXMLEvent * ScXMLEventTarget::getCurrentEvent(void) const

  This method returns the current event during event processing, and NULL
  when not processing events.

  Event processing is in special cases done with NULL as the current event,
  as for instance during state machine initialization.
*/

// inter-system communication
/*!
*/
SbBool
ScXMLEventTarget::sendExternalEvent(const ScXMLSendElt * sendelt)
{
  // SCXML errors produced:
  //   internal: error.send.TargetTypeInvalid
  //   - event "targettype" specified is not supported
  //   internal: error.send.TargetUnavailable
  //   - value of the "target" attribute is not supported, invalid or unreachable
  //   internal: error.Fetch
  //   - event and namelist or inline content requirement error
  //   4.1.1:
  //   send.failed.targettypenotsupported.stateID.sendID
  //   send.failed.targetnotfound.stateID.sendID

  const char * sendtargettypeattr = sendelt->getTargetTypeAttribute();
  const char * sendtargetattr = sendelt->getTargetAttribute();

  if (!sendtargettypeattr && !sendtargetattr) { // FIXME: necessary by spec?
    sendtargettypeattr = this->getEventTargetType();
    sendtargetattr = this->getEventTargetName();
  }

  if (!sendtargettypeattr) {
    this->queueInternalEvent(SbName("error.send.TargetTypeInvalid"));
    return FALSE;
  }

  if (strcmp(sendtargettypeattr, "scxml") == 0) {
  }
  else if (strcmp(sendtargettypeattr, "x-coin-navigation") == 0) {
  }
  else {
    this->queueInternalEvent(SbName("error.send.TargetTypeInvalid"));
    // FIXME: is this something we know? should we perhaps unwind the map to see that
    // no such target type is registered at all first before instead flagging
    // targetunavailable?
    return FALSE;
  }

  float delay = 0.0f;
  const char * delaystr = sendelt->getDelayAttribute();
  if (delaystr) {
    // format: "<num>s" or "<num>ms"
    int len = strlen(delaystr);
    if (len > 1) {
      if (delaystr[len-1] != 's') {
        SoDebugError::post("ScXMLEventTarget::sendExternalEvent",
                           "delay attribute format error (was: %s)", delaystr);
        return FALSE;
      }
      if (delaystr[len-2] == 'm') {
        delay = float(atof(delaystr)) / 1000.0f;
      } else {
        delay = float(atof(delaystr));
      }
    } else {
      SoDebugError::post("ScXMLEventTarget::sendExternalEvent",
                         "delay attribute format error (was: %s)", delaystr);
      return FALSE;
    }
  }

  ScXMLEventTarget * target =
    ScXMLEventTarget::getEventTarget(sendtargettypeattr, sendtargetattr);
  if (unlikely(!target)) {
    this->queueInternalEvent(SbName("error.send.TargetUnavailable"));
    return FALSE;
  }

  const ScXMLEvent * event = sendelt->createEvent(this);
  if (!event) {
    return FALSE;
  }

  if (delay > 0.0f) {
    DelayEventData * eventdata = new DelayEventData;
    eventdata->targettype = new char [strlen(sendtargettypeattr) + 1];
    strcpy(eventdata->targettype, sendtargettypeattr);
    eventdata->targetname = new char [strlen(sendtargetattr) + 1];
    strcpy(eventdata->targetname, sendtargetattr);
    eventdata->event = event;
    eventdata->alarm.reset(new SoAlarmSensor(PImpl::delay_event_alarm_cb,
                                             eventdata));
    eventdata->alarm->setTimeFromNow(SbTime(double(delay)));
    eventdata->alarm->schedule();
  } else {
    target->queueEvent(event);
    delete event;
    std::list<ScXMLEventTarget *>::iterator findit =
      std::find(PRIVATE(this)->targetqueue.begin(),
                PRIVATE(this)->targetqueue.end(),
                target);
    if (findit == PRIVATE(this)->targetqueue.end()) {
      ScXMLP::lock();
      PRIVATE(this)->targetqueue.push_back(target);
      ScXMLP::unlock();
    }
  }

  return TRUE;
}

/*!
*/
SbBool
ScXMLEventTarget::sendInternalEvent(const ScXMLEventElt * eventelt)
{
  assert(eventelt);
  ScXMLEvent * event = eventelt->createEvent(this);
  this->queueInternalEvent(event);
  delete event;
  return TRUE;
}

#undef PRIVATE
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.