ConfigElementHandler.cpp

Go to the documentation of this file.
00001 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00002  *
00003  * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Christopher Just,
00007  *   Patrick Hartling, Kevin Meinert,
00008  *   Carolina Cruz-Neira, Albert Baker
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  *
00025  * -----------------------------------------------------------------
00026  * File:          $RCSfile$
00027  * Date modified: $Date: 2005-01-01 13:24:32 -0600 (Sat, 01 Jan 2005) $
00028  * Version:       $Revision: 16522 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #include <jccl/jcclConfig.h>
00034 #include <iomanip>
00035 #include <typeinfo>
00036 #include <string>
00037 #include <jccl/RTRC/ConfigManager.h>
00038 #include <jccl/Config/ConfigElement.h>
00039 #include <jccl/RTRC/DependencyManager.h>
00040 #include <jccl/Util/Debug.h>
00041 #include <jccl/RTRC/ConfigElementHandler.h>
00042 
00043 namespace jccl
00044 {
00045 
00046 enum PendItemResult
00047 { SUCCESS, FAILED, NEED_DEPS };
00048 
00049 
00050 static void outputPendingItemState(int debugLevel,
00051                                    const std::string& elementName,
00052                                    const std::string& elementType,
00053                                    PendItemResult result);
00054 
00055 
00056 int ConfigElementHandler::configProcessPending()
00057 {
00058    ConfigManager*     cfg_mgr = ConfigManager::instance();
00059    DependencyManager* dep_mgr = DependencyManager::instance();
00060 
00061    bool scan_for_lost_dependants(false);       // Do we need to scan for un-filled dependencies
00062 
00063    // We need to track the number vefore and after to know if we changed anything
00064    std::list<ConfigManager::PendingElement>::size_type num_pending_before =
00065       cfg_mgr->getNumPending();
00066    std::list<ConfigManager::PendingElement>::size_type num_pending_after(0);
00067 
00068    vprDEBUG_BEGIN(vprDBG_ALL,vprDBG_STATE_LVL)
00069       << typeid(*this).name() << "::configProcessPending: Entering: "
00070       << num_pending_before << " items pending.\n" << vprDEBUG_FLUSH;
00071 
00072    // Perform the work of processing pending config elements.
00073    {
00074       std::list<ConfigManager::PendingElement>::iterator current, end, remove_me;
00075       current = cfg_mgr->getPendingBegin();
00076       end = cfg_mgr->getPendingEnd();
00077 
00078       // --- For each item in pending list --- //
00079       while(current != end)
00080       {
00081          // Get information about the current element
00082          ConfigElementPtr cur_element = (*current).mElement;
00083          vprASSERT(cur_element.get() != NULL && "Trying to use an invalid element");
00084          std::string element_name = cur_element->getName();
00085          std::string element_type = cur_element->getID();
00086 
00087          vprDEBUG_BEGIN(vprDBG_ALL,vprDBG_VERB_LVL)
00088             << "Item: name:" << element_name << " type:" << element_type
00089             << std::endl << vprDEBUG_FLUSH;
00090 
00091          // If the current handler (this) knows about the element
00092          if(this->configCanHandle(cur_element))
00093          {
00094             // ---- HANDLE THE ELEMENT ---- //
00095             switch ((*current).mType)
00096             {
00097             case ConfigManager::PendingElement::ADD:         // -- CONFIG ADD -- //
00098                if(dep_mgr->isSatisfied(cur_element))         // Are all the dependencies satisfied
00099                {
00100                   bool added = this->configAdd(cur_element);
00101                   if(added)                                 // SUCCESS adding
00102                   {
00103                      remove_me = current;
00104                      current++;                          // Goto next item
00105                      cfg_mgr->removePending(remove_me);  // Delete previous item
00106                      cfg_mgr->addActive(cur_element);    // Add it to the current config
00107 
00108                      outputPendingItemState(vprDBG_CONFIG_STATUS_LVL,
00109                                             element_name, element_type,
00110                                             SUCCESS);
00111                   }
00112                   else  // FAILED adding
00113                   {
00114                      outputPendingItemState(vprDBG_CRITICAL_LVL, element_name,
00115                                             element_type, FAILED);
00116                      current++;
00117                   }
00118                }
00119                else     // Dependency failed
00120                {
00121                   outputPendingItemState(vprDBG_CONFIG_LVL, element_name,
00122                                          element_type, NEED_DEPS);
00123                   vprDEBUG_CONT(vprDBG_ALL,vprDBG_CONFIG_LVL)
00124                      << std::endl << vprDEBUG_FLUSH;
00125                   dep_mgr->debugOutDependencies(cur_element,vprDBG_CONFIG_LVL);
00126                   current++;
00127                }
00128                break;
00129 
00130             case ConfigManager::PendingElement::REMOVE:      // Config remove
00131                {
00132                   bool removed = this->configRemove(cur_element);
00133                   if(removed)      // Was there success adding
00134                   {
00135                      remove_me = current;
00136                      current++;                          // Goto next item
00137                      cfg_mgr->removePending(remove_me);  // Delete previous item
00138                      cfg_mgr->removeActive(cur_element->getName());     // Add it to the current config
00139                      scan_for_lost_dependants = true;    // We have to scan to see if somebody depended on that element
00140                   }
00141                   else // Failed to remove
00142                   {
00143                      current++;
00144                   }
00145                }
00146                break;
00147 
00148             default:
00149                current++;  // Goto next entry
00150                break;
00151             }
00152          }
00153          // ---- CAN'T HANDLE THE ELEMENT --- //
00154          else           // if(can_handle)
00155          {
00156             vprDEBUG_NEXT(vprDBG_ALL,vprDBG_STATE_LVL)
00157                << "Pending item: " << cur_element->getName()
00158                << " type: " << cur_element->getID()
00159                << " --> Not handled by this handler.\n" << vprDEBUG_FLUSH;
00160             current++;
00161          }
00162 
00163          vprDEBUG_END(vprDBG_ALL,vprDBG_VERB_LVL) << "==== End item =====\n"
00164                                                   << vprDEBUG_FLUSH;
00165       }        // END: while(current != end)
00166    }
00167 
00168    num_pending_after = cfg_mgr->getNumPending();
00169 
00170    vprDEBUG_END(vprDBG_ALL,vprDBG_STATE_LVL)
00171       << "              Exiting: "
00172       << num_pending_after << " items now pending ==> We processed "
00173       << (num_pending_before-num_pending_after) << " items.\n"
00174       << vprDEBUG_FLUSH;
00175 
00176    // Check for items that have lost their dependencies dues to a remove item being processed
00177    if(scan_for_lost_dependants)
00178    {
00179       cfg_mgr->scanForLostDependencies();
00180    }
00181 
00182    return (num_pending_before-num_pending_after);
00183 }
00184 
00185 void outputPendingItemState(int debugLevel, const std::string& elementName,
00186                             const std::string& elementType,
00187                             PendItemResult result)
00188 {
00189    const int item_width(39);
00190    const int type_width(20);
00191 
00192    vprDEBUG(vprDBG_ALL,debugLevel)
00193       << "Type: " << std::setiosflags(std::ios::right)
00194       << std::setfill(' ') << std::setw(type_width) << elementType
00195       << std::setiosflags(std::ios::right)
00196       << std::setfill(' ') << std::setw(item_width) << elementName
00197       << std::resetiosflags(std::ios::right) << "  ";
00198 
00199    /*
00200    const int prefix_len = name_prefix.length() + type_prefix.length();
00201    int item_and_type_len = elementName.length() + elementType.length() + prefix_len;
00202    const int state_offset(60);
00203 
00204    for(int c=0;c<(state_offset-item_and_type_len);c++)
00205    {
00206       vprDEBUG_CONTnl(vprDBG_ALL,debugLevel) << " ";
00207    }
00208    */
00209 
00210    switch(result)
00211    {
00212    case SUCCESS:
00213       vprDEBUG_CONTnl(vprDBG_ALL,debugLevel) << "[ " << clrSetNORM(clrGREEN) << "OK" << clrRESET << " ]";
00214       break;
00215    case FAILED:
00216       vprDEBUG_CONTnl(vprDBG_ALL,debugLevel) << "[ " << clrSetNORM(clrRED) << "FAILED" << clrRESET << " ]";
00217       break;
00218    case NEED_DEPS:
00219       vprDEBUG_CONTnl(vprDBG_ALL,debugLevel) << "[ " << clrSetNORM(clrYELLOW) << "NEED DEPS" << clrRESET << " ]";
00220       break;
00221    }
00222 
00223    vprDEBUG_CONTnl(vprDBG_ALL,debugLevel) << std::endl << vprDEBUG_FLUSH;
00224 }
00225 
00226 }

Generated on Thu Jan 4 10:49:32 2007 for JCCL: Juggler Configuration and Control Library by  doxygen 1.5.1