GizmoDistribution

gzDistSession.h

Go to the documentation of this file.
00001 //*****************************************************************************
00002 // File         : gzDistSession.h
00003 // Module       : gzDistribution
00004 // Description  : Definition of a gzDistributionSession
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 // CAN  031021  Created file
00019 //
00020 //******************************************************************************
00021 #ifndef __GZ_DIST_SESSION_H__
00022 #define __GZ_DIST_SESSION_H__
00023 
00028 // Includes
00029 #include "gzDistBase.h"
00030 #include "gzDistEvaluator.h"
00031 #include "gzDistNotification.h"
00032 #include "gzDistServer.h"
00033 
00034 // Forward declarations
00035 class gzDistSessionServer;
00036 class gzDistSessionObjectIterator;
00037 class gzDistSessionMessage;
00038 class gzDistDebugClient;
00039 class gzDistObjectAttribute;
00040 class gzDistClientInterface;
00041 
00043 enum gzDistEvaluatorPrio
00044 {
00045     GZ_DIST_EVALUATOR_PRIO_LOWEST   = 1,    
00046     GZ_DIST_EVALUATOR_PRIO_LOW      = 3,    
00047     GZ_DIST_EVALUATOR_PRIO_MEDIUM   = 5,    
00048     GZ_DIST_EVALUATOR_PRIO_HIGH     = 7,    
00049     GZ_DIST_EVALUATOR_PRIO_HIGHEST  = 10    
00050 };
00051 
00052 //------------------------------- gzDistSession -------------------------------
00053 
00054 //*****************************************************************************
00055 // Class    : gzDistSession
00056 //
00057 // Purpose  : The base def class of a gzDistSession.
00058 //
00059 // Notes    : -
00060 //
00061 // Revision History...
00062 //
00063 // Who  Date    Description
00064 //
00065 // CAN  031021  Created 
00066 //
00067 //*****************************************************************************
00069 
00084 class gzDistSession : public gzDistThreadSafeReference
00085 {
00086     friend class gzDistManager;
00087     friend class gzDistSessionObjectIterator;
00088     friend class gzDistObject;
00089 
00090     public:
00091 
00093         GZ_DIST_EXPORT virtual ~gzDistSession();
00094 
00096 
00097         GZ_DIST_EXPORT const gzString& getName();
00098 
00100 
00103         GZ_DIST_EXPORT gzDistObjectPtr findObject(const gzString& name);
00104 
00106 
00111         GZ_DIST_EXPORT gzBool findObject(const gzString& name, gzDistObjectPtr& object);
00112 
00114 
00115         GZ_DIST_EXPORT gzBool isGlobal();
00116 
00118 
00122         GZ_DIST_EXPORT gzBool registerEvaluator(const gzDistEvaluatorInterfacePtr& evaluator, const gzDistEvaluatorPrio prio = GZ_DIST_EVALUATOR_PRIO_MEDIUM);
00123 
00125 
00128         GZ_DIST_EXPORT gzBool unregisterEvaluator(gzDistEvaluatorInterface* evaluator);
00129 
00131         /* \return  The number of objects in a session.*/
00132         GZ_DIST_EXPORT gzUInt getNumberOfObjects();
00133 
00135 
00144         GZ_DIST_EXPORT gzDistObjectPtr waitForObject(const gzString& objectName, gzInt timeOut = 10);
00145 
00147 
00154         GZ_DIST_EXPORT gzBool evaluate(gzDistObject* object, gzDistEvaluatorType type, gzDistTransaction* transaction = NULL);
00155 
00157 
00166         GZ_DIST_EXPORT gzDistObjectPtr getNextObject(gzDistObject* object, gzType* objectType);
00167 
00168     private:
00169     // None exported (Internal manager calls).
00170 
00171         // Constructor
00172         gzDistSession(gzDistManager* manager, const gzString& sessionName, gzBool global, gzDistServerPriority prio);
00173 
00174         // Subscribe an event type
00175         gzBool subscribeEvents(gzDistClientInterface* client, gzType* eventType);
00176 
00177         // Subscribe an object type
00178         gzBool subscribeObjects(gzDistClientInterface* client, gzType* objectType, const gzBool notifyExisting);
00179 
00180         // Unsubscribe an event type
00181         gzBool unsubscribeEvents(gzDistClientInterface* client, const gzType* eventType);
00182 
00183         // Unsubscribe an object type
00184         gzBool unsubscribeObjects(gzDistClientInterface* client, const gzType* objectType);
00185 
00186         // Subscribe on an attribute
00187         gzBool subscribeAttributeValue(const gzString& attributeName, gzDistObject* object, gzDistClientInterface* client, const gzBool notifyExisting);
00188 
00189         // Subscribe on attributes
00190         gzBool subscribeAttributeValue(gzDistTransaction* transaction, gzDistObject* object, gzDistClientInterface* client, const gzBool notifyExisting);
00191 
00192         // Unsubscribe on an attribute
00193         gzBool unsubscribeAttributeValue(const gzString& attributeName, gzDistObject* object, gzDistClientInterface* client);
00194 
00195         // Subscribe on new attributes
00196         gzBool subscribeNewAttributes(gzDistObject* object, gzDistClientInterface* client, const gzBool notifyExisting);
00197 
00198         // Unsubscribe on new attributes
00199         gzBool unsubscribeNewAttributes(gzDistObject* object, gzDistClientInterface* client);
00200 
00201         // Join session
00202         gzBool join(gzDistClientInterface* client);
00203 
00204         // Resign session.
00205         gzBool resign(gzDistClientInterface* client);
00206         
00207         // Process all session messages.
00208         gzBool process();
00209 
00210         // Process a gzDistSessionMessage.
00211         gzBool processSessionServerMessage(gzDistSessionMessage* message);
00212 
00213         // Release the manager and all objects.
00214         gzVoid shutDown();
00215 
00216         // Remove subscriptions if there are any lower than type.
00217         gzVoid removeLowerSubscriptions(gzDistClientInterface* client, gzType* type, gzDict<gzInstanceCompareInterface, gzList<gzDistClientInterface> >& subscribers);
00218 
00219         // Check if the client has a higher subscription than type.
00220         gzBool hasHigherSubscription(gzDistClientInterface* client, gzType* type, gzDict<gzInstanceCompareInterface, gzList<gzDistClientInterface> >& subscribers);
00221 
00222         // Notify client of all existing objects of current type in current session.
00223         gzVoid notifyExistingObjects(gzDistClientInterface* client, gzType* type);
00224 
00225         // Notify clients of event.
00226         gzVoid sendEvent(gzDistEvent* event, const gzDistClientID& clientId);
00227 
00228         // Do the insertion of the object to session and notify clients.
00229         gzBool addObject(gzDistObject* object, const gzDistClientID& clientId);
00230 
00231         // Update an object with data in transaction.
00232         gzBool updateObject(gzDistObject* object, gzDistTransaction* transaction, const gzDistClientID& clientId);
00233 
00234         // Do the remove of the object from session and notify clients.
00235         gzBool removeObject(gzDistObject* object, const gzDistClientID& clientId);
00236 
00237         // Remove attributes from an object.
00238         gzBool removeAttributes(gzDistTransaction* transaction, gzDistObject* object, const gzDistClientID& clientId);
00239 
00240         // Notify local clients of event.
00241         gzVoid localSendEvent(gzDistEvent* event, const gzDistClientID& clientId);
00242 
00243         // Do the insertion of the object to session and notify local clients.
00244         gzBool localAddObject(gzDistObject* object);
00245 
00246         // Update an object with data in transaction and notify local clients..
00247         gzBool localUpdateObject(gzDistObject* object, gzDistTransaction* transaction);
00248         
00249         // Do the remove of the attributes from object and notify local clients.
00250         gzBool localRemoveAttributes(gzDistTransaction* transaction, gzDistObject* object, const gzDistClientID& client);
00251 
00252         // Do the remove of the object from session and notify local clients.
00253         gzBool localRemoveObject(gzDistObject* object);
00254 
00255         // Request ownership on attribute and notify owner if the owner is localy joined.
00256         gzBool localRequestOwnership(gzDistObjectAttribute* attribute, const gzDistClientID& clientId);
00257 
00258         // Drop ownership/owner-request on attribute and notify client if the client is localy joined.
00259         // Next requesting client (if exist) will get the ownership.
00260         gzBool localDropOwnership(gzDistObjectAttribute* attribute, const gzDistClientID& clientId);
00261 
00262         // Finish current transaction to all clients.
00263         gzVoid endTransaction(const gzDistNotificationType type, gzDistObject* object);
00264 
00265         // Notify a client of ownership grant.
00266         gzVoid notifyGrantOwnership(gzDistObjectAttribute* attribute);
00267 
00268         // Notify a client of ownership drop.
00269         gzVoid notifyDropOwnership(gzDistObjectAttribute* attribute, gzDistClientInterface* client);
00270 
00271         // Notify all existing ownership requests for an attribute to a client.
00272         gzVoid notifyOwnersipRequests(gzDistObjectAttribute* attribute, gzDistClientInterface* client);
00273 
00274         // Get local client
00275         gzDistClientInterface* getClient(const gzDistClientID& clientId);
00276 
00277         // Returns the number of joined clients.
00278         gzUInt joinedClients();
00279 
00280         // Returns TRUE if the client is joined to session.
00281         gzBool isJoinedClient(const gzDistClientID& clientId);
00282 
00283         // Request ownership on attribute.
00284         gzBool requestOwnership(const gzString& attributeName, gzDistObject* object, gzDistClientInterface* client);
00285 
00286         // Drop ownership for attribute.
00287         gzBool dropOwnership(const gzString& attributeName, gzDistObject* object, gzDistClientInterface* client);
00288 
00289         // Take ownership on attribute by force.
00290         gzBool pullOwnership(const gzString& attributeName, gzDistObject* object, gzDistClientInterface* client);
00291 
00292         // Update/add object independent of owner etc.
00293         gzVoid synchronizeObject(gzDistObject* object);
00294 
00295         // Synchronize ownership for attribute
00296         gzVoid synchronizeOwnership(gzDistObjectAttribute* existingAttribute, gzDistObjectAttribute* syncAttribute);
00297 
00298         // Force an ownersip set.
00299         gzBool localSetOwnership(gzDistObjectAttribute* attribute, const gzDistClientID& clientId);
00300 
00301         // Validate an insertion of an object to session.
00302         gzBool isValidInsertion(gzDistObject* object);
00303 
00304         // Check if client is allowed to update the attributes in transaction.
00305         gzBool isAllowedUpdate(gzDistTransaction* transaction, gzDistObject* object, const gzDistClientID& clientId);
00306 
00307         // Clean up all ownership etc. referring to instance.
00308         gzVoid cleanUpInstance(const gzDistInstanceID& instance);
00309 
00310         // Check if the session has incoming or outgoing data at the moment. Either from clients or session server.
00311         gzBool hasPendingData();
00312 
00313         // Set debug client.
00314         gzVoid setDebugClient(gzDistDebugClient* debugClient);
00315 
00316         // Send attribute (value) subscribers to debugger.
00317         gzVoid debugAttributeSubscriptions(const gzString& attributeName, gzDistObject* object, gzDistClientInterface* subscriber, gzBool newClient);
00318 
00319         // Send attribute (value) subscribers to debugger.
00320         gzVoid debugAttributeSubscriptions(gzDistTransaction* transaction, gzDistObject* object, gzDistClientInterface* subscriber, gzBool newClient);
00321 
00322         // Send subscriptions on new/removed attributes to debugger.
00323         gzVoid debugNewAttributesSubscriptions(gzDistObject* object, gzDistClientInterface* subscriber, gzBool newClient);
00324 
00325         gzVoid debugOwnership(gzDistObjectAttribute* attribute);
00326 
00327         gzVoid addLinkObject(gzDistObject* object);
00328 
00329         gzVoid removeLinkObject(gzDistObject* object);
00330 
00331         gzDistObject* firstObject();
00332 
00333         gzBool isServer();
00334 
00335     // Private members
00336 
00337         // The name of the session.
00338         const gzString m_name;
00339 
00340         const gzDouble m_creationTime;
00341 
00342         // This list contains all subscribers of events. The key is a gzType*.
00343         gzDict<gzInstanceCompareInterface, gzList<gzDistClientInterface> > m_eventSubscribers;
00344 
00345         // This list contains all subscribers of objects. The key is a gzType*.
00346         gzDict<gzInstanceCompareInterface, gzList<gzDistClientInterface> > m_objectSubscribers;
00347 
00348         // This list contains all joined clients.
00349         gzDict<gzDistClientID, gzDistClientInterface> m_joinedClients;
00350 
00351         // All objects in session.
00352         gzRefDict<gzString, gzDistObject> m_objects;
00353 
00354         // Mutex for m_objects
00355         gzMutex m_objectsLocker;
00356 
00357         // Reference to current gzDistManager
00358         gzDistManagerPtr m_manager;
00359 
00360         // A list containing all evaluators for current session. The key is a gzType*.
00361         gzRefList<gzDistEvaluatorInterface> m_evaluators;
00362 
00363         // Thread mutex for m_evaluators
00364         gzMutex m_evaluatorLocker;
00365 
00366         // Pointer to current session manager.
00367         // If the pointer is NULL, the session is local.
00368         gzDistSessionServer* m_sessionServer;
00369 
00370         // Pointer to a list that will receive global messages from session server.
00371         gzList<gzDistSessionMessage>* m_sessionServerMessages;
00372 
00373         // Is non-zero if debug is enabled.
00374         gzDistDebugClient* m_debugClient;
00375 
00376         gzDistObject* m_firstObject;
00377 
00378         gzDistObject* m_lastObject;
00379 };
00380 
00381 
00382 //------------------------ gzDistSessionObjectIterator ------------------------
00383 
00385 class gzDistSessionObjectIterator
00386 {
00387     public:
00388 
00390 
00393         GZ_DIST_EXPORT gzDistSessionObjectIterator(const gzDistSessionPtr& session, gzType* objectType);
00394 
00396 
00398         GZ_DIST_EXPORT gzDistSessionObjectIterator(const gzDistSessionPtr& session);
00399 
00401 
00402         GZ_DIST_EXPORT ~gzDistSessionObjectIterator();
00403 
00405 
00407         GZ_DIST_EXPORT gzDistObject* operator()();
00408 
00410         GZ_DIST_EXPORT gzDistObject* current();
00411 
00413         GZ_DIST_EXPORT gzVoid reset();
00414 
00416 
00422         GZ_DIST_EXPORT gzVoid unLockSession();
00423 
00424     private:
00425 
00426         const gzDistSessionPtr m_session;
00427         gzDistObject* m_currentObject;
00428         gzType* m_objectType;
00429         gzBool m_lockedSession;
00430 };
00431 
00432 //------------------------ gzDistSessionIterator ------------------------
00433 
00435 class gzDistSessionIterator
00436 {
00437     public:
00438         GZ_DIST_EXPORT gzDistSessionIterator();
00439         GZ_DIST_EXPORT ~gzDistSessionIterator();
00440 
00442 
00444         GZ_DIST_EXPORT gzDistSession* operator()();
00445 
00447         GZ_DIST_EXPORT gzDistSession* current();
00448 
00450         GZ_DIST_EXPORT gzVoid reset();
00451 
00452     private:
00453 
00454         gzDistManagerPtr m_manager;
00455 
00456         gzDictIterator<gzString, gzDistSession>* m_iterator;
00457         gzDictEntry<gzString, gzDistSession>* m_entry;
00458         
00459         gzBool m_sessionsLocked;
00460 };
00461 
00462 #endif //__GZ_DIST_SESSION_H__

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