From dcdc516f254a8985424e319cd9ae0ee061e58fb0 Mon Sep 17 00:00:00 2001 From: Eduardo Ramos Testillano Date: Sun, 1 Sep 2013 23:41:40 -0700 Subject: [PATCH] Environment redesign and test/examples review. English API revisions --- example/dbos/workdir/storage/Directory.hpp | 110 +++++++++---------- example/dbos/workdir/storage/File.hpp | 116 ++++++++++----------- include/anna/comm/INetAddress.hpp | 12 +-- include/anna/comm/LiteTransport.hpp | 4 +- include/anna/comm/ReceiverFactory.hpp | 6 +- include/anna/comm/SureTransport.hpp | 2 +- include/anna/comm/functions.hpp | 2 +- include/anna/core/util/Environment.hpp | 2 +- source/core/util/Environment.cpp | 38 ++++--- test/core/main.cpp | 2 - 10 files changed, 145 insertions(+), 149 deletions(-) diff --git a/example/dbos/workdir/storage/Directory.hpp b/example/dbos/workdir/storage/Directory.hpp index 9f4f670..694e2c3 100644 --- a/example/dbos/workdir/storage/Directory.hpp +++ b/example/dbos/workdir/storage/Directory.hpp @@ -42,19 +42,19 @@ #include namespace anna { - namespace dbms { - class Database; - class Connection; - } - namespace dbos { - class Repository; - } +namespace dbms { +class Database; +class Connection; +} +namespace dbos { +class Repository; +} } namespace workdir { namespace filesystem { - class Directory; +class Directory; } namespace storage { @@ -63,56 +63,56 @@ using namespace anna; class Directory : public dbos::Object, public dbos::ObjectFacade { public: - const filesystem::Directory* getFilesystemDirectory () const throw () { return a_filesystemDirectory; } - int getINode () const throw () { return a_inode; } - - std::string asString () const throw (); + const filesystem::Directory* getFilesystemDirectory() const throw() { return a_filesystemDirectory; } + int getINode() const throw() { return a_inode; } + + std::string asString() const throw(); + + static void setup(dbos::Repository&, const int maxSize) throw(RuntimeException); + static Directory* instantiate(const filesystem::Directory*) throw(RuntimeException); - static void setup (dbos::Repository&, const int maxSize) throw (RuntimeException); - static Directory* instantiate (const filesystem::Directory*) throw (RuntimeException); - - static const char* getStorageAreaName () throw () { return "storage::Directory"; } - static const dbos::Size getMaxSize () throw () { return st_maxSize; } + static const char* getStorageAreaName() throw() { return "storage::Directory"; } + static const dbos::Size getMaxSize() throw() { return st_maxSize; } private: - class Loader : public dbos::Loader { - public: - Loader () : dbos::Loader () {;} - - Loader& setKey (const filesystem::Directory* directory) throw () { - a_filesystemDirectory = directory; - return *this; - } - - const filesystem::Directory* getDirectory () const throw () { return a_filesystemDirectory; } - int getINode () const throw () { return a_inode; } - - dbos::Index getIndex () const throw (); - std::string asString () const throw (); - - private: - const filesystem::Directory* a_filesystemDirectory; - int a_inode; - - // dbms::Statement is not required - dbms::Statement* initialize (dbms::Database&) throw (RuntimeException) { return NULL; } - bool load (dbms::Connection*, const dbos::StorageArea*) throw (RuntimeException); - }; - - const filesystem::Directory* a_filesystemDirectory; - int a_inode; - - static Loader* st_loader; - static ExclusiveHash st_hash; - static int st_maxSize; - - Directory () { ; } - Directory (const Directory&); - - void initialize (dbos::Loader& loader) throw (RuntimeException, dbms::DatabaseException); - void destroy () throw (); - - dbos_declare_object (Directory); + class Loader : public dbos::Loader { + public: + Loader() : dbos::Loader() {;} + + Loader& setKey(const filesystem::Directory* directory) throw() { + a_filesystemDirectory = directory; + return *this; + } + + const filesystem::Directory* getDirectory() const throw() { return a_filesystemDirectory; } + int getINode() const throw() { return a_inode; } + + dbos::Index getIndex() const throw(); + std::string asString() const throw(); + + private: + const filesystem::Directory* a_filesystemDirectory; + int a_inode; + + // dbms::Statement is not required + dbms::Statement* initialize(dbms::Database&) throw(RuntimeException) { return NULL; } + bool load(dbms::Connection*, const dbos::StorageArea*) throw(RuntimeException); + }; + + const filesystem::Directory* a_filesystemDirectory; + int a_inode; + + static Loader* st_loader; + static ExclusiveHash st_hash; + static int st_maxSize; + + Directory() { ; } + Directory(const Directory&); + + void initialize(dbos::Loader& loader) throw(RuntimeException, dbms::DatabaseException); + void destroy() throw(); + + dbos_declare_object(Directory); }; } diff --git a/example/dbos/workdir/storage/File.hpp b/example/dbos/workdir/storage/File.hpp index 6cee61e..4752bc0 100644 --- a/example/dbos/workdir/storage/File.hpp +++ b/example/dbos/workdir/storage/File.hpp @@ -42,19 +42,19 @@ #include namespace anna { - namespace dbms { - class Database; - class Connection; - } - namespace dbos { - class Repository; - } +namespace dbms { +class Database; +class Connection; +} +namespace dbos { +class Repository; +} } namespace workdir { namespace filesystem { - class File; +class File; } namespace storage { @@ -65,58 +65,58 @@ using namespace anna; class File : public dbos::Object, public dbos::ObjectFacade { public: - const Directory* getParent () const throw () { return a_parent; } - const filesystem::File* getFilesystemFile () const throw () { return a_filesystemFile; } - int getINode () const throw () { return a_inode; } - - std::string asString () const throw (); - - static void setup (dbos::Repository&, const int maxSize) throw (RuntimeException); - static File* instantiate (const filesystem::File*) throw (RuntimeException); - - static const char* getStorageAreaName () throw () { return "storage::File"; } - static const dbos::Size getMaxSize () throw () { return st_maxSize; } + const Directory* getParent() const throw() { return a_parent; } + const filesystem::File* getFilesystemFile() const throw() { return a_filesystemFile; } + int getINode() const throw() { return a_inode; } + + std::string asString() const throw(); + + static void setup(dbos::Repository&, const int maxSize) throw(RuntimeException); + static File* instantiate(const filesystem::File*) throw(RuntimeException); + + static const char* getStorageAreaName() throw() { return "storage::File"; } + static const dbos::Size getMaxSize() throw() { return st_maxSize; } private: - class Loader : public dbos::Loader { - public: - Loader () : dbos::Loader () {;} - - Loader& setKey (const filesystem::File* file) throw () { - a_filesystemFile = file; - return *this; - } - - const filesystem::File* getFile () const throw () { return a_filesystemFile; } - int getINode () const throw () { return a_inode; } - - dbos::Index getIndex () const throw (); - std::string asString () const throw (); - - private: - const filesystem::File* a_filesystemFile; - int a_inode; - - // dbms::Statement is not required - dbms::Statement* initialize (dbms::Database&) throw (RuntimeException) { return NULL; } - bool load (dbms::Connection*, const dbos::StorageArea*) throw (RuntimeException); - }; - - Directory* a_parent; - const filesystem::File* a_filesystemFile; - int a_inode; - - static Loader* st_loader; - static ExclusiveHash st_hash; - static int st_maxSize; - - File () : a_parent (NULL) { ; } - File (const File&); - - void initialize (dbos::Loader& loader) throw (RuntimeException, dbms::DatabaseException); - void destroy () throw (); - - dbos_declare_object (File); + class Loader : public dbos::Loader { + public: + Loader() : dbos::Loader() {;} + + Loader& setKey(const filesystem::File* file) throw() { + a_filesystemFile = file; + return *this; + } + + const filesystem::File* getFile() const throw() { return a_filesystemFile; } + int getINode() const throw() { return a_inode; } + + dbos::Index getIndex() const throw(); + std::string asString() const throw(); + + private: + const filesystem::File* a_filesystemFile; + int a_inode; + + // dbms::Statement is not required + dbms::Statement* initialize(dbms::Database&) throw(RuntimeException) { return NULL; } + bool load(dbms::Connection*, const dbos::StorageArea*) throw(RuntimeException); + }; + + Directory* a_parent; + const filesystem::File* a_filesystemFile; + int a_inode; + + static Loader* st_loader; + static ExclusiveHash st_hash; + static int st_maxSize; + + File() : a_parent(NULL) { ; } + File(const File&); + + void initialize(dbos::Loader& loader) throw(RuntimeException, dbms::DatabaseException); + void destroy() throw(); + + dbos_declare_object(File); }; } diff --git a/include/anna/comm/INetAddress.hpp b/include/anna/comm/INetAddress.hpp index 6ca22f7..c8d4bb4 100644 --- a/include/anna/comm/INetAddress.hpp +++ b/include/anna/comm/INetAddress.hpp @@ -68,7 +68,7 @@ public: /** Constructor. - \param device Instance for device (address). + \param device Instance for device (address). \param port Port number. */ INetAddress(const Device* device, const int port = -1) : a_device(device), a_port(port) {;} @@ -106,25 +106,25 @@ public: /** Copy operator. - \param right Source address to be copied. + \param right Source address to be copied. */ INetAddress& operator = (const INetAddress& right) throw() { a_device = right.a_device; a_port = right.a_port; return *this; } /** - Comparison operator. + Comparison operator. \param right Source address to be compared. @return \em true when address provided is equal to this \em false in other case. */ bool operator == (const INetAddress& right) const throw() { return a_device == right.a_device && a_port == right.a_port; } /** - Returns the initialized state for this network address. - @return \em true when initialized, \em false when not. + Returns the initialized state for this network address. + @return \em true when initialized, \em false when not. */ bool isNull() const throw() { return (a_device == NULL || a_port == -1); } /** - Clear the content for this instance. + Clear the content for this instance. */ void clear() throw() { a_device = NULL; a_port = -1; } diff --git a/include/anna/comm/LiteTransport.hpp b/include/anna/comm/LiteTransport.hpp index ce9365d..e20dc30 100644 --- a/include/anna/comm/LiteTransport.hpp +++ b/include/anna/comm/LiteTransport.hpp @@ -64,8 +64,8 @@ public: ~LiteTransport(); /** - Returns the class name literal. - @return class name literal. + Returns the class name literal. + @return class name literal. */ static const char* className() throw() { return "anna::comm::LiteTransport"; } diff --git a/include/anna/comm/ReceiverFactory.hpp b/include/anna/comm/ReceiverFactory.hpp index 3b44c8e..712a09a 100644 --- a/include/anna/comm/ReceiverFactory.hpp +++ b/include/anna/comm/ReceiverFactory.hpp @@ -81,8 +81,8 @@ class Receiver; class ReceiverFactory : public Mutex { public: /** - Returns the logical name for this receiver factory. - @return logical name for this receiver factory. + Returns the logical name for this receiver factory. + @return logical name for this receiver factory. */ const std::string& getName() const throw() { return a_name; } @@ -123,7 +123,7 @@ public: protected: /** Constructor. - \param name Logical name for this receivers factory. + \param name Logical name for this receivers factory. */ ReceiverFactory(const char* name); diff --git a/include/anna/comm/SureTransport.hpp b/include/anna/comm/SureTransport.hpp index 892d9e0..9837927 100644 --- a/include/anna/comm/SureTransport.hpp +++ b/include/anna/comm/SureTransport.hpp @@ -69,7 +69,7 @@ public: /** Returns the transport layer manager associated to this class. - \return transport layer manager associated to this class. + \return transport layer manager associated to this class. */ static TransportFactory& getFactory() throw() { return st_transportFactory; } diff --git a/include/anna/comm/functions.hpp b/include/anna/comm/functions.hpp index 73d53b5..046b999 100644 --- a/include/anna/comm/functions.hpp +++ b/include/anna/comm/functions.hpp @@ -69,7 +69,7 @@ struct functions : public anna::app::functions { @param hostname Logical name for the server used to resolve (could be www.gopher.net i.e.). @return First IP address returned by the system. - + \see man gethostbyname. */ static std::string resolveIP(const char* hostname) throw(RuntimeException); diff --git a/include/anna/core/util/Environment.hpp b/include/anna/core/util/Environment.hpp index 7db6aa1..b349f29 100644 --- a/include/anna/core/util/Environment.hpp +++ b/include/anna/core/util/Environment.hpp @@ -59,7 +59,7 @@ public: /** Parses the environment data (all variables available) when process was started. - @param envp Environment array passed on main function as third argument. + @param envp Environment array passed on main function as third argument. Cache data is cleared if NULL passed, allowing to get current environment values for variables. */ void initialize(char **envp = NULL) throw(); diff --git a/source/core/util/Environment.cpp b/source/core/util/Environment.cpp index 820c9ab..0df7d6f 100644 --- a/source/core/util/Environment.cpp +++ b/source/core/util/Environment.cpp @@ -55,18 +55,22 @@ void Environment::initialize(char **envp) throw() { LOGMETHOD(TraceMethod tm("Environment", "initialize", ANNA_FILE_LOCATION)); // clear data a_vars.clear(); - if (!envp) return; + + if(!envp) return; // register data std::string assignment, var, val; - while (*envp) { - assignment = *envp; + + while(*envp) { + assignment = *envp; std::size_t equalPos = assignment.find("="); - if (equalPos != string::npos) { // protection + + if(equalPos != string::npos) { // protection var = assignment.substr(0, equalPos - 1); val = assignment.substr(equalPos, assignment.size() - 1); a_vars[var] = val; } + envp++; } } @@ -76,32 +80,29 @@ std::string Environment::getValue(const char* variableName, bool exceptionIfMiss throw RuntimeException("Invalid NULL variable name!", ANNA_FILE_LOCATION); std::string var = variableName; - return getValue(var, exceptionIfMissing); } std::string Environment::getValue(const std::string &variableName, bool exceptionIfMissing) throw(RuntimeException) { std::string result = ""; - std::map::const_iterator it = a_vars.find(variableName); if(it == a_vars.end()) { - char *current = getenv (variableName.c_str()); - if (!current) { + char *current = getenv(variableName.c_str()); + + if(!current) { std::string msg = "The variable '"; msg += variableName; msg += "' is not defined in the environment."; LOGDEBUG(Logger::debug(msg, ANNA_FILE_LOCATION)); if(exceptionIfMissing) throw RuntimeException(msg, ANNA_FILE_LOCATION); - } - else { + } else { // assignment a_vars[variableName] = current; result = current; } - } - else { + } else { result = it->second; } @@ -110,40 +111,37 @@ std::string Environment::getValue(const std::string &variableName, bool exceptio void Environment::setVariable(const std::string &name, const std::string &value, bool overwrite) throw(RuntimeException) { + if(name == "") throw RuntimeException("Must provide non-empty variable name", ANNA_FILE_LOCATION); - if (name == "") throw RuntimeException("Must provide non-empty variable name", ANNA_FILE_LOCATION); - - if(setenv(name.c_str(), value.c_str(), overwrite ? 1:0) != 0) { + if(setenv(name.c_str(), value.c_str(), overwrite ? 1 : 0) != 0) { std::string msg = "Cannot set the environment variable '"; msg += name; msg += "=\""; msg += value; msg += "\"'. The errno is "; msg += anna::functions::asString(errno); - throw RuntimeException(msg, ANNA_FILE_LOCATION); } // optimization - if (overwrite) + if(overwrite) a_vars[name] = value; } void Environment::unsetVariable(const std::string &name) throw(RuntimeException) { - - if (name == "") throw RuntimeException("Must provide non-empty variable name", ANNA_FILE_LOCATION); + if(name == "") throw RuntimeException("Must provide non-empty variable name", ANNA_FILE_LOCATION); if(unsetenv(name.c_str()) != 0) { std::string msg = "Cannot unset the environment variable named '"; msg += name; msg += "'. The errno is "; msg += anna::functions::asString(errno); - throw RuntimeException(msg, ANNA_FILE_LOCATION); } std::map::iterator it = a_vars.find(name); + if(it != a_vars.end()) a_vars.erase(it); } diff --git a/test/core/main.cpp b/test/core/main.cpp index b6d4c73..8734a8d 100644 --- a/test/core/main.cpp +++ b/test/core/main.cpp @@ -132,14 +132,12 @@ BOOST_AUTO_TEST_CASE(environment) { env.initialize(); BOOST_CHECK_THROW(env.getValue("WRONG_ENV_VAR", true), anna::RuntimeException); // true => exception if missing BOOST_CHECK_THROW(env.getValue(NULL), anna::RuntimeException); - env.setVariable("TEST_VAR", "my test var value"); BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string("my test var value")); env.setVariable("TEST_VAR", "my new test var value", false /* no overwritting */); BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string("my test var value")); env.unsetVariable("TEST_VAR"); BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string("")); - BOOST_REQUIRE_EQUAL(env.getValue("MISSING_ENV_VAR"), std::string("")); } -- 2.20.1