OW_CIMOMLocatorSLP.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_CIMOMLocatorSLP.hpp"
00038 #include "OW_CIMOMInfo.hpp"
00039 #include "OW_Array.hpp"
00040 #include "OW_String.hpp"
00041 #include "OW_Format.hpp"
00042 
00043 #ifdef OW_HAVE_SLP_H
00044 
00045 namespace OW_NAMESPACE
00046 {
00047 
00048 struct CBData
00049 {
00050    Array<String> urls;
00051    Array<UInt16> lifetimes;
00052    SLPError errcode;
00053 };
00054 extern "C"
00055 {
00056 SLPBoolean MySLPSrvURLCallback( SLPHandle /*hslp*/, 
00057    const char* srvurl, 
00058    unsigned short lifetime, 
00059    SLPError errcode, 
00060    void* cookie ) 
00061 {
00062    switch (errcode)
00063    {
00064       case SLP_OK:
00065          (static_cast<CBData*>(cookie))->urls.push_back(srvurl);
00066          (static_cast<CBData*>(cookie))->lifetimes.push_back(lifetime);
00067          break;
00068       case SLP_LAST_CALL:
00069          (static_cast<CBData*>(cookie))->errcode = SLP_OK; 
00070          break;
00071       default:
00072          (static_cast<CBData*>(cookie))->errcode = errcode;
00073          break;
00074    }
00075    /* return SLP_TRUE because we want to be called again */ 
00076    /* if more services were found                        */ 
00077    return SLP_TRUE; 
00078 } 
00080 SLPBoolean MySLPAttrCallback(SLPHandle /*hslp*/, 
00081                      const char* attrlist, 
00082                      SLPError errcode, 
00083                      void* cookie ) 
00084 { 
00085    if (errcode == SLP_OK) 
00086    { 
00087       (*static_cast<String*>(cookie)) = attrlist;
00088    } 
00089    
00090    return SLP_FALSE; 
00091 }
00092 } // extern "C"
00094 CIMOMLocatorSLP::CIMOMLocatorSLP()  
00095 {
00096    SLPError err = SLPOpen("en", SLP_FALSE, &m_hslp);
00097    if (err != SLP_OK)
00098    {
00099       SLPClose(m_hslp);
00100       OW_THROW(CIMOMLocatorException, 
00101          Format("Error opening SLP handle: %1", err).c_str());
00102    }
00103 }
00105 // STATIC
00106 void
00107 CIMOMLocatorSLP::processAttributes(const String& attrs, 
00108       CIMOMInfo& info)
00109 {
00110    String sattrs(attrs);
00111    size_t idx = sattrs.indexOf('(');
00112    while (idx != String::npos)
00113    {
00114       sattrs = sattrs.substring(idx + 1);
00115       size_t endIdx = sattrs.indexOf('=');
00116       String key = sattrs.substring(0, endIdx);
00117       sattrs = sattrs.substring(endIdx + 1);
00118       endIdx = sattrs.indexOf(')');
00119       String val = sattrs.substring(0, endIdx);
00120       info[key] = val;
00121       idx = sattrs.indexOf('(');
00122    }
00123 }
00125 CIMOMInfoArray 
00126 CIMOMLocatorSLP::findCIMOMs()
00127 {
00128    SLPError err;
00129    CBData data;
00130    CIMOMInfoArray rval;
00131    err = SLPFindSrvs(m_hslp, OW_CIMOM_SLP_SERVICE_TYPE, 0, 0, MySLPSrvURLCallback, &data);
00132    if ((err != SLP_OK) || (data.errcode != SLP_OK)) 
00133    { 
00134       OW_THROW(CIMOMLocatorException, 
00135          Format("Error finding service: %1.  SLP Error code: %2",
00136             OW_CIMOM_SLP_SERVICE_TYPE, err).c_str());
00137    } 
00138    for (size_t i = 0; i < data.urls.size(); ++i)
00139    {
00140       CIMOMInfo info;
00141       String SLPUrl = data.urls[i];
00142       size_t idx = SLPUrl.indexOf("http");
00143       info.setURL(SLPUrl.substring(idx));
00144       String attrList;
00145       err = SLPFindAttrs(m_hslp, data.urls[i].c_str(), "", "", 
00146          MySLPAttrCallback, &attrList);
00147       if (err != SLP_OK)
00148       {
00149          OW_THROW(CIMOMLocatorException, 
00150             Format("Error retrieving attributes for %1",
00151                data.urls[i]).c_str());
00152       }
00153       processAttributes(attrList, info);
00154       
00155       String url = info.getURL();
00156       char* cpUrl = 0;
00157       err = SLPUnescape(url.c_str(), &cpUrl, SLP_FALSE);
00158       if (err != SLP_OK)
00159       {
00160          if (cpUrl)
00161          {
00162             SLPFree(cpUrl);
00163          }
00164          OW_THROW(CIMOMLocatorException, 
00165             Format("Error unescaping URL: %1", err).c_str());
00166       }
00167       url = cpUrl;
00168       SLPFree(cpUrl);
00169       info.setURL(url);
00170       rval.push_back(info);
00171    }
00172    
00173    return rval;
00174 }
00176 CIMOMLocatorSLP::~CIMOMLocatorSLP() 
00177 {
00178    SLPClose(m_hslp);
00179 }
00180 
00181 } // end namespace OW_NAMESPACE
00182 
00183 #endif // OW_HAVE_SLP_H
00184 

Generated on Thu Feb 9 08:47:54 2006 for openwbem by  doxygen 1.4.6