unsigned int Agent::addToBuffer (DataItem * dataItem, const string & value, string time ) { if ( dataItem == NULL ) { return 0; } dlib::auto_mutex lock(*mSequenceLock); uint64_t seqNum = mSequence++; ComponentEvent *event = new ComponentEvent(*dataItem, seqNum, time, value); ( *mSlidingBuffer )[seqNum] = event; mLatest.addComponentEvent(event); event->unrefer( ); // Special case for the first event in the series to prime the first checkpoint. if ( seqNum == 1 ) { mFirst.addComponentEvent(event); } // Checkpoint management int index = mSlidingBuffer->get_element_id(seqNum); if ( ( mCheckpointCount > 0 ) && ( index % mCheckpointFreq == 0 ) ) { // Copy the checkpoint from the current into the slot mCheckpoints[index / mCheckpointFreq].copy(mLatest); } // See if the next sequence has an event. If the event exists it // should be added to the first checkpoint. if ( ( *mSlidingBuffer )[mSequence] != NULL ) { // Keep the last checkpoint up to date with the last. mFirst.addComponentEvent(( *mSlidingBuffer )[mSequence]); } dataItem->signalObservers(seqNum); return seqNum; }
void Checkpoint::addComponentEvent (ComponentEvent *anEvent) { if ( mHasFilter ) { if ( mFilter.count(anEvent->getDataItem( )->getId( )) == 0 ) { return; } } DataItem * item = anEvent->getDataItem( ); string id = item->getId( ); ComponentEventPtr *ptr = mEvents[id]; if ( ptr != NULL ) { bool assigned = false; if ( item->isCondition( ) ) { // Chain event only if it is normal or unavailable and the // previous condition was not normal or unavailable if ( ( ( *ptr )->getLevel( ) != ComponentEvent::NORMAL ) && ( anEvent->getLevel( ) != ComponentEvent::NORMAL ) && ( ( *ptr )->getLevel( ) != ComponentEvent::UNAVAILABLE ) && ( anEvent->getLevel( ) != ComponentEvent::UNAVAILABLE ) ) { // Check to see if the native code matches an existing // active condition ComponentEvent *e = ( *ptr )->find(anEvent->getCode( )); if ( e != NULL ) { // Replace in chain. ComponentEvent *n = ( *ptr )->deepCopyAndRemove(e); // Check if this is the only event... ( *ptr ) = n; if ( n != NULL ) { n->unrefer( ); } } // Chain the event if ( ptr->getObject( ) != NULL ) { anEvent->appendTo(*ptr); } } else if ( anEvent->getLevel( ) == ComponentEvent::NORMAL ) { // Check for a normal that clears an active condition by code if ( anEvent->getCode( )[0] != '\0' ) { ComponentEvent *e = ( *ptr )->find(anEvent->getCode( )); if ( e != NULL ) { // Clear the one condition by removing it from the chain ComponentEvent *n = ( *ptr )->deepCopyAndRemove(e); ( *ptr ) = n; if ( n != NULL ) { n->unrefer( ); } else { // Need to put a normal event in with no code since this // is the last one. n = new ComponentEvent(*anEvent); n->normal( ); ( *ptr ) = n; n->unrefer( ); } } else { // Not sure if we should register code specific normals if // previous normal was not found // (*ptr) = anEvent; } assigned = true; } } } if ( !assigned ) { ( *ptr ) = anEvent; } } else { mEvents[id] = new ComponentEventPtr(anEvent); } }