servantlocators.h

Go to the documentation of this file.
00001 // OSB library ********************************************* -*- C++ -*-
00008 /*
00009   AUTHOR(S): Darryl Kang (dk)
00010              Huang Yong Feng (hyf)
00011 
00012   RCS information
00013    $Name: OSB_060808 $
00014    $Revision: 1.25 $
00015 
00016   License
00017    OSB rating and billing library for communication networks
00018    Copyright (C) 2004, 2005, 2006  OSB systems
00019 
00020    This file may be distributed and/or modify under the terms of the
00021    GNU General Public License (GPL) as published by the Free Software
00022    Foundation which is provided in the file LICENSE.GPL included in the
00023    packaging of this file.
00024 
00025    The file is distributed in the hope that it will be useful, but WITHOUT
00026    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00027    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00028    for more details.
00029 
00030    Holders of a OSB Commercial License may use this file under the terms
00031    and conditions of this commercial license.
00032  */
00033 #ifndef _SERVANTLOCATORS_H_
00034 #define _SERVANTLOCATORS_H_
00035 
00036 // *********************************************************************
00037 // included header files
00038 // + standard includes
00039 #include <map>
00040 #include <list>
00041 
00042 // + libraries
00043 #include <tao/PortableServer/Servant_Base.h>
00044 #include <tao/PortableServer/ServantLocatorC.h>
00045 #include <osbid.h>
00046 #include <mutex.h>
00047 
00048 // + local headers
00049 #include "exceptionS.h"
00050 
00051 // *********************************************************************
00052 // namespace extensions
00053 namespace OSB_CORBA {
00054 
00055     using corba::common::OsbCorbaException;
00056 
00058     class Locators : public virtual PortableServer::ServantLocator
00059     {
00060     public:
00062         typedef std::pair<unsigned long, unsigned long> Key;
00063 
00064     public:                             // static definition
00066         static const std::string IFC;
00067 
00074         static PortableServer::ObjectId_var keyToOid(
00075             const Key& key
00076         );
00077 
00088         static Key makeKey(long k1, long k2 = 0);
00089 
00099         template<typename T>
00100         static Key makeKey(const OSB_LIB::Id<T>& id)
00101         {
00102             return makeKey(id.id());
00103         }
00104 
00114         template<typename T>
00115         static Key makeKey(const OSB_LIB::Id2<T>& id)
00116         {
00117             return makeKey(id.id1_, id.id2_);
00118         }
00119 
00120     public:
00121         // Constructor with optional max. size.
00122         explicit Locators(unsigned long maxSize = 10000);
00123 
00124         // Destructor: deletes all registered servants.
00125         ~Locators();
00126 
00127     private:
00129         Locators(const Locators&);
00131         Locators& operator=(const Locators&);
00132 
00133     public:
00145         template<typename T>
00146         Key regServant(
00147             const OSB_LIB::Id2<T>&  id,
00148             PortableServer::Servant servant
00149         )
00150         {
00151             Locators::Key key = Locators::makeKey(id.id1_, id.id2_);
00152             return registerServant(key, servant);
00153         }
00154 
00166         template<typename T>
00167         Key regServant(
00168             const OSB_LIB::Id<T>&   id,
00169             PortableServer::Servant servant
00170         )
00171         {
00172             Locators::Key key = Locators::makeKey(id.id());
00173             return registerServant(key, servant);
00174         }
00175 
00188         Key regServant(
00189             PortableServer::Servant servant
00190         );
00191 
00216         template<typename T>
00217         Key regServantInc(
00218             const OSB_LIB::Id<T>&   id,
00219             PortableServer::Servant servant
00220         )
00221         {
00222             unsigned long c = increaseCounter();
00223             Locators::Key key = Locators::makeKey(id.id(), c);
00224             return registerServant(key, servant);
00225         }
00226 
00239         Key regServantInc(
00240             PortableServer::Servant servant
00241         );
00242 
00256         void unregServant(
00257             const Key& key,
00258                   bool destroy
00259         );
00260 
00273         PortableServer::Servant find(
00274             const Key&    key,
00275             unsigned long oid
00276         );
00277 
00279         PortableServer::Servant find(const Key& key);
00280 
00282         template<typename T>
00283         PortableServer::Servant find(
00284             const OSB_LIB::Id<T>& id
00285         )
00286         {
00287             return find(makeKey(id));
00288         }
00289 
00291         template<typename T>
00292         PortableServer::Servant find(
00293             const OSB_LIB::Id2<T>& id
00294         )
00295         {
00296             return find(makeKey(id));
00297         }
00298 
00312         virtual PortableServer::Servant preinvoke(
00313             const PortableServer::ObjectId& oid,
00314                   PortableServer::POA_ptr   poa,
00315             const char*                     operation,
00316                   void* &                   cookie
00317         ) throw (
00318             CORBA::SystemException, PortableServer::ForwardRequest
00319         );
00320 
00322         virtual void postinvoke(
00323             const PortableServer::ObjectId& oid,
00324                   PortableServer::POA_ptr   poa,
00325             const char*                     operation,
00326                   void*                     cookie,
00327                   PortableServer::Servant   servant
00328         ) throw (CORBA::SystemException);
00329 
00330     private:
00344         Key registerServant(
00345             Key                     key,
00346             PortableServer::Servant servant
00347         );
00348 
00350         unsigned long increaseCounter();
00351 
00365         CORBA::Boolean isEqual(
00366             const Key&    key,
00367             unsigned long oid
00368         );
00369 
00378         Key oidToKey(
00379             const PortableServer::ObjectId& oid
00380         ) throw (OsbCorbaException);
00381 
00382     private:
00384         typedef OSB_LIB::RwGuard RwGuard;
00386         RwGuard readlock();
00388         RwGuard writelock();
00389 
00390     private:
00392         typedef std::list<Key> EvictorQueue;
00393 
00395         typedef std::map<Key, PortableServer::Servant> ActiveObjectMap;
00396 
00398         OSB_LIB::RwMutex mutex_;
00399 
00401         unsigned long maxSize_;
00403         unsigned long count_;
00404         // ! Flag the locator is inside the destrctor.
00405         bool inDtor_;
00407         EvictorQueue evictorQ_;
00409         ActiveObjectMap servantMap_;
00410     };                                  // class Locators
00411 }                                       // namespace OSB_CORBA
00412 #endif                                  // #ifndef _SERVANTLOCATORS_H_

Generated on Sat Sep 2 14:25:53 2006 for OSB Library by  doxygen 1.4.7