2016-06-15 23:48:41 +02:00
# include <framework/events/EventManager.h>
# include <framework/events/EventMessage.h>
# include <framework/serviceinterface/ServiceInterfaceStream.h>
2018-07-12 16:29:32 +02:00
# include <framework/ipc/QueueFactory.h>
# include <framework/ipc/MutexFactory.h>
2016-06-15 23:48:41 +02:00
const uint16_t EventManager : : POOL_SIZES [ N_POOLS ] = {
sizeof ( EventMatchTree : : Node ) , sizeof ( EventIdRangeMatcher ) ,
sizeof ( ReporterRangeMatcher ) } ;
2018-07-12 16:29:32 +02:00
//If one checks registerListener calls, there are around 40 (to max 50) objects registering for certain events.
//Each listener requires 1 or 2 EventIdMatcher and 1 or 2 ReportRangeMatcher. So a good guess is 75 to a max of 100 pools required for each, which fits well.
2016-06-15 23:48:41 +02:00
const uint16_t EventManager : : N_ELEMENTS [ N_POOLS ] = { 240 , 120 , 120 } ;
EventManager : : EventManager ( object_id_t setObjectId ) :
2018-07-12 16:29:32 +02:00
SystemObject ( setObjectId ) , eventReportQueue ( NULL ) , mutex ( NULL ) , factoryBackend (
2016-06-15 23:48:41 +02:00
0 , POOL_SIZES , N_ELEMENTS , false , true ) {
2018-07-12 16:29:32 +02:00
mutex = MutexFactory : : instance ( ) - > createMutex ( ) ;
eventReportQueue = QueueFactory : : instance ( ) - > createMessageQueue (
MAX_EVENTS_PER_CYCLE , EventMessage : : EVENT_MESSAGE_SIZE ) ;
2016-06-15 23:48:41 +02:00
}
EventManager : : ~ EventManager ( ) {
2018-07-12 16:29:32 +02:00
QueueFactory : : instance ( ) - > deleteMessageQueue ( eventReportQueue ) ;
MutexFactory : : instance ( ) - > deleteMutex ( mutex ) ;
2016-06-15 23:48:41 +02:00
}
MessageQueueId_t EventManager : : getEventReportQueue ( ) {
2018-07-12 16:29:32 +02:00
return eventReportQueue - > getId ( ) ;
2016-06-15 23:48:41 +02:00
}
2018-07-12 16:29:32 +02:00
ReturnValue_t EventManager : : performOperation ( uint8_t opCode ) {
2016-06-15 23:48:41 +02:00
ReturnValue_t result = HasReturnvaluesIF : : RETURN_OK ;
while ( result = = HasReturnvaluesIF : : RETURN_OK ) {
EventMessage message ;
2018-07-12 16:29:32 +02:00
result = eventReportQueue - > receiveMessage ( & message ) ;
2016-06-15 23:48:41 +02:00
if ( result = = HasReturnvaluesIF : : RETURN_OK ) {
2018-07-12 16:29:32 +02:00
# ifdef DEBUG
2016-06-15 23:48:41 +02:00
printEvent ( & message ) ;
2018-07-12 16:29:32 +02:00
# endif
2016-06-15 23:48:41 +02:00
notifyListeners ( & message ) ;
}
}
return HasReturnvaluesIF : : RETURN_OK ;
}
void EventManager : : notifyListeners ( EventMessage * message ) {
lockMutex ( ) ;
for ( auto iter = listenerList . begin ( ) ; iter ! = listenerList . end ( ) ; + + iter ) {
if ( iter - > second . match ( message ) ) {
2018-07-12 16:29:32 +02:00
MessageQueueSenderIF : : sendMessage ( iter - > first , message ,
2016-06-15 23:48:41 +02:00
message - > getSender ( ) ) ;
}
}
unlockMutex ( ) ;
}
ReturnValue_t EventManager : : registerListener ( MessageQueueId_t listener ,
bool forwardAllButSelected ) {
auto result = listenerList . insert (
std : : pair < MessageQueueId_t , EventMatchTree > ( listener ,
EventMatchTree ( & factoryBackend , forwardAllButSelected ) ) ) ;
if ( ! result . second ) {
return HasReturnvaluesIF : : RETURN_FAILED ;
}
return HasReturnvaluesIF : : RETURN_OK ;
}
ReturnValue_t EventManager : : subscribeToEvent ( MessageQueueId_t listener ,
EventId_t event ) {
return subscribeToEventRange ( listener , event ) ;
}
ReturnValue_t EventManager : : subscribeToAllEventsFrom ( MessageQueueId_t listener ,
object_id_t object ) {
return subscribeToEventRange ( listener , 0 , 0 , true , object ) ;
}
ReturnValue_t EventManager : : subscribeToEventRange ( MessageQueueId_t listener ,
EventId_t idFrom , EventId_t idTo , bool idInverted ,
object_id_t reporterFrom , object_id_t reporterTo ,
bool reporterInverted ) {
auto iter = listenerList . find ( listener ) ;
if ( iter = = listenerList . end ( ) ) {
return LISTENER_NOT_FOUND ;
}
lockMutex ( ) ;
ReturnValue_t result = iter - > second . addMatch ( idFrom , idTo , idInverted ,
reporterFrom , reporterTo , reporterInverted ) ;
unlockMutex ( ) ;
return result ;
}
ReturnValue_t EventManager : : unsubscribeFromEventRange ( MessageQueueId_t listener ,
EventId_t idFrom , EventId_t idTo , bool idInverted ,
object_id_t reporterFrom , object_id_t reporterTo ,
bool reporterInverted ) {
auto iter = listenerList . find ( listener ) ;
if ( iter = = listenerList . end ( ) ) {
return LISTENER_NOT_FOUND ;
}
lockMutex ( ) ;
ReturnValue_t result = iter - > second . removeMatch ( idFrom , idTo , idInverted ,
reporterFrom , reporterTo , reporterInverted ) ;
unlockMutex ( ) ;
return result ;
}
2018-07-12 16:29:32 +02:00
# ifdef DEBUG
//forward declaration, should be implemented by mission
const char * translateObject ( object_id_t object ) ;
const char * translateEvents ( Event event ) ;
2016-06-15 23:48:41 +02:00
void EventManager : : printEvent ( EventMessage * message ) {
const char * string = 0 ;
switch ( message - > getSeverity ( ) ) {
case SEVERITY : : INFO :
2018-07-12 16:29:32 +02:00
// string = translateObject(message->getReporter());
// info << "EVENT: ";
// if (string != 0) {
// info << string;
// } else {
// info << "0x" << std::hex << message->getReporter() << std::dec;
// }
// info << " reported " << translateEvents(message->getEvent()) << " ("
// << std::dec << message->getEventId() << std::hex << ") P1: 0x"
// << message->getParameter1() << " P2: 0x"
// << message->getParameter2() << std::dec << std::endl;
2016-06-15 23:48:41 +02:00
break ;
default :
string = translateObject ( message - > getReporter ( ) ) ;
error < < " EVENT: " ;
if ( string ! = 0 ) {
error < < string ;
} else {
error < < " 0x " < < std : : hex < < message - > getReporter ( ) < < std : : dec ;
}
error < < " reported " < < translateEvents ( message - > getEvent ( ) ) < < " ( "
2019-11-09 13:07:26 +01:00
< < std : : dec < < message - > getEventId ( ) < < " ) " < < std : : endl ;
error < < std : : hex < < " P1 Hex: 0x " < < message - > getParameter1 ( ) < < " , P1 Dec: " < < std : : dec < < message - > getParameter1 ( ) < < std : : endl ;
error < < std : : hex < < " P2 Hex: 0x " < < message - > getParameter2 ( ) < < " , P2 Dec: " < < std : : dec < < message - > getParameter2 ( ) < < std : : endl ;
2016-06-15 23:48:41 +02:00
break ;
}
}
2018-07-12 16:29:32 +02:00
# endif
2016-06-15 23:48:41 +02:00
void EventManager : : lockMutex ( ) {
2018-07-12 16:29:32 +02:00
mutex - > lockMutex ( MutexIF : : NO_TIMEOUT ) ;
2016-06-15 23:48:41 +02:00
}
void EventManager : : unlockMutex ( ) {
2018-07-12 16:29:32 +02:00
mutex - > unlockMutex ( ) ;
2016-06-15 23:48:41 +02:00
}