Vault
4.1
|
00001 /* 00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved. 00003 This file is part of the Code Vault version 4.1 00004 http://www.bombaydigital.com/ 00005 License: MIT. See LICENSE.md in the Vault top level directory. 00006 */ 00007 00008 #ifndef vclientsession_h 00009 #define vclientsession_h 00010 00013 #include "vstring.h" 00014 #include "vmutex.h" 00015 #include "vmutexlocker.h" 00016 #include "vmessagequeue.h" 00017 #include "vsocketstream.h" 00018 #include "vbinaryiostream.h" 00019 00024 class VMessageInputThread; 00025 class VMessageOutputThread; 00026 class VManagementInterface; 00027 class VSocket; 00028 class VSocketThread; 00029 class VListenerThread; 00030 class VMessage; 00031 class VMessageHandlerTask; 00032 class VServer; 00033 class VBentoNode; 00034 00035 typedef std::vector<const VMessageHandlerTask*> SessionTaskList; 00036 00045 class VClientSession : public VEnableSharedFromThis<VClientSession> { 00046 public: 00047 00068 VClientSession(const VString& sessionBaseName, VServer* server, const VString& clientType, VSocket* socket, VMessageInputThread* inputThread, VMessageOutputThread* outputThread, const VDuration& standbyTimeLimit, Vs64 maxQueueDataSize); 00069 00078 virtual void initIOThreads(); 00079 00080 const VString& getName() const { return mName; } 00081 00082 const VString& getClientType() const { return mClientType; } 00083 VMessageInputThread* getInputThread() const { return mInputThread; } 00084 VMessageOutputThread* getOutputThread() const { return mOutputThread; } 00085 00093 virtual bool isClientOnline() const = 0; 00100 virtual bool isClientGoingOffline() const = 0; 00106 virtual void shutdown(VThread* callingThread); 00118 void postOutputMessage(VMessagePtr message, bool isForBroadcast = false); 00125 void postBroadcastOutputMessage(VMessagePtr message) { this->postOutputMessage(message, true); } 00138 void sendMessageToClient(VMessagePtr message, const VString& sessionLabel, VBinaryIOStream& out); 00143 virtual const VString& getClientAddress() const { return mClientAddress; } 00144 00151 virtual VBentoNode* getSessionInfo() const; 00152 00153 protected: 00154 00155 virtual ~VClientSession(); // protected because only friend class VServer may delete us (when garbage collecting) 00156 00157 void _moveStandbyMessagesToAsyncOutputQueue(); 00158 int _getOutputQueueSize() const; 00159 00166 virtual void _postStandbyMessageToAsyncOutputQueue(VMessagePtr message); 00167 00168 VString mName; 00169 VString mLoggerName; 00170 VMutex mMutex; 00171 VServer* mServer; 00172 VString mClientType; 00173 VString mClientIP; 00174 int mClientPort; 00175 VString mClientAddress; 00176 VMessageInputThread* mInputThread; 00177 VMessageOutputThread* mOutputThread; 00178 bool mIsShuttingDown; 00179 00180 private: 00181 00182 VClientSession(const VClientSession&); // not copyable 00183 VClientSession& operator=(const VClientSession&); // not assignable 00184 00185 void _releaseQueuedClientMessages(); 00186 00187 VMessageQueue mStartupStandbyQueue; 00188 VInstant mStandbyStartTime; 00189 VDuration mStandbyTimeLimit; 00190 Vs64 mMaxClientQueueDataSize; 00191 00192 // We only access the socket i/o stream if postOutputMessage() is called 00193 // and we are not set up to use a separate output message thread. However, we are responsible 00194 // for deleting the socket object. 00195 VSocket* mSocket; 00196 VSocketStream mSocketStream; 00197 VBinaryIOStream mIOStream; 00198 }; 00199 00200 typedef VSharedPtr<VClientSession> VClientSessionPtr; 00201 typedef VSharedPtr<const VClientSession> VClientSessionConstPtr; 00202 typedef std::vector<VClientSessionPtr> VClientSessionList; 00203 00210 class VClientSessionFactory { 00211 public: 00212 00218 VClientSessionFactory(VManagementInterface* manager, VServer* server) : mManager(manager), mServer(server) {} 00219 virtual ~VClientSessionFactory() {} 00220 00227 virtual VClientSessionPtr createSession(VSocket* socket, VListenerThread* ownerThread) = 0; 00228 00234 void addSessionToServer(VClientSessionPtr session); 00235 00242 void setManager(VManagementInterface* manager) { mManager = manager; } 00243 00244 protected: 00245 00246 VClientSessionFactory(const VClientSessionFactory&); // not copyable 00247 VClientSessionFactory& operator=(const VClientSessionFactory&); // not assignable 00248 00249 VManagementInterface* mManager; 00250 VServer* mServer; 00251 }; 00252 00253 #endif /* vclientsession_h */