62af02c636d93121247f956e17e45c067956190d
[public/netxms.git] / src / libnxmap / objlist.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Network Maps Library
4 ** Copyright (C) 2006, 2007, 2008 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 ** File: objlist.cpp
21 **
22 **/
23
24 #include "libnxmap.h"
25
26
27 //
28 // Constructors
29 //
30
31 nxmap_ObjList::nxmap_ObjList()
32 {
33 m_dwNumObjects = 0;
34 m_pdwObjectList = NULL;
35 m_dwNumLinks = 0;
36 m_pLinkList = NULL;
37 }
38
39 nxmap_ObjList::nxmap_ObjList(CSCPMessage *pMsg)
40 {
41 DWORD i, dwId;
42
43 m_dwNumObjects = pMsg->GetVariableLong(VID_NUM_OBJECTS);
44 m_pdwObjectList = (DWORD *)malloc(m_dwNumObjects * sizeof(DWORD));
45 pMsg->GetVariableInt32Array(VID_OBJECT_LIST, m_dwNumObjects, m_pdwObjectList);
46 m_dwNumLinks = pMsg->GetVariableLong(VID_NUM_LINKS);
47 m_pLinkList = (OBJLINK *)malloc(m_dwNumLinks * sizeof(OBJLINK));
48 for(i = 0, dwId = VID_OBJECT_LINKS_BASE; i < m_dwNumLinks; i++, dwId += 5)
49 {
50 m_pLinkList[i].dwId1 = pMsg->GetVariableLong(dwId++);
51 m_pLinkList[i].dwId2 = pMsg->GetVariableLong(dwId++);
52 m_pLinkList[i].nType = (int)pMsg->GetVariableShort(dwId++);
53 pMsg->GetVariableStr(dwId++, m_pLinkList[i].szPort1, MAX_CONNECTOR_NAME);
54 pMsg->GetVariableStr(dwId++, m_pLinkList[i].szPort2, MAX_CONNECTOR_NAME);
55 }
56 }
57
58 nxmap_ObjList::nxmap_ObjList(nxmap_ObjList *pSrc)
59 {
60 m_dwNumObjects = pSrc->m_dwNumObjects;
61 m_pdwObjectList = (DWORD *)nx_memdup(pSrc->m_pdwObjectList, sizeof(DWORD) * m_dwNumObjects);
62 m_dwNumLinks = pSrc->m_dwNumLinks;
63 m_pLinkList = (OBJLINK *)nx_memdup(pSrc->m_pLinkList, sizeof(OBJLINK) * m_dwNumLinks);
64 }
65
66
67 //
68 // Destructor
69 //
70
71 nxmap_ObjList::~nxmap_ObjList()
72 {
73 safe_free(m_pdwObjectList);
74 safe_free(m_pLinkList);
75 }
76
77
78 //
79 // Clear list
80 //
81
82 void nxmap_ObjList::Clear(void)
83 {
84 safe_free_and_null(m_pdwObjectList);
85 safe_free_and_null(m_pLinkList);
86 m_dwNumObjects = 0;
87 m_dwNumLinks = 0;
88 }
89
90
91 //
92 // Add object to list
93 //
94
95 void nxmap_ObjList::AddObject(DWORD dwId)
96 {
97 DWORD i;
98
99 for(i = 0; i < m_dwNumObjects; i++)
100 {
101 if (m_pdwObjectList[i] == dwId)
102 break;
103 }
104
105 if (i == m_dwNumObjects)
106 {
107 m_dwNumObjects++;
108 m_pdwObjectList = (DWORD *)realloc(m_pdwObjectList, sizeof(DWORD) * m_dwNumObjects);
109 m_pdwObjectList[i] = dwId;
110 }
111 }
112
113
114 //
115 // Link two objects
116 //
117
118 void nxmap_ObjList::LinkObjects(DWORD dwId1, DWORD dwId2)
119 {
120 DWORD i;
121 int nCount;
122
123 // Validate object IDs
124 for(i = 0, nCount = 0; (i < m_dwNumObjects) && (nCount < 2); i++)
125 {
126 if ((m_pdwObjectList[i] == dwId1) ||
127 (m_pdwObjectList[i] == dwId2))
128 nCount++;
129 }
130
131 if (nCount == 2) // Both objects exist?
132 {
133 // Check for duplicate links
134 for(i = 0; i < m_dwNumLinks; i++)
135 {
136 if (((m_pLinkList[i].dwId1 == dwId1) && (m_pLinkList[i].dwId2 == dwId2)) ||
137 ((m_pLinkList[i].dwId2 == dwId1) && (m_pLinkList[i].dwId1 == dwId2)))
138 break;
139 }
140 if (i == m_dwNumLinks)
141 {
142 m_dwNumLinks++;
143 m_pLinkList = (OBJLINK *)realloc(m_pLinkList, sizeof(OBJLINK) * m_dwNumLinks);
144 m_pLinkList[i].dwId1 = dwId1;
145 m_pLinkList[i].dwId2 = dwId2;
146 m_pLinkList[i].nType = LINK_TYPE_NORMAL;
147 m_pLinkList[i].szPort1[0] = 0;
148 m_pLinkList[i].szPort2[0] = 0;
149 }
150 }
151 }
152
153
154 //
155 // Link two objects with named links
156 //
157
158 void nxmap_ObjList::LinkObjectsEx(DWORD dwId1, DWORD dwId2, TCHAR *pszPort1, TCHAR *pszPort2)
159 {
160 DWORD i;
161 int nCount;
162
163 // Validate object IDs
164 for(i = 0, nCount = 0; (i < m_dwNumObjects) && (nCount < 2); i++)
165 {
166 if ((m_pdwObjectList[i] == dwId1) ||
167 (m_pdwObjectList[i] == dwId2))
168 nCount++;
169 }
170
171 if (nCount == 2) // Both objects exist?
172 {
173 // Check for duplicate links
174 for(i = 0; i < m_dwNumLinks; i++)
175 {
176 if (((m_pLinkList[i].dwId1 == dwId1) && (m_pLinkList[i].dwId2 == dwId2) &&
177 (!_tcsicmp(m_pLinkList[i].szPort1, pszPort1)) && (!_tcsicmp(m_pLinkList[i].szPort2, pszPort2))) ||
178 ((m_pLinkList[i].dwId2 == dwId1) && (m_pLinkList[i].dwId1 == dwId2) &&
179 (!_tcsicmp(m_pLinkList[i].szPort2, pszPort1)) && (!_tcsicmp(m_pLinkList[i].szPort1, pszPort2))))
180 break;
181 }
182 if (i == m_dwNumLinks)
183 {
184 m_dwNumLinks++;
185 m_pLinkList = (OBJLINK *)realloc(m_pLinkList, sizeof(OBJLINK) * m_dwNumLinks);
186 m_pLinkList[i].dwId1 = dwId1;
187 m_pLinkList[i].dwId2 = dwId2;
188 m_pLinkList[i].nType = LINK_TYPE_NORMAL;
189 nx_strncpy(m_pLinkList[i].szPort1, pszPort1, MAX_CONNECTOR_NAME);
190 nx_strncpy(m_pLinkList[i].szPort2, pszPort2, MAX_CONNECTOR_NAME);
191 }
192 }
193 }
194
195
196 //
197 // Create NXCP message
198 //
199
200 void nxmap_ObjList::CreateMessage(CSCPMessage *pMsg)
201 {
202 DWORD i, dwId;
203
204 // Object list
205 pMsg->SetVariable(VID_NUM_OBJECTS, m_dwNumObjects);
206 if (m_dwNumObjects > 0)
207 pMsg->SetVariableToInt32Array(VID_OBJECT_LIST, m_dwNumObjects, m_pdwObjectList);
208
209 // Links between objects
210 pMsg->SetVariable(VID_NUM_LINKS, m_dwNumLinks);
211 for(i = 0, dwId = VID_OBJECT_LINKS_BASE; i < m_dwNumLinks; i++, dwId += 5)
212 {
213 pMsg->SetVariable(dwId++, m_pLinkList[i].dwId1);
214 pMsg->SetVariable(dwId++, m_pLinkList[i].dwId2);
215 pMsg->SetVariable(dwId++, (WORD)m_pLinkList[i].nType);
216 pMsg->SetVariable(dwId++, m_pLinkList[i].szPort1);
217 pMsg->SetVariable(dwId++, m_pLinkList[i].szPort2);
218 }
219 }