Changeset 2116 for box/trunk/lib/common


Ignore:
Timestamp:
28/03/2008 22:59:28 (4 years ago)
Author:
chris
Message:

Allow configuration of the server port that the client will connect to
(bbackupd and bbackupquery).

Redesign ConfigurationVerify? to use classes instead of structs.

Use port 22011 instead of 2201 during tests, to reduce the chances of
conflicting with a running bbstored or other process.

Ignore autogen_* in svn:ignore everywhere instead of individual per-file
ignores.

Location:
box/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • box/trunk

    • Property svn:ignore
      •  

        old new  
        1111release 
        1212runtest.pl 
         13.hg 
  • box/trunk/lib/common

    • Property svn:ignore
      •  

        old new  
        1 autogen_CommonException.cpp 
        2 autogen_CommonException.h 
        3 autogen_ConversionException.cpp 
        4 autogen_ConversionException.h 
         1autogen_* 
        52BoxConfig.h 
        63BoxConfig.h.in 
        74Makefile 
        85makeexception.pl 
         6BoxPortsAndFiles.h 
  • box/trunk/lib/common/Configuration.cpp

    r1865 r2116  
    1212#include <stdlib.h> 
    1313#include <limits.h> 
     14 
     15#include <sstream> 
    1416 
    1517#include "Configuration.h" 
     
    3032static const bool sValueBooleanValue[] = {true, true, false, false}; 
    3133 
    32  
     34ConfigurationVerifyKey::ConfigurationVerifyKey 
     35( 
     36        std::string name, 
     37        int flags, 
     38        void *testFunction 
     39) 
     40: mName(name), 
     41  mHasDefaultValue(false), 
     42  mFlags(flags), 
     43  mTestFunction(testFunction) 
     44{ } 
     45 
     46// to allow passing NULL for default ListenAddresses 
     47 
     48ConfigurationVerifyKey::ConfigurationVerifyKey 
     49( 
     50        std::string name, 
     51        int flags, 
     52        NoDefaultValue_t t, 
     53        void *testFunction 
     54) 
     55: mName(name), 
     56  mHasDefaultValue(false), 
     57  mFlags(flags), 
     58  mTestFunction(testFunction) 
     59{ } 
     60 
     61ConfigurationVerifyKey::ConfigurationVerifyKey 
     62( 
     63        std::string name, 
     64        int flags, 
     65        std::string defaultValue, 
     66        void *testFunction 
     67) 
     68: mName(name), 
     69  mDefaultValue(defaultValue), 
     70  mHasDefaultValue(true), 
     71  mFlags(flags), 
     72  mTestFunction(testFunction) 
     73{ } 
     74 
     75ConfigurationVerifyKey::ConfigurationVerifyKey 
     76( 
     77        std::string name, 
     78        int flags, 
     79        const char *defaultValue, 
     80        void *testFunction 
     81) 
     82: mName(name), 
     83  mDefaultValue(defaultValue), 
     84  mHasDefaultValue(true), 
     85  mFlags(flags), 
     86  mTestFunction(testFunction) 
     87{ } 
     88 
     89ConfigurationVerifyKey::ConfigurationVerifyKey 
     90( 
     91        std::string name, 
     92        int flags, 
     93        int defaultValue, 
     94        void *testFunction 
     95) 
     96: mName(name), 
     97  mHasDefaultValue(true), 
     98  mFlags(flags), 
     99  mTestFunction(testFunction) 
     100{ 
     101        ASSERT(flags & ConfigTest_IsInt); 
     102        std::ostringstream val; 
     103        val << defaultValue; 
     104        mDefaultValue = val.str(); 
     105} 
     106 
     107ConfigurationVerifyKey::ConfigurationVerifyKey 
     108( 
     109        std::string name, 
     110        int flags, 
     111        bool defaultValue, 
     112        void *testFunction 
     113) 
     114: mName(name), 
     115  mHasDefaultValue(true), 
     116  mFlags(flags), 
     117  mTestFunction(testFunction) 
     118{ 
     119        ASSERT(flags & ConfigTest_IsBool); 
     120        mDefaultValue = defaultValue ? "yes" : "no"; 
     121} 
     122 
     123ConfigurationVerifyKey::ConfigurationVerifyKey 
     124( 
     125        const ConfigurationVerifyKey& rToCopy 
     126) 
     127: mName(rToCopy.mName), 
     128  mDefaultValue(rToCopy.mDefaultValue), 
     129  mHasDefaultValue(rToCopy.mHasDefaultValue), 
     130  mFlags(rToCopy.mFlags), 
     131  mTestFunction(rToCopy.mTestFunction) 
     132{ } 
    33133 
    34134// -------------------------------------------------------------------------- 
     
    121221                        if(!Verify(*pconfig, *pVerify, std::string(), rErrorMsg)) 
    122222                        { 
    123                                 //TRACE1("Error message from Verify: %s", rErrorMsg.c_str()); 
    124                                 TRACE0("Error at Configuration::Verify\n"); 
     223                                BOX_ERROR("Error verifying configuration: " << 
     224                                        rErrorMsg); 
    125225                                delete pconfig; 
    126226                                pconfig = 0; 
     
    190290                        else 
    191291                        { 
    192                                 rErrorMsg += "Unexpected start block in " + rConfig.mName + "\n"; 
     292                                rErrorMsg += "Unexpected start block in " + 
     293                                        rConfig.mName + "\n"; 
    193294                        } 
    194295                } 
     
    291392// 
    292393// Function 
    293 //              Name:    Configuration::KeyExists(const char *) 
     394//              Name:    Configuration::KeyExists(const std::string&) 
    294395//              Purpose: Checks to see if a key exists 
    295396//              Created: 2003/07/23 
    296397// 
    297398// -------------------------------------------------------------------------- 
    298 bool Configuration::KeyExists(const char *pKeyName) const 
    299 { 
    300         if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 
    301  
    302         return mKeys.find(pKeyName) != mKeys.end(); 
    303 } 
    304  
    305  
    306 // -------------------------------------------------------------------------- 
    307 // 
    308 // Function 
    309 //              Name:    Configuration::GetKeyValue(const char *) 
     399bool Configuration::KeyExists(const std::string& rKeyName) const 
     400{ 
     401        return mKeys.find(rKeyName) != mKeys.end(); 
     402} 
     403 
     404 
     405// -------------------------------------------------------------------------- 
     406// 
     407// Function 
     408//              Name:    Configuration::GetKeyValue(const std::string&) 
    310409//              Purpose: Returns the value of a configuration variable 
    311410//              Created: 2003/07/23 
    312411// 
    313412// -------------------------------------------------------------------------- 
    314 const std::string &Configuration::GetKeyValue(const char *pKeyName) const 
    315 { 
    316         if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 
    317  
    318         std::map<std::string, std::string>::const_iterator i(mKeys.find(pKeyName)); 
     413const std::string &Configuration::GetKeyValue(const std::string& rKeyName) const 
     414{ 
     415        std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName)); 
    319416         
    320417        if(i == mKeys.end()) 
    321418        { 
    322                 BOX_ERROR("Missing configuration key: " << pKeyName); 
     419                BOX_ERROR("Missing configuration key: " << rKeyName); 
    323420                THROW_EXCEPTION(CommonException, ConfigNoKey) 
    324421        } 
     
    333430// 
    334431// Function 
    335 //              Name:    Configuration::GetKeyValueInt(const char *) 
     432//              Name:    Configuration::GetKeyValueInt(const std::string& rKeyName) 
    336433//              Purpose: Gets a key value as an integer 
    337434//              Created: 2003/07/23 
    338435// 
    339436// -------------------------------------------------------------------------- 
    340 int Configuration::GetKeyValueInt(const char *pKeyName) const 
    341 { 
    342         if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 
    343  
    344         std::map<std::string, std::string>::const_iterator i(mKeys.find(pKeyName)); 
     437int Configuration::GetKeyValueInt(const std::string& rKeyName) const 
     438{ 
     439        std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName)); 
    345440         
    346441        if(i == mKeys.end()) 
     
    363458// 
    364459// Function 
    365 //              Name:    Configuration::GetKeyValueBool(const char *) const 
     460//              Name:    Configuration::GetKeyValueBool(const std::string&) 
    366461//              Purpose: Gets a key value as a boolean 
    367462//              Created: 17/2/04 
    368463// 
    369464// -------------------------------------------------------------------------- 
    370 bool Configuration::GetKeyValueBool(const char *pKeyName) const 
    371 { 
    372         if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 
    373  
    374         std::map<std::string, std::string>::const_iterator i(mKeys.find(pKeyName)); 
     465bool Configuration::GetKeyValueBool(const std::string& rKeyName) const 
     466{ 
     467        std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName)); 
    375468         
    376469        if(i == mKeys.end()) 
     
    429522// 
    430523// Function 
    431 //              Name:    Configuration::SubConfigurationExists(const char *) 
     524//              Name:    Configuration::SubConfigurationExists(const 
     525//                       std::string&) 
    432526//              Purpose: Checks to see if a sub configuration exists 
    433527//              Created: 2003/07/23 
    434528// 
    435529// -------------------------------------------------------------------------- 
    436 bool Configuration::SubConfigurationExists(const char *pSubName) const 
    437 { 
    438         if(pSubName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 
    439  
     530bool Configuration::SubConfigurationExists(const std::string& rSubName) const 
     531{ 
    440532        // Attempt to find it... 
    441533        std::list<std::pair<std::string, Configuration> >::const_iterator i(mSubConfigurations.begin()); 
     
    444536        { 
    445537                // This the one? 
    446                 if(i->first == pSubName) 
     538                if(i->first == rSubName) 
    447539                { 
    448540                        // Yes. 
     
    459551// 
    460552// Function 
    461 //              Name:    Configuration::GetSubConfiguration(const char *) 
     553//              Name:    Configuration::GetSubConfiguration(const 
     554//                       std::string&) 
    462555//              Purpose: Gets a sub configuration 
    463556//              Created: 2003/07/23 
    464557// 
    465558// -------------------------------------------------------------------------- 
    466 const Configuration &Configuration::GetSubConfiguration(const char *pSubName) const 
    467 { 
    468         if(pSubName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 
    469  
     559const Configuration &Configuration::GetSubConfiguration(const std::string& 
     560        rSubName) const 
     561{ 
    470562        // Attempt to find it... 
    471563        std::list<std::pair<std::string, Configuration> >::const_iterator i(mSubConfigurations.begin()); 
     
    474566        { 
    475567                // This the one? 
    476                 if(i->first == pSubName) 
     568                if(i->first == rSubName) 
    477569                { 
    478570                        // Yes. 
     
    529621                { 
    530622                        // Can the key be found? 
    531                         ASSERT(pvkey->mpName); 
    532                         if(rConfig.KeyExists(pvkey->mpName)) 
     623                        if(rConfig.KeyExists(pvkey->Name())) 
    533624                        { 
    534625                                // Get value 
    535                                 const std::string &rval = rConfig.GetKeyValue(pvkey->mpName); 
     626                                const std::string &rval = rConfig.GetKeyValue(pvkey->Name()); 
    536627                                const char *val = rval.c_str(); 
    537628 
    538629                                // Check it's a number? 
    539                                 if((pvkey->Tests & ConfigTest_IsInt) == ConfigTest_IsInt) 
     630                                if((pvkey->Flags() & ConfigTest_IsInt) == ConfigTest_IsInt) 
    540631                                {                                        
    541632                                        // Test it... 
     
    546637                                                // not a good value 
    547638                                                ok = false; 
    548                                                 rErrorMsg += rLevel + rConfig.mName +"." + pvkey->mpName + " (key) is not a valid integer.\n"; 
     639                                                rErrorMsg += rLevel + rConfig.mName + "." + pvkey->Name() + " (key) is not a valid integer.\n"; 
    549640                                        } 
    550641                                } 
    551642                                 
    552643                                // Check it's a bool? 
    553                                 if((pvkey->Tests & ConfigTest_IsBool) == ConfigTest_IsBool) 
     644                                if((pvkey->Flags() & ConfigTest_IsBool) == ConfigTest_IsBool) 
    554645                                {                                
    555646                                        // See if it's one of the allowed strings. 
     
    569660                                        { 
    570661                                                ok = false; 
    571                                                 rErrorMsg += rLevel + rConfig.mName +"." + pvkey->mpName + " (key) is not a valid boolean value.\n"; 
     662                                                rErrorMsg += rLevel + rConfig.mName + "." + pvkey->Name() + " (key) is not a valid boolean value.\n"; 
    572663                                        } 
    573664                                } 
    574665                                 
    575666                                // Check for multi valued statments where they're not allowed 
    576                                 if((pvkey->Tests & ConfigTest_MultiValueAllowed) == 0) 
     667                                if((pvkey->Flags() & ConfigTest_MultiValueAllowed) == 0) 
    577668                                { 
    578669                                        // Check to see if this key is a multi-value -- it shouldn't be 
     
    580671                                        { 
    581672                                                ok = false; 
    582                                                 rErrorMsg += rLevel + rConfig.mName +"." + pvkey->mpName + " (key) multi value not allowed (duplicated key?).\n"; 
     673                                                rErrorMsg += rLevel + rConfig.mName +"." + pvkey->Name() + " (key) multi value not allowed (duplicated key?).\n"; 
    583674                                        } 
    584675                                }                                
     
    587678                        { 
    588679                                // Is it required to exist? 
    589                                 if((pvkey->Tests & ConfigTest_Exists) == ConfigTest_Exists) 
     680                                if((pvkey->Flags() & ConfigTest_Exists) == ConfigTest_Exists) 
    590681                                { 
    591682                                        // Should exist, but doesn't. 
    592683                                        ok = false; 
    593                                         rErrorMsg += rLevel + rConfig.mName + "." + pvkey->mpName + " (key) is missing.\n"; 
    594                                 } 
    595                                 else if(pvkey->mpDefaultValue) 
    596                                 { 
    597                                         rConfig.mKeys[std::string(pvkey->mpName)] = std::string(pvkey->mpDefaultValue); 
     684                                        rErrorMsg += rLevel + rConfig.mName + "." + pvkey->Name() + " (key) is missing.\n"; 
     685                                } 
     686                                else if(pvkey->HasDefaultValue()) 
     687                                { 
     688                                        rConfig.mKeys[pvkey->Name()] = 
     689                                                pvkey->DefaultValue(); 
    598690                                } 
    599691                        } 
    600692                 
    601                         if((pvkey->Tests & ConfigTest_LastEntry) == ConfigTest_LastEntry) 
     693                        if((pvkey->Flags() & ConfigTest_LastEntry) == ConfigTest_LastEntry) 
    602694                        { 
    603695                                // No more! 
     
    619711                        while(scan) 
    620712                        { 
    621                                 if(scan->mpName == i->first) 
     713                                if(scan->Name() == i->first) 
    622714                                { 
    623715                                        found = true; 
     
    626718                                 
    627719                                // Next? 
    628                                 if((scan->Tests & ConfigTest_LastEntry) == ConfigTest_LastEntry) 
     720                                if((scan->Flags() & ConfigTest_LastEntry) == ConfigTest_LastEntry) 
    629721                                { 
    630722                                        break; 
     
    651743                while(scan) 
    652744                { 
    653                         ASSERT(scan->mpName); 
    654                         if(scan->mpName[0] == '*') 
     745                        if(scan->mName.length() > 0 && scan->mName[0] == '*') 
    655746                        { 
    656747                                wildcardverify = scan; 
     
    660751                        if((scan->Tests & ConfigTest_Exists) == ConfigTest_Exists) 
    661752                        { 
    662                                 if(scan->mpName[0] == '*') 
     753                                if(scan->mName.length() > 0 && 
     754                                        scan->mName[0] == '*') 
    663755                                { 
    664756                                        // Check something exists 
     
    673765                                { 
    674766                                        // Check real thing exists 
    675                                         if(!rConfig.SubConfigurationExists(scan->mpName)) 
     767                                        if(!rConfig.SubConfigurationExists(scan->mName)) 
    676768                                        { 
    677769                                                // Should exist, but doesn't. 
    678770                                                ok = false; 
    679                                                 rErrorMsg += rLevel + rConfig.mName + "." + scan->mpName + " (block) is missing.\n"; 
     771                                                rErrorMsg += rLevel + rConfig.mName + "." + scan->mName + " (block) is missing.\n"; 
    680772                                        } 
    681773                                } 
     
    702794                        while(scan) 
    703795                        { 
    704                                 if(strcmp(scan->mpName, name) == 0) 
     796                                if(scan->mName == name) 
    705797                                { 
    706798                                        // found it! 
  • box/trunk/lib/common/Configuration.h

    r1776 r2116  
    3030{ 
    3131public: 
    32         const char *mpName;                     // "*" for all other keys (not implemented yet) 
    33         const char *mpDefaultValue;     // default for when it's not present 
    34         int Tests; 
    35         void *TestFunction;                     // set to zero for now, will implement later 
     32        typedef enum 
     33        { 
     34                NoDefaultValue = 1 
     35        } NoDefaultValue_t; 
     36 
     37        ConfigurationVerifyKey(std::string name, int flags, 
     38                void *testFunction = NULL); 
     39        // to allow passing ConfigurationVerifyKey::NoDefaultValue 
     40        // for default ListenAddresses 
     41        ConfigurationVerifyKey(std::string name, int flags, 
     42                NoDefaultValue_t t, void *testFunction = NULL); 
     43        ConfigurationVerifyKey(std::string name, int flags, 
     44                std::string defaultValue, void *testFunction = NULL); 
     45        ConfigurationVerifyKey(std::string name, int flags, 
     46                const char* defaultValue, void *testFunction = NULL); 
     47        ConfigurationVerifyKey(std::string name, int flags, 
     48                int defaultValue, void *testFunction = NULL); 
     49        ConfigurationVerifyKey(std::string name, int flags, 
     50                bool defaultValue, void *testFunction = NULL); 
     51        const std::string& Name() const { return mName; } 
     52        const std::string& DefaultValue() const { return mDefaultValue; } 
     53        const bool HasDefaultValue() const { return mHasDefaultValue; } 
     54        const int Flags() const { return mFlags; } 
     55        const void* TestFunction() const { return mTestFunction; } 
     56        ConfigurationVerifyKey(const ConfigurationVerifyKey& rToCopy); 
     57 
     58private: 
     59        ConfigurationVerifyKey& operator=(const ConfigurationVerifyKey& 
     60                noAssign); 
     61 
     62        std::string mName;         // "*" for all other keys (not implemented yet) 
     63        std::string mDefaultValue; // default for when it's not present 
     64        bool mHasDefaultValue; 
     65        int mFlags; 
     66        void *mTestFunction; // set to zero for now, will implement later 
    3667}; 
    3768 
     
    3970{ 
    4071public: 
    41         const char *mpName;                     // "*" for all other sub config names 
     72        std::string mName; // "*" for all other sub config names 
    4273        const ConfigurationVerify *mpSubConfigurations; 
    4374        const ConfigurationVerifyKey *mpKeys; 
    4475        int Tests;       
    45         void *TestFunction;                     // set to zero for now, will implement later 
     76        void *TestFunction; // set to zero for now, will implement later 
    4677}; 
    4778 
     
    80111        { return LoadAndVerify(rFilename, 0, rErrorMsg); } 
    81112         
    82         bool KeyExists(const char *pKeyName) const; 
    83         const std::string &GetKeyValue(const char *pKeyName) const; 
    84         int GetKeyValueInt(const char *pKeyName) const; 
    85         bool GetKeyValueBool(const char *pKeyName) const; 
     113        bool KeyExists(const std::string& rKeyName) const; 
     114        const std::string &GetKeyValue(const std::string& rKeyName) const; 
     115        int GetKeyValueInt(const std::string& rKeyName) const; 
     116        bool GetKeyValueBool(const std::string& rKeyName) const; 
    86117        std::vector<std::string> GetKeyNames() const; 
    87118         
    88         bool SubConfigurationExists(const char *pSubName) const; 
    89         const Configuration &GetSubConfiguration(const char *pSubName) const; 
     119        bool SubConfigurationExists(const std::string& rSubName) const; 
     120        const Configuration &GetSubConfiguration(const std::string& rSubName) const; 
    90121        std::vector<std::string> GetSubConfigurationNames() const; 
    91122         
Note: See TracChangeset for help on using the changeset viewer.