00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #ifndef AbstractSkelH
00054 #define AbstractSkelH
00055
00056 #include <zeusbase/System/Thread.h>
00057 #include <zeusbase/System/ZVariant.h>
00058 #include <zeusbase/System/ByteArray.hpp>
00059 #include <zeusbase/Net/ServerSocket.h>
00060 #include <zeusbase/Remote/RemoteDefines.hpp>
00061 #include <zeusbase/Remote/RemoteMethodCall.h>
00062 #include <zeusbase/Remote/RemoteReference.h>
00063 #include <zeusbase/Remote/Interfaces/IRemoteObject.hpp>
00064
00065
00071
00072 #define SKEL_DECL(classid, objectid) \
00073 class classid : public TAbstractSkel \
00074 { \
00075 public : \
00076 classid(objectid& rInstance)\
00077 : TAbstractSkel(), \
00078 m_rInstance(rInstance)\
00079 { \
00080 } \
00081 \
00082 protected: \
00083 virtual ~classid() \
00084 { \
00085 } \
00086 virtual Retval dispatchCommand(const IRemoteMethodCall& rMethod, IRemoteMethodCall*& rMethodreturn); \
00087 \
00088 private: \
00089 objectid& m_rInstance; \
00090 }; \
00091
00092
00093
00097
00098 #define SKEL_REMOTEREF(__param) pRemote##__param
00099
00100
00101
00105
00106 #define SKEL_DISPATCH_START(classid) \
00107 Retval classid::dispatchCommand(const IRemoteMethodCall& rMethod, IRemoteMethodCall*& rpMethodreturn) \
00108 { \
00109 Retval retValue = RET_NOERROR; \
00110 \
00111 TSingleLinkedList<IZUnknown*> lstParams; \
00112 TString strMethodName; \
00113 TRemoteMethodCall* pReturnCall = new TRemoteMethodCall(strMethodName); \
00114 TZVariant* pVarRetval = new TZVariant(); \
00115 rMethod.getMethodName(strMethodName); \
00116 RootLogger.printfln(LOGMODE_INFO, "Method received [%s]", strMethodName.c_str(NULL)); \
00117
00118
00124
00125 #define SKEL_DISP_SWITCH(__name) \
00126 if (strMethodName == __name) \
00127 {
00128
00129
00132
00133 #define SKEL_DISP_SWITCH_END }
00134
00135
00136
00152
00153 #define SKEL_READ_PARAM(__name, __param) \
00154 TString strParam##__param = __name; \
00155 if (rMethod.getParameterByName(strParam##__param, __param) ==RET_NOERROR) \
00156 { \
00157 lstParams.add(__param); \
00158 RootLogger.printfln(LOGMODE_INFO, "Read param %s...ok",strParam##__param.c_str(NULL)); \
00159 } \
00160 else \
00161 { \
00162 RootLogger.printfln(LOGMODE_ERROR, "Read param %s...failed", strParam##__param.c_str(NULL)); \
00163 retValue = RET_REQUEST_FAILED; \
00164 } \
00165
00166
00172
00173 #define SKEL_READ_PARAM_REMOTEREF(__name, __param) \
00174 SKEL_READ_PARAM(__name, __param) \
00175 ISerializable* pSerial##__param = NULL; \
00176 IRemoteObject* SKEL_REMOTEREF(__param) = NULL; \
00177 if (__param != NULL && __param->getObject(pSerial##__param) == RET_NOERROR) \
00178 { \
00179 if (pSerial##__param->askForInterface(INTERFACE_IRemoteObject, ICAST(SKEL_REMOTEREF(__param))) == RET_NOERROR) \
00180 { \
00181 lstParams.add(SKEL_REMOTEREF(__param)); \
00182 } \
00183 else \
00184 { \
00185 RootLogger.printfln(LOGMODE_ERROR, "Failed. Object is not of type IRemoteObject"); \
00186 retValue = RET_REQUEST_FAILED; \
00187 } \
00188 pSerial##__param->release(); \
00189 } \
00190 else \
00191 { \
00192 RootLogger.printfln(LOGMODE_ERROR, "Failed. Object is NULL"); \
00193 retValue = RET_REQUEST_FAILED; \
00194 } \
00195
00196
00199
00200 #define SKEL_REMOTEREF_CHECK(__param) (SKEL_REMOTEREF(__param) != NULL)
00201
00202
00205
00206 #define SKEL_DISPATCH_END \
00207 while(lstParams.getCount() > 0) \
00208 { \
00209 lstParams[0]->release(); \
00210 lstParams.deleteItem(0); \
00211 } \
00212 rpMethodreturn = pReturnCall; \
00213 rpMethodreturn->setReturnValue(*pVarRetval); \
00214 pVarRetval->release(); \
00215 return retValue; \
00216 }
00217
00218 BEGIN_NAMESPACE_Zeus
00219
00220 class TCCTPRequest;
00221 class TCCTPResponse;
00222 class TCriticalSection;
00223
00224
00231
00232 zeusbase_class TAbstractSkel : public TThread
00233 {
00234 public:
00235 TAbstractSkel();
00236
00237 virtual Retval startSkeleton(IString& rAddress, Uint& rPort, bool bDynamic);
00238 virtual void kill(Float64 dTimeOut = 5.0);
00239 virtual void execute();
00240 void dispatch1(const TCCTPRequest& request, TCCTPResponse& response);
00241
00242 Retval getClientSocket(Uint uiThreadID, TSocket*& rpSocket) const;
00243 bool isRunning() const;
00244 void setCleanFlag();
00245 void setUseRemoteObjectAsReferencesOnly(bool bFlag);
00246
00247 protected:
00248
00250
00251 #ifdef _MSC_VER
00252 public:
00253 #endif
00255
00256
00257
00258
00259
00260 class TClientHandlerThread : public TThread
00261 {
00262 public :
00263 TClientHandlerThread(TAbstractSkel& rParent, TSocket& rSocket);
00264
00265 Retval getSocket(TSocket*& rpSocket) const;
00266
00267 virtual void kill(Float64 dTimeOut = 5.0);
00268 virtual void execute();
00269
00270 protected:
00271 virtual ~TClientHandlerThread();
00272
00273 TAbstractSkel& m_rParent;
00274 TSocket& m_rClientSocket;
00275 };
00277
00278 #ifdef _MSC_VER
00279 protected:
00280 #endif
00282
00284 TSingleLinkedList<TClientHandlerThread*> m_lstClients;
00286 TCriticalSection& m_rLock;
00288 TAutoPtr<TServerSocket> m_ptrSocket;
00290 bool m_bCleanFlag;
00292 bool m_bUseRemoteObjectAsReferencesOnly;
00293
00294 virtual ~TAbstractSkel();
00295 virtual Retval dispatchCommand(const IRemoteMethodCall& rMethod, IRemoteMethodCall*& rpMethodreturn)=0;
00296 virtual void startNewHandler(TSocket& rClient);
00297
00298 };
00299
00300
00301
00305
00306 inline bool TAbstractSkel::isRunning() const
00307 {
00308 return (this->isAlive() && m_ptrSocket != NULL && m_ptrSocket->isBound());
00309 }
00310
00311
00316
00317 inline void TAbstractSkel::setUseRemoteObjectAsReferencesOnly(bool bFlag)
00318 {
00319 m_bUseRemoteObjectAsReferencesOnly = bFlag;
00320 }
00321
00322
00327
00328 namespace TAbstractSkelObjectRelease
00329 {
00330
00331
00334
00335 template <class T> inline void releaseInstances(T& rObject)
00336 {
00337 rObject.release();
00338 }
00339
00340
00343
00344 template <class T> inline void releaseInstances(T*& rpObject)
00345 {
00346 if (rpObject != NULL)
00347 {
00348 rpObject->release();
00349 rpObject = NULL;
00350 }
00351 }
00352
00353
00356
00357 template <> void inline releaseInstances(IRemoteObjectList& rObjectList)
00358 {
00359 while(rObjectList.getCount() > 0)
00360 {
00361 rObjectList.getItem(0)->release();
00362 rObjectList.deleteItem(0);
00363 }
00364 }
00365
00366
00369
00370 template <> void inline releaseInstances(IStringList& ) {}
00371 }
00372
00373 END_NAMESPACE_Zeus
00374
00375 #endif
00376