GizmoDistribution

gzDistClient.h

Go to the documentation of this file.
00001 //*****************************************************************************
00002 // File         : gzDistClient.h
00003 // Module       : gzDistribution
00004 // Description  : Definition of gzDistClientInterface
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_CLIENT_H__
00022 #define __GZ_DIST_CLIENT_H__
00023 
00028 // Includes
00029 #include "gzDistBase.h"
00030 #include "gzDistNotification.h"
00031 #include "gzDistServer.h"
00032 
00033 //-------------------------- gzDistClientInterface ----------------------------
00034 
00035 //*****************************************************************************
00036 // Class    : gzDistClientInterface
00037 //                                  
00038 // Purpose  : The standard interface to derive your user class from
00039 //                                  
00040 // Notes    : - 
00041 //                                  
00042 // Revision History...                          
00043 //                                  
00044 // Who  Date    Description                     
00045 //                                  
00046 // CAN  031021  Created 
00047 //                                  
00048 //*****************************************************************************
00050 
00074 class gzDistClientInterface
00075 {
00076     friend class gzDistSession;
00077     friend class gzDistManager;
00078     friend class gzDistClientThread;
00079     friend class gzDistClientThreadPool;
00080 
00081     public:
00082 
00084 
00087         GZ_DIST_EXPORT gzDistClientInterface(const gzString& clientName);
00088 
00090 
00091         GZ_DIST_EXPORT gzDistClientInterface(const gzDistClientInterface& original);
00092 
00094 
00096         GZ_DIST_EXPORT virtual ~gzDistClientInterface();
00097 
00099 
00111         GZ_DIST_EXPORT gzBool initialize(const gzDouble& tickInterval = 0, gzULong poolId = GZ_DIST_POOL_ID_DEFAULT, gzBool responseBoost = FALSE);
00112 
00114 
00119         GZ_DIST_EXPORT gzBool uninitialize(gzBool wait = FALSE);
00120 
00122 
00125         GZ_DIST_EXPORT gzBool isInitialized();
00126 
00128 
00132         GZ_DIST_EXPORT const gzDistClientID& getID();
00133 
00135 
00149         GZ_DIST_EXPORT gzDistSessionPtr getSession(const gzString& sessionName, gzBool create = FALSE, gzBool global = FALSE, gzDistServerPriority prio = GZ_DIST_SERVER_PRIO_NORMAL);
00150 
00152 
00159         GZ_DIST_EXPORT gzBool joinSession(const gzDistSessionPtr& session, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00160 
00162 
00168         GZ_DIST_EXPORT gzBool resignSession(const gzDistSessionPtr& session, gzInt timeOut = GZ_DIST_SYNCHRONOUS);
00169 
00171 
00177         GZ_DIST_EXPORT gzBool subscribeSessions(gzBool notifyExisting = FALSE, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00178 
00180 
00183         GZ_DIST_EXPORT gzBool unsubscribeSessions(gzInt timeOut = GZ_DIST_SYNCHRONOUS);
00184 
00186 
00194         GZ_DIST_EXPORT gzBool subscribeEvents(gzType* classType, const gzDistSessionPtr& session, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00195 
00197 
00203         GZ_DIST_EXPORT gzBool unsubscribeEvents(gzType* classType, const gzDistSessionPtr& session, gzInt timeOut = GZ_DIST_SYNCHRONOUS);
00204 
00206 
00210         GZ_DIST_EXPORT gzBool sendEvent(gzDistEvent* event, const gzDistSessionPtr& session);
00211 
00213 
00221         GZ_DIST_EXPORT gzBool subscribeObjects(gzType* classType, const gzDistSessionPtr& session, gzBool notifyExisting = FALSE, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00222 
00224 
00230         GZ_DIST_EXPORT gzBool unsubscribeObjects(gzType* classType, const gzDistSessionPtr& session, gzInt timeOut = GZ_DIST_SYNCHRONOUS);
00231 
00233 
00240         GZ_DIST_EXPORT gzBool subscribeAttributes(const gzDistObjectPtr& object, gzBool notifyExisting = FALSE, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00241 
00243 
00248         GZ_DIST_EXPORT gzBool unsubscribeAttributes(const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_SYNCHRONOUS);
00249 
00251 
00259         GZ_DIST_EXPORT gzBool subscribeAttributeValue(const gzString& attributeName, const gzDistObjectPtr& object, gzBool notifyExisting = FALSE, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00260 
00262 
00267         GZ_DIST_EXPORT gzBool subscribeAttributeValue(const gzDistObjectPtr& object, gzBool notifyExisting = FALSE, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00268 
00270 
00274         GZ_DIST_EXPORT gzBool unsubscribeAttributeValue(const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00275 
00277 
00290         GZ_DIST_EXPORT gzBool subscribeAttributeValue(const gzDistTransactionPtr& transaction, const gzDistObjectPtr& object, gzBool notifyExisting = FALSE, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00291 
00293 
00307         GZ_DIST_EXPORT gzBool subscribeAttributeValue(const gzDistNotificationSet& attributes, const gzDistObjectPtr& object, gzBool notifyExisting = FALSE, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00308 
00310 
00317         GZ_DIST_EXPORT gzBool unsubscribeAttributeValue(const gzString& attributeName, const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_SYNCHRONOUS);
00318 
00320 
00328         GZ_DIST_EXPORT gzBool unsubscribeAttributeValue(const gzDistTransactionPtr& transaction, const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00329 
00331 
00341         GZ_DIST_EXPORT gzBool updateObject(const gzDistTransactionPtr& transaction, const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00342 
00344 
00352         GZ_DIST_EXPORT gzBool updateObject(const gzString& attributeName, const gzDynamicType& value, const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00353 
00355 
00363         GZ_DIST_EXPORT gzBool removeAttributes(const gzDistTransactionPtr& transaction, const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00364 
00366 
00372         GZ_DIST_EXPORT gzBool removeAttribute(const gzString& attributeName, const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00373 
00375 
00386         GZ_DIST_EXPORT gzBool addObject(const gzDistObjectPtr& object, const gzDistSessionPtr& session, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00387 
00389 
00396         GZ_DIST_EXPORT gzDistObjectPtr findObject(const gzString& objectName, const gzDistSessionPtr& session);
00397 
00399 
00410         GZ_DIST_EXPORT gzDistObjectPtr waitForObject(const gzString& objectName, const gzDistSessionPtr& session, gzInt timeOut = 10);
00411 
00413 
00419         GZ_DIST_EXPORT gzBool removeObject(const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00420 
00422 
00435         GZ_DIST_EXPORT gzBool requestOwnership(const gzString& attributeName, const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00436 
00438 
00447         GZ_DIST_EXPORT gzBool dropOwnership(const gzString& attributeName, const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00448 
00450 
00459         GZ_DIST_EXPORT gzBool pullOwnership(const gzString& attributeName, const gzDistObjectPtr& object, gzInt timeOut = GZ_DIST_ASYNCHRONOUS);
00460 
00462 
00465         GZ_DIST_EXPORT gzVoid setAccumulateTick(gzBool accumulate);
00466 
00468         /* Typically used at a shutdown situation to ensure that all data are processed before shutdown.
00469            \param outQueue  If outQueue is TRUE, the queue with messages from the client is verifyed.
00470                             If outQueue is FALSE, the queue with messages to the client is verifyed.
00471            \return          TRUE if there are any data, otherwise false.*/
00472         GZ_DIST_EXPORT gzBool hasPendingData(gzBool outQueue = TRUE);
00473 
00475         /* One of the purposes with gzDistCilentInterface is to create a single threaded environment.
00476            If you need to communicate directly to a client and still remain the single threaded environment in the client,
00477            you can invoke calls via the invoke method, and the call cill be marshalled to the client thread if the calling thread is 
00478            not the same as the "client thread".
00479            \invokeData  The data that should be passed to the client.
00480            \return      The return value from the client.
00481            \note        The return value is only valid if the synchronous is set to TRUE.
00482                         The client must be initialized to use this call.
00483            \sa          onInvoke(const gzDynamicType&)*/
00484         GZ_DIST_EXPORT gzDynamicType invoke(const class gzDistInvokeMessage& message, gzBool synchronous = FALSE);
00485 
00486     protected:
00487 
00489 
00493         virtual gzVoid onClientTick() {}
00494 
00496 
00498         virtual gzBool canProcessClientMessages() {return TRUE;}
00499 
00500         //----- notification callbacks -----
00501     
00503 
00508         virtual gzVoid onEvent(gzDistEvent* event) {}
00509 
00511 
00517         virtual gzVoid onNewObject(gzDistObject* object, gzDistSession* session) {}
00518 
00520 
00525         virtual gzVoid onRemoveObject(gzDistObject* object, gzDistSession* session) {}
00526 
00528 
00535         virtual gzVoid onUpdateAttributes(const gzDistNotificationSet& attributes, gzDistObject* object, gzDistSession* session) {}
00536 
00538 
00544         virtual gzVoid onNewAttributes(const gzDistNotificationSet& attributes, gzDistObject* object, gzDistSession* session) {}
00545 
00547 
00553         virtual gzVoid onRemoveAttributes(const gzDistNotificationSet& attributes, gzDistObject* object, gzDistSession* session) {}
00554 
00556 
00561         virtual gzVoid onNewSession(gzDistSession* session) {}
00562 
00564 
00568         virtual gzVoid onRemoveSession(gzDistSession* session) {}
00569 
00571 
00578         virtual gzVoid onGrantOwnership(const gzString& attributeName, gzDistObject* object, gzDistSession* session) {}
00579 
00581 
00588         virtual gzVoid onDropOwnership(const gzString& attributeName, gzDistObject* object, gzDistSession* session) {}
00589 
00591 
00600         virtual gzVoid onRequestOwnership(const gzString& attributeName, gzDistObject* object, const gzDistClientID& requester, gzDistSession* session) {}
00601 
00603 
00609         virtual gzVoid onSystemShutdown() {}
00610 
00612 
00613         virtual gzVoid onDebugMessage(class gzDistDebugNotification* notification) {}
00614 
00616         /*  \return The value that should be passed to the caller
00617             \sa invoke(const gzDynamicType&)*/
00618         virtual gzDynamicType onInvoke(const gzDistInvokeMessage& message) { GZ_DIST_ASSERT(FALSE); return 0.0; }
00619 
00620     private:
00621         gzVoid create();
00622 
00623         // This is the method the manager calls to add messages to client.
00624         gzVoid addClientMessage(gzDistNotification* notification);
00625 
00626         // Process the messages from sessions etc. Called by gzDistClientThread.
00627         gzBool processClientMessages();
00628         
00629         // Put a message in out queue.
00630         gzVoid addOutMessage(gzDistClientMessage* message, gzBool prio = FALSE);
00631 
00632         // Wait until operation is complete or timeout has elapsed,
00633         // if the message was an acknowledge message.
00634         gzBool waitAck(gzDistClientMessage* message, gzInt timeOut);
00635 
00636         // Switch list with client. Called by the manager when processing the client message.
00637         gzVoid switchList(gzDistClientMessageList** list);
00638 
00639         // Switch list with client.
00640         // Called by the gzDistClientThread when processing the received messages.
00641         gzUInt switchNotificationList();
00642 
00643         gzVoid onNotification(gzDistNotification* notification);
00644 
00645         gzVoid addToTransaction(gzDistNotificationData* data);
00646 
00647         //gzVoid setTransaction(gzRefList<gzDistNotificationData>* data);
00648         gzVoid setTransaction(gzDistNotificationSet* data);
00649 
00650         gzVoid endTransaction(const gzDistNotificationType type, gzDistObject* object);
00651 
00652         // Lock and return a pointer to m_managerProcessList. Null if no messages are available.
00653         gzDistClientMessageList* getManagerDataList();
00654 
00655         // Unlock m_managerProcessList.
00656         gzVoid dropManagerDataList();
00657 
00658         gzBool isBoosted();
00659 
00660         gzULong getThreadPoolId();
00661 
00662     // Members:
00663 
00664         // The global identification of the client.
00665         const gzDistClientID m_identification;
00666 
00667 
00668         // This pointer will point on the notification list, that is processed by the client thread.
00669         gzDistNotificationList* m_clientNotificationList;
00670 
00671         // This pointer will point on the list where all notifications to a client are placed.
00672         gzDistNotificationList* m_managerNotificationList;
00673         
00674         // This mutex is used when accessing m_clientNotificationListPtr
00675         gzMutex m_clientNotificationListMutex;
00676 
00677         // This mutex is used when accessing m_managerNotificationListPtr
00678         gzMutex m_managerNotificationListMutex;
00679 
00680 
00681         // This member will point on a list that the manager is processing.
00682         gzDistClientMessageList* m_managerProcessList;
00683 
00684         // This member will point on a list where all outgoing messages are put.
00685         gzDistClientMessageList* m_clientSendList;
00686 
00687         // This mutex is used to access m_managerProcessList
00688         gzMutex m_managerProcessListMutex;
00689 
00690         // This mutex is used to access m_sendQueue
00691         gzMutex m_clientSendListMutex;
00692 
00693         
00694         // The interval the client has desired to be "ticked" with.
00695         gzDouble m_clientTickInterval;
00696 
00697         // Next time the client will be "ticked".
00698         gzDouble m_nextTickTime;
00699 
00700         // Previous tick time. Used to calculate a more precise m_nextTickTime.
00701         gzDouble m_lastTickTime;
00702 
00703         // The thread pool id the client is using.
00704         gzULong m_threadPoolId;
00705 
00706         // Pointer to the thread this client is using.
00707         class gzDistThreadInterface* m_thread;
00708 
00709         // This member will temporary hold the transaction data during a transaction notification.
00710         //gzRefList<gzDistNotificationData>* m_transactionData;
00711         gzDistNotificationSet* m_transactionData;
00712 
00713         // Tells if missed "ticks" to onClientTick() shall be accumulated or skipped.
00714         gzBool m_skipTick;
00715 
00716         // Tells if the interface is initialized.
00717         gzBool m_initialized;
00718 
00719         gzBool m_responseBoost;
00720 
00721         // Flag to prevent recursive calls
00722         gzBool m_processing;
00723 };
00724 
00725 
00726 // Internal client message types
00727 enum gzDistClientMessageType
00728 {
00729     GZ_CLIENT_MESSAGE_TYPE_JOIN_SESSION = 1,
00730     GZ_CLIENT_MESSAGE_TYPE_RESIGN_SESSION,
00731     GZ_CLIENT_MESSAGE_TYPE_SEND_EVENT,
00732     GZ_CLIENT_MESSAGE_TYPE_ADD_OBJECT,
00733     GZ_CLIENT_MESSAGE_TYPE_REMOVE_OBJECT,
00734     GZ_CLIENT_MESSAGE_TYPE_SUBSCRIBE_OBJECT,
00735     GZ_CLIENT_MESSAGE_TYPE_UNSUBSCRIBE_OBJECT,
00736     GZ_CLIENT_MESSAGE_TYPE_SUBSCRIBE_EVENT,
00737     GZ_CLIENT_MESSAGE_TYPE_UNSUBSCRIBE_EVENT,
00738     GZ_CLIENT_MESSAGE_TYPE_SUBSCRIBE_NEW_ATTRIBUTES,
00739     GZ_CLIENT_MESSAGE_TYPE_UNSUBSCRIBE_NEW_ATTRIBUTES,
00740     GZ_CLIENT_MESSAGE_TYPE_SUBSCRIBE_ATTRIBUTE,         // Subscribe an attribute value
00741     GZ_CLIENT_MESSAGE_TYPE_UNSUBSCRIBE_ATTRIBUTE,       // Unsubscribe an attribute value
00742     GZ_CLIENT_MESSAGE_TYPE_SUBSCRIBE_SESSIONS,
00743     GZ_CLIENT_MESSAGE_TYPE_UNSUBSCRIBE_SESSIONS,
00744     GZ_CLIENT_MESSAGE_TYPE_GET_SESSION,
00745     GZ_CLIENT_MESSAGE_TYPE_GET_ADD_SESSION,
00746     GZ_CLIENT_MESSAGE_TYPE_REMOVE_SESSION,
00747     GZ_CLIENT_MESSAGE_TYPE_UPDATE_OBJECT,
00748     GZ_CLIENT_MESSAGE_TYPE_REMOVE_ATTRIBUTES,
00749     GZ_CLIENT_MESSAGE_TYPE_REQUEST_OWNERSHIP,
00750     GZ_CLIENT_MESSAGE_TYPE_DROP_OWNERSHIP,
00751     GZ_CLIENT_MESSAGE_TYPE_PULL_OWNERSHIP
00752 };
00753 
00754 GZ_DIST_DECLARE_MEMBASE(gzDistClientMessage);
00755 
00756 // Internal client message
00757 class gzDistClientMessage : public gzDistThreadSafeReference GZ_DIST_MEMBASE(gzDistClientMessage)
00758 {
00759     public:
00760         GZ_DIST_MEMBASE_IMP(gzDistClientMessage);
00761 
00762         GZ_DIST_MEMBASE_CALL(gzDistClientMessage);
00763 
00764         // Constructor for join/resign session and remove object.
00765         gzDistClientMessage(const gzDistClientMessageType type, const gzDistSessionPtr& reference, gzBool ack);
00766 
00767         // Constructor for subscribe/unsubscribe object and event.
00768         gzDistClientMessage(const gzDistClientMessageType type, const gzDistSessionPtr& session, gzType* classType, gzBool ack, gzBool notifyExisting = FALSE);
00769 
00770         // Constructor for subscribe/unsubscribe an attribute in object.
00771         gzDistClientMessage(const gzDistClientMessageType type, const gzDistObjectPtr& object, const gzString& attributeName, gzBool ack, gzBool notifyExisting = FALSE);
00772 
00773         // Constructor for send/add event/object to session. Update object. Subscribe new attributes on object.
00774         gzDistClientMessage(const gzDistClientMessageType type, const gzDistThreadSafeReferencePtr& ref, const gzDistThreadSafeReferencePtr& addRef, gzBool ack, gzBool notifyExisting = FALSE);
00775 
00776         // Destructor
00777         virtual ~gzDistClientMessage();
00778 
00779         // Get session pointer.
00780         gzDistSession* getSession();
00781 
00782         // Get object pointer.
00783         gzDistObject* getObject();
00784 
00785         // Get event pointer.
00786         gzDistEvent* getEvent();
00787 
00788         // Get transaction pointer.
00789         gzDistTransaction* getTransaction();
00790 
00791         // Set transaction pointer.
00792         gzVoid getTransaction(gzDistTransaction* transaction);
00793 
00794         // The attribute in an attribute subscription.
00795         gzString* m_attributeName;
00796 
00797         // This member will contain the class type that should be subscribed/unsubscribed.
00798         gzType* m_classType;
00799 
00800         // Type of message
00801         gzDistClientMessageType m_messageType;
00802 
00803         // Tells if a subscription shall generate notification on existing data.
00804         gzBool m_notifyExisting;
00805 
00806         // This member is set to TRUE if a synchronous message is desired. The manager will zero this flag when operation is complete.
00807         gzBool m_acknowledge;
00808 
00809         // The distribution manager will put the result of the message here.
00810         gzBool m_result;
00811 
00812     private:
00813 
00814         // A reference to data that will be sent (object, event etc.)
00815         gzDistThreadSafeReferencePtr m_reference;
00816 
00817         // A reference a session or a transaction.
00818         gzDistThreadSafeReferencePtr m_additional_ref;
00819 
00820 };
00821 
00823 
00824 class gzDistInvokeMessage
00825 {
00826     public:
00827         GZ_DIST_EXPORT gzDistInvokeMessage(){}
00828         GZ_DIST_EXPORT gzDistInvokeMessage(gzULong type, const gzDynamicType& data) : messageType(type), messageData(data) {}
00829         GZ_DIST_EXPORT gzDistInvokeMessage(gzULong type) : messageType(type) {}
00830         GZ_DIST_EXPORT ~gzDistInvokeMessage(){}
00831 
00832         gzULong messageType;
00833         gzDynamicType messageData;
00834 };
00835 
00836 #endif
00837 

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