summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/LoadBalancing/HasherFactory.h
blob: 11ffeee1f59a3f152a761bfd5d33dd9b286161cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// -*- C++ -*-
//
// $Id$

//=============================================================================
/**
 * @file HasherFactory.h
 *
 * $Id$
 *
 * @author Ossama Othman <ossama@uci.edu>
 */
//=============================================================================

#ifndef TAO_HASHER_FACTORY_H
#define TAO_HASHER_FACTORY_H

#include "ace/config-all.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
#pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/Functor.h"
#include "ace/Hash_Map_Manager_T.h"

#include "orbsvcs/LoadBalancingS.h"

#include "Hasher_i.h"


/**
 * @class HasherFactory
 *
 * @brief GenericFactory implementation used to create instances of
 *        "Hasher" objects.
 *
 * This factory is only designed to create Hasher objects.  However, a
 * GenericFactory need not be limited creating only one type of
 * object.  This HasherFactory has been designed specifically for this
 * test, and is designed to create objects at multiple logical
 * locations.  For example, the all replicas will reside in the same
 * process but each will reside at different logical locations.
 */
class HasherFactory
  : public virtual POA_LoadBalancing::GenericFactory
{
public:

  /// Constructor.
  HasherFactory (CORBA::ORB_ptr orb,
                 PortableServer::POA_ptr root_poa);

  /// Destructor.
  ~HasherFactory (void);

  /// Return a reference to a "Hasher" object.
  virtual CORBA::Object_ptr create_object (
      const char *type_id,
      const LoadBalancing::Criteria &the_criteria,
      LoadBalancing::GenericFactory::FactoryCreationId_out
        factory_creation_id,
      CORBA::Environment &ACE_TRY_ENV = TAO_default_environment ())
    ACE_THROW_SPEC ((CORBA::SystemException,
                     LoadBalancing::NoFactory,
                     LoadBalancing::ObjectNotCreated,
                     LoadBalancing::InvalidCriteria,
                     LoadBalancing::InvalidProperty,
                     LoadBalancing::CannotMeetCriteria));

  /// Destroy a "Hasher" object created by this factory that
  /// corresponds to the given FactoryCreationId.
  virtual void delete_object (
     const LoadBalancing::GenericFactory::FactoryCreationId
       &factory_creation_id,
     CORBA::Environment &ACE_TRY_ENV = TAO_default_environment ())
    ACE_THROW_SPEC ((CORBA::SystemException,
                     LoadBalancing::ObjectNotFound));

  /// Return the RepositoryId of the type of object this Factory
  /// creates.
  static const char *replica_type_id (void);

  /// This Factory internally implements the FactoryCreationId as a
  /// CORBA::ULong for efficiency reasons.  Interactions with this
  /// Factory from objects is still done via the
  /// LoadBalancing::GenericFactory::FactoryCreationId type.
  typedef CORBA::ULong FactoryCreationId;

  typedef ACE_Hash_Map_Manager_Ex<FactoryCreationId, PortableServer::ObjectId, ACE_Hash<ACE_UINT32>, ACE_Equal_To<ACE_UINT32>, ACE_Null_Mutex> Table;

private:

  /// Initialize the HasherFactory if it hasn't already been
  /// initialized.
  void init (CORBA::Environment &ACE_TRY_ENV);

  /// Helper method that parses any Criteria passed to this Factory.
  void parse_criteria (const LoadBalancing::Criteria &criteria,
                       CORBA::Environment &ACE_TRY_ENV);

  /// Generate a FactoryCreationId for the given ObjectId.
  /**
   * This method also binds the generated FactoryCreationId to the
   * ObjectId in the underlying table.
   */
  FactoryCreationId bind_fcid (const PortableServer::ObjectId &oid,
                               CORBA::Environment &ACE_TRY_ENV);

  /// Unbind the ObjectId associated with the given
  /// FactoryCreationId from the underlying table.  The ObjectId is
  /// returned as an "out" argument.
  void unbind_fcid (const FactoryCreationId &fcid,
                    PortableServer::ObjectId_out oid,
                    CORBA::Environment &ACE_TRY_ENV);

private:

  /// Pseudo-reference to the ORB.
  CORBA::ORB_var orb_;

  /// Reference to the RootPOA.
  PortableServer::POA_var root_poa_;

  /// POA responsible for creation of the replica references.
  PortableServer::POA_var poa_;

  /// The shared Hasher servant.
  /*
   * Multiple ObjectIds map to this servant.  Only one servant
   * instance is necessary for this test, hence multiple object
   * references point to this servant.
   */
  Hasher_i hasher_;

  /// Lock used to provide synchronization.
  TAO_SYNCH_MUTEX lock_;

  /// Flag that specifies whether or not the Factory has been
  /// initialized.
  CORBA::Boolean initialized_;

  /// The FactoryCreationId that will be assigned to the object
  /// created by this factory.
  HasherFactory::FactoryCreationId next_fcid_;

  /// Table that maps FactoryCreationId to ObjectId.
  Table table_;

};

#endif  /* TAO_HASHER_FACTORY_H */