=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / examples / Load_Balancing_persistent / Load_Balancer_i.h
blob76defd00883c65ea9dadb4ce1776f494d18e9f77
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> with modifications by Bala Natarajan <bala@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_With_Allocator_T.h"
18 #include "ace/SString.h"
19 #include "ace/Synch.h"
20 #include "ace/Containers.h"
21 #include "ace/Stats.h"
22 #include "ace/Throughput_Stats.h"
23 #include "ace/High_Res_Timer.h"
24 #include "ace/Memory_Pool.h"
25 #include "ace/Malloc_T.h"
27 #if !defined (ACE_LACKS_PRAGMA_ONCE)
28 #pragma once
29 #endif /* ACE_LACKS_PRAGMA_ONCE */
31 typedef ACE_Allocator_Adapter <ACE_Malloc<ACE_MMAP_MEMORY_POOL,
32 TAO_SYNCH_MUTEX> > ALLOCATOR;
34 typedef ACE_Hash_Map_With_Allocator<char *, char *> HASH_MAP;
36 class Object_Group_Factory_i :
37 public virtual POA_Load_Balancer::Object_Group_Factory
39 // = TITLE
40 // This class implements Load_Balancer::Object_Group_Factory idl
41 // interface.
43 // = DESCRIPTION
44 // This implementation uses two <ACE_Hash_Map_Manager>s
45 // to store <Group_ID> to <Object_Group> associations for all
46 // load balancing groups created by this factory (one map keeps
47 // track of all random groups, and the other keeps track of all
48 // round robin groups).
50 public:
51 /// Constructor.
52 Object_Group_Factory_i (CORBA::ORB_ptr orb,
53 PortableServer::POA_ptr poa);
55 /// Destructor.
56 ~Object_Group_Factory_i ();
59 /// Method for the POA that will return the persistent POA_ptr stored
60 /// in here..
61 PortableServer::POA_ptr _default_POA ();
63 // = Load_Balancer::Object_Group_Factory idl methods.
65 /**
66 * Creates an <Object_Group> that resolves requests for arbitrary
67 * members in round robin order. If an <Object_Group>, of any
68 * type, with Group_ID <id> has already been created by this
69 * factory, and hasn't been destroyed, a <duplicate_group>
70 * exception is thrown.
72 Load_Balancer::Object_Group_ptr make_round_robin (const char * id);
74 void unbind_round_robin (const char *id);
77 /**
78 * Creates an <Object_Group> that resolves requests for arbitrary
79 * members in random order. If an <Object_Group>, of any
80 * type, with Group_ID <id> has already been created by this
81 * factory, and hasn't been destroyed, a <duplicate_group>
82 * exception is thrown.
84 Load_Balancer::Object_Group_ptr make_random (const char * id);
86 void unbind_random (const char *id);
88 /**
89 * Locates and returns an <Object_Group IOR> by its <Group_ID>. If
90 * no <Object_Group> has <Group_ID> of <id>, throw a
91 * <no_such_group> exception.
93 Load_Balancer::Object_Group_ptr resolve (const char * id);
95 /**
96 * Lists all the round robin <Object_Group>s which were created
97 * by this factory, and haven't been destroyed yet, i.e., return
98 * a sequence of <Group_ID>s of all existing round robin
99 * <Object_Group>s created by this factory.
101 Load_Balancer::Group_List * round_robin_groups ();
104 * Lists all the random <Object_Group>s which were created
105 * by this factory, and haven't been destroyed yet, i.e., return
106 * a sequence of <Group_ID>s of all existing random
107 * <Object_Group>s created by this factory.
109 Load_Balancer::Group_List * random_groups ();
111 private:
112 /// Our ORB
113 CORBA::ORB_var orb_;
115 /// Our POA
116 PortableServer::POA_var poa_;
118 // = Helper methods.
121 * This function factors out common code in <make_round_robin> and
122 * <make_random>. Creates a random <Object_Group> if <random> parameter is
123 * set to 1 and round robin <Object_Group> if it is 0.
125 Load_Balancer::Object_Group_ptr make_group (int random,
126 const char * id);
129 * This function factors out common code in <random_groups> and
130 * <round_robin_groups>. Returns a sequence of its random
131 * groups if <random> parameter is set to 1 and a sequence of its
132 * round robin groups if it is 0.
134 Load_Balancer::Group_List * list_groups (int random);
136 /// The helper that updates the vlaue of the variable flags_
137 void update_flags (int random);
139 /// This rolls back the status of the objects in the POA if the
140 /// service had failed..
141 void update_objects ();
143 /// Map containing all random <Object_Group>s created by this factory.
144 HASH_MAP *random_groups_;
146 /// Map containing all round robin <Object_Group>s created by this factory.
147 HASH_MAP *rr_groups_;
149 /// Memory pool that will have the data
150 ALLOCATOR *mem_pool_;
153 * This would be kind of a hack.. As I am not able to think of
154 * anything at present let us live with this.. OK.. Here is how it
155 * works.. This value will be stored in the MMAP file. If the value
156 * is 1 then the Round Robin group object is registered with the
157 * Services POA. If the value is 2 then the Random group object is
158 * registered with the POA. If the value is 3 both of them are
159 * registered with the POA.. The initial value would be 0 when this
160 * object initialises and binded as "FLAGS"..
162 CORBA::Short *flags_;
164 ACE_Throughput_Stats throughput_;
167 class Object_Group_i
168 : public virtual POA_Load_Balancer::Object_Group
171 // = TITLE
172 // This abstract class partially implements
173 // Load_Balancer::Object_Group idl interface.
175 // = DESCRIPTION
176 // <Resolve> is the only abstract method - subclasses should
177 // define it in order to implement an appropriate load balancing
178 // policy. Other methods can be overridden as needed. This class
179 // factors out code common to <Object_Group> implementations with
180 // different load balancing policies.
182 public:
183 /// Constructor.
184 Object_Group_i (const char * id,
185 PortableServer::POA_ptr poa);
187 /// Destructor.
188 ~Object_Group_i ();
190 // Persistent POA
191 // Method for the POA
192 PortableServer::POA_ptr _default_POA ();
194 // = Load_Balancer::Object_Group idl methods.
196 /// Get group's id.
197 char * id ();
200 * Adds a new <member> to the <Object_Group>. Note that each
201 * <Member_ID> in an <Object_Group> must be unique. If the
202 * group already contains a member with the same <Member_ID>, a
203 * <duplicate_member> exceptions is thrown.
205 void bind (const Load_Balancer::Member & member);
208 * Removes a member with the specified <Member_ID> from the
209 * <Object_Group>. If none of the group's members have a
210 * Member_ID of <id>, <no_such_member> exception is thrown.
212 void unbind (const char * id);
215 * Returns a member object from this <Object_Group> in accordance with
216 * load balancing policy it implements, i.e., ``random'' or
217 * ``round robin.'' If the group contains no members, <no_such_member>
218 * exception is thrown.
220 char * resolve () = 0;
223 * Returns an object with the specified <Member_ID>. If this
224 * <Object_Group> contains no members with the specified
225 * <Member_ID>, <no_such_member> exception is thrown.
227 char * resolve_with_id (const char * id);
229 /// Return a sequence of <Member_ID>s of all of its members.
230 Load_Balancer::Member_ID_List * members ();
233 * Cleanup the resources associated with this <Object_Group>.
234 * Subsequent calls to this <Object_Group> should fail, and its
235 * <id> should become available. <Object_Group_Factory>
236 * should no longer list this <Object_Group>.
238 void destroy ();
240 protected:
241 /// This will replenish all the pointers that could have been lost
242 /// because of failure
243 void read_from_memory ();
245 /// Our POA
246 PortableServer::POA_var poa_;
248 /// Typedefs for ease of use.
249 typedef ACE_DLList<char *> LIST;
250 typedef ACE_DLList_Iterator<char *> ITERATOR;
252 /// List of ids of all the members of this group.
253 LIST *member_id_list_;
255 /// Mapping of member_id to obj for all the members of this group.
256 HASH_MAP *members_;
258 // Note, we store information redundantly in this implementation,
259 // i.e., both <member_id_list_> and <members_> store member ids.
260 // However, this redundancy eases/speeds up the implementation of
261 // certain operations. <member_id_list_> is useful for implementing
262 // variations of <resolve> method to implement different policies.
263 // <members_> is useful for doing id-based look-up.
265 /// This group's id.
266 ACE_CString id_;
268 /// Pointer to the location where I can allocate memory...
269 ALLOCATOR *allocator_;
274 * @class Random_Object_Group
276 * @brief This class implements <Object_Group> idl interface with the
277 * random policy for <resolve>.
279 class Random_Object_Group : public Object_Group_i
281 public:
282 /// Constructor.
283 Random_Object_Group (const char *id,
284 PortableServer::POA_ptr poa);
286 /// Returns a member object from this <Object_Group> in accordance with
287 /// the "random" load balancing policy.
288 char * resolve ();
292 * @class RR_Object_Group:
294 * @brief This class implements <Object_Group> idl interface with the
295 * round robin policy for <resolve>.
297 class RR_Object_Group: public Object_Group_i
299 public:
300 /// Constructor.
301 RR_Object_Group (const char *id,
302 PortableServer::POA_ptr poa);
305 * We need to override the implementation of <unbind> from
306 * Object_Group_i to make sure <resolve>
307 * works correctly.
309 void unbind (const char * id);
311 /// Returns a member object from this <Object_Group> in accordance with
312 /// the "round robin" load balancing policy.
313 char * resolve ();
315 private:
316 /// Index into the Object_Group_i::member_id_list_: keeps track of
317 /// the member_id to return on the next invocation of <resolve>.
318 size_t next_;
321 #endif /* LOAD_BALANCER_I_H_ */