Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.44 KB | None | 0 0
  1. /* -------------------------------------------------------------------------- */
  2. /* Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs        */
  3. /*                                                                            */
  4. /* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
  5. /* not use this file except in compliance with the License. You may obtain    */
  6. /* a copy of the License at                                                   */
  7. /*                                                                            */
  8. /* http://www.apache.org/licenses/LICENSE-2.0                                 */
  9. /*                                                                            */
  10. /* Unless required by applicable law or agreed to in writing, software        */
  11. /* distributed under the License is distributed on an "AS IS" BASIS,          */
  12. /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
  13. /* See the License for the specific language governing permissions and        */
  14. /* limitations under the License.                                             */
  15. /* -------------------------------------------------------------------------- */
  16.  
  17. #include <climits>
  18. #include <sstream>
  19. #include <iostream>
  20. #include <stdexcept>
  21. #include <algorithm>
  22.  
  23. #include "PoolSQL.h"
  24. #include "RequestManagerPoolInfoFilter.h"
  25.  
  26. #include <errno.h>
  27.  
  28. /* ************************************************************************** */
  29. /* PoolSQL constructor/destructor                                             */
  30. /* ************************************************************************** */
  31.  
  32. /* -------------------------------------------------------------------------- */
  33. /* -------------------------------------------------------------------------- */
  34.  
  35. const unsigned int PoolSQL::MAX_POOL_SIZE = 15000;
  36.  
  37. /* -------------------------------------------------------------------------- */
  38. /* -------------------------------------------------------------------------- */
  39.  
  40. int PoolSQL::init_cb(void *nil, int num, char **values, char **names)
  41. {
  42.     lastOID = -1;
  43.  
  44.     if ( values[0] != 0 )
  45.     {
  46.         lastOID = atoi(values[0]);
  47.     }
  48.  
  49.     return 0;
  50. }
  51.  
  52. /* -------------------------------------------------------------------------- */
  53.  
  54. PoolSQL::PoolSQL(SqlDB * _db, const char * _table, bool _cache, bool cache_by_name):
  55.     db(_db), lastOID(-1), table(_table), cache(_cache), uses_name_pool(cache_by_name)
  56. {
  57.     ostringstream   oss;
  58.  
  59.     pthread_mutex_init(&mutex,0);
  60.  
  61.     set_callback(static_cast<Callbackable::Callback>(&PoolSQL::init_cb));
  62.  
  63.     oss << "SELECT last_oid FROM pool_control WHERE tablename='" << table <<"'";
  64.  
  65.     db->exec(oss,this);
  66.  
  67.     unset_callback();
  68. };
  69.  
  70. /* -------------------------------------------------------------------------- */
  71. /* -------------------------------------------------------------------------- */
  72.  
  73. PoolSQL::~PoolSQL()
  74. {
  75.     map<int,PoolObjectSQL *>::iterator  it;
  76.  
  77.     pthread_mutex_lock(&mutex);
  78.  
  79.     for ( it = pool.begin(); it != pool.end(); it++)
  80.     {
  81.         it->second->lock();
  82.  
  83.         delete it->second;
  84.     }
  85.  
  86.     pthread_mutex_unlock(&mutex);
  87.  
  88.     pthread_mutex_destroy(&mutex);
  89. }
  90.  
  91.  
  92. /* ************************************************************************** */
  93. /* PoolSQL public interface                                                   */
  94. /* ************************************************************************** */
  95.  
  96. /* -------------------------------------------------------------------------- */
  97. /* -------------------------------------------------------------------------- */
  98.  
  99. int PoolSQL::allocate(
  100.     PoolObjectSQL   *objsql,
  101.     string&         error_str)
  102. {
  103.     int rc;
  104.  
  105.     lock();
  106.  
  107.     if (lastOID == INT_MAX)
  108.     {
  109.         lastOID = -1;
  110.     }
  111.  
  112.     objsql->lock();
  113.  
  114.     objsql->oid = ++lastOID;
  115.  
  116.     rc = objsql->insert(db,error_str);
  117.  
  118.     if ( rc != 0 )
  119.     {
  120.         lastOID--;
  121.         rc = -1;
  122.     }
  123.     else
  124.     {
  125.         rc = lastOID;
  126.         do_hooks(objsql, Hook::ALLOCATE);
  127.     }
  128.  
  129.     objsql->unlock();
  130.     delete objsql;
  131.  
  132.     if( rc != -1 )
  133.     {
  134.         update_lastOID();
  135.     }
  136.  
  137.     unlock();
  138.  
  139.     return rc;
  140. }
  141.  
  142. /* -------------------------------------------------------------------------- */
  143. /* -------------------------------------------------------------------------- */
  144.  
  145. void PoolSQL::update_lastOID()
  146. {
  147.     // db->escape_str is not used for 'table' since its name can't be set in
  148.     // any way by the user, it is hardcoded.
  149.  
  150.     ostringstream oss;
  151.  
  152.     oss << "REPLACE INTO pool_control (tablename, last_oid) VALUES ("
  153.         << "'" <<   table       << "',"
  154.         <<          lastOID     << ")";
  155.  
  156.     db->exec(oss);
  157. }
  158.  
  159. /* -------------------------------------------------------------------------- */
  160. /* -------------------------------------------------------------------------- */
  161.  
  162. PoolObjectSQL * PoolSQL::get(
  163.     int     oid,
  164.     bool    olock)
  165. {
  166.     map<int,PoolObjectSQL *>::iterator  index;
  167.     PoolObjectSQL *                     objectsql;
  168.     int                                 rc;
  169.  
  170.     if ( oid < 0 )
  171.     {
  172.         objectsql = 0;
  173.         return objectsql;
  174.     }
  175.  
  176.     lock();
  177.  
  178.     if (!cache)
  179.     {
  180.         flush_cache(oid);
  181.     }
  182.  
  183.     index = pool.find(oid);
  184.  
  185.     if ( index != pool.end() )
  186.     {
  187.         if ( index->second->isValid() == false )
  188.         {
  189.             objectsql = 0;
  190.         }
  191.         else
  192.         {
  193.             objectsql = index->second;
  194.  
  195.             if ( olock == true )
  196.             {
  197.                 objectsql->lock();
  198.  
  199.                 if ( objectsql->isValid() == false )
  200.                 {
  201.                     objectsql->unlock();
  202.                     objectsql = 0;
  203.                 }
  204.             }
  205.         }
  206.  
  207.         unlock();
  208.  
  209.         return objectsql;
  210.     }
  211.     else
  212.     {
  213.         objectsql = create();
  214.  
  215.         objectsql->oid = oid;
  216.  
  217.         rc = objectsql->select(db);
  218.  
  219.         if ( rc != 0 )
  220.         {
  221.             //objectsql->lock();    
  222.         delete objectsql;
  223.  
  224.             unlock();
  225.  
  226.             objectsql = 0;
  227.             return 0;
  228.         }
  229.  
  230.         if ( uses_name_pool )
  231.         {
  232.             map<string,PoolObjectSQL *>::iterator name_index;
  233.             string okey;
  234.  
  235.             okey       = key(objectsql->name,objectsql->uid);
  236.             name_index = name_pool.find(okey);
  237.  
  238.             if ( name_index != name_pool.end() )
  239.             {
  240.                 name_index->second->lock();
  241.  
  242.                 PoolObjectSQL * tmp_ptr  = name_index->second;
  243.  
  244.                 name_pool.erase(okey);
  245.                 pool.erase(tmp_ptr->oid);
  246.  
  247.         //tmp_ptr->lock();
  248.  
  249.                 delete tmp_ptr;
  250.             }
  251.  
  252.             name_pool.insert(make_pair(okey, objectsql));
  253.         }
  254.  
  255.         pool.insert(make_pair(objectsql->oid,objectsql));
  256.  
  257.         if ( olock == true )
  258.         {
  259.             objectsql->lock();
  260.         }
  261.  
  262.         if (cache)
  263.         {
  264.             oid_queue.push(objectsql->oid);
  265.  
  266.             if ( pool.size() > MAX_POOL_SIZE )
  267.             {
  268.                 replace();
  269.             }
  270.         }
  271.  
  272.         unlock();
  273.  
  274.         return objectsql;
  275.     }
  276. }
  277.  
  278. /* -------------------------------------------------------------------------- */
  279. /* -------------------------------------------------------------------------- */
  280.  
  281. PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock)
  282. {
  283.     map<string,PoolObjectSQL *>::iterator  index;
  284.  
  285.     PoolObjectSQL *  objectsql;
  286.     int              rc;
  287.     string           name_key;
  288.  
  289.     if ( uses_name_pool == false )
  290.     {
  291.         return 0;
  292.     }
  293.  
  294.     lock();
  295.  
  296.     name_key = key(name,ouid);
  297.  
  298.     if (!cache)
  299.     {
  300.         flush_cache(name_key);
  301.     }
  302.  
  303.     index = name_pool.find(name_key);
  304.  
  305.     if ( index != name_pool.end() && index->second->isValid() == true )
  306.     {
  307.         objectsql = index->second;
  308.  
  309.         if ( olock == true )
  310.         {
  311.             objectsql->lock();
  312.  
  313.             if ( objectsql->isValid() == false )
  314.             {
  315.                 objectsql->unlock();
  316.                 objectsql = 0;
  317.             }
  318.         }
  319.  
  320.         unlock();
  321.  
  322.         return objectsql;
  323.     }
  324.     else
  325.     {
  326.         if ( index != name_pool.end() && index->second->isValid() == false )
  327.         {
  328.             index->second->lock();
  329.  
  330.             PoolObjectSQL * tmp_ptr  = index->second;
  331.             string          tmp_okey = key(tmp_ptr->name,tmp_ptr->uid);
  332.  
  333.             pool.erase(tmp_ptr->oid);
  334.             name_pool.erase(tmp_okey);
  335.  
  336.             delete tmp_ptr;
  337.         }
  338.  
  339.         objectsql = create();
  340.  
  341.         rc = objectsql->select(db,name,ouid);
  342.  
  343.         if ( rc != 0 )
  344.         {
  345.         //objectsql->lock();
  346.             delete objectsql;
  347.  
  348.             unlock();
  349.  
  350.             return 0;
  351.         }
  352.  
  353.         string okey = key(objectsql->name,objectsql->uid);
  354.  
  355.         pool.insert(make_pair(objectsql->oid, objectsql));
  356.         name_pool.insert(make_pair(okey, objectsql));
  357.  
  358.         if ( olock == true )
  359.         {
  360.             objectsql->lock();
  361.         }
  362.  
  363.         if (cache)
  364.         {
  365.             oid_queue.push(objectsql->oid);
  366.  
  367.             if ( pool.size() > MAX_POOL_SIZE )
  368.             {
  369.                 replace();
  370.             }
  371.         }
  372.  
  373.         unlock();
  374.  
  375.         return objectsql;
  376.     }
  377. }
  378.  
  379. /* -------------------------------------------------------------------------- */
  380. /* -------------------------------------------------------------------------- */
  381.  
  382. void PoolSQL::update_cache_index(string& old_name,
  383.                                  int     old_uid,
  384.                                  string& new_name,
  385.                                  int     new_uid)
  386. {
  387.     map<string,PoolObjectSQL *>::iterator  index;
  388.     PoolObjectSQL * the_object;
  389.  
  390.     lock();
  391.  
  392.     if ( uses_name_pool == false )
  393.     {
  394.         unlock();
  395.         return;
  396.     }
  397.  
  398.     string old_key  = key(old_name, old_uid);
  399.     string new_key  = key(new_name, new_uid);
  400.  
  401.     index = name_pool.find(old_key);
  402.  
  403.     if ( index != name_pool.end() )
  404.     {
  405.         the_object = index->second;
  406.  
  407.         name_pool.erase(old_key);
  408.  
  409.         if ( name_pool.find(new_key) == name_pool.end())
  410.         {
  411.             name_pool.insert(make_pair(new_key, the_object));
  412.         }
  413.     }
  414.  
  415.     unlock();
  416. }
  417.  
  418. /* -------------------------------------------------------------------------- */
  419. /* -------------------------------------------------------------------------- */
  420.  
  421. void PoolSQL::replace()
  422. {
  423.     bool removed = false;
  424.     int  oid;
  425.     int  rc;
  426.  
  427.     map<int,PoolObjectSQL *>::iterator  index;
  428.  
  429.     while (!removed)
  430.     {
  431.         oid   = oid_queue.front();
  432.         index = pool.find(oid);
  433.  
  434.         if ( index == pool.end())
  435.         {
  436.             oid_queue.pop();
  437.             continue;
  438.         }
  439.  
  440.         rc = pthread_mutex_trylock(&(index->second->mutex));
  441.  
  442.         if ( rc == EBUSY ) // In use by other thread, move to back
  443.         {
  444.             oid_queue.pop();
  445.             oid_queue.push(oid);
  446.         }
  447.         else
  448.         {
  449.             PoolObjectSQL * tmp_ptr = index->second;
  450.  
  451.             pool.erase(index);
  452.  
  453.             if ( uses_name_pool )
  454.             {
  455.                 string okey = key(tmp_ptr->name,tmp_ptr->uid);
  456.                 name_pool.erase(okey);
  457.             }
  458.  
  459.             delete tmp_ptr;
  460.  
  461.             oid_queue.pop();
  462.             removed = true;
  463.         }
  464.     }
  465. }
  466.  
  467. /* -------------------------------------------------------------------------- */
  468. /* -------------------------------------------------------------------------- */
  469.  
  470. void PoolSQL::flush_cache(int oid)
  471. {
  472.     int  rc;
  473.     PoolObjectSQL * tmp_ptr;
  474.  
  475.     map<int,PoolObjectSQL *>::iterator  it;
  476.  
  477.     for (it = pool.begin(); it != pool.end(); )
  478.     {
  479.         // The object we are looking for in ::get(). Will wait until it is
  480.         // unlocked()
  481.         if (it->second->oid == oid)
  482.         {
  483.             it->second->lock();
  484.         }
  485.         else
  486.         {
  487.             // Any other locked object is just ignored
  488.             rc = pthread_mutex_trylock(&(it->second->mutex));
  489.  
  490.             if ( rc == EBUSY ) // In use by other thread
  491.             {
  492.                 it++;
  493.                 continue;
  494.             }
  495.         }
  496.  
  497.         tmp_ptr = it->second;
  498.  
  499.         // map::erase does not invalidate the iterator, except for the current
  500.         // one
  501.         pool.erase(it++);
  502.  
  503.         if ( uses_name_pool )
  504.         {
  505.             string okey = key(tmp_ptr->name,tmp_ptr->uid);
  506.             name_pool.erase(okey);
  507.         }
  508.  
  509.         delete tmp_ptr;
  510.     }
  511. }
  512.  
  513. /* -------------------------------------------------------------------------- */
  514. /* -------------------------------------------------------------------------- */
  515.  
  516. void PoolSQL::flush_cache(const string& name_key)
  517. {
  518.     int  rc;
  519.     PoolObjectSQL * tmp_ptr;
  520.  
  521.     map<string,PoolObjectSQL *>::iterator it;
  522.  
  523.     for (it = name_pool.begin(); it != name_pool.end(); )
  524.     {
  525.         string okey = key(it->second->name, it->second->uid);
  526.  
  527.         // The object we are looking for in ::get(). Will wait until it is
  528.         // unlocked()
  529.         if (name_key == okey)
  530.         {
  531.             it->second->lock();
  532.         }
  533.         else
  534.         {
  535.             // Any other locked object is just ignored
  536.             rc = pthread_mutex_trylock(&(it->second->mutex));
  537.  
  538.             if ( rc == EBUSY ) // In use by other thread
  539.             {
  540.                 it++;
  541.                 continue;
  542.             }
  543.         }
  544.  
  545.         tmp_ptr = it->second;
  546.  
  547.         // map::erase does not invalidate the iterator, except for the current
  548.         // one
  549.         name_pool.erase(it++);
  550.         pool.erase(tmp_ptr->oid);
  551.  
  552.         delete tmp_ptr;
  553.     }
  554. }
  555.  
  556. /* -------------------------------------------------------------------------- */
  557. /* -------------------------------------------------------------------------- */
  558.  
  559. void PoolSQL::clean()
  560. {
  561.     map<int,PoolObjectSQL *>::iterator  it;
  562.  
  563.     lock();
  564.  
  565.     for ( it = pool.begin(); it != pool.end(); it++)
  566.     {
  567.         it->second->lock();
  568.  
  569.         delete it->second;
  570.     }
  571.  
  572.     pool.clear();
  573.     name_pool.clear();
  574.  
  575.     unlock();
  576. }
  577.  
  578. /* -------------------------------------------------------------------------- */
  579. /* -------------------------------------------------------------------------- */
  580.  
  581. int PoolSQL::dump_cb(void * _oss, int num, char **values, char **names)
  582. {
  583.     ostringstream * oss;
  584.  
  585.     oss = static_cast<ostringstream *>(_oss);
  586.  
  587.     if ( (!values[0]) || (num != 1) )
  588.     {
  589.         return -1;
  590.     }
  591.  
  592.     *oss << values[0];
  593.     return 0;
  594. }
  595.  
  596. /* -------------------------------------------------------------------------- */
  597.  
  598. int PoolSQL::dump(ostringstream& oss,
  599.                   const string& elem_name,
  600.                   const char * table,
  601.                   const string& where,
  602.                   const string& limit)
  603. {
  604.     ostringstream   cmd;
  605.  
  606.     cmd << "SELECT body FROM " << table;
  607.  
  608.     if ( !where.empty() )
  609.     {
  610.         cmd << " WHERE " << where;
  611.     }
  612.  
  613.     cmd << " ORDER BY oid";
  614.  
  615.     if ( !limit.empty() )
  616.     {
  617.         cmd << " LIMIT " << limit;
  618.     }
  619.  
  620.     return dump(oss, elem_name, cmd);
  621. }
  622.  
  623. /* -------------------------------------------------------------------------- */
  624. /* -------------------------------------------------------------------------- */
  625.  
  626. int PoolSQL::dump(ostringstream&  oss,
  627.                   const string&   root_elem_name,
  628.                   ostringstream&  sql_query)
  629. {
  630.     int rc;
  631.  
  632.     oss << "<" << root_elem_name << ">";
  633.  
  634.     set_callback(static_cast<Callbackable::Callback>(&PoolSQL::dump_cb),
  635.                  static_cast<void *>(&oss));
  636.  
  637.     rc = db->exec(sql_query, this);
  638.  
  639.     add_extra_xml(oss);
  640.  
  641.     oss << "</" << root_elem_name << ">";
  642.  
  643.     unset_callback();
  644.  
  645.     return rc;
  646. }
  647.  
  648. /* -------------------------------------------------------------------------- */
  649. /* -------------------------------------------------------------------------- */
  650.  
  651. int PoolSQL:: search_cb(void * _oids, int num, char **values, char **names)
  652. {
  653.     vector<int> *  oids;
  654.  
  655.     oids = static_cast<vector<int> *>(_oids);
  656.  
  657.     if ( num == 0 || values == 0 || values[0] == 0 )
  658.     {
  659.         return -1;
  660.     }
  661.  
  662.     oids->push_back(atoi(values[0]));
  663.  
  664.     return 0;
  665. }
  666.  
  667. /* -------------------------------------------------------------------------- */
  668.  
  669. int PoolSQL::search(
  670.     vector<int>&    oids,
  671.     const char *    table,
  672.     const string&   where)
  673. {
  674.     ostringstream   sql;
  675.     int             rc;
  676.  
  677.     set_callback(static_cast<Callbackable::Callback>(&PoolSQL::search_cb),
  678.                  static_cast<void *>(&oids));
  679.  
  680.     sql  << "SELECT oid FROM " <<  table;
  681.  
  682.     if (!where.empty())
  683.     {
  684.         sql << " WHERE " << where;
  685.     }
  686.  
  687.     rc = db->exec(sql, this);
  688.  
  689.     unset_callback();
  690.  
  691.     return rc;
  692. }
  693.  
  694. /* -------------------------------------------------------------------------- */
  695. /* -------------------------------------------------------------------------- */
  696.  
  697. void PoolSQL::acl_filter(int                       uid,
  698.                          const set<int>&           user_groups,
  699.                          PoolObjectSQL::ObjectType auth_object,
  700.                          bool&                     all,
  701.                          bool                      disable_all_acl,
  702.                          bool                      disable_cluster_acl,
  703.                          bool                      disable_group_acl,
  704.                          string&                   filter)
  705. {
  706.     filter.clear();
  707.  
  708.     if ( uid == UserPool::ONEADMIN_ID || user_groups.count( GroupPool::ONEADMIN_ID ) == 1 )
  709.     {
  710.         all = true;
  711.         return;
  712.     }
  713.  
  714.     Nebula&     nd   = Nebula::instance();
  715.     AclManager* aclm = nd.get_aclm();
  716.  
  717.     ostringstream         acl_filter;
  718.     vector<int>::iterator it;
  719.  
  720.     vector<int> oids;
  721.     vector<int> gids;
  722.     vector<int> cids;
  723.  
  724.     aclm->reverse_search(uid,
  725.                          user_groups,
  726.                          auth_object,
  727.                          AuthRequest::USE,
  728.                          disable_all_acl,
  729.                          disable_cluster_acl,
  730.                          disable_group_acl,
  731.                          all,
  732.                          oids,
  733.                          gids,
  734.                          cids);
  735.  
  736.     for ( it = oids.begin(); it < oids.end(); it++ )
  737.     {
  738.         acl_filter << " OR oid = " << *it;
  739.     }
  740.  
  741.     for ( it = gids.begin(); it < gids.end(); it++ )
  742.     {
  743.         acl_filter << " OR gid = " << *it;
  744.     }
  745.  
  746.     for ( it = cids.begin(); it < cids.end(); it++ )
  747.     {
  748.         acl_filter << " OR cid = " << *it;
  749.     }
  750.  
  751.     filter = acl_filter.str();
  752. }
  753.  
  754. /* -------------------------------------------------------------------------- */
  755.  
  756. void PoolSQL::usr_filter(int                uid,
  757.                          const set<int>&    user_groups,
  758.                          int                filter_flag,
  759.                          bool               all,
  760.                          const string&      acl_str,
  761.                          string&            filter)
  762. {
  763.     ostringstream uid_filter;
  764.  
  765.     set<int>::iterator g_it;
  766.  
  767.     if ( filter_flag == RequestManagerPoolInfoFilter::MINE )
  768.     {
  769.         uid_filter << "uid = " << uid;
  770.     }
  771.     else if ( filter_flag == RequestManagerPoolInfoFilter::MINE_GROUP )
  772.     {
  773.         uid_filter << " uid = " << uid;
  774.  
  775.         for (g_it = user_groups.begin(); g_it != user_groups.end(); g_it++)
  776.         {
  777.             uid_filter << " OR ( gid = " << *g_it << " AND group_u = 1 )";
  778.         }
  779.     }
  780.     else if ( filter_flag == RequestManagerPoolInfoFilter::ALL )
  781.     {
  782.         if (!all)
  783.         {
  784.             uid_filter << " uid = " << uid
  785.                     << " OR other_u = 1";
  786.  
  787.             for (g_it = user_groups.begin(); g_it != user_groups.end(); g_it++)
  788.             {
  789.                 uid_filter << " OR ( gid = " << *g_it << " AND group_u = 1 )";
  790.             }
  791.  
  792.             uid_filter << acl_str;
  793.         }
  794.     }
  795.     else
  796.     {
  797.         uid_filter << "uid = " << filter_flag;
  798.  
  799.         if ( filter_flag != uid && !all )
  800.         {
  801.             uid_filter << " AND ( other_u = 1";
  802.  
  803.             for (g_it = user_groups.begin(); g_it != user_groups.end(); g_it++)
  804.             {
  805.                 uid_filter << " OR ( gid = " << *g_it << " AND group_u = 1 )";
  806.             }
  807.  
  808.             uid_filter << acl_str << ")";
  809.         }
  810.     }
  811.  
  812.     filter = uid_filter.str();
  813. }
  814.  
  815. /* -------------------------------------------------------------------------- */
  816.  
  817. void PoolSQL::oid_filter(int     start_id,
  818.                          int     end_id,
  819.                          string& filter)
  820. {
  821.     ostringstream idfilter;
  822.  
  823.     if ( end_id >= -1 && start_id != -1 )
  824.     {
  825.         idfilter << "oid >= " << start_id;
  826.  
  827.         if ( end_id != -1 )
  828.         {
  829.             idfilter << " AND oid <= " << end_id;
  830.         }
  831.     }
  832.  
  833.     filter = idfilter.str();
  834. }
  835.  
  836. /* -------------------------------------------------------------------------- */
  837. /* -------------------------------------------------------------------------- */
  838.  
  839. void PoolSQL::register_hooks(vector<const Attribute *> hook_mads,
  840.                              const string&             remotes_location)
  841. {
  842.     const VectorAttribute * vattr;
  843.  
  844.     string name;
  845.     string on;
  846.     string cmd;
  847.     string arg;
  848.  
  849.     for (unsigned int i = 0 ; i < hook_mads.size() ; i++ )
  850.     {
  851.         vattr = static_cast<const VectorAttribute *>(hook_mads[i]);
  852.  
  853.         name = vattr->vector_value("NAME");
  854.         on   = vattr->vector_value("ON");
  855.         cmd  = vattr->vector_value("COMMAND");
  856.         arg  = vattr->vector_value("ARGUMENTS");
  857.  
  858.         transform (on.begin(),on.end(),on.begin(),(int(*)(int))toupper);
  859.  
  860.         if ( on.empty() || cmd.empty() )
  861.         {
  862.             NebulaLog::log("VM", Log::WARNING, "Empty ON or COMMAND attribute"
  863.                 " in Hook, not registered!");
  864.  
  865.             continue;
  866.         }
  867.  
  868.         if ( name.empty() )
  869.         {
  870.             name = cmd;
  871.         }
  872.  
  873.         if (cmd[0] != '/')
  874.         {
  875.             ostringstream cmd_os;
  876.  
  877.             cmd_os << remotes_location << "/hooks/" << cmd;
  878.  
  879.             cmd = cmd_os.str();
  880.         }
  881.  
  882.         if ( on == "CREATE" )
  883.         {
  884.             AllocateHook * hook;
  885.  
  886.             hook = new AllocateHook(name, cmd, arg, false);
  887.  
  888.             add_hook(hook);
  889.         }
  890.         else if ( on == "REMOVE" )
  891.         {
  892.             RemoveHook * hook;
  893.  
  894.             hook = new RemoveHook(name, cmd, arg, false);
  895.  
  896.             add_hook(hook);
  897.         }
  898.     }
  899. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement