Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / examples / Load_Balancing / Load_Balancer_i.h
blobdafa78778de322524e259c3bc627f8bd64f05538
1 /* -*- C++ -*- */
2 //=============================================================================
3 /**
4 * @file Load_Balancer_i.h
6 * Defines classes that implement interfaces in Load_Balancer.idl
8 * @author Marina Spivak <marina@cs.wustl.edu>
9 */
10 //=============================================================================
13 #ifndef LOAD_BALANCER_I_H_
14 #define LOAD_BALANCER_I_H_
16 #include "Load_BalancerS.h"
17 #include "ace/Hash_Map_Manager.h"
18 #include "ace/SString.h"
19 #include "ace/Synch.h"
20 #include "ace/Containers.h"
21 #include "ace/Null_Mutex.h"
23 #if !defined (ACE_LACKS_PRAGMA_ONCE)
24 #pragma once
25 #endif /* ACE_LACKS_PRAGMA_ONCE */
27 class Object_Group_Factory_i :
28 public virtual POA_Load_Balancer::Object_Group_Factory
30 // = TITLE
31 // This class implements Load_Balancer::Object_Group_Factory idl
32 // interface.
34 // = DESCRIPTION
35 // This implementation uses two <ACE_Hash_Map_Manager>s
36 // to store <Group_ID> to <Object_Group> associations for all
37 // load balancing groups created by this factory (one map keeps
38 // track of all random groups, and the other keeps track of all
39 // round robin groups).
41 public:
42 /// Constructor.
43 Object_Group_Factory_i ();
45 /// Destructor.
46 ~Object_Group_Factory_i ();
48 // = Load_Balancer::Object_Group_Factory idl methods.
50 /**
51 * Creates an <Object_Group> that resolves requests for arbitrary
52 * members in round robin order. If an <Object_Group>, of any
53 * type, with Group_ID <id> has already been created by this
54 * factory, and hasn't been destroyed, a <duplicate_group>
55 * exception is thrown.
57 Load_Balancer::Object_Group_ptr make_round_robin (const char * id);
59 /**
60 * Creates an <Object_Group> that resolves requests for arbitrary
61 * members in random order. If an <Object_Group>, of any
62 * type, with Group_ID <id> has already been created by this
63 * factory, and hasn't been destroyed, a <duplicate_group>
64 * exception is thrown.
66 Load_Balancer::Object_Group_ptr make_random (const char * id);
68 /**
69 * Locates and returns an <Object_Group> by its <Group_ID>. If
70 * no <Object_Group> has <Group_ID> of <id>, throw a
71 * <no_such_group> exception.
73 Load_Balancer::Object_Group_ptr resolve (const char * id);
75 /**
76 * Lists all the round robin <Object_Group>s which were created
77 * by this factory, and haven't been destroyed yet, i.e., return
78 * a sequence of <Group_ID>s of all existing round robin
79 * <Object_Group>s created by this factory.
81 Load_Balancer::Group_List * round_robin_groups ();
83 /**
84 * Lists all the random <Object_Group>s which were created
85 * by this factory, and haven't been destroyed yet, i.e., return
86 * a sequence of <Group_ID>s of all existing random
87 * <Object_Group>s created by this factory.
89 Load_Balancer::Group_List * random_groups ();
91 // = Implementation detail methods.
93 /**
94 * This method is invoked by an <Object_Group> with group id <id> when it
95 * is being destroyed. The method removes entry corresponding to
96 * group <id> from <random_groups_> if <random> is 1 or from
97 * <rr_groups_> if <random> is 0. This recycles <id>, allowing it
98 * to be used for new <Object_Group>, and prevents the destroyed
99 * group from being included in lists returned from <random_groups>
100 * and <round_robin_groups> methods.
102 void remove_group (const ACE_CString &id, int random);
104 private:
105 // = Helper methods.
108 * This function factors out common code in <make_round_robin> and
109 * <make_random>. Creates a random <Object_Group> if <random> parameter is
110 * set to 1 and round robin <Object_Group> if it is 0.
112 Load_Balancer::Object_Group_ptr make_group (int random,
113 const char * id);
116 * This function factors out common code in <random_groups> and
117 * <round_robin_groups>. Returns a sequence of its random
118 * groups if <random> parameter is set to 1 and a sequence of its
119 * round robin groups if it is 0.
121 Load_Balancer::Group_List * list_groups (int random);
123 /// Typedef for ease of use: hash map associating group ids to
124 /// <Object_Group> references.
125 typedef ACE_Hash_Map_Manager<ACE_CString,
126 Load_Balancer::Object_Group_var, ACE_Null_Mutex> HASH_MAP;
128 /// Map containing all random <Object_Group>s created by this factory.
129 HASH_MAP random_groups_;
131 /// Map containing all round robin <Object_Group>s created by this factory.
132 HASH_MAP rr_groups_;
135 class Object_Group_i : public virtual POA_Load_Balancer::Object_Group
138 // = TITLE
139 // This abstract class partially implements
140 // Load_Balancer::Object_Group idl interface.
142 // = DESCRIPTION
143 // <Resolve> is the only abstract method - subclasses should
144 // define it in order to implement an appropriate load balancing
145 // policy. Other methods can be overridden as needed. This class
146 // factors out code common to <Object_Group> implementations with
147 // different load balancing policies.
149 public:
150 /// Constructor.
151 Object_Group_i (const char * id,
152 Object_Group_Factory_i * my_factory);
154 /// Destructor.
155 ~Object_Group_i ();
157 // = Load_Balancer::Object_Group idl methods.
159 /// Get group's id.
160 char * id ();
163 * Adds a new <member> to the <Object_Group>. Note that each
164 * <Member_ID> in an <Object_Group> must be unique. If the
165 * group already contains a member with the same <Member_ID>, a
166 * <duplicate_member> exceptions is thrown.
168 void bind (const Load_Balancer::Member & member);
171 * Removes a member with the specified <Member_ID> from the
172 * <Object_Group>. If none of the group's members have a
173 * Member_ID of <id>, <no_such_member> exception is thrown.
175 void unbind (const char * id);
178 * Returns a member object from this <Object_Group> in accordance with
179 * load balancing policy it implements, i.e., ``random'' or
180 * ``round robin.'' If the group contains no members, <no_such_member>
181 * exception is thrown.
183 CORBA::Object_ptr resolve () = 0;
186 * Returns an object with the specified <Member_ID>. If this
187 * <Object_Group> contains no members with the specified
188 * <Member_ID>, <no_such_member> exception is thrown.
190 CORBA::Object_ptr resolve_with_id (const char * id);
192 /// Return a sequence of <Member_ID>s of all of its members.
193 Load_Balancer::Member_ID_List * members ();
196 * Cleanup the resources associated with this <Object_Group>.
197 * Subsequent calls to this <Object_Group> should fail, and its
198 * <id> should become available. <Object_Group_Factory>
199 * should no longer list this <Object_Group>.
201 void destroy ();
203 protected:
204 /// Typedefs for ease of use.
205 typedef ACE_DLList<ACE_CString> LIST;
206 typedef ACE_DLList_Iterator<ACE_CString> ITERATOR;
207 typedef ACE_Hash_Map_Manager<ACE_CString, CORBA::Object_var,
208 ACE_Null_Mutex> HASH_MAP;
210 /// List of ids of all the members of this group.
211 LIST member_id_list_;
213 /// Mapping of member_id to obj for all the members of this group.
214 HASH_MAP members_;
216 // Note, we store information redundantly in this implementation,
217 // i.e., both <member_id_list_> and <members_> store member ids.
218 // However, this redundancy eases/speeds up the implementation of
219 // certain operations. <member_id_list_> is useful for implementing
220 // variations of <resolve> method to implement different policies.
221 // <members_> is useful for doing id-based look-up.
223 /// This group's id.
224 ACE_CString id_;
227 * Pointer to the <Object_Group_Factory> servant, which created this
228 * <Object_Group> servant. We need this pointer to be able to
229 * notify the factory when this <Object_Group> is destroyed. Upon
230 * notification, the factory can update its records and release
231 * resources as necessary.
233 Object_Group_Factory_i *my_factory_;
238 * @class Random_Object_Group
240 * @brief This class implements <Object_Group> idl interface with the
241 * random policy for <resolve>.
243 class Random_Object_Group : public Object_Group_i
245 public:
246 /// Constructor.
247 Random_Object_Group (const char *id,
248 Object_Group_Factory_i *my_factory);
250 /// Destructor.
251 ~Random_Object_Group ();
253 /// Returns a member object from this <Object_Group> in accordance with
254 /// the "random" load balancing policy.
255 CORBA::Object_ptr resolve ();
258 * Cleanup the resources associated with this <Object_Group>.
259 * Subsequent calls to this <Object_Group> should fail, and its
260 * <id> should become available. <Object_Group_Factory>
261 * should no longer list this <Object_Group>.
263 void destroy ();
267 * @class RR_Object_Group:
269 * @brief This class implements <Object_Group> idl interface with the
270 * round robin policy for <resolve>.
272 class RR_Object_Group: public Object_Group_i
274 public:
275 /// Constructor.
276 RR_Object_Group (const char *id,
277 Object_Group_Factory_i *my_factory);
279 /// Destructor.
280 ~RR_Object_Group ();
283 * We need to override the implementation of <unbind> from
284 * Object_Group_i to make sure <resolve>
285 * works correctly.
287 void unbind (const char * id);
289 /// Returns a member object from this <Object_Group> in accordance with
290 /// the "round robin" load balancing policy.
291 CORBA::Object_ptr resolve ();
294 * Cleanup the resources associated with this <Object_Group>.
295 * Subsequent calls to this <Object_Group> should fail, and its
296 * <id> should become available. <Object_Group_Factory>
297 * should no longer list this <Object_Group>.
299 void destroy ();
301 private:
302 /// Index into the Object_Group_i::member_id_list_: keeps track of
303 /// the member_id to return on the next invocation of <resolve>.
304 size_t next_;
307 #endif /* LOAD_BALANCER_I_H_ */