| 1 |
#pragma once |
| 2 |
|
| 3 |
////////////////////////////////////////////////////////////////////////// |
| 4 |
|
| 5 |
#include "Interlocked.h" |
| 6 |
#include "Types.h" |
| 7 |
#include "Stopwatch.h" |
| 8 |
#include "StringConvert.h" |
| 9 |
#include <afxmt.h> |
| 10 |
#include <process.h> // For _beginthreadex |
| 11 |
|
| 12 |
////////////////////////////////////////////////////////////////////////// |
| 13 |
|
| 14 |
class CWcsAPI |
| 15 |
{ |
| 16 |
public: |
| 17 |
CWcsAPI() {} |
| 18 |
virtual ~CWcsAPI() {} |
| 19 |
|
| 20 |
public: |
| 21 |
// WinCS API |
| 22 |
virtual BOOL API_StartPlugin(WORD wPluginID, const CByteArray &arg) |
| 23 |
{ return FALSE; } |
| 24 |
virtual BOOL API_StopPlugin(double dbRuntime) // For Single mode or Slave |
| 25 |
{ return FALSE; } |
| 26 |
virtual BOOL API_StopPlugin(const CalcReportList &list) // For System mode |
| 27 |
{ return FALSE; } |
| 28 |
virtual BOOL API_Broadcast(const CByteArray &data) |
| 29 |
{ return FALSE; } |
| 30 |
virtual BOOL API_SendToMaster(const CByteArray &data) |
| 31 |
{ return FALSE; } |
| 32 |
virtual BOOL API_CreateGraphWindow(const CString &csName, int cx, int cy, BOOL bFront) |
| 33 |
{ return FALSE;} |
| 34 |
virtual BOOL API_SetPixel(int x, int y, COLORREF color) |
| 35 |
{ return FALSE;} |
| 36 |
virtual BOOL API_SetXLine(int y, int cx, LPCOLORREF lpColor) |
| 37 |
{ return FALSE;} |
| 38 |
virtual BOOL API_SetYLine(int x, int cy, LPCOLORREF lpColor) |
| 39 |
{ return FALSE;} |
| 40 |
virtual BOOL API_SetTaskSchedule(const NodeInitList &list) |
| 41 |
{ return FALSE; } |
| 42 |
virtual void API_SetProgress(DWORD dwIndex, int nPoint) |
| 43 |
{} |
| 44 |
virtual void API_InitProgress(DWORD dwProcessors) |
| 45 |
{} |
| 46 |
virtual void API_SetGraphWindowSize(int cx, int cy, int x, int y) |
| 47 |
{} |
| 48 |
virtual void API_SetCenterWindow() |
| 49 |
{} |
| 50 |
virtual void API_ResetWindow() |
| 51 |
{} |
| 52 |
virtual void API_StartTimer() |
| 53 |
{} |
| 54 |
virtual void API_StopTimer() |
| 55 |
{} |
| 56 |
}; |
| 57 |
|
| 58 |
////////////////////////////////////////////////////////////////////////// |
| 59 |
|
| 60 |
// Base class |
| 61 |
class CPlugin : public CStringConvert |
| 62 |
{ |
| 63 |
protected: |
| 64 |
explicit CPlugin(); |
| 65 |
virtual ~CPlugin(); |
| 66 |
|
| 67 |
public: |
| 68 |
// Override function |
| 69 |
virtual BOOL LoadInitialize() { return TRUE; } |
| 70 |
virtual void ReceiveCallBack(const CByteArray &data) {} |
| 71 |
|
| 72 |
virtual BOOL MasterInitialize(const NodeDataList &list, BOOL fSystem) { return TRUE; } |
| 73 |
virtual BOOL MasterFinalize() { return TRUE; } |
| 74 |
virtual DWORD MasterTransaction(LPVOID lpParams = NULL); |
| 75 |
|
| 76 |
virtual BOOL SlaveInitialize(const SLAVE_PLUGIN &init); |
| 77 |
virtual BOOL SlaveFinalize() { return TRUE; } |
| 78 |
virtual DWORD SlaveTransaction(LPVOID lpParams = NULL); |
| 79 |
|
| 80 |
public: |
| 81 |
BOOL StartPlugin(const CByteArray &arg); |
| 82 |
BOOL CancelPlugin(DWORD dwTimeout); |
| 83 |
void GetPluginInfo(LPPLUGIN_INFO lpInfo); |
| 84 |
void SetAPI(CWcsAPI *pAPI) { m_pAPI = pAPI; } |
| 85 |
void SetCalcSize(CALCSIZE size) { m_CalcSize = size; } |
| 86 |
void GetCalcSize(CALCSIZE &size) { size = m_CalcSize; } |
| 87 |
BOOL IsMaster() { return m_fMaster; } |
| 88 |
|
| 89 |
// WinCS API wrapper |
| 90 |
// Master function |
| 91 |
BOOL API_StartPlugin(WORD wPluginID, const CByteArray &arg); |
| 92 |
BOOL API_StopPlugin(double dbRuntime); |
| 93 |
BOOL API_StopPlugin(const CalcReportList &list); |
| 94 |
BOOL API_Broadcast(const CByteArray &data); |
| 95 |
BOOL API_CreateGraphWindow(const CString &csName, int cx, int cy, BOOL bFront = FALSE); |
| 96 |
BOOL API_SetPixel(int x, int y, COLORREF color); |
| 97 |
BOOL API_SetXLine(int y, int cx, LPCOLORREF lpColor); |
| 98 |
BOOL API_SetYLine(int x, int cy, LPCOLORREF lpColor); |
| 99 |
BOOL API_SetTaskSchedule(const NodeInitList &list); |
| 100 |
void API_SetGraphWindowSize(int cx, int cy, int x = 0, int y = 0); |
| 101 |
void API_SetCenterWindow(); |
| 102 |
void API_ResetWindow(); |
| 103 |
void API_StartTimer(); |
| 104 |
void API_StopTimer(); |
| 105 |
|
| 106 |
// Slave function |
| 107 |
BOOL API_SendToMaster(const CByteArray &data); |
| 108 |
void API_SetProgress(DWORD dwIndex, int nPoint); |
| 109 |
void API_InitProgress(DWORD dwProcessors); |
| 110 |
|
| 111 |
protected: |
| 112 |
typedef CZeroEvent<SHORT> ZeroEvent; |
| 113 |
typedef CArray<ZeroEvent> EventList; |
| 114 |
|
| 115 |
void SetCalcReport(DWORD dwIndex, double dbRuntime); |
| 116 |
BOOL doCreateWindow(const MASTER_PLUGIN &init); |
| 117 |
static DWORD WINAPI doMasterTransaction(LPVOID lpParams); |
| 118 |
static DWORD WINAPI doSlaveTransaction(LPVOID lpParams); |
| 119 |
|
| 120 |
private: |
| 121 |
void CloseHandleList(BOOL fWait = FALSE, DWORD dwMilliseconds = INFINITE); |
| 122 |
void ThreadFinish(); |
| 123 |
|
| 124 |
protected: |
| 125 |
typedef struct _PLUGIN_PARAMS |
| 126 |
{ |
| 127 |
CPlugin *obj; |
| 128 |
DWORD dwIndex; |
| 129 |
DWORD dwAffinity; |
| 130 |
DWORD dwThdCount; |
| 131 |
BOOL fAlive; |
| 132 |
BOOL fSystem; |
| 133 |
CByteArray arg; |
| 134 |
} |
| 135 |
PLUGIN_PARAMS, *LPPLUGIN_PARAMS; |
| 136 |
|
| 137 |
typedef struct _THREAD_COUNT |
| 138 |
{ |
| 139 |
ATM_DWORD dwCount; |
| 140 |
ATM_DWORD dwFinish; |
| 141 |
} |
| 142 |
THREAD_COUNT, *LPTHREAD_COUNT; |
| 143 |
|
| 144 |
typedef struct _CALCCOUNT |
| 145 |
{ |
| 146 |
ATM_DWORD dwFinCount; |
| 147 |
ATM_DWORD dwAllCount; |
| 148 |
} |
| 149 |
CALCCOUNT, *LPCALCCOUNT; |
| 150 |
|
| 151 |
typedef CArray<PLUGIN_PARAMS> PlgPrmList; |
| 152 |
|
| 153 |
CCriticalSection m_NetLock; |
| 154 |
CalcReportList m_Reportlist; // For Single mode |
| 155 |
NodeInitList m_NodeInitList; // For System mode |
| 156 |
PLUGIN_INFO m_PlgInfo; |
| 157 |
CByteArray m_Argument; |
| 158 |
CStopwatch m_Stopwatch; |
| 159 |
PlgPrmList m_PlgPrmList; |
| 160 |
HandleList m_ThdList; |
| 161 |
DWORDList m_ThdIDList; |
| 162 |
EventList m_EventList; |
| 163 |
CALCCOUNT m_CalcCount; |
| 164 |
CALCSIZE m_CalcSize; |
| 165 |
BOOL m_fRunning; |
| 166 |
BOOL m_fMaster; |
| 167 |
BOOL m_fSystem; |
| 168 |
|
| 169 |
MASTER_PLUGIN m_MasterPlugin; |
| 170 |
SLAVE_PLUGIN m_SlavePlugin; |
| 171 |
|
| 172 |
private: |
| 173 |
CCriticalSection m_ThdLock; |
| 174 |
THREAD_COUNT m_ThdCount; |
| 175 |
CWcsAPI *m_pAPI; |
| 176 |
}; |
| 177 |
|
| 178 |
////////////////////////////////////////////////////////////////////////// |
| 179 |
|
| 180 |
inline BOOL CPlugin::API_StartPlugin(WORD wPluginID, const CByteArray &arg) |
| 181 |
{ |
| 182 |
ASSERT(m_pAPI); |
| 183 |
return m_pAPI->API_StartPlugin(wPluginID, arg); |
| 184 |
} |
| 185 |
|
| 186 |
inline BOOL CPlugin::API_StopPlugin(double dbRuntime) |
| 187 |
{ |
| 188 |
ASSERT(m_pAPI); |
| 189 |
return m_pAPI->API_StopPlugin(dbRuntime); |
| 190 |
} |
| 191 |
|
| 192 |
inline BOOL CPlugin::API_StopPlugin(const CalcReportList &list) |
| 193 |
{ |
| 194 |
ASSERT(m_pAPI); |
| 195 |
return m_pAPI->API_StopPlugin(list); |
| 196 |
} |
| 197 |
|
| 198 |
inline BOOL CPlugin::API_Broadcast(const CByteArray &data) |
| 199 |
{ |
| 200 |
ASSERT(m_pAPI); |
| 201 |
return m_pAPI->API_Broadcast(data); |
| 202 |
} |
| 203 |
|
| 204 |
inline BOOL CPlugin::API_SendToMaster(const CByteArray &data) |
| 205 |
{ |
| 206 |
ASSERT(m_pAPI); |
| 207 |
return m_pAPI->API_SendToMaster(data); |
| 208 |
} |
| 209 |
|
| 210 |
inline BOOL CPlugin::API_CreateGraphWindow(const CString &csName, int cx, int cy, BOOL bFront) |
| 211 |
{ |
| 212 |
ASSERT(m_pAPI); |
| 213 |
return m_pAPI->API_CreateGraphWindow(csName, cx, cy, bFront); |
| 214 |
} |
| 215 |
|
| 216 |
inline BOOL CPlugin::API_SetPixel(int x, int y, COLORREF color) |
| 217 |
{ |
| 218 |
ASSERT(m_pAPI); |
| 219 |
return m_pAPI->API_SetPixel(x, y, color); |
| 220 |
} |
| 221 |
|
| 222 |
inline BOOL CPlugin::API_SetXLine(int y, int cx, LPCOLORREF lpColor) |
| 223 |
{ |
| 224 |
ASSERT(m_pAPI); |
| 225 |
return m_pAPI->API_SetXLine(y, cx, lpColor); |
| 226 |
} |
| 227 |
|
| 228 |
inline BOOL CPlugin::API_SetYLine(int x, int cy, LPCOLORREF lpColor) |
| 229 |
{ |
| 230 |
ASSERT(m_pAPI); |
| 231 |
return m_pAPI->API_SetYLine(x, cy, lpColor); |
| 232 |
} |
| 233 |
|
| 234 |
inline BOOL CPlugin::API_SetTaskSchedule(const NodeInitList &list) |
| 235 |
{ |
| 236 |
ASSERT(m_pAPI); |
| 237 |
return m_pAPI->API_SetTaskSchedule(list); |
| 238 |
} |
| 239 |
|
| 240 |
inline void CPlugin::API_SetProgress(DWORD dwIndex, int nPoint) |
| 241 |
{ |
| 242 |
ASSERT(m_pAPI); |
| 243 |
m_pAPI->API_SetProgress(dwIndex, nPoint); |
| 244 |
} |
| 245 |
|
| 246 |
inline void CPlugin::API_InitProgress(DWORD dwProcessors) |
| 247 |
{ |
| 248 |
ASSERT(m_pAPI); |
| 249 |
m_pAPI->API_InitProgress(dwProcessors); |
| 250 |
} |
| 251 |
|
| 252 |
inline void CPlugin::API_SetGraphWindowSize(int cx, int cy, int x, int y) |
| 253 |
{ |
| 254 |
ASSERT(m_pAPI); |
| 255 |
m_pAPI->API_SetGraphWindowSize(cx, cy, x, y); |
| 256 |
} |
| 257 |
|
| 258 |
inline void CPlugin::API_SetCenterWindow() |
| 259 |
{ |
| 260 |
ASSERT(m_pAPI); |
| 261 |
m_pAPI->API_SetCenterWindow(); |
| 262 |
} |
| 263 |
|
| 264 |
inline void CPlugin::API_ResetWindow() |
| 265 |
{ |
| 266 |
ASSERT(m_pAPI); |
| 267 |
m_pAPI->API_ResetWindow(); |
| 268 |
} |
| 269 |
|
| 270 |
inline void CPlugin::API_StartTimer() |
| 271 |
{ |
| 272 |
ASSERT(m_pAPI); |
| 273 |
m_pAPI->API_StartTimer(); |
| 274 |
} |
| 275 |
|
| 276 |
inline void CPlugin::API_StopTimer() |
| 277 |
{ |
| 278 |
ASSERT(m_pAPI); |
| 279 |
m_pAPI->API_StopTimer(); |
| 280 |
} |
| 281 |
|
| 282 |
////////////////////////////////////////////////////////////////////////// |