1 tony 1.1 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2003 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
14 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 tony 1.1 //==============================================================================
23 //
24 // Author: Tony Fiorentino (fiorentino_tony@emc.com)
25 //
26 // Modified By:
27 //
28 //%/////////////////////////////////////////////////////////////////////////////
29
30 #include "OpenSLPWrapper.h"
31
32 PEGASUS_NAMESPACE_BEGIN
33
34 static SLPBoolean
35 wbemSrvUrlCallback(SLPHandle hslp,
36 const char* srvurl,
37 unsigned short lifetime,
38 SLPError errcode,
39 void* cookie)
40 {
41 if (errcode == SLP_OK)
42 {
43 tony 1.1 Array<String> *url_cookie = static_cast<Array<String> *>(cookie);
44 url_cookie->append(srvurl);
45 }
46
47 return SLP_TRUE;
48 }
49
50 static SLPBoolean
51 wbemAttrCallback( SLPHandle hslp,
52 const char* attrlist,
53 SLPError errcode,
54 void* cookie)
55 {
56 if (errcode == SLP_OK)
57 {
58 Array<Attribute> *attr_cookie = static_cast<Array<Attribute> *>(cookie);
59
60 String attrList(attrlist);
61 Uint32 posAttrKey=0, posEqual=0;
62
63 posAttrKey = attrList.find(PEG_SLP_ATTR_BEGIN);
64 tony 1.1 while (posAttrKey != PEG_NOT_FOUND && (posAttrKey+1) < attrList.size())
65 {
66 posEqual = attrList.find(posEqual+1, PEG_SLP_ATTR_DELIMITER);
67 String attrKey(attrList.subString((posAttrKey+1), (posEqual-posAttrKey-1)));
68
69 // SLPParseAttrs()
70 // @param1 - attribute list - from attribute call back
71 // @param2 - attribute id - this the name of the attribute to obtain
72 // @param3 - value of attribute - output param
73 String attrValue;
74 char *attr_value = NULL;
75 if (SLP_OK == ::SLPParseAttrs(
76 (const char *)attrList.getCString(),
77 (const char *)attrKey.getCString(),
78 &attr_value))
79 {
80 attr_cookie->append(Attribute(attrKey + "=" + String(attr_value)));
81 attrValue = attr_value;
82
83 // free up slp library memory
84 ::SLPFree(attr_value);
85 tony 1.1 }
86
87 // ATTN: skip over anything in value that is a '(', '=', ')', or ','?
88 posAttrKey = attrList.find(posAttrKey+1, PEG_SLP_ATTR_BEGIN);
89 }
90 }
91
92 return SLP_TRUE;
93 }
94
95 CIMServerDiscoveryRep::CIMServerDiscoveryRep()
96 {
97 }
98
99 CIMServerDiscoveryRep::~CIMServerDiscoveryRep()
100 {
101 }
102
103 Array<CIMServerDescription>
104 CIMServerDiscoveryRep::lookup(const Array<Attribute> & criteria)
105 {
106 tony 1.1 SLPError result;
107 SLPHandle hslp;
108 Array<CIMServerDescription> connections;
109
110 // SLPOpen()
111 // @param1 - language - NULL is the default locale
112 // @param2 - async - FALSE is synchronous slp handle
113 // @param3 - handle - pointer to slp handle
114 if (SLPOpen(NULL, SLP_FALSE, &hslp) == SLP_OK)
115 {
116 Attribute attrServiceId;
117 for (Uint32 idx=0; idx<criteria.size(); idx++)
118 {
119 if (criteria[idx].find(PEG_WBEM_SLP_SERVICE_ID) == 0)
120 {
121 attrServiceId = criteria[idx];
122 }
123 }
124
125 String serviceType(PEG_WBEM_SLP_TYPE);
126 String serviceId(attrServiceId.getValue(String()));
127 tony 1.1 if (serviceId != String::EMPTY)
128 serviceType = serviceId;
129
130 // SLPFindSrvs()
131 // @param1 - handle - slp handle
132 // @param2 - service type - wbem
133 // @param3 - scope list - NULL is all localhost can query
134 // @param4 - filter - NULL is all that match type
135 // @param5 - pointer to custom data to use in callback
136 Array<String> urls;
137 result = ::SLPFindSrvs(hslp,
138 (const char *)serviceType.getCString(),
139 NULL,
140 NULL,
141 wbemSrvUrlCallback,
142 (void *)&urls);
143
144 if (result == SLP_OK)
145 {
146 for (Uint32 i=0; i<urls.size(); i++)
147 {
148 tony 1.1 CIMServerDescription connection(urls[i]);
149
150 Array<Attribute> attributes;
151
152 // SLPFindAttrs()
153 // @param1 - handle - slp handle
154 // @param2 - service url or type
155 // @param3 - scope list - NULL is all localhost can query
156 // @param4 - attribute list - NULL is all attributes
157 // @param5 - pointer to custom data to use in callback
158 result = ::SLPFindAttrs( hslp,
159 (const char *)urls[i].getCString(),
160 NULL,
161 NULL,
162 wbemAttrCallback,
163 (void *)&attributes);
164
165 // SLPParseSrvURL()
166 // @param1 - url - obtained from SLPFindSrvs()
167 // @param2 - parsed url - output param
168 SLPSrvURL *pSrvUrl = NULL;
169 tony 1.1 if ( SLP_OK == ::SLPParseSrvURL(
170 (const char *)urls[i].getCString(),
171 &pSrvUrl))
172 {
173 // add to the end to protect against existing attributes of the same name.
174 attributes.append(Attribute(PEG_CUSTOM_ATTR_HOST"=" + String(pSrvUrl->s_pcHost)));
175 CIMValue value(Uint32(pSrvUrl->s_iPort));
176 attributes.append(Attribute(PEG_CUSTOM_ATTR_PORT"=" + String(value.toString())));
177
178 // free up slp library memory
179 ::SLPFree(pSrvUrl);
180 }
181 connection.setAttributes(attributes);
182 connections.append(connection);
183 }
184 }
185
186 // SLPClose()
187 // @param1 - handle - slp handle
188 SLPClose(hslp);
189 }
190 tony 1.1
191 return connections;
192 }
193
194 PEGASUS_NAMESPACE_END
195
|