First version of event sender
[public/netxms.git] / src / libnxcl / events.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 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: events.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Process events coming from server
29 //
30
31 void ProcessEvent(CSCPMessage *pMsg, CSCP_MESSAGE *pRawMsg)
32 {
33 NXC_EVENT *pEvent;
34 WORD wCode;
35
36 wCode = (pMsg != NULL) ? pMsg->GetCode() : pRawMsg->wCode;
37
38 switch(wCode)
39 {
40 case CMD_EVENT_LIST_END:
41 CompleteSync(RCC_SUCCESS);
42 break;
43 case CMD_EVENT:
44 if (pRawMsg != NULL) // We should receive events as raw data
45 {
46 // Allocate new event structure and fill it with values from message
47 pEvent = (NXC_EVENT *)malloc(sizeof(NXC_EVENT));
48 memcpy(pEvent, pRawMsg->df, sizeof(NXC_EVENT));
49 pEvent->dwEventId = ntohl(pEvent->dwEventId);
50 pEvent->dwSeverity = ntohl(pEvent->dwSeverity);
51 pEvent->dwSourceId = ntohl(pEvent->dwSourceId);
52 pEvent->dwTimeStamp = ntohl(pEvent->dwTimeStamp);
53
54 // Call client's callback to handle new record
55 // It's up to client to destroy allocated event structure
56 CallEventHandler(NXC_EVENT_NEW_ELOG_RECORD, 0, pEvent);
57 }
58 break;
59 default:
60 break;
61 }
62 }
63
64
65 //
66 // Synchronize event log
67 // This function is NOT REENTRANT
68 //
69
70 DWORD LIBNXCL_EXPORTABLE NXCSyncEvents(void)
71 {
72 CSCPMessage msg;
73 DWORD dwRetCode, dwRqId;
74
75 dwRqId = g_dwMsgId++;
76 PrepareForSync();
77
78 msg.SetCode(CMD_GET_EVENTS);
79 msg.SetId(dwRqId);
80 SendMsg(&msg);
81
82 dwRetCode = WaitForRCC(dwRqId);
83 if (dwRetCode == RCC_SUCCESS)
84 dwRetCode = WaitForSync(INFINITE);
85
86 return dwRetCode;
87 }
88
89
90 //
91 // Send event to server
92 //
93
94 DWORD LIBNXCL_EXPORTABLE NXCSendEvent(DWORD dwEventCode, int iNumArgs, TCHAR **pArgList)
95 {
96 CSCPMessage msg;
97 DWORD dwRqId;
98 int i;
99
100 dwRqId = g_dwMsgId++;
101
102 msg.SetCode(CMD_TRAP);
103 msg.SetId(dwRqId);
104 msg.SetVariable(VID_EVENT_ID, dwEventCode);
105 msg.SetVariable(VID_NUM_ARGS, (WORD)iNumArgs);
106 for(i = 0; i < iNumArgs; i++)
107 msg.SetVariable(VID_EVENT_ARG_BASE + i, pArgList[i]);
108 SendMsg(&msg);
109
110 return WaitForRCC(dwRqId);
111 }