Console compiles on UNIX with UNICODE
[public/netxms.git] / src / libnxcl / events.cpp
1 /* $Id: events.cpp,v 1.22 2008-01-28 20:23:46 victor Exp $ */
2 /*
3 ** NetXMS - Network Management System
4 ** Client Library
5 ** Copyright (C) 2004, 2005, 2006, 2007, 2008 Victor Kirhenshtein
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 **
21 ** File: events.cpp
22 **
23 **/
24
25 #include "libnxcl.h"
26
27
28 //
29 // Process events coming from server
30 //
31
32 void ProcessEvent(NXCL_Session *pSession, CSCPMessage *pMsg, CSCP_MESSAGE *pRawMsg)
33 {
34 WORD wCode;
35 #ifdef UNICODE
36 WCHAR *pSrc, *pDst;
37 #endif
38
39 wCode = (pMsg != NULL) ? pMsg->GetCode() : pRawMsg->wCode;
40
41 switch(wCode)
42 {
43 case CMD_EVENT_LIST_END:
44 pSession->CompleteSync(SYNC_EVENTS, RCC_SUCCESS);
45 break;
46 case CMD_EVENT:
47 if (pRawMsg != NULL) // We should receive events as raw data
48 {
49 NXC_EVENT event;
50
51 // Fill event structure with values from message
52 event.dwEventCode = ntohl(((NXC_EVENT *)pRawMsg->df)->dwEventCode);
53 event.qwEventId = ntohq(((NXC_EVENT *)pRawMsg->df)->qwEventId);
54 event.dwSeverity = ntohl(((NXC_EVENT *)pRawMsg->df)->dwSeverity);
55 event.dwSourceId = ntohl(((NXC_EVENT *)pRawMsg->df)->dwSourceId);
56 event.dwTimeStamp = ntohl(((NXC_EVENT *)pRawMsg->df)->dwTimeStamp);
57
58 // Convert bytes in message characters to host byte order
59 // and than to single-byte if we building non-unicode library
60 #ifdef UNICODE
61 /*#if WORDS_BIGENDIAN
62 memcpy(event.szMessage, ((NXC_EVENT *)pRawMsg->df)->szMessage,
63 MAX_EVENT_MSG_LENGTH * sizeof(WCHAR));
64 #else
65 for(pSrc = ((NXC_EVENT *)pRawMsg->df)->szMessage,
66 pDst = event.szMessage; *pSrc != 0; pSrc++, pDst++)
67 *pDst = ntohs(*pSrc);
68 *pDst = ntohs(*pSrc);
69 #endif*/
70 event.szMessage[0] = 0;
71 #else
72 SwapWideString((UCS2CHAR *)((NXC_EVENT *)pRawMsg->df)->szMessage);
73 ucs2_to_mb((UCS2CHAR *)((NXC_EVENT *)pRawMsg->df)->szMessage, -1,
74 event.szMessage, MAX_EVENT_MSG_LENGTH);
75 event.szMessage[MAX_EVENT_MSG_LENGTH - 1] = 0;
76 #endif
77
78 // Call client's callback to handle new record
79 pSession->CallEventHandler(NXC_EVENT_NEW_ELOG_RECORD,
80 (pRawMsg->wFlags & MF_REVERSE_ORDER) ?
81 RECORD_ORDER_REVERSED : RECORD_ORDER_NORMAL,
82 &event);
83 }
84 break;
85 default:
86 break;
87 }
88 }
89
90
91 //
92 // Synchronize event log
93 // This function is NOT REENTRANT
94 //
95
96 DWORD LIBNXCL_EXPORTABLE NXCSyncEvents(NXC_SESSION hSession, DWORD dwMaxRecords)
97 {
98 CSCPMessage msg;
99 DWORD dwRetCode, dwRqId;
100
101 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
102 ((NXCL_Session *)hSession)->PrepareForSync(SYNC_EVENTS);
103
104 msg.SetCode(CMD_GET_EVENTS);
105 msg.SetId(dwRqId);
106 msg.SetVariable(VID_MAX_RECORDS, dwMaxRecords);
107 ((NXCL_Session *)hSession)->SendMsg(&msg);
108
109 dwRetCode = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
110 if (dwRetCode == RCC_SUCCESS)
111 dwRetCode = ((NXCL_Session *)hSession)->WaitForSync(SYNC_EVENTS, INFINITE);
112 else
113 ((NXCL_Session *)hSession)->UnlockSyncOp(SYNC_EVENTS);
114
115 return dwRetCode;
116 }
117
118
119 //
120 // Send event to server
121 //
122
123 DWORD LIBNXCL_EXPORTABLE NXCSendEvent(NXC_SESSION hSession, DWORD dwEventCode,
124 DWORD dwObjectId, int iNumArgs, TCHAR **pArgList,
125 TCHAR *pszUserTag)
126 {
127 CSCPMessage msg;
128 DWORD dwRqId;
129 int i;
130
131 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
132
133 msg.SetCode(CMD_TRAP);
134 msg.SetId(dwRqId);
135 msg.SetVariable(VID_EVENT_CODE, dwEventCode);
136 msg.SetVariable(VID_OBJECT_ID, dwObjectId);
137 msg.SetVariable(VID_USER_TAG, CHECK_NULL_EX(pszUserTag));
138 msg.SetVariable(VID_NUM_ARGS, (WORD)iNumArgs);
139 for(i = 0; i < iNumArgs; i++)
140 msg.SetVariable(VID_EVENT_ARG_BASE + i, pArgList[i]);
141 ((NXCL_Session *)hSession)->SendMsg(&msg);
142
143 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
144 }
145
146
147 //
148 // Process syslog records coming from server
149 //
150
151 void ProcessSyslogRecords(NXCL_Session *pSession, CSCPMessage *pMsg)
152 {
153 DWORD i, dwNumRecords, dwId;
154 NXC_SYSLOG_RECORD rec;
155 int nOrder;
156
157 dwNumRecords = pMsg->GetVariableLong(VID_NUM_RECORDS);
158 nOrder = (int)pMsg->GetVariableShort(VID_RECORDS_ORDER);
159 DebugPrintf(_T("ProcessSyslogRecords(): %d records in message, in %s order"),
160 dwNumRecords, (nOrder == RECORD_ORDER_NORMAL) ? _T("normal") : _T("reversed"));
161 for(i = 0, dwId = VID_SYSLOG_MSG_BASE; i < dwNumRecords; i++)
162 {
163 rec.qwMsgId = pMsg->GetVariableInt64(dwId++);
164 rec.dwTimeStamp = pMsg->GetVariableLong(dwId++);
165 rec.wFacility = pMsg->GetVariableShort(dwId++);
166 rec.wSeverity = pMsg->GetVariableShort(dwId++);
167 rec.dwSourceObject = pMsg->GetVariableLong(dwId++);
168 pMsg->GetVariableStr(dwId++, rec.szHost, MAX_SYSLOG_HOSTNAME_LEN);
169 pMsg->GetVariableStr(dwId++, rec.szTag, MAX_SYSLOG_TAG_LEN);
170 rec.pszText = pMsg->GetVariableStr(dwId++);
171
172 // Call client's callback to handle new record
173 pSession->CallEventHandler(NXC_EVENT_NEW_SYSLOG_RECORD, nOrder, &rec);
174 free(rec.pszText);
175 }
176
177 // Notify requestor thread if all messages was received
178 if (pMsg->IsEndOfSequence())
179 pSession->CompleteSync(SYNC_SYSLOG, RCC_SUCCESS);
180 }
181
182
183 //
184 // Synchronize syslog
185 // This function is NOT REENTRANT
186 //
187
188 DWORD LIBNXCL_EXPORTABLE NXCSyncSyslog(NXC_SESSION hSession, DWORD dwMaxRecords)
189 {
190 CSCPMessage msg;
191 DWORD dwRetCode, dwRqId;
192
193 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
194 ((NXCL_Session *)hSession)->PrepareForSync(SYNC_SYSLOG);
195
196 msg.SetCode(CMD_GET_SYSLOG);
197 msg.SetId(dwRqId);
198 msg.SetVariable(VID_MAX_RECORDS, dwMaxRecords);
199 ((NXCL_Session *)hSession)->SendMsg(&msg);
200
201 dwRetCode = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
202 if (dwRetCode == RCC_SUCCESS)
203 dwRetCode = ((NXCL_Session *)hSession)->WaitForSync(SYNC_SYSLOG, INFINITE);
204 else
205 ((NXCL_Session *)hSession)->UnlockSyncOp(SYNC_SYSLOG);
206
207 return dwRetCode;
208 }