1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_GRID_SERVERCACHE_H
6 #define ICE_GRID_SERVERCACHE_H
7 
8 #include <IceUtil/Mutex.h>
9 #include <IceUtil/Shared.h>
10 #include <Ice/UniquePtr.h>
11 #include <IceGrid/Descriptor.h>
12 #include <IceGrid/Internal.h>
13 #include <IceGrid/Registry.h>
14 #include <IceGrid/Allocatable.h>
15 #include <IceGrid/Cache.h>
16 
17 namespace IceGrid
18 {
19 
20 class ServerCache;
21 class ObjectCache;
22 class AdapterCache;
23 class AllocatableObjectCache;
24 class NodeCache;
25 
26 class NodeEntry;
27 typedef IceUtil::Handle<NodeEntry> NodeEntryPtr;
28 
29 class CheckServerResult;
30 typedef IceUtil::Handle<CheckServerResult> CheckServerResultPtr;
31 
32 class NodeObserverTopic;
33 typedef IceUtil::Handle<NodeObserverTopic> NodeObserverTopicPtr;
34 
35 class CheckUpdateResult : public IceUtil::Shared
36 {
37 public:
38 
39     CheckUpdateResult(const std::string&, const std::string&, bool, bool, const Ice::AsyncResultPtr&);
40 
41     bool getResult();
42 
getServer()43     const std::string& getServer() { return _server; }
44 
45 private:
46 
47     const std::string _server;
48     const std::string _node;
49     const bool _remove;
50     const bool _noRestart;
51     const Ice::AsyncResultPtr _result;
52 };
53 typedef IceUtil::Handle<CheckUpdateResult> CheckUpdateResultPtr;
54 
55 class ServerEntry : public Allocatable
56 {
57 public:
58 
59     ServerEntry(ServerCache&, const std::string&);
60 
61     void sync();
62     void waitForSync(int);
waitForSync()63     void waitForSync()
64     {
65         waitForSync(-1);
66     }
67     void waitForSyncNoThrow(int);
waitForSyncNoThrow()68     void waitForSyncNoThrow()
69     {
70         waitForSyncNoThrow(-1);
71     }
72     void unsync();
73 
74     bool addSyncCallback(const SynchronizationCallbackPtr&);
75 
76     void update(const ServerInfo&, bool);
77 
78     void destroy(bool);
79 
80     ServerInfo getInfo(bool = false) const;
81     std::string getId() const;
82 
83     ServerPrx getProxy(int&, int&, std::string&, bool = true, int = 0);
84     ServerPrx getProxy(bool = true, int = 0);
85     Ice::ObjectPrx getAdminProxy();
86 
87     AdapterPrx getAdapter(const std::string&, bool);
88     AdapterPrx getAdapter(int&, int&, const std::string&, bool);
89     float getLoad(LoadSample) const;
90 
91     bool canRemove();
92     CheckUpdateResultPtr checkUpdate(const ServerInfo&, bool);
93     bool isDestroyed();
94 
95     void loadCallback(const ServerPrx&, const AdapterPrxDict&, int, int);
96     void destroyCallback();
97     void exception(const Ice::Exception&);
98 
99     virtual bool isEnabled() const;
100     virtual void allocated(const SessionIPtr&);
101     virtual void allocatedNoSync(const SessionIPtr&);
102     virtual void released(const SessionIPtr&);
103     virtual void releasedNoSync(const SessionIPtr&);
104 
105 private:
106 
107     void syncImpl();
108     void waitImpl(int);
109     void synchronized();
110     void synchronized(const Ice::Exception&);
111 
112     ServerCache& _cache;
113     const std::string _id;
114     IceInternal::UniquePtr<ServerInfo> _loaded;
115     IceInternal::UniquePtr<ServerInfo> _load;
116     IceInternal::UniquePtr<ServerInfo> _destroy;
117 
118     ServerPrx _proxy;
119     AdapterPrxDict _adapters;
120     int _activationTimeout;
121     int _deactivationTimeout;
122 
123     bool _synchronizing;
124     bool _updated;
125     IceInternal::UniquePtr<Ice::Exception> _exception;
126     bool _noRestart;
127     std::vector<SynchronizationCallbackPtr> _callbacks;
128 
129     SessionIPtr _allocationSession;
130 };
131 typedef IceUtil::Handle<ServerEntry> ServerEntryPtr;
132 typedef std::vector<ServerEntryPtr> ServerEntrySeq;
133 
134 class ServerCache : public CacheByString<ServerEntry>
135 {
136 public:
137 
138 #ifdef __SUNPRO_CC
139     using CacheByString<ServerEntry>::remove;
140 #endif
141 
142     ServerCache(const Ice::CommunicatorPtr&, const std::string&, NodeCache&, AdapterCache&, ObjectCache&,
143                 AllocatableObjectCache&);
144 
145     ServerEntryPtr add(const ServerInfo&);
146     ServerEntryPtr get(const std::string&) const;
147     bool has(const std::string&) const;
148     ServerEntryPtr remove(const std::string&, bool);
149 
150     void preUpdate(const ServerInfo&, bool);
151     ServerEntryPtr postUpdate(const ServerInfo&, bool);
152 
153     void clear(const std::string&);
154 
getNodeCache()155     NodeCache& getNodeCache() const { return _nodeCache; }
getCommunicator()156     Ice::CommunicatorPtr getCommunicator() const { return _communicator; }
getInstanceName()157     const std::string& getInstanceName() const { return _instanceName; }
158 
getNodeObserverTopic()159     const NodeObserverTopicPtr& getNodeObserverTopic() const { return _nodeObserverTopic; }
160     void setNodeObserverTopic(const NodeObserverTopicPtr&);
161 
162 private:
163 
164     void addCommunicator(const CommunicatorDescriptorPtr&, const CommunicatorDescriptorPtr&, const ServerEntryPtr&,
165                          const std::string&);
166     void removeCommunicator(const CommunicatorDescriptorPtr&, const CommunicatorDescriptorPtr&, const ServerEntryPtr&);
167 
168     friend struct AddCommunicator;
169     friend struct RemoveCommunicator;
170 
171     const Ice::CommunicatorPtr _communicator;
172     const std::string _instanceName;
173     NodeCache& _nodeCache;
174     AdapterCache& _adapterCache;
175     ObjectCache& _objectCache;
176     AllocatableObjectCache& _allocatableObjectCache;
177     NodeObserverTopicPtr _nodeObserverTopic;
178 };
179 
180 };
181 
182 #endif
183