Bugfixes in action-related code on server
[public/netxms.git] / include / nxcscpapi.h
CommitLineData
0d9c1b96
VK
1/*
2** NetXMS - Network Management System
3** CSCP API Library
4** Copyright (C) 2003, 2004 Victor Kirhenshtein
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** $module: nxcscpapi.h
21**
22**/
23
24#ifndef _nxcscpapi_h_
25#define _nxcscpapi_h_
26
27
28#ifdef _WIN32
29#ifdef LIBNXCSCP_EXPORTS
30#define LIBNXCSCP_EXPORTABLE __declspec(dllexport)
31#else
32#define LIBNXCSCP_EXPORTABLE __declspec(dllimport)
33#endif
34#else /* _WIN32 */
35#define LIBNXCSCP_EXPORTABLE
36#endif
37
38
39//
40// Temporary buffer structure for RecvCSCPMessage() function
41//
42
43typedef struct
44{
45 DWORD dwBufSize;
46 DWORD dwBufPos;
47 char szBuffer[CSCP_TEMP_BUF_SIZE];
48} CSCP_BUFFER;
49
50
51//
52// Class for holding CSCP messages
53//
54
55class LIBNXCSCP_EXPORTABLE CSCPMessage
56{
57private:
58 WORD m_wCode;
59 DWORD m_dwId;
60 DWORD m_dwNumVar; // Number of variables
61 CSCP_DF **m_ppVarList; // List of variables
62
a7d07192 63 void *Set(DWORD dwVarId, BYTE bType, void *pValue, DWORD dwSize = 0);
0d9c1b96
VK
64 void *Get(DWORD dwVarId, BYTE bType);
65 DWORD FindVariable(DWORD dwVarId);
66
67public:
68 CSCPMessage();
69 CSCPMessage(CSCP_MESSAGE *pMsg);
70 ~CSCPMessage();
71
72 CSCP_MESSAGE *CreateMessage(void);
73
74 WORD GetCode(void) { return m_wCode; }
75 void SetCode(WORD wCode) { m_wCode = wCode; }
76
77 DWORD GetId(void) { return m_dwId; }
78 void SetId(DWORD dwId) { m_dwId = dwId; }
79
80 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
81
42d7ed00
VK
82 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
83 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
84 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
85 void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
86 void SetVariable(DWORD dwVarId, char *szValue) { Set(dwVarId, CSCP_DT_STRING, szValue); }
87 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
a7d07192 88 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
0d9c1b96
VK
89
90 DWORD GetVariableLong(DWORD dwVarId);
91 QWORD GetVariableInt64(DWORD dwVarId);
92 WORD GetVariableShort(DWORD dwVarId);
f86ffdd2 93 double GetVariableDouble(DWORD dwVarId);
0d9c1b96
VK
94 char *GetVariableStr(DWORD dwVarId, char *szBuffer = NULL, DWORD dwBufSize = 0);
95 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
a7d07192 96 DWORD GetVariableInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwBuffer);
0d9c1b96
VK
97
98 void DeleteAllVariables(void);
99};
100
101
deaa4491
VK
102//
103// Message waiting queue element structure
104//
105
106typedef struct
107{
108 WORD wCode; // Message code
109 WORD wIsBinary; // 1 for binary (raw) messages
110 DWORD dwId; // Message ID
111 DWORD dwTTL; // Message time-to-live in milliseconds
112 void *pMsg; // Pointer to message, either to CSCPMessage object or raw message
113} WAIT_QUEUE_ELEMENT;
114
115
116//
117// Message waiting queue class
118//
119
120class LIBNXCSCP_EXPORTABLE MsgWaitQueue
121{
deaa4491 122private:
cea97623
VK
123 MUTEX m_hMutexDataAccess;
124 MUTEX m_hMutexIsRunning;
deaa4491
VK
125 CONDITION m_hStopCondition;
126 DWORD m_dwMsgHoldTime;
127 DWORD m_dwNumElements;
128 WAIT_QUEUE_ELEMENT *m_pElements;
cea97623 129 BOOL m_bIsRunning;
deaa4491 130
cea97623
VK
131 void Lock(void) { MutexLock(m_hMutexDataAccess, INFINITE); }
132 void Unlock(void) { MutexUnlock(m_hMutexDataAccess); }
deaa4491
VK
133 void HousekeeperThread(void);
134 void *WaitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
1ba9a162
VK
135
136 static void MWQThreadStarter(void *);
deaa4491
VK
137
138public:
139 MsgWaitQueue();
140 ~MsgWaitQueue();
141
142 void Put(CSCPMessage *pMsg);
143 void Put(CSCP_MESSAGE *pMsg);
144 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
145 {
146 return (CSCPMessage *)WaitForMessageInternal(0, wCode, dwId, dwTimeOut);
147 }
148 CSCP_MESSAGE *WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
149 {
150 return (CSCP_MESSAGE *)WaitForMessageInternal(1, wCode, dwId, dwTimeOut);
151 }
152
153 void Clear(void);
154 void SetHoldTime(DWORD dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
155};
156
157
0d9c1b96
VK
158//
159// Functions
160//
161
162#ifdef __cplusplus
163extern "C" {
164#endif
165
4c20673b
VK
166int LIBNXCSCP_EXPORTABLE RecvCSCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
167 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize);
b9b4ec17
VK
168CSCP_MESSAGE LIBNXCSCP_EXPORTABLE *CreateRawCSCPMessage(WORD wCode, DWORD dwId,
169 DWORD dwDataSize, void *pData,
0d9c1b96
VK
170 CSCP_MESSAGE *pBuffer);
171char LIBNXCSCP_EXPORTABLE *CSCPMessageCodeName(WORD wCode, char *pszBuffer);
172
173#ifdef __cplusplus
174}
175#endif
176
177#endif /* _nxcscpapi_h_ */