GizmoDistribution

gzDistServerInternal.h

Go to the documentation of this file.
00001 //*****************************************************************************
00002 // File         : gzDistServerInternal.h
00003 // Module       : gzDistribution
00004 // Description  : Definition of internal server classes
00005 // Author       : Anders Sandblad       
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  040226  Created file    
00019 //
00020 //******************************************************************************
00021 #ifndef __GZ_DIST_SERVER_INTERNAL_H__
00022 #define __GZ_DIST_SERVER_INTERNAL_H__
00023 
00029 // Includes
00030 #include "gzDistBase.h"
00031 #include "gzDistRemoteMessage.h"
00032 #include "gzDistServer.h"
00033 
00034 
00035 // Forward declarations
00036 class gzDistServerEngine;
00037 
00038 
00039 //----------------------------- gzDistServerData ------------------------------
00040 
00041 //*****************************************************************************
00042 // Class    : gzDistServerData
00043 //                                  
00044 // Purpose  : Server instance data
00045 //                                  
00046 // Notes    : - 
00047 //                                  
00048 // Revision History...                          
00049 //                                  
00050 // Who  Date    Description                     
00051 //                                  
00052 // XAA  031030  Created 
00053 //                                  
00054 //*****************************************************************************
00055 // Internal server instance data
00056 class gzDistServerData : public gzDistThreadSafeReference
00057 {
00058     public:
00059 
00060         gzDistServerData();
00061 
00062         explicit gzDistServerData(const gzDistInstanceID& id,
00063                                     gzDistServerPriority prio = GZ_DIST_SERVER_PRIO_NORMAL,
00064                                     gzDistServerStatus stat = GZ_DIST_SERVER_STATUS_IDLE);
00065 
00066         explicit gzDistServerData(const gzDistRemoteMessageServer* message);
00067 
00068         virtual ~gzDistServerData() {};
00069 
00070         gzReference* clone() const;
00071 
00072         gzBool useDeepCopy() { return FALSE; };
00073 
00074 
00075         gzDistInstanceID instanceID;
00076 
00077         gzDistServerStatus status : 8;
00078 
00079         gzDistServerPriority priority : 8;
00080 
00081         gzDouble lastPingTime;
00082 
00083 };
00084 
00085 
00086 // Reference pointer type declaration
00087 GZ_DIST_PTR(gzDistServerData);
00088 
00089 // Pointer list type declaration
00090 GZ_DIST_LIST(gzDistServerData);
00091 
00092 
00093 //--------------------------- gzDistServerMessage -----------------------------
00094 
00095 // Server message type
00096 enum gzDistServerMessageType
00097 {
00098     GZ_DIST_SERVER_MESSAGE_UNKNOWN = 0,
00099 
00100     // from server
00101     GZ_DIST_SERVER_MESSAGE_NEW_INSTANCE,        // When a new remote instance has been discovered
00102     GZ_DIST_SERVER_MESSAGE_INSTANCE_SHUTDOWN,   // When a remote instance has been removed after shutdown
00103     GZ_DIST_SERVER_MESSAGE_INSTANCE_TIMEOUT,    // When a remote instance has been removed after timeout
00104     GZ_DIST_SERVER_MESSAGE_INSTANCE_ACTIVE,     // When a remote instance has entered active state
00105     GZ_DIST_SERVER_MESSAGE_UPDATE_STATUS,       // When the status of the local instance has changed 
00106 
00107     // to server
00108     GZ_DIST_SERVER_MESSAGE_SHUTDOWN,            // Enter passive state before uninstall (optionally)
00109     GZ_DIST_SERVER_MESSAGE_BEGIN_SYNCHRONIZE,   // When a passive instance need to be synchronized
00110     GZ_DIST_SERVER_MESSAGE_BEGIN_UPDATE,        // When two active instances need to be synchronized
00111 
00112     // from server
00113     GZ_DIST_SERVER_MESSAGE_END_SYNCHRONIZE,     // When the passive instance has been syncronized
00114     GZ_DIST_SERVER_MESSAGE_END_UPDATE           // When the active instances has been syncronized
00115 };
00116 
00117 
00118 //*****************************************************************************
00119 // Class    : gzDistServerMessage
00120 //                                  
00121 // Purpose  : Priority based distributed server support
00122 //                                  
00123 // Notes    : - 
00124 //                                  
00125 // Revision History...                          
00126 //                                  
00127 // Who  Date    Description                     
00128 //                                  
00129 // XAA  040226  Created 
00130 //                                  
00131 //*****************************************************************************
00132 // Internal server message
00133 class gzDistServerMessage
00134 {
00135     public:
00136         
00137         gzDistServerMessage();
00138         
00139         gzDistServerMessage(gzDistServerMessageType t, const gzDistInstanceID& id, gzDistServerStatus s = GZ_DIST_SERVER_STATUS_UNKNOWN);
00140 
00141         gzDistServerMessage(gzDistServerMessageType t, gzDistServerData* d);
00142 
00143         virtual ~gzDistServerMessage();
00144         
00145         
00146         gzDistServerMessageType type;
00147                         
00148         gzDistInstanceID instanceID;
00149 
00150         gzDistServerStatus status : 8;
00151 
00152 
00153 };
00154 
00155 
00156 //--------------------------- gzDistServerImpl --------------------------------
00157 
00158 //*****************************************************************************
00159 // Class    : gzDistServerImpl
00160 //                                  
00161 // Purpose  : Priority based distributed server support internal implementation
00162 //                                  
00163 // Notes    : - 
00164 //                                  
00165 // Revision History...                          
00166 //                                  
00167 // Who  Date    Description                     
00168 //                                  
00169 // XAA  031030  Created 
00170 //                                  
00171 //*****************************************************************************
00172 // Internal server implementation
00173 class gzDistServerImpl
00174 {
00175     public:
00176 
00177         // Constructor
00178         gzDistServerImpl(const gzString& functionID, gzDistServerPriority prio);
00179 
00180         // Destructor
00181         virtual ~gzDistServerImpl();
00182 
00183         
00184         // Install server
00185         gzBool install();
00186 
00187         // Uninstall server
00188         gzBool uninstall();
00189 
00190 
00191         // Is the server installed ?
00192         gzBool isInstalled() const { return m_installed; };
00193 
00194         // Has the server got an error ?
00195         gzBool hasError() const { return (m_instance->status == GZ_DIST_SERVER_STATUS_ERROR); };
00196 
00197         // Get function ID
00198         const gzString& getFunctionID() const { return m_functionID; };
00199 
00200         // Get priority
00201         gzDistServerPriority getPriority() const { return m_instance->priority; };
00202 
00203         // Get current status
00204         gzDistServerStatus getStatus() const { return m_instance->status; };
00205 
00206         // Set current status
00207         gzVoid setStatus(gzDistServerStatus status) { m_instance->status = status; };
00208 
00209         // Get the id of current active server
00210         gzVoid getCurrentActiveID(gzDistInstanceID& id);
00211 
00212         // Call this when requested synchronize is finished
00213         gzVoid endSynchronize(const gzDistInstanceID& id);
00214 
00215         // Call this when requested update is finished
00216         gzVoid endUpdate(const gzDistInstanceID& id);
00217 
00218         // Call this to (optionally) enter the shutdown state before uninstallation
00219         gzVoid shutDown();
00220 
00221         // Swap outgoing message buffer
00222         gzVoid swapMessages(gzList<gzDistServerMessage>** list);
00223 
00224 
00225         //------ engine callbacks ------
00226 
00227         // Server engine install callback
00228         gzVoid onInstall();
00229 
00230         // Tick engine callback
00231         gzVoid onTick();
00232 
00233         // Server engine uninstall callback
00234         gzVoid onUninstall();
00235 
00236         // Remote message receive callback
00237         gzVoid onRemoteMessage(gzDistRemoteMessageServer* message);
00238 
00239 
00240     private:
00241 
00242         // Process server
00243         gzVoid processServer();
00244 
00245         // Process messages
00246         gzVoid processMessages();
00247 
00248         // Handle received ping message
00249         gzVoid receivePing(const gzDistRemoteMessageServer* message);
00250 
00251         // Handle received synchronize request
00252         gzVoid receiveRequestSynchronize(const gzDistRemoteMessageServer* message);
00253 
00254         // Handle received synchronize end
00255         gzVoid receiveEndSynchronize(const gzDistRemoteMessageServer* message);
00256 
00257         // Handle received update request
00258         gzVoid receiveRequestUpdate(const gzDistRemoteMessageServer* message);
00259 
00260         // Handle received update end
00261         gzVoid receiveEndUpdate(const gzDistRemoteMessageServer* message);
00262 
00263 
00264         // Send ping
00265         gzVoid sendPing();
00266 
00267         // Send synchronize request
00268         gzVoid sendRequestSynchronize(const gzDistInstanceID& destination);
00269 
00270         // Send synchronize end
00271         gzVoid sendEndSynchronize(const gzDistInstanceID& destination);
00272 
00273         // Send update request
00274         gzVoid sendRequestUpdate(const gzDistInstanceID& destination);
00275 
00276         // Send update end
00277         gzVoid sendEndUpdate(const gzDistInstanceID& destination);
00278 
00279 
00280         // Create remote message
00281         gzDistRemoteMessageServerPtr createMessage(gzDistRemoteMessageType type, const gzDistInstanceID& destination = GZ_DIST_NULL_INSTANCE);
00282 
00283 
00284         // Update current status
00285         gzVoid updateStatus(gzDistServerStatus status, gzBool send = TRUE);
00286 
00287         // Set the current active server instance, and notify to client
00288         gzVoid setCurrentActiveInstance(gzDistServerData* instance);
00289 
00290         // Find instance data
00291         gzDistServerDataPtr findInstance(const gzDistInstanceID& id);
00292 
00293         // Add instance data
00294         gzVoid addInstance(gzDistServerData* newInstance);
00295 
00296         // Remove instance data
00297         gzVoid removeInstance(gzDistServerData* newInstance);
00298 
00299         // Check status of instances
00300         gzVoid checkInstances();
00301     
00302         // Add message to out queue (notify client)
00303         gzVoid notify(gzDistServerMessageType type, gzDistServerData* data);
00304 
00305         // Check if the local instance is in a running state (passive or active)
00306         gzBool isRunningState() const;
00307 
00308 
00309         // Debugging
00310         gzVoid traceStatus(const gzDistServerData* instance);
00311         gzVoid traceInstances();
00312 
00313 
00314     private:
00315 
00316         // The engine
00317         gzDistServerEngine* m_engine;
00318 
00319         // Incoming messages
00320         gzMutex m_inMessageLocker;
00321         gzList<gzDistServerMessage>* m_inMessageList;
00322         gzList<gzDistServerMessage>* m_inMessageListLocal;
00323         
00324         // Outgoing messages
00325         gzMutex m_outMessageLocker;
00326         gzList<gzDistServerMessage>* m_outMessageList;
00327 
00328         // Local server data
00329         const gzString m_functionID;
00330         gzDouble m_startTime;
00331         gzDouble m_lastCheckTime;
00332         gzDouble m_lastActiveTime;
00333         gzUShort m_synchronizing;
00334         gzUShort m_updating;
00335         gzBool m_shutdownPending;
00336         gzBool m_activeWarning;
00337         gzBool m_installed;
00338 
00339 
00340         // Instance data
00341         gzDistServerDataPtr m_instance;         // me
00342         gzDistServerDataList m_instanceList;    // others
00343         gzULong m_potentialActiveInstances;
00344         gzDistServerDataPtr m_activeInstance;   // current active
00345         gzMutex m_activeInstanceLocker;
00346 
00347 
00348         // Disable copy and assignment
00349         gzDistServerImpl(const gzDistServerImpl&);
00350         gzDistServerImpl operator=(const gzDistServerImpl&);
00351 };
00352 
00353 
00354 //--------------------------- gzDistServerEngine ------------------------------
00355 
00356 //*****************************************************************************
00357 // Class    : gzDistServerEngine
00358 //                                  
00359 // Purpose  : The internal manager/engine for distributed servers
00360 //                                  
00361 // Notes    : - 
00362 //                                  
00363 // Revision History...                          
00364 //                                  
00365 // Who  Date    Description                     
00366 //                                  
00367 // XAA  031030  Created 
00368 //                                  
00369 //*****************************************************************************
00370 // The internal thread engine for servers
00371 class gzDistServerEngine : protected gzThread
00372 {
00373     public:
00374 
00375         // Create and start the singleton engine instance
00376         static gzDistServerEngine* startEngine(gzDistRemoteChannelInterface* serverChannel);
00377 
00378         // Get the singleton engine instance
00379         static gzDistServerEngine* getEngine();
00380 
00381         // Shutdown the engine
00382         gzVoid shutDown();
00383 
00384         // Install server
00385         gzBool install(gzDistServerImpl* server);
00386 
00387         // Uninstall server
00388         gzBool uninstall(gzDistServerImpl* server);
00389 
00390 
00391         // Send remote message
00392         gzBool send(gzDistRemoteMessageServer* message);
00393 
00394 
00395     protected:
00396 
00397         // Constructor
00398         gzDistServerEngine(gzDistRemoteChannelInterface* serverChannel);
00399 
00400         // Destructor
00401         virtual ~gzDistServerEngine();
00402 
00403 
00404     private:
00405 
00406         // Engine thread
00407         gzVoid process();
00408         gzVoid processServers();
00409         gzULong receiveMessages();
00410         
00411         // Instance ID for this process
00412         gzDistInstanceID m_instanceID;
00413         
00414         // Remote distribution server channel
00415         gzDistRemoteChannelInterface* m_serverChannel;
00416 
00417         // Servers to be installed
00418         gzList<gzDistServerImpl> m_installList;
00419         gzMutex m_installLocker;
00420         
00421         // Servers to be uninstalled
00422         gzList<gzDistServerImpl> m_uninstallList;
00423         gzMutex m_uninstallLocker;
00424         
00425         // Installed servers (to be ticked)
00426         gzDict<gzString, gzDistServerImpl> m_serverDict;
00427 
00428         // Singleton instance
00429         static gzDistServerEngine* m_instance;
00430         static gzMutex m_instanceLocker;
00431 
00432         // Disable copy and assignment
00433         gzDistServerEngine(const gzDistServerEngine&);
00434         gzDistServerEngine operator=(const gzDistServerEngine&);
00435 
00436 };
00437 
00438 
00439 #endif // __GZ_DIST_SERVER_INTERNAL_H__

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