Salome HOME
Move Events messages to boost pointers: partially done
[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 #ifdef WIN32
29     aResult = _strdup(theName);  // copy to make unique internal pointer
30 #else
31     aResult = strdup(theName);  // copy to make unique internal pointer
32 #endif
33     CREATED_EVENTS[aName] = aResult;
34   } else
35     aResult = aFound->second;
36
37   return Events_ID(aResult);
38 }
39
40 void Events_Loop::send(const boost::shared_ptr<Events_Message>& theMessage, bool isGroup)
41 {
42   // if it is grouped message, just accumulate it
43   if (isGroup) {
44     boost::shared_ptr<Events_MessageGroup> aGroup = 
45       boost::dynamic_pointer_cast<Events_MessageGroup>(theMessage);
46     if (aGroup) {
47       std::map<char*, boost::shared_ptr<Events_Message> >::iterator aMyGroup = myGroups.find(
48           aGroup->eventID().eventText());
49       if (aMyGroup == myGroups.end()) {  // create a new group of messages for accumulation
50         myGroups[aGroup->eventID().eventText()] = aGroup->newEmpty();
51         aMyGroup = myGroups.find(aGroup->eventID().eventText());
52       }
53       boost::shared_ptr<Events_MessageGroup> aNewOne =
54         boost::dynamic_pointer_cast<Events_MessageGroup>(aMyGroup->second);
55       aGroup->Join(aNewOne);
56       return;
57     }
58   }
59
60   // TO DO: make it in thread and with usage of semaphores
61
62   map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
63       theMessage->eventID().eventText());
64   if (aFindID != myListeners.end()) {
65     map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(
66         theMessage->sender());
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     if (theMessage->sender()) {  // also call for NULL senders registered
73       aFindSender = aFindID->second.find(NULL);
74       if (aFindSender != aFindID->second.end()) {
75         list<Events_Listener*>& aListeners = aFindSender->second;
76         for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
77           (*aL)->processEvent(theMessage);
78       }
79     }
80   }
81 }
82
83 void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
84                                    void* theSender)
85 {
86   map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
87       theID.eventText());
88   if (aFindID == myListeners.end()) {  // create container associated with ID
89     myListeners[theID.eventText()] = map<void*, list<Events_Listener*> >();
90     aFindID = myListeners.find(theID.eventText());
91   }
92
93   map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(theSender);
94   if (aFindSender == aFindID->second.end()) {  // create container associated with sender
95     aFindID->second[theSender] = list<Events_Listener*>();
96     aFindSender = aFindID->second.find(theSender);
97   }
98   // check that listener was not registered wit hsuch parameters before
99   list<Events_Listener*>& aListeners = aFindSender->second;
100   for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
101     if (*aL == theListener)
102       return;  // avoid duplicates
103
104   aListeners.push_back(theListener);
105 }
106
107 void Events_Loop::flush(const Events_ID& theID)
108 {
109   if (!myFlushActive)
110     return;
111   std::map<char*, boost::shared_ptr<Events_Message>>::iterator aMyGroup =
112     myGroups.find(theID.eventText());
113   if (aMyGroup != myGroups.end()) {  // really sends
114     boost::shared_ptr<Events_Message> aGroup = aMyGroup->second;
115     myGroups.erase(aMyGroup);
116     send(aGroup, false);
117   }
118 }
119
120 void Events_Loop::activateFlushes(const bool theActivate)
121 {
122   myFlushActive = theActivate;
123 }