Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Events / Events_Loop.cpp
1 // File:        Events_Loop.hxx
2 // Created:     Thu Mar 13 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Events_Loop.h>
6 #include <Events_MessageGroup.h>
7
8 #include <string>
9 #include <cstring>
10
11 using namespace std;
12
13 Events_Loop* Events_Loop::loop()
14 {
15   // initialized on initialization of the application
16   static Events_Loop MAIN_LOOP;
17   return &MAIN_LOOP;
18 }
19
20 Events_ID Events_Loop::eventByName(const char* theName)
21 {
22   ///! All events created in this session, uniquely identified by the text and char pointer
23   static map<string, char*> CREATED_EVENTS;
24   char* aResult;
25   string aName(theName);
26   map<string, char*>::iterator aFound = CREATED_EVENTS.find(aName);
27   if (aFound == CREATED_EVENTS.end()) {  //not created yet
28     aResult = strdup(theName);  // copy to make unique internal pointer
29     CREATED_EVENTS[aName] = aResult;
30   } else
31     aResult = aFound->second;
32
33   return Events_ID(aResult);
34 }
35
36 void Events_Loop::send(Events_Message& theMessage, bool isGroup)
37 {
38   // if it is grouped message, just accumulate it
39   if (isGroup) {
40     Events_MessageGroup* aGroup = dynamic_cast<Events_MessageGroup*>(&theMessage);
41     if (aGroup) {
42       std::map<char*, Events_MessageGroup*>::iterator aMyGroup = myGroups.find(
43           aGroup->eventID().eventText());
44       if (aMyGroup == myGroups.end()) {  // create a new group of messages for accumulation
45         myGroups[aGroup->eventID().eventText()] = aGroup->newEmpty();
46         aMyGroup = myGroups.find(aGroup->eventID().eventText());
47       }
48       aMyGroup->second->Join(*aGroup);
49       return;
50     }
51   }
52
53   // TO DO: make it in thread and with usage of semaphores
54
55   map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
56       theMessage.eventID().eventText());
57   if (aFindID != myListeners.end()) {
58     map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(
59         theMessage.sender());
60     if (aFindSender != aFindID->second.end()) {
61       list<Events_Listener*>& aListeners = aFindSender->second;
62       for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
63         (*aL)->processEvent(&theMessage);
64     }
65     if (theMessage.sender()) {  // also call for NULL senders registered
66       aFindSender = aFindID->second.find(NULL);
67       if (aFindSender != aFindID->second.end()) {
68         list<Events_Listener*>& aListeners = aFindSender->second;
69         for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
70           (*aL)->processEvent(&theMessage);
71       }
72     }
73   }
74 }
75
76 void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
77                                    void* theSender)
78 {
79   map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
80       theID.eventText());
81   if (aFindID == myListeners.end()) {  // create container associated with ID
82     myListeners[theID.eventText()] = map<void*, list<Events_Listener*> >();
83     aFindID = myListeners.find(theID.eventText());
84   }
85
86   map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(theSender);
87   if (aFindSender == aFindID->second.end()) {  // create container associated with sender
88     aFindID->second[theSender] = list<Events_Listener*>();
89     aFindSender = aFindID->second.find(theSender);
90   }
91   // check that listener was not registered wit hsuch parameters before
92   list<Events_Listener*>& aListeners = aFindSender->second;
93   for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
94     if (*aL == theListener)
95       return;  // avoid duplicates
96
97   aListeners.push_back(theListener);
98 }
99
100 void Events_Loop::flush(const Events_ID& theID)
101 {
102   std::map<char*, Events_MessageGroup*>::iterator aMyGroup = myGroups.find(theID.eventText());
103   if (aMyGroup != myGroups.end()) {  // really sends
104     Events_MessageGroup* aGroup = aMyGroup->second;
105     myGroups.erase(aMyGroup);
106     send(*aGroup, false);
107     delete aGroup;
108   }
109 }