Salome HOME
Merge remote-tracking branch 'remotes/origin/MessagesGroups' into SolveSpace_MessageG...
[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)
37 {
38   // if it is grouped message, just accumulate it
39   Events_MessageGroup* aGroup = dynamic_cast<Events_MessageGroup*>(&theMessage);
40   if (aGroup) {
41     std::map<char*, Events_MessageGroup*>::iterator aMyGroup = 
42       myGroups.find(aGroup->eventID().eventText());
43     if (aMyGroup == myGroups.end()) { // create a new group of messages for accumulation
44       myGroups[aGroup->eventID().eventText()] = aGroup->newEmpty();
45       aMyGroup = myGroups.find(aGroup->eventID().eventText());
46     }
47     aMyGroup->second->Join(*aGroup);
48     return;
49   }
50
51   // TO DO: make it in thread and with usage of semaphores
52
53   map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
54       theMessage.eventID().eventText());
55   if (aFindID != myListeners.end()) {
56     map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(
57         theMessage.sender());
58     if (aFindSender != aFindID->second.end()) {
59       list<Events_Listener*>& aListeners = aFindSender->second;
60       for(list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
61         (*aL)->processEvent(&theMessage);
62     }
63     if (theMessage.sender()) { // also call for NULL senders registered
64       aFindSender = aFindID->second.find(NULL);
65       if (aFindSender != aFindID->second.end()) {
66         list<Events_Listener*>& aListeners = aFindSender->second;
67         for(list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
68           (*aL)->processEvent(&theMessage);
69       }
70     }
71   }
72 }
73
74 void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
75                                   void* theSender)
76 {
77   map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
78       theID.eventText());
79   if (aFindID == myListeners.end()) { // create container associated with ID
80     myListeners[theID.eventText()] = map<void*, list<Events_Listener*> >();
81     aFindID = myListeners.find(theID.eventText());
82   }
83
84   map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(theSender);
85   if (aFindSender == aFindID->second.end()) { // create container associated with sender
86     aFindID->second[theSender] = list<Events_Listener*>();
87     aFindSender = aFindID->second.find(theSender);
88   }
89   // check that listener was not registered wit hsuch parameters before
90   list<Events_Listener*>& aListeners = aFindSender->second;
91   for(list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
92     if (*aL == theListener)
93       return; // avoid duplicates
94
95   aListeners.push_back(theListener);
96 }
97
98 void Events_Loop::flush(const Events_ID& theID)
99 {
100   std::map<char*, Events_MessageGroup*>::iterator aMyGroup = 
101     myGroups.find(theID.eventText());
102   if (aMyGroup != myGroups.end()) { // really sends
103     Events_MessageGroup* aGroup = aMyGroup->second;
104     send(*aGroup);
105     myGroups.erase(aMyGroup);
106     delete aGroup;
107   }
108 }