1 /*------------------------------------------------------------------------------
  2 Name:      CorbaDriverFactory.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   The Factory for the client driver for the corba protocol
  6 ------------------------------------------------------------------------------*/
  7 
  8 #ifndef _CLIENT_PROTOCOL_CORBA_CORBADRIVERFACTORY
  9 #define _CLIENT_PROTOCOL_CORBA_CORBADRIVERFACTORY
 10 
 11 #include <util/xmlBlasterDef.h>
 12 #include <client/protocol/corba/CorbaDriver.h>
 13 #include <util/objman.h> // for managed objects
 14 
 15 
 16 
 17 
 18 
 19 
 20 namespace org {
 21  namespace xmlBlaster {
 22   namespace client {
 23    namespace protocol {
 24     namespace corba {
 25 
 26 typedef std::pair<CorbaDriver*, int>  DriverEntry;
 27 typedef std::map<org::xmlBlaster::util::Global*, DriverEntry> DriversMap;
 28 
 29 /**
 30  * Factory used to create instances of CorbaDriver objects. It currently is a singleton class and has for
 31  * that reason private constructors, destructor and assignment operator. 
 32  * To get a reference to the singleton instance you must invoke getFactory(...).
 33  * 
 34  * This factory has a running thread in which the orb performs its work. For threadsafe orbs, this is a
 35  * blocking invocation to orb.run() (which exits when shutting down the orb). For singlethreaded orbs, it is
 36  * a loop invoking orb.perform_work() with a sleep interval of 20 ms between each invocation to that method.
 37  *
 38  * You can either pass to this factory a previously instantiated orb by passing it explicitly, or you can 
 39  * let it instantiate the default orb (with the arguments you passed at application startup) by not passing
 40  * any orb or by passing NULL.
 41  * If you pass an external orb, then this factory will not start the mentionned working thread nor it will 
 42  * free the orb resources. You must handle that on your own.
 43  * If you let the factory create an orb instance, then this factory will start the thread and will cleanup
 44  * all resources used by the orb.
 45  *
 46  */
 47 class Dll_Export CorbaDriverFactory : public org::xmlBlaster::util::thread::Thread
 48 {
 49 friend CorbaDriverFactory& getFactory(org::xmlBlaster::util::Global& global, CORBA::ORB_ptr orb=NULL);
 50 
 51 // required for the managed objects
 52 friend class Object_Lifetime_Manager;
 53 friend class ManagedObject;
 54 
 55 private:
 56    const std::string   ME;
 57    //org::xmlBlaster::util::Global&        global_;
 58    //org::xmlBlaster::util::I_Log&           log_;
 59    DriversMap     drivers_;         // the std::map containing all drivers created by this factory
 60    bool           doRun_;           // the command: if set to 'false' the thread will stop.
 61    bool           isRunning_;       // the status: if the thread is running it is 'true'
 62    org::xmlBlaster::util::thread::Mutex          mutex_,           // the mutex passed to all CorbaDriver instances (for singlethreaded)
 63                   getterMutex_;     // the mutex used for creating/deleting CorbaDriver instances
 64    bool           orbIsThreadSafe_; // flag telling if the orb is a singletheraded or multithreaded orb
 65    CORBA::ORB_ptr orb_;             // the orb used (either created here or passed in constructor
 66    bool           isOwnOrb_;        // 'true' if the orb has been created by this factory.
 67 
 68    static CorbaDriverFactory* factory_;
 69 
 70    /**
 71     * Only used by getInstance()
 72     * @param global
 73     * @param mutex   org::xmlBlaster::util::Global thread synchronization (to avoid static variable)
 74     * @param doRun   Only for internal main loop for single threaded orbs. false stops the loop
 75     *                
 76     * @param isRunning    Feedback is doRun has stopped
 77     * @param orb
 78     */
 79 
 80    /**
 81     * For single threaded CORBA implementations only (like MICO).
 82     * One instance (the first) starts a main loop and checks if the
 83     * orb has some work to perform (every 20 millis).
 84     * In your real application this should be done by your main loop (e.g. from X-Window)
 85     * E.g. mico has a helper implementation to register its file descriptors with another main loop.
 86     */
 87    void run();
 88 
 89    /** This is specific for threadsafe orbs like TAO */
 90    bool orbRun();
 91 
 92    CorbaDriverFactory(org::xmlBlaster::util::Global& global, CORBA::ORB_ptr orb=NULL);
 93 
 94    CorbaDriverFactory(const CorbaDriverFactory& factory);
 95    CorbaDriverFactory& operator =(const CorbaDriverFactory& factory);
 96 
 97 public:
 98    ~CorbaDriverFactory(); // Should be private, VC7 is ok with private, g++ 3.3 does not like it
 99 
100    /**
101     * You can assign only one orb per CorbaDriverFactory object. Since this class is currently a singleton,
102     * you can only assign one single orb to handle communication to xmlBlaster. So mixed orb implementation 
103     * usage is still allowed, but to communicate to xmlBlaster you must use one single orb implementation per
104     * client application.
105     *
106     * @param global the global parameter to pass
107     * @param orb the orb to pass. Note that if you pass NULL (the default) then an own orb is initialized in
108     *        the CorbaConnection class encapsulated by CorbaDriver. If you pass an orb different from NULL,
109     *        then you are responsible of making the orb perform work and you must clean up its resources
110     *        i.e. you must call expicitly shutdown and destroy on work completition.
111     */
112    static CorbaDriverFactory& getFactory(org::xmlBlaster::util::Global& global, CORBA::ORB_ptr orb=NULL);
113 
114    /**
115     * gets an instance of a corba driver with the specified name.
116     */
117    CorbaDriver& getDriverInstance(org::xmlBlaster::util::Global* global);
118 
119    /**
120     * Kills the driver instance owned by the specified Global. Note that if you invoked getDriverInstance several 
121     * times with the same instanceName, you just decrement the internal reference counter. When the reference
122     * counter reaches zero, the driver is really destroyed.
123     */
124    int killDriverInstance(org::xmlBlaster::util::Global* global);
125 
126 };
127 
128 }}}}} // namespaces
129 
130 #endif


syntax highlighted by Code2HTML, v. 0.9.1