Commit ac12047f68955770c0a7326bd21047c063577437

Authored by Grzegorz Jabłoński
1 parent f68550b1

Cleaned up default servant example

cpp11/Ice/default_servant/EvictorBase.cpp deleted 100644 → 0
1 -#include <EvictorBase.h>  
2 -  
3 -using namespace std;  
4 -  
5 -EvictorBase::EvictorBase(int size) :  
6 - _size(size)  
7 -{  
8 - if (_size < 0)  
9 - {  
10 - _size = 1000;  
11 - }  
12 -}  
13 -  
14 -shared_ptr<Ice::Object>  
15 -EvictorBase::locate(const Ice::Current& c, shared_ptr<void>& cookie)  
16 -{  
17 - lock_guard<mutex> lk(_mutex);  
18 -  
19 - //  
20 - // Check if we have a servant in the map already.  
21 - //  
22 - shared_ptr<EvictorEntry> entry;  
23 - auto i = _map.find(c.id);  
24 - if(i != _map.end())  
25 - {  
26 - //  
27 - // Got an entry already, dequeue the entry from its current position.  
28 - //  
29 - entry = i->second;  
30 - _queue.erase(entry->queuePos);  
31 - }  
32 - else  
33 - {  
34 - //  
35 - // We do not have an entry. Ask the derived class to  
36 - // instantiate a servant and add a new entry to the map.  
37 - //  
38 - entry = make_shared<EvictorEntry>();  
39 - entry->servant = add(c, entry->userCookie); // Down-call  
40 - if(!entry->servant)  
41 - {  
42 - return 0;  
43 - }  
44 - entry->useCount = 0;  
45 - i = _map.insert(make_pair(c.id, entry)).first;  
46 - }  
47 -  
48 - //  
49 - // Increment the use count of the servant and enqueue  
50 - // the entry at the front, so we get LRU order.  
51 - //  
52 - ++(entry->useCount);  
53 - entry->queuePos = _queue.insert(_queue.begin(), i);  
54 -  
55 - cookie = entry;  
56 -  
57 - return entry->servant;  
58 -}  
59 -  
60 -void  
61 -EvictorBase::finished(const Ice::Current&, const shared_ptr<Ice::Object>&, const shared_ptr<void>& cookie)  
62 -{  
63 - lock_guard<mutex> lk(_mutex);  
64 -  
65 - auto entry = static_pointer_cast<EvictorEntry>(cookie);  
66 -  
67 - //  
68 - // Decrement use count and check if there is something to evict.  
69 - //  
70 - --(entry->useCount);  
71 - evictServants();  
72 -}  
73 -  
74 -void  
75 -EvictorBase::deactivate(const string&)  
76 -{  
77 - lock_guard<mutex> lk(_mutex);  
78 -  
79 - _size = 0;  
80 - evictServants();  
81 -}  
82 -  
83 -void  
84 -EvictorBase::evictServants()  
85 -{  
86 - //  
87 - // If the evictor queue has grown larger than the limit,  
88 - // look at the excess elements to see whether any of them  
89 - // can be evicted.  
90 - //  
91 - auto p = _queue.rbegin();  
92 - auto excessEntries = static_cast<int>(_map.size() - _size);  
93 -  
94 - for(int i = 0; i < excessEntries; ++i)  
95 - {  
96 - auto mapPos = *p;  
97 - if(mapPos->second->useCount == 0)  
98 - {  
99 - evict(mapPos->second->servant, mapPos->second->userCookie); // Down-call  
100 - p = EvictorQueue::reverse_iterator(_queue.erase(mapPos->second->queuePos));  
101 - _map.erase(mapPos);  
102 - }  
103 - else  
104 - {  
105 - ++p;  
106 - }  
107 - }  
108 -}  
cpp11/Ice/default_servant/EvictorBase.h deleted 100644 → 0
1 -#ifndef EVICTORBASE_H  
2 -#define EVICTORBASE_H  
3 -  
4 -#include <Ice/Ice.h>  
5 -#include <map>  
6 -#include <list>  
7 -#include <mutex>  
8 -  
9 -class EvictorBase : public Ice::ServantLocator  
10 -{  
11 -public:  
12 -  
13 - EvictorBase(int size = 1000);  
14 -  
15 - virtual std::shared_ptr<Ice::Object> locate(const Ice::Current&, std::shared_ptr<void>&) override;  
16 - virtual void finished(const Ice::Current&, const std::shared_ptr<Ice::Object>&, const std::shared_ptr<void>&) override;  
17 - virtual void deactivate(const std::string&) override;  
18 -  
19 -protected:  
20 -  
21 - virtual std::shared_ptr<Ice::Object> add(const Ice::Current&, std::shared_ptr<void>&) = 0;  
22 - virtual void evict(const std::shared_ptr<Ice::Object>&, const std::shared_ptr<void>&) = 0;  
23 -  
24 -private:  
25 -  
26 - struct EvictorEntry;  
27 -  
28 - using EvictorMap = std::map<Ice::Identity, std::shared_ptr<EvictorEntry>>;  
29 - using EvictorQueue = std::list<EvictorMap::iterator>;  
30 -  
31 - struct EvictorEntry  
32 - {  
33 - std::shared_ptr<Ice::Object> servant;  
34 - std::shared_ptr<void> userCookie;  
35 - EvictorQueue::iterator queuePos;  
36 - int useCount;  
37 - };  
38 -  
39 - EvictorMap _map;  
40 - EvictorQueue _queue;  
41 - int _size;  
42 -  
43 - std::mutex _mutex;  
44 -  
45 - void evictServants();  
46 -};  
47 -#endif  
cpp11/Ice/default_servant/Server.cpp
@@ -4,7 +4,6 @@ @@ -4,7 +4,6 @@
4 4
5 #include <Ice/Ice.h> 5 #include <Ice/Ice.h>
6 #include <Printer.h> 6 #include <Printer.h>
7 -#include "EvictorBase.h"  
8 7
9 using namespace std; 8 using namespace std;
10 using namespace Demo; 9 using namespace Demo;