OW_LifecycleIndicationPoller.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 
00035 #include "OW_config.h"
00036 #include "OW_LifecycleIndicationPoller.hpp"
00037 #include "OW_CIMException.hpp"
00038 #include "OW_Format.hpp"
00039 #include "OW_CIMObjectPath.hpp"
00040 #include "OW_SortedVectorSet.hpp"
00041 #include "OW_CIMValue.hpp"
00042 #include "OW_MutexLock.hpp"
00043 #include "OW_ResultHandlerIFC.hpp"
00044 #include "OW_CIMOMHandleIFC.hpp"
00045 #include "OW_Logger.hpp"
00046 #include "OW_RequestHandlerIFC.hpp"
00047 #include "OW_CIMDateTime.hpp"
00048 
00049 namespace OW_NAMESPACE
00050 {
00051 
00052 using namespace WBEMFlags;
00054 LifecycleIndicationPoller::LifecycleIndicationPoller(
00055    const String& ns, const CIMName& className,
00056    UInt32 pollInterval)
00057    : m_ns(ns)
00058    , m_classname(className)
00059    , m_pollInterval(pollInterval)
00060    , m_pollCreation(0)
00061    , m_pollModification(0)
00062    , m_pollDeletion(0)
00063    , m_initializedInstances(false)
00064 {
00065 }
00067 void
00068 LifecycleIndicationPoller::addPollOp(PollOp op)
00069 {
00070    MutexLock l(m_guard);
00071    switch (op)
00072    {
00073       case POLL_FOR_INSTANCE_CREATION:
00074          ++m_pollCreation;
00075          break;
00076       case POLL_FOR_INSTANCE_MODIFICATION:
00077          ++m_pollModification;
00078          break;
00079       case POLL_FOR_INSTANCE_DELETION:
00080          ++m_pollDeletion;
00081          break;
00082    }
00083 }
00085 // takes a POLL_FOR_INSTANCE* flag
00086 bool
00087 LifecycleIndicationPoller::removePollOp(PollOp op)
00088 {
00089    MutexLock l(m_guard);
00090    switch (op)
00091    {
00092       case POLL_FOR_INSTANCE_CREATION:
00093          --m_pollCreation;
00094          break;
00095       case POLL_FOR_INSTANCE_MODIFICATION:
00096          --m_pollModification;
00097          break;
00098       case POLL_FOR_INSTANCE_DELETION:
00099          --m_pollDeletion;
00100          break;
00101    }
00102    return !willPoll();
00103 }
00105 bool
00106 LifecycleIndicationPoller::willPoll() const
00107 {
00108    MutexLock l(m_guard);
00109    return m_pollCreation > 0 || m_pollModification > 0 || m_pollDeletion > 0;
00110 }
00112 UInt32
00113 LifecycleIndicationPoller::addPollInterval(UInt32 newPollInterval)
00114 {
00115    MutexLock l(m_guard);
00116    m_pollInterval = newPollInterval < m_pollInterval ? newPollInterval : m_pollInterval;
00117    return m_pollInterval;
00118 }
00119 
00121 UInt32
00122 LifecycleIndicationPoller::getPollInterval() const
00123 {
00124    MutexLock l(m_guard);
00125    return m_pollInterval;
00126 }
00127 namespace
00128 {
00130    class InstanceArrayBuilder : public CIMInstanceResultHandlerIFC
00131    {
00132    public:
00133       InstanceArrayBuilder(CIMInstanceArray& cia_)
00134       : cia(cia_)
00135       {}
00136    protected:
00137       virtual void doHandle(const CIMInstance &i)
00138       {
00139          cia.push_back(i);
00140       }
00141    private:
00142       CIMInstanceArray& cia;
00143    };
00144 } // end anonymous namespace
00146 Int32
00147 LifecycleIndicationPoller::getInitialPollingInterval(
00148    const ProviderEnvironmentIFCRef &)
00149 {
00150    return 1; // have poll called again in 1 second.
00151 }
00152 namespace
00153 {
00154 struct sortByInstancePath
00155 {
00156    bool operator()(const CIMInstance& x, const CIMInstance& y) const
00157    {
00158       return CIMObjectPath("", x) < CIMObjectPath("", y);
00159    }
00160 };
00161 
00162 const String COMPONENT_NAME("ow.owcimomd.indication.LifecyclePoller");
00163    
00164 } // end anonymous namespace
00166 Int32
00167 LifecycleIndicationPoller::poll(const ProviderEnvironmentIFCRef &env)
00168 {
00169    LoggerRef logger(env->getLogger(COMPONENT_NAME));
00170    // do enumInstances to populate m_prevInsts
00171    if (!m_initializedInstances)
00172    {
00173       InstanceArrayBuilder iab(m_prevInsts);
00174       env->getCIMOMHandle()->enumInstances(m_ns, m_classname.toString(), iab, E_SHALLOW, E_NOT_LOCAL_ONLY, E_INCLUDE_QUALIFIERS, E_INCLUDE_CLASS_ORIGIN, 0);
00175       m_initializedInstances = true;
00176       return 1; // have poll called again in 1 second.
00177    }
00178 
00179    OW_LOG_DEBUG(logger, Format("LifecycleIndicationPoller::poll creation %1 modification %2 deletion %3", m_pollCreation, m_pollModification, m_pollDeletion));
00180    if (!willPoll())
00181    {
00182       // nothing to do, so return 0 to stop polling.
00183       OW_LOG_DEBUG(logger, "LifecycleIndicationPoller::poll nothing to do, returning 0");
00184       return 0;
00185    }
00186    
00187    // do enumInstances of the class
00188    CIMInstanceArray curInstances;
00189    InstanceArrayBuilder iab(curInstances);
00190    CIMOMHandleIFCRef hdl = env->getCIMOMHandle();
00191    try
00192    {
00193       hdl->enumInstances(m_ns, m_classname.toString(), iab, E_SHALLOW, E_NOT_LOCAL_ONLY, E_INCLUDE_QUALIFIERS, E_INCLUDE_CLASS_ORIGIN, 0);
00194    }
00195    catch (const CIMException& e)
00196    {
00197       OW_LOG_ERROR(logger, Format("LifecycleIndicationPoller::poll caught exception: %1", e));
00198       return 0;
00199    }
00200    
00201    OW_LOG_DEBUG(logger, Format("LifecycleIndicationPoller::poll got %1 instances", curInstances.size()));
00202    // Compare the new instances with the previous instances
00203    // and send any indications that may be necessary.
00204    typedef SortedVectorSet<CIMInstance, sortByInstancePath> instSet_t;
00205    instSet_t prevSet(m_prevInsts.begin(), m_prevInsts.end());
00206    instSet_t curSet(curInstances.begin(), curInstances.end());
00207    typedef instSet_t::const_iterator iter_t;
00208    iter_t pi = prevSet.begin();
00209    iter_t ci = curSet.begin();
00210    while (pi != prevSet.end() && ci != curSet.end())
00211    {
00212       if (sortByInstancePath()(*pi, *ci))
00213       {
00214          // *pi has been deleted
00215          if (m_pollDeletion)
00216          {
00217             CIMInstance expInst;
00218             expInst.setClassName("CIM_InstDeletion");
00219             expInst.setProperty("SourceInstance", CIMValue(*pi));
00220             expInst.setProperty("IndicationTime", CIMValue(CIMDateTime(DateTime::getCurrent())));
00221             hdl->exportIndication(expInst, m_ns);
00222          }
00223          ++pi;
00224       }
00225       else if (sortByInstancePath()(*ci, *pi))
00226       {
00227          // *ci is new
00228          if (m_pollCreation)
00229          {
00230             CIMInstance expInst;
00231             expInst.setClassName("CIM_InstCreation");
00232             expInst.setProperty("SourceInstance", CIMValue(*ci));
00233             expInst.setProperty("IndicationTime", CIMValue(CIMDateTime(DateTime::getCurrent())));
00234             hdl->exportIndication(expInst, m_ns);
00235          }
00236          ++ci;
00237       }
00238       else // *pi == *ci
00239       {
00240          if (m_pollModification)
00241          {
00242             if (!pi->propertiesAreEqualTo(*ci))
00243             {
00244                CIMInstance expInst;
00245                expInst.setClassName("CIM_InstModification");
00246                expInst.setProperty("PreviousInstance", CIMValue(*pi));
00247                expInst.setProperty("SourceInstance", CIMValue(*ci));
00248                expInst.setProperty("IndicationTime", CIMValue(CIMDateTime(DateTime::getCurrent())));
00249                hdl->exportIndication(expInst, m_ns);
00250             }
00251          }
00252          ++pi;
00253          ++ci;
00254       }
00255    }
00256    while (pi != prevSet.end())
00257    {
00258       // *pi has been deleted
00259       if (m_pollDeletion)
00260       {
00261          CIMInstance expInst;
00262          expInst.setClassName("CIM_InstDeletion");
00263          expInst.setProperty("SourceInstance", CIMValue(*pi));
00264          expInst.setProperty("IndicationTime", CIMValue(CIMDateTime(DateTime::getCurrent())));
00265          hdl->exportIndication(expInst, m_ns);
00266       }
00267       ++pi;
00268    }
00269    while (ci != curSet.end())
00270    {
00271       // *ci is new
00272       if (m_pollCreation)
00273       {
00274          CIMInstance expInst;
00275          expInst.setClassName("CIM_InstCreation");
00276          expInst.setProperty("SourceInstance", CIMValue(*ci));
00277          expInst.setProperty("IndicationTime", CIMValue(CIMDateTime(DateTime::getCurrent())));
00278          hdl->exportIndication(expInst, m_ns);
00279       }
00280       ++ci;
00281    }
00282    
00283    // save the current instances to m_prevInsts
00284    m_prevInsts = curInstances;
00285    return getPollInterval();
00286 }
00287 
00288 } // end namespace OW_NAMESPACE
00289 

Generated on Thu Feb 9 08:48:01 2006 for openwbem by  doxygen 1.4.6