GizmoDistribution

gzDistManager.h

Go to the documentation of this file.
00001 //*****************************************************************************
00002 // File         : gzDistributionManager.h
00003 // Module       : gzDistribution
00004 // Description  : Distribution manager
00005 // Author       : Christian Andersson
00006 // Product      : GizmoDistribution 2.1.1
00007 //
00008 // Copyright © 2004 - Saab Training Systems AB, Sweden
00009 //
00010 // NOTE:    GizmoDistribution is a toolkit used for implementing distributed
00011 //          objects and events in C++
00012 //
00013 //
00014 // Revision History...
00015 //
00016 // Who  Date    Description
00017 //
00018 // XAA  040108  Created file
00019 //
00020 //*****************************************************************************
00021 #ifndef __GZ_DIST_MANAGER_H__
00022 #define __GZ_DIST_MANAGER_H__
00023 
00029 // Includes
00030 #include "gzDistBase.h"
00031 #include "gzDistRemoteMessage.h"
00032 #include "gzDistClientThreadPool.h"
00033 #include "gzDistServer.h"
00034 
00035 // Forward declarations
00036 class gzDistManagerMessage;
00037 class gzDistRemoteChannelInterface;
00038 class gzDistDebugClient;
00039 
00040 
00041 
00042 //----------------------------- gzDistManager ---------------------------------
00043 
00045 
00054 class gzDistManager : public gzDistThreadSafeReference, protected gzThread
00055 {
00056     friend class gzDistSession;
00057     friend class gzDistSessionIterator;
00058     friend class gzDistClientInterface;
00059 
00060     public:
00061 
00063 
00078         GZ_DIST_EXPORT gzBool start(gzDistRemoteChannelInterface* sessionChannel = NULL,
00079                                     gzDistRemoteChannelInterface* serverChannel = NULL);
00080 
00082 
00088         GZ_DIST_EXPORT gzVoid shutDown(gzBool wait = FALSE);
00089 
00091 
00093         GZ_DIST_EXPORT gzBool isRunning();
00094 
00096 
00106         GZ_DIST_EXPORT gzDistSessionPtr getSession(const gzString& sessionName, const gzBool create = FALSE, gzBool global = FALSE, gzDistServerPriority prio = GZ_DIST_SERVER_PRIO_NORMAL);
00107 
00108 
00110 
00115         GZ_DIST_EXPORT gzVoid registerFactory(const gzDistEventPtr& event); 
00116 
00118 
00123         GZ_DIST_EXPORT gzVoid registerFactory(const gzDistObjectPtr& object);
00124 
00126 
00132         GZ_DIST_EXPORT gzVoid unregisterFactory(gzType* classType);
00133 
00135 
00138         GZ_DIST_EXPORT gzVoid clearEventFactories();
00139 
00141 
00144         GZ_DIST_EXPORT gzVoid clearObjectFactories();
00145         
00147 
00152         GZ_DIST_EXPORT gzDistObject* getObject(const gzString& className, const gzString& objectName = GZ_DIST_NO_NAME);
00153 
00155 
00159         GZ_DIST_EXPORT gzDistEvent* getEvent(const gzString& className);
00160 
00162 
00169         GZ_DIST_EXPORT gzType* getObjectType(const gzString& className);
00170 
00172 
00179         GZ_DIST_EXPORT gzType* getEventType(const gzString& className);
00180 
00182 
00193         GZ_DIST_EXPORT gzBool processCustomThreadClients(gzBool waitForTrigger = FALSE);
00194 
00196 
00200         GZ_DIST_EXPORT gzBool enableDebug(gzBool enable);
00201 
00203 
00208         GZ_DIST_EXPORT gzBool hasPendingData();
00209 
00211 
00216 //      GZ_DIST_EXPORT gzBool isSessionServer(const gzString& sessionName);
00217 
00218     // Statics:
00219 
00221 
00223         GZ_DIST_EXPORT static gzDistManagerPtr getManager(gzBool create = FALSE);
00224     
00226 
00232         GZ_DIST_EXPORT static gzBool enableFastMemory(gzUInt size = 10);
00233 
00235 
00239         GZ_DIST_EXPORT static gzVoid trigger();
00240     
00241     protected:
00242 
00244 
00245         gzDistManager();
00246 
00248         virtual ~gzDistManager();
00249     
00250     private:
00251 
00252     // --- Interface calls ---
00253 
00254         // Sign up to the manager and a thread pool.
00255         gzBool registerClient(gzDistClientInterface* client, gzULong poolId, gzDistThreadInterface*& thread);
00256 
00257         // Resign the manager and thread pool.
00258         gzBool unregisterClient(gzDistClientInterface* client, gzULong poolId);
00259 
00260         // Subscribe on new/removed sessions.
00261         gzBool subscribeSessions(gzDistClientInterface* client, gzBool notifyExisting = FALSE, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00262 
00263         // Unsubscribe on new/removed sessions.
00264         gzBool unsubscribeSessions(gzDistClientInterface* client, gzInt timeOut = GZ_DIST_SYNCHRONOUS);
00265 
00266     // ------
00267 
00268         // The kernel thread implementation
00269         gzVoid process();
00270 
00271         // Process all sessions.
00272         gzBool processSessions();
00273 
00274         // Process messages that are posted to manager.
00275         // Returns TRUE if messages are processed, otherwise FALSE.
00276         gzBool processManagerMessages();
00277 
00278         // Process a single manager message.
00279         gzVoid processManagerMessage(gzDistManagerMessage* message);
00280 
00281         // Process all messages from all clients.
00282         gzBool processClients();
00283 
00284         // Process a single client message.
00285         gzBool processClientMessage(gzDistClientInterface* client, gzDistClientMessage* message);
00286 
00287         // Create a session. The created session is placed in "session" (parameter one).
00288         gzVoid createSession(gzDistSessionPtr& session, const gzString& sessionName, gzBool global, gzDistServerPriority prio);
00289 
00290         // Add a message to m_managerMessages
00291         gzVoid addManagerMessage(gzDistManagerMessage* message);
00292 
00293         // Notify all subscribers of new/removed sessions of the session.
00294         // If newSession is TRUE, the session is new otherwise it is removed.
00295         gzVoid notifySessionSubscribers(gzDistSession* session, gzBool newSession);
00296 
00297         // Add a message to the manager queue.
00298         gzVoid addManagerMessager(gzDistManagerMessage* message);
00299 
00300         // Wait until operation is complete or timeout has elapsed,
00301         // if the message was an acknowledge message.
00302         gzBool waitAck(gzDistManagerMessage* message, const gzInt timeOut);
00303 
00304         // Removes the session if session has no clients.
00305         // Returns TRUE if session is removed.
00306         gzBool checkRemoveSession(gzDistSession* session);
00307 
00308         // Send a shutdown message to all initialized clients.
00309         gzVoid shutDownClients();
00310 
00311         // See hasPendingData().
00312         gzBool checkPendingData();
00313 
00314         // Unsubscribe an attribute value an debug in if debugger is enabled.
00315         gzBool unsubscribeAttributeValue(const gzString& attrName, gzDistObject* object, gzDistSession* session, gzDistClientInterface* client);
00316 
00317     // Debug methods:
00318 
00319         // Send session subscribers to debugger.
00320         gzVoid debugSessionSubscribers(gzDistClientInterface* subscriber, gzBool newClient);
00321 
00322         // Send initialized clients to debugger.
00323         gzVoid debugManagerClients(gzDistClientInterface* subscriber, gzBool newClient);
00324 
00325         // Send joined/resigned clients to debugger.
00326         gzVoid debugJoinedClients(gzDistClientInterface* subscriber, gzDistSession* session, gzBool newClient);
00327 
00328         // Send object type subscribers to debugger.
00329         gzVoid debugObjectSubscribers(gzType* objectType, gzDistSession* session, gzDistClientInterface* subscriber, gzBool newClient);
00330 
00331         // Send event type subscribers to debugger.
00332         gzVoid debugEventSubscribers(gzType* eventType, gzDistSession* session, gzDistClientInterface* subscriber, gzBool newClient);
00333 
00334     // Members:
00335 
00336         // This dictionary contains all sessions.
00337         gzRefDict<gzString, gzDistSession> m_sessions;
00338 
00339         // The thread lock for m_sessions.
00340         gzMutex m_sessionsLocker;
00341 
00342         // Containing clients which are interested in new and removed sessions.
00343         gzList<gzDistClientInterface> m_sessionSubscribers;
00344 
00345         // Messages to the manager will be placed here.
00346         gzRefList<gzDistManagerMessage> m_managerMessages;
00347 
00348         // This list contain all initialized clients.
00349         gzList<gzDistClientInterface> m_clients;
00350 
00351         // Thread locker for m_managerMessages.
00352         gzMutex m_managerMessagesLocker;
00353 
00354         // The client thread pool
00355         gzDistClientThreadPool m_clientThreadPool;
00356 
00357         // The dictionary all registered event classes are stored.
00358         gzRefDict<gzString, gzDistEvent> m_eventFactory;
00359 
00360         // Mutex for m_eventFactory
00361         gzMutex m_eventFactoryLocker;
00362 
00363         // The dictionary all registered object classes are stored.
00364         gzRefDict<gzString, gzDistObject> m_objectFactory;
00365 
00366         // Mutex for m_objectFactory
00367         gzMutex m_objectFactoryLocker;
00368 
00369         // A trigger used to trigger the manager thread.
00370         gzDistTrigger m_trigger;
00371 
00372         // Debug client
00373         gzDistDebugClient* m_debugClient;
00374 
00375         // Will be set to TRUE when the manager is shutting down.
00376         gzBool m_closing;
00377 
00378         // Is TRUE when the worker thread is active
00379         gzBool m_workerThreadActive;
00380 
00381         // Tells if the manager has an error. Check last error to get the error.
00382         gzBool m_error;
00383 
00384         // Tells if remote channels were provided.
00385         gzBool m_hasRemoteChannels;
00386         gzBool m_remoteChannelWarning;
00387 
00388         gzRefList<gzDistManagerMessage> m_pendingManagerMessages;
00389 
00390         gzDict<gzString, gzVoid> m_missedObjectFactories;
00391 
00392         gzDict<gzString, gzVoid> m_missedEventFactories;
00393 
00394     // Static members:
00395 
00396         // Reference to current distribution manager
00397         static gzDistManagerPtr m_instance;
00398 
00399         // Mutex for m_instance
00400         static gzMutex m_instanceLocker;
00401 };
00402 
00403 
00404 //-------------------------- gzDistManagerMessage -----------------------------
00405 
00406 // Manager message types
00407 enum gzDistManagerMessageType
00408 {
00409     GZ_DIST_MANAGER_MESSAGE_TYPE_CREATE_SESSION = 1,
00410     GZ_DIST_MANAGER_MESSAGE_TYPE_REMOVE_SESSION,
00411     GZ_DIST_MANAGER_MESSAGE_TYPE_SUBSCRIBE_SESSIONS,
00412     GZ_DIST_MANAGER_MESSAGE_TYPE_UNSUBSCRIBE_SESSIONS,
00413     GZ_DIST_MANAGER_MESSAGE_TYPE_REGISTER_CLIENT,
00414     GZ_DIST_MANAGER_MESSAGE_TYPE_UNREGISTER_CLIENT,
00415     GZ_DIST_MANAGER_MESSAGE_TYPE_ENABLE_DEBUG,
00416     GZ_DIST_MANAGER_MESSAGE_TYPE_CHECK_PENDING_DATA
00417 //  GZ_DIST_MANAGER_MESSAGE_TYPE_IS_SESSION_SERVER
00418 //  GZ_DIST_MANAGER_MESSAGE_TYPE_CLEAR_SESSION
00419 };
00420 
00421 
00422 // Message to/from the manager thread. For internal use.
00423 class gzDistManagerMessage : public gzDistThreadSafeReference
00424 {
00425     public:
00426 
00427         // Constructor
00428         gzDistManagerMessage(const gzDistManagerMessageType type);
00429 
00430         // Constructor
00431         gzDistManagerMessage(const gzDistManagerMessageType type, const gzString& sessionName);
00432         
00433         // Constructor
00434         gzDistManagerMessage(const gzDistManagerMessageType type, const gzString& sessionName, gzBool global, gzDistServerPriority prio);
00435 
00436         // Constructor
00437         gzDistManagerMessage(const gzDistManagerMessageType type, gzDistClientInterface* client, gzULong poolId);
00438 
00439         // Constructor
00440         gzDistManagerMessage(const gzDistManagerMessageType type, gzDistClientInterface* client, gzBool notifyExisting);
00441 
00442         // Destructor
00443         virtual ~gzDistManagerMessage();
00444 
00445         // --- Set/Get methods ---
00446         gzDistThreadInterface* getThread();
00447 
00448         gzVoid setThread(gzDistThreadInterface* thread);
00449 
00450         gzVoid setDebugInterface(gzDistDebugClient* client);
00451 
00452         gzDistDebugClient* getDebugInterface();
00453         // ---
00454 
00455         // The name of the session that shall be created.
00456         gzString m_sessionName;
00457         
00458         // This member will have a reference to the created session.
00459         gzDistSessionPtr m_reference;
00460 
00461         // The client who created the message.
00462         gzDistClientInterface* m_client;
00463 
00464         // Used to return a thread pointer to client or a debug client.
00465         gzVoid* m_void;
00466 
00467         // Type of message.
00468         gzDistManagerMessageType m_messageType;
00469 
00470         // Used to store thread pool id, session size etc.
00471         gzULong m_intData;
00472 
00473         // The id of the calling thread. This must be known to avoid a deadlock if uninitialize is called from a client thread.
00474         gzULong m_threadId;
00475 
00476         // The priority a sessionserver shall have in a global session.
00477         gzDistServerPriority m_priority;
00478 
00479         // Tells if a subscription shall generate notification on existing data.
00480         gzBool m_notifyExisting;
00481 
00482         // This member is set to TRUE if a synchronous message is desired. The manager will zero this flag when operation is complete.
00483         gzBool m_acknowledge;
00484 
00485         // Tells if a session should be created as a glohbal session.
00486         gzBool m_global;
00487 
00488         // The manager puts the result here.
00489         gzBool m_result;
00490 };
00491 
00492 
00493 #endif // __GZ_DIST_MANAGER_H__

Documentation for GizmoDistribution generated at Wed Feb 20 11:59:21 2008 by   Saab Training Systems AB, ¸ (c) 2003-and beyond