2
// Load_Balancer_i.h,v 1.7 2003/11/04 05:21:30 dhinton Exp
3
// ============================================================================
9
// Defines classes that implement interfaces in Load_Balancer.idl
12
// Marina Spivak <marina@cs.wustl.edu>
14
// ============================================================================
16
#ifndef LOAD_BALANCER_I_H_
17
#define LOAD_BALANCER_I_H_
19
#include "Load_BalancerS.h"
20
#include "ace/Hash_Map_Manager.h"
21
#include "ace/SString.h"
22
#include "ace/Synch.h"
23
#include "ace/Containers.h"
24
#include "ace/Null_Mutex.h"
26
#if !defined (ACE_LACKS_PRAGMA_ONCE)
28
#endif /* ACE_LACKS_PRAGMA_ONCE */
30
// This is to remove "inherits via dominance" warnings from MSVC.
31
#if defined (_MSC_VER)
32
# pragma warning (disable : 4250)
35
class Object_Group_Factory_i :
36
public virtual POA_Load_Balancer::Object_Group_Factory,
37
public virtual PortableServer::RefCountServantBase
40
// This class implements Load_Balancer::Object_Group_Factory idl
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).
52
// = Initialization and termination methods.
54
Object_Group_Factory_i (void);
57
~Object_Group_Factory_i (void);
60
// = Load_Balancer::Object_Group_Factory idl methods.
62
Load_Balancer::Object_Group_ptr make_round_robin (const char * id
64
ACE_THROW_SPEC ((CORBA::SystemException,
65
Load_Balancer::duplicate_group));
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_random (const char * id
74
ACE_THROW_SPEC ((CORBA::SystemException,
75
Load_Balancer::duplicate_group));
76
// Creates an <Object_Group> that resolves requests for arbitrary
77
// members in random order. If an <Object_Group>, of any
78
// type, with Group_ID <id> has already been created by this
79
// factory, and hasn't been destroyed, a <duplicate_group>
80
// exception is thrown.
82
Load_Balancer::Object_Group_ptr resolve (const char * id
84
ACE_THROW_SPEC ((CORBA::SystemException,
85
Load_Balancer::no_such_group));
86
// Locates and returns an <Object_Group> by its <Group_ID>. If
87
// no <Object_Group> has <Group_ID> of <id>, throw a
88
// <no_such_group> exception.
90
Load_Balancer::Group_List * round_robin_groups (ACE_ENV_SINGLE_ARG_DECL)
91
ACE_THROW_SPEC ((CORBA::SystemException));
92
// Lists all the round robin <Object_Group>s which were created
93
// by this factory, and haven't been destroyed yet, i.e., return
94
// a sequence of <Group_ID>s of all existing round robin
95
// <Object_Group>s created by this factory.
97
Load_Balancer::Group_List * random_groups (ACE_ENV_SINGLE_ARG_DECL)
98
ACE_THROW_SPEC ((CORBA::SystemException));
99
// Lists all the random <Object_Group>s which were created
100
// by this factory, and haven't been destroyed yet, i.e., return
101
// a sequence of <Group_ID>s of all existing random
102
// <Object_Group>s created by this factory.
104
// = Implementation detail methods.
106
void remove_group (const ACE_CString &id, int random);
107
// This method is invoked by an <Object_Group> with group id <id> when it
108
// is being destroyed. The method removes entry corresponding to
109
// group <id> from <random_groups_> if <random> is 1 or from
110
// <rr_groups_> if <random> is 0. This recycles <id>, allowing it
111
// to be used for new <Object_Group>, and prevents the destroyed
112
// group from being included in lists returned from <random_groups>
113
// and <round_robin_groups> methods.
118
Load_Balancer::Object_Group_ptr make_group (int random,
121
ACE_THROW_SPEC ((CORBA::SystemException,
122
Load_Balancer::duplicate_group));
123
// This function factors out common code in <make_round_robin> and
124
// <make_random>. Creates a random <Object_Group> if <random> parameter is
125
// set to 1 and round robin <Object_Group> if it is 0.
127
Load_Balancer::Group_List * list_groups (int random
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
typedef ACE_Hash_Map_Manager<ACE_CString,
135
Load_Balancer::Object_Group_var, ACE_Null_Mutex> HASH_MAP;
136
// Typedef for ease of use: hash map associating group ids to
137
// <Object_Group> references.
139
HASH_MAP random_groups_;
140
// Map containing all random <Object_Group>s created by this factory.
143
// Map containing all round robin <Object_Group>s created by this factory.
146
class Object_Group_i : public virtual POA_Load_Balancer::Object_Group,
147
public virtual PortableServer::RefCountServantBase
151
// This abstract class partially implements
152
// Load_Balancer::Object_Group idl interface.
155
// <Resolve> is the only abstract method - subclasses should
156
// define it in order to implement an appropriate load balancing
157
// policy. Other methods can be overridden as needed. This class
158
// factors out code common to <Object_Group> implementations with
159
// different load balancing policies.
163
// = Initialization and termination methods.
165
Object_Group_i (const char * id,
166
Object_Group_Factory_i * my_factory);
169
~Object_Group_i (void);
172
// = Load_Balancer::Object_Group idl methods.
174
char * id (ACE_ENV_SINGLE_ARG_DECL)
175
ACE_THROW_SPEC ((CORBA::SystemException));
178
void bind (const Load_Balancer::Member & member
180
ACE_THROW_SPEC ((CORBA::SystemException,
181
Load_Balancer::duplicate_member));
182
// Adds a new <member> to the <Object_Group>. Note that each
183
// <Member_ID> in an <Object_Group> must be unique. If the
184
// group already contains a member with the same <Member_ID>, a
185
// <duplicate_member> exceptions is thrown.
187
void unbind (const char * id
189
ACE_THROW_SPEC ((CORBA::SystemException,
190
Load_Balancer::no_such_member));
191
// Removes a member with the specified <Member_ID> from the
192
// <Object_Group>. If none of the group's members have a
193
// Member_ID of <id>, <no_such_member> exception is thrown.
195
CORBA::Object_ptr resolve (ACE_ENV_SINGLE_ARG_DECL)
196
ACE_THROW_SPEC ((CORBA::SystemException,
197
Load_Balancer::no_such_member)) = 0;
198
// Returns a member object from this <Object_Group> in accordance with
199
// load balancing policy it implements, i.e., ``random'' or
200
// ``round robin.'' If the group contains no members, <no_such_member>
201
// exception is thrown.
203
CORBA::Object_ptr resolve_with_id (const char * id
205
ACE_THROW_SPEC ((CORBA::SystemException,
206
Load_Balancer::no_such_member));
207
// Returns an object with the specified <Member_ID>. If this
208
// <Object_Group> contains no members with the specified
209
// <Member_ID>, <no_such_member> exception is thrown.
211
Load_Balancer::Member_ID_List * members (ACE_ENV_SINGLE_ARG_DECL)
212
ACE_THROW_SPEC ((CORBA::SystemException));
213
// Return a sequence of <Member_ID>s of all of its members.
215
void destroy (ACE_ENV_SINGLE_ARG_DECL)
216
ACE_THROW_SPEC ((CORBA::SystemException));
217
// Cleanup the resources associated with this <Object_Group>.
218
// Subsequent calls to this <Object_Group> should fail, and its
219
// <id> should become available. <Object_Group_Factory>
220
// should no longer list this <Object_Group>.
224
typedef ACE_DLList<ACE_CString> LIST;
225
typedef ACE_DLList_Iterator<ACE_CString> ITERATOR;
226
typedef ACE_Hash_Map_Manager<ACE_CString, CORBA::Object_var,
227
ACE_Null_Mutex> HASH_MAP;
228
// Typedefs for ease of use.
230
LIST member_id_list_;
231
// List of ids of all the members of this group.
234
// Mapping of member_id to obj for all the members of this group.
236
// Note, we store information redundantly in this implementation,
237
// i.e., both <member_id_list_> and <members_> store member ids.
238
// However, this redundancy eases/speeds up the implementation of
239
// certain operations. <member_id_list_> is useful for implementing
240
// variations of <resolve> method to implement different policies.
241
// <members_> is useful for doing id-based look-up.
246
Object_Group_Factory_i *my_factory_;
247
// Pointer to the <Object_Group_Factory> servant, which created this
248
// <Object_Group> servant. We need this pointer to be able to
249
// notify the factory when this <Object_Group> is destroyed. Upon
250
// notification, the factory can update its records and release
251
// resources as necessary.
255
class Random_Object_Group : public Object_Group_i
258
// This class implements <Object_Group> idl interface with the
259
// random policy for <resolve>.
262
Random_Object_Group (const char *id,
263
Object_Group_Factory_i *my_factory);
266
~Random_Object_Group (void);
269
CORBA::Object_ptr resolve (ACE_ENV_SINGLE_ARG_DECL)
270
ACE_THROW_SPEC ((CORBA::SystemException,
271
Load_Balancer::no_such_member));
272
// Returns a member object from this <Object_Group> in accordance with
273
// the "random" load balancing policy.
275
void destroy (ACE_ENV_SINGLE_ARG_DECL)
276
ACE_THROW_SPEC ((CORBA::SystemException));
277
// Cleanup the resources associated with this <Object_Group>.
278
// Subsequent calls to this <Object_Group> should fail, and its
279
// <id> should become available. <Object_Group_Factory>
280
// should no longer list this <Object_Group>.
283
class RR_Object_Group: public Object_Group_i
286
// This class implements <Object_Group> idl interface with the
287
// round robin policy for <resolve>.
291
RR_Object_Group (const char *id,
292
Object_Group_Factory_i *my_factory);
295
~RR_Object_Group (void);
298
void unbind (const char * id
300
ACE_THROW_SPEC ((CORBA::SystemException,
301
Load_Balancer::no_such_member));
302
// We need to override the implementation of <unbind> from
303
// Object_Group_i to make sure <resolve>
306
CORBA::Object_ptr resolve (ACE_ENV_SINGLE_ARG_DECL)
307
ACE_THROW_SPEC ((CORBA::SystemException,
308
Load_Balancer::no_such_member));
309
// Returns a member object from this <Object_Group> in accordance with
310
// the "round robin" load balancing policy.
312
void destroy (ACE_ENV_SINGLE_ARG_DECL)
313
ACE_THROW_SPEC ((CORBA::SystemException));
314
// Cleanup the resources associated with this <Object_Group>.
315
// Subsequent calls to this <Object_Group> should fail, and its
316
// <id> should become available. <Object_Group_Factory>
317
// should no longer list this <Object_Group>.
322
// Index into the Object_Group_i::member_id_list_: keeps track of
323
// the member_id to return on the next invocation of <resolve>.
326
#endif /* LOAD_BALANCER_I_H_ */