Added support for setting event information
[public/netxms.git] / include / nms_util.h
CommitLineData
e9246d26 1/*
7ac71e60 2** NetXMS - Network Management System
e9246d26
VK
3** Copyright (C) 2003 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** $module: nms_util.h
20**
21**/
22
23#ifndef _nms_util_h_
24#define _nms_util_h_
25
26#include <nms_common.h>
27#include <nms_cscp.h>
5fad5c32 28#include <nms_threads.h>
e9246d26 29
5d63b3c1
VK
30#define INVALID_INDEX 0xFFFFFFFF
31#define CSCP_TEMP_BUF_SIZE 4096
32
33
34//
35// Temporary buffer structure for RecvCSCPMessage() function
36//
37
38typedef struct
39{
40 DWORD dwBufSize;
41 DWORD dwBufPos;
42 char szBuffer[CSCP_TEMP_BUF_SIZE];
43} CSCP_BUFFER;
f44f6a97 44
e9246d26
VK
45
46//
47// Class for holding CSCP messages
48//
49
50class EXPORTABLE CSCPMessage
51{
52private:
53 WORD m_wCode;
54 DWORD m_dwId;
55 DWORD m_dwNumVar; // Number of variables
56 CSCP_DF **m_ppVarList; // List of variables
57
93b18807 58 void Set(DWORD dwVarId, BYTE bType, void *pValue, DWORD dwSize = 0);
a5f8dbb8
VK
59 void *Get(DWORD dwVarId, BYTE bType);
60 DWORD FindVariable(DWORD dwVarId);
e9246d26
VK
61
62public:
63 CSCPMessage();
64 CSCPMessage(CSCP_MESSAGE *pMsg);
65 ~CSCPMessage();
66
67 CSCP_MESSAGE *CreateMessage(void);
68
69 WORD GetCode(void) { return m_wCode; }
70 void SetCode(WORD wCode) { m_wCode = wCode; }
71
72 DWORD GetId(void) { return m_dwId; }
73 void SetId(DWORD dwId) { m_dwId = dwId; }
74
a5f8dbb8 75 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
f44f6a97 76
a5f8dbb8
VK
77 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, DT_INT16, &wValue); }
78 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, DT_INTEGER, &dwValue); }
79 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, DT_INT64, &qwValue); }
80 void SetVariable(DWORD dwVarId, char *szValue) { Set(dwVarId, DT_STRING, szValue); }
93b18807 81 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, DT_BINARY, pValue, dwSize); }
e9246d26 82
a5f8dbb8
VK
83 DWORD GetVariableLong(DWORD dwVarId);
84 QWORD GetVariableInt64(DWORD dwVarId);
85 WORD GetVariableShort(DWORD dwVarId);
86 char *GetVariableStr(DWORD dwVarId, char *szBuffer = NULL, DWORD dwBufSize = 0);
93b18807 87 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
b5972f5d
VK
88
89 void DeleteAllVariables(void);
e9246d26
VK
90};
91
92
93//
5fad5c32
VK
94// Queue class
95//
96
97class EXPORTABLE Queue
98{
99private:
100 MUTEX m_hQueueAccess;
101 CONDITION m_hConditionNotEmpty;
102 void **m_pElements;
103 DWORD m_dwNumElements;
104 DWORD m_dwBufferSize;
105 DWORD m_dwFirst;
106 DWORD m_dwLast;
107 DWORD m_dwBufferIncrement;
108
109 void Lock(void) { MutexLock(m_hQueueAccess, INFINITE); }
110 void Unlock(void) { MutexUnlock(m_hQueueAccess); }
111
112public:
113 Queue(DWORD dwInitialSize = 256, DWORD dwBufferIncrement = 32);
114 ~Queue();
115
116 void Put(void *pObject);
117 void *Get(void);
118 void *GetOrBlock(void);
119 DWORD Size(void) { return m_dwNumElements; }
120};
121
122
123//
e9246d26
VK
124// Functions
125//
126
43d92b4c
VK
127
128#if __BYTE_ORDER == __LITTLE_ENDIAN
129#define htonq(x) __bswap_64(x)
130#define ntohq(x) __bswap_64(x)
131#else
132#define htonq(x) (x)
133#define ntohq(x) (x)
134#endif
15929b31 135
e9246d26
VK
136extern "C"
137{
5d63b3c1 138 QWORD EXPORTABLE __bswap_64(QWORD qwVal);
ab588c84 139
5d63b3c1 140 int EXPORTABLE RecvCSCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg, CSCP_BUFFER *pBuffer);
ab588c84 141 CSCP_MESSAGE EXPORTABLE *CreateRawCSCPMessage(WORD wCode, DWORD dwId, DWORD dwDataSize, void *pData, CSCP_MESSAGE *pBuffer);
a1dc75b0 142
830629e0
VK
143 int EXPORTABLE BitsInMask(DWORD dwMask);
144 char EXPORTABLE *IpToStr(DWORD dwAddr, char *szBuffer);
a1dc75b0
VK
145
146 void EXPORTABLE *MemAlloc(DWORD dwSize);
147 void EXPORTABLE *MemReAlloc(void *pBlock, DWORD dwNewSize);
148 void EXPORTABLE MemFree(void *pBlock);
93b18807
VK
149
150 void EXPORTABLE CreateSHA1Hash(char *pszSource, BYTE *pBuffer);
e9246d26
VK
151}
152
153#endif /* _nms_util_h_ */