Refactored code for SNMP traps and NXSL scripts and implemented guids for both objects.
[public/netxms.git] / src / libnxsl / library.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Scripting Language Interpreter
4 ** Copyright (C) 2003-2014 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 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 Lesser 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 ** File: library.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25
26 /**
27 * Constructor
28 */
29 NXSL_Library::NXSL_Library()
30 {
31 m_scriptList = new ObjectArray<NXSL_LibraryScript>(16, 16, true);
32 m_mutex = MutexCreate();
33 }
34
35 /**
36 * Destructor
37 */
38 NXSL_Library::~NXSL_Library()
39 {
40 delete m_scriptList;
41 MutexDestroy(m_mutex);
42 }
43
44 /**
45 * Add script to library
46 */
47 BOOL NXSL_Library::addScript(NXSL_LibraryScript *script)
48 {
49 m_scriptList->add(script);
50 return TRUE;
51 }
52
53 /**
54 * Delete script by name
55 */
56 void NXSL_Library::deleteScript(const TCHAR *pszName)
57 {
58 for(int i = 0; i < m_scriptList->size(); i++)
59 if (!_tcsicmp(m_scriptList->get(i)->getName(), pszName))
60 {
61 m_scriptList->remove(i);
62 break;
63 }
64 }
65
66 /**
67 * Delete script by id
68 */
69 void NXSL_Library::deleteScript(UINT32 dwId)
70 {
71 for(int i = 0; i < m_scriptList->size(); i++)
72 if (m_scriptList->get(i)->getId() == dwId)
73 {
74 m_scriptList->remove(i);
75 break;
76 }
77 }
78
79 /**
80 * Find compiled NXSL program by name
81 */
82 NXSL_Program *NXSL_Library::findNxslProgram(const TCHAR *name)
83 {
84 for(int i = 0; i < m_scriptList->size(); i++)
85 if (!_tcsicmp(m_scriptList->get(i)->getName(), name))
86 {
87 return m_scriptList->get(i)->getProgram();
88 }
89 return NULL;
90 }
91
92 /**
93 * Find script object by ID
94 */
95 NXSL_LibraryScript *NXSL_Library::findScript(UINT32 id)
96 {
97 for(int i = 0; i < m_scriptList->size(); i++)
98 if (m_scriptList->get(i)->getId() == id)
99 {
100 return m_scriptList->get(i);
101 }
102 return NULL;
103 }
104
105 /**
106 * Create ready to run VM for given script. This method will do library lock internally.
107 * VM must be deleted by caller when no longer needed.
108 */
109 NXSL_VM *NXSL_Library::createVM(const TCHAR *name, NXSL_Environment *env)
110 {
111 NXSL_VM *vm = NULL;
112 lock();
113 NXSL_Program *p = findNxslProgram(name);
114 if (p != NULL)
115 {
116 vm = new NXSL_VM(env);
117 if (!vm->load(p))
118 {
119 delete vm;
120 vm = NULL;
121 }
122 }
123 else
124 {
125 delete env;
126 }
127 unlock();
128 return vm;
129 }
130
131 /**
132 * Fill message with script library
133 */
134 void NXSL_Library::fillMessage(NXCPMessage *msg)
135 {
136 lock();
137 msg->setField(VID_NUM_SCRIPTS, m_scriptList->size());
138 UINT32 fieldId = VID_SCRIPT_LIST_BASE;
139 for(int i = 0; i < m_scriptList->size(); i++, fieldId+=2)
140 {
141 m_scriptList->get(i)->fillMessage(msg, fieldId);
142 }
143 unlock();
144 }
145
146 /**
147 * Create empty NXSL Script
148 */
149 NXSL_LibraryScript::NXSL_LibraryScript()
150 {
151 m_id = 0;
152 nx_strncpy(m_name, _T(""), 1);
153 m_source = NULL;
154 m_program = new NXSL_Program();
155 }
156
157 /**
158 * Create NXSL Script
159 */
160 NXSL_LibraryScript::NXSL_LibraryScript(UINT32 id, uuid guid, const TCHAR *name, TCHAR *source)
161 {
162 m_id = id;
163 m_guid = guid;
164 nx_strncpy(m_name, name, 1024);
165 m_source = source;
166 m_program = (NXSL_Program *)NXSLCompile(m_source, m_error, 1024, NULL);
167 }
168
169 /**
170 * NXSL Script destructor
171 */
172 NXSL_LibraryScript::~NXSL_LibraryScript()
173 {
174 free(m_source);
175 delete m_program;
176 }
177
178 /**
179 * Fill message with script id and name for list
180 */
181 void NXSL_LibraryScript::fillMessage(NXCPMessage *msg, UINT32 base)
182 {
183 msg->setField(base, m_id);
184 msg->setField(base + 1, m_name);
185 }
186
187 /**
188 * Fill message with script data
189 */
190 void NXSL_LibraryScript::fillMessage(NXCPMessage *msg)
191 {
192 msg->setField(VID_SCRIPT_ID, m_id);
193 msg->setField(VID_NAME, m_name);
194 msg->setField(VID_SCRIPT_CODE, m_source);
195 }
196