8d1a3b53ad22a6d82c499b2b32e276b919429c1a
[public/netxms.git] / src / libnxcl / script.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2004, 2005, 2006 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: script.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Get list of scripts in the library
29 //
30
31 DWORD LIBNXCL_EXPORTABLE NXCGetScriptList(NXC_SESSION hSession, DWORD *pdwNumScripts,
32 NXC_SCRIPT_INFO **ppList)
33 {
34 CSCPMessage msg, *pResponse;
35 DWORD i, dwId, dwResult, dwRqId;
36
37 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
38
39 msg.SetCode(CMD_GET_SCRIPT_LIST);
40 msg.SetId(dwRqId);
41 ((NXCL_Session *)hSession)->SendMsg(&msg);
42
43 *pdwNumScripts = 0;
44 *ppList = NULL;
45
46 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
47 if (pResponse != NULL)
48 {
49 dwResult = pResponse->GetVariableLong(VID_RCC);
50 if (dwResult == RCC_SUCCESS)
51 {
52 *pdwNumScripts = pResponse->GetVariableLong(VID_NUM_SCRIPTS);
53 *ppList = (NXC_SCRIPT_INFO *)malloc(sizeof(NXC_SCRIPT_INFO) * (*pdwNumScripts));
54 for(i = 0, dwId = VID_SCRIPT_LIST_BASE; i < *pdwNumScripts; i++)
55 {
56 (*ppList)[i].dwId = pResponse->GetVariableLong(dwId++);
57 pResponse->GetVariableStr(dwId++, (*ppList)[i].szName, MAX_DB_STRING);
58 }
59 }
60 delete pResponse;
61 }
62 else
63 {
64 dwResult = RCC_TIMEOUT;
65 }
66 return dwResult;
67 }
68
69
70 //
71 // Get script
72 //
73
74 DWORD LIBNXCL_EXPORTABLE NXCGetScript(NXC_SESSION hSession, DWORD dwId, TCHAR **ppszCode)
75 {
76 CSCPMessage msg, *pResponse;
77 DWORD dwResult, dwRqId;
78
79 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
80
81 msg.SetCode(CMD_GET_SCRIPT);
82 msg.SetId(dwRqId);
83 msg.SetVariable(VID_SCRIPT_ID, dwId);
84 ((NXCL_Session *)hSession)->SendMsg(&msg);
85
86 *ppszCode = NULL;
87
88 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
89 if (pResponse != NULL)
90 {
91 dwResult = pResponse->GetVariableLong(VID_RCC);
92 if (dwResult == RCC_SUCCESS)
93 {
94 *ppszCode = pResponse->GetVariableStr(VID_SCRIPT_CODE);
95 }
96 delete pResponse;
97 }
98 else
99 {
100 dwResult = RCC_TIMEOUT;
101 }
102 return dwResult;
103 }
104
105
106 //
107 // Update script
108 //
109
110 DWORD LIBNXCL_EXPORTABLE NXCUpdateScript(NXC_SESSION hSession, DWORD *pdwId,
111 TCHAR *pszName, TCHAR *pszCode)
112 {
113 CSCPMessage msg, *pResponse;
114 DWORD dwRqId, dwResult;
115
116 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
117
118 msg.SetCode(CMD_UPDATE_SCRIPT);
119 msg.SetId(dwRqId);
120 msg.SetVariable(VID_SCRIPT_ID, *pdwId);
121 msg.SetVariable(VID_NAME, pszName);
122 msg.SetVariable(VID_SCRIPT_CODE, pszCode);
123 ((NXCL_Session *)hSession)->SendMsg(&msg);
124
125 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
126 if (pResponse != NULL)
127 {
128 dwResult = pResponse->GetVariableLong(VID_RCC);
129 if (dwResult == RCC_SUCCESS)
130 *pdwId = pResponse->GetVariableLong(VID_SCRIPT_ID);
131 delete pResponse;
132 }
133 else
134 {
135 dwResult = RCC_TIMEOUT;
136 }
137 return dwResult;
138 }
139
140
141 //
142 // Rename script
143 //
144
145 DWORD LIBNXCL_EXPORTABLE NXCRenameScript(NXC_SESSION hSession, DWORD dwId,
146 TCHAR *pszName)
147 {
148 CSCPMessage msg;
149 DWORD dwRqId;
150
151 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
152
153 msg.SetCode(CMD_RENAME_SCRIPT);
154 msg.SetId(dwRqId);
155 msg.SetVariable(VID_SCRIPT_ID, dwId);
156 msg.SetVariable(VID_NAME, pszName);
157 ((NXCL_Session *)hSession)->SendMsg(&msg);
158
159 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
160 }
161
162
163 //
164 // Delete script
165 //
166
167 DWORD LIBNXCL_EXPORTABLE NXCDeleteScript(NXC_SESSION hSession, DWORD dwId)
168 {
169 CSCPMessage msg;
170 DWORD dwRqId;
171
172 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
173
174 msg.SetCode(CMD_DELETE_SCRIPT);
175 msg.SetId(dwRqId);
176 msg.SetVariable(VID_SCRIPT_ID, dwId);
177 ((NXCL_Session *)hSession)->SendMsg(&msg);
178
179 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
180 }