From: cherierm Date: Fri, 16 May 2008 15:04:54 +0000 (+0000) Subject: Code refactoring X-Git-Tag: v3.3~502 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/21722d01338d0cf9a704fdb226507d1be787871a?ds=sidebyside Code refactoring git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5434 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/cxx/ApplicationHandler.cxx b/src/cxx/ApplicationHandler.cxx index 7b5f81692a..425700f2ae 100644 --- a/src/cxx/ApplicationHandler.cxx +++ b/src/cxx/ApplicationHandler.cxx @@ -1,70 +1,146 @@ #include -namespace msg +namespace SimGrid { - -ApplicationHandler::ProcessFactory::processFactory = NULL; - -void ApplicationHandler::onStartDocument(void) -{ - processFactory = new ProcessFactory(); -} - -void ApplicationHandler::onEndDocument(void) -{ - if(processFactory) - delete processFactroy; -} - -void ApplicationHandler::onBeginProcess(void) -{ - processFactory->setProcessIdentity(A_surfxml_process_host, A_surfxml_process_function); -} - -void ApplicationHandler::onProcessArg(void) -{ - processFactory->registerProcessArg(A_surfxml_argument_value); -} - -void ApplicationHandler::OnProperty(void) -{ - processFactory->setProperty(A_surfxml_prop_id, A_surfxml_prop_value); -} - -void ApplicationHandler::onEndProcess(void) -{ - processFactory->createProcess(); -} - -void ApplicationHandler::ProcessFactory::createProcess() + namespace Msg { - Process* process = (Process*)Class::fromName(this->function); - - Host host = Host::getByName(this->hostName); - - process->create(); - - Strings* args = processFactory->args; - - int size = args->size(); - - for (int index = 0; index < size; index++) - process->args->push_back(args->at(index)); - - process->properties = this->properties; - this->properties = new Properties(); - } - -void ApplicationHandler::ProcessFactory::setProcessIdentity(const string& hostName, const string& function) -{ - this->hostName = hostName; - this->function = function; - - if (!this->args->empty()) - this->args->clear(); - if(!this->properties->empty()) - this->properties->clear(); -} + ApplicationHandler::ProcessFactory::processFactory = NULL; + + // Desable the default constructor, the copy constructor , the assignement operator + // and the destructor of this class. Assume that this class is static. + + // Default constructor. + ApplicationHandler::ApplicationHandler() + { + // NOTHING TODO + } + + // Copy constructor. + ApplicationHandler::ApplicationHandler(const ApplicationHandler& rApplicationHandler) + { + // NOTHING TODO + } + + // Destructor + ApplicationHandler::~ApplicationHandler() + { + // NOTHING TODO + } + + // Assignement operator. + const ApplicationHandler& ApplicationHandler::operator = (const ApplicationHandler& rApplicationHandler) + { + return *this; + } + + void ApplicationHandler::onStartDocument(void) + { + // instanciate the factory at the begining of the parsing + processFactory = new ProcessFactory(); + } + + void ApplicationHandler::onEndDocument(void) + { + // release the handler at the end of the parsing. + if(processFactory) + delete processFactroy; + } + + void ApplicationHandler::onBeginProcess(void) + { + // set the process identity at the begin of the xml process element. + processFactory->setProcessIdentity(A_surfxml_process_host, A_surfxml_process_function); + } + + void ApplicationHandler::onProcessArg(void) + { + // register the argument of the current xml process element. + processFactory->registerProcessArg(A_surfxml_argument_value); + } + + void ApplicationHandler::OnProperty(void) + { + // set the property of the current xml process element. + processFactory->setProperty(A_surfxml_prop_id, A_surfxml_prop_value); + } + + void ApplicationHandler::onEndProcess(void) + { + // at the end of the xml process element create the wrapper process (of the native Msg process) + processFactory->createProcess(); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////// + // Process factory connected member functions + + ApplicationHandler::ProcessFactory::ProcessFactory() + { + this->args = xbt_dynar_new(sizeof(char*),ProcessFactory::freeCstr); + this->properties = NULL; // TODO instanciate the dictionary + this->hostName = NULL; + this->function = NULL; + } + + // create the cxx process wrapper. + void ApplicationHandler::ProcessFactory::createProcess() + { + // dynamic creation of a instance fo the process from its name (which is specified by the element function + // in the xml application file. + Process* process = (Process*)Class::fromName(this->function); + + // retrieve the host of the process from its name + Host host = Host::getByName(this->hostName); + + // build the list of the arguments of the newly created process. + int argc = xbt_dynar_length(this->args); + + char** argv = (char**)calloc(argc, sizeof(char*)); + + for(int i = 0; i < argc; i++) + xbt_dynar_pop(this->args, &(argv[i])); + + // finaly create the process (for more detail on the process creation see Process::create() + process->create(host, this->function , argc, argv); + + // TODO add the properties of the process + /*process->properties = this->properties; + this->properties = new Properties();*/ + } + + void ApplicationHandler::ProcessFactory::setProcessIdentity(const string& hostName, const string& function) + { + this->hostName = hostName; + this->function = function; + + /*if (!this->args->empty()) + this->args->clear(); + + if(!this->properties->empty()) + this->properties->clear();*/ + } + + // callback function used by the dynamic array to cleanup all of its elements. + void ApplicationHandler::ProcessFactory::freeCstr(void* cstr) + { + free(*(void**)str); + } + + void ApplicationHandler::ProcessFactory::registerProcessArg(const char* arg) + { + char* cstr = strdup(arg); + xbt_dynar_push(this->args, &cstr); + } + + void ApplicationHandler::ProcessFactory::setProperty(const char* id, const char* value) + { + // TODO implement this function; + } + + const const char* ApplicationHandler::ProcessFactory::getHostName(void) + { + return this->hostName; + } -} \ No newline at end of file + } // namespace Msg +} // namespace SimGrid \ No newline at end of file diff --git a/src/cxx/ApplicationHandler.hpp b/src/cxx/ApplicationHandler.hpp index d45032132d..c33cc76925 100644 --- a/src/cxx/ApplicationHandler.hpp +++ b/src/cxx/ApplicationHandler.hpp @@ -6,97 +6,101 @@ #error ApplicationHandler.hpp requires C++ compilation (use a .cxx suffix) #endif -#include -#include -namespace msg +namespace SimGrid { - - struct Compare + namespace Msg { - public bool operator(const string& first, const string& second) + // Declaration of the class ApplicationHandler. + class ApplicationHandler { - return first.compare(second); - } - }; - - typedef vector Strings; - - typedef map Properties; - - -class ApplicationHandler -{ -private : - // Default constructor. - ApplicationHandler(); - - // Copy constructor. - ApplicationHandler(const ApplicationHandler& rApplicationHandler); - - // Destructor - virtual ~ApplicationHandler(); - - class ProcessFactory - { - public: - - Strings* args; - Properties* properties; - - private: - string hostName; - string function; - - public : - - ProcessFactory() - { - this->args = new Strings(); - this->properties = new Properties(); - this->hostName = NULL; - this->function = NULL; - } - - void setProcessIdentity(const string& hostName, const string& function); - - void registerProcessArg(const string& arg) - { - this->args->push_back(arg); - } - - void setProperty(const string& id, const string& value) - { - this->properties->insert(Properties::value_type(id,value)); - } - - const String& getHostName(void) - { - return this->hostName; - } - - void createProcess(void); - - }; - - static ProcessFactory* processFactory; - - public: - - static void onStartDocument(void); - - static void onEndDocument(void); - - static void onBeginProcess(void); - - static void onProcessArg(void); - - static void OnProperty(void); - - static void onEndProcess(void); -}; + private : + + // Desable the default constructor, the copy constructor , the assignement operator + // and the destructor of this class. Assume that this class is static. + + // Default constructor. + ApplicationHandler(); + + // Copy constructor. + ApplicationHandler(const ApplicationHandler& rApplicationHandler); + + // Destructor + virtual ~ApplicationHandler(); + + // Assignement operator. + const ApplicationHandler& operator = (const ApplicationHandler& rApplicationHandler); + + class ProcessFactory + { + public: + + // the list of the argument of the process to create. + xbt_dynar_t args; + // the properties of the process to create + xbt_dict_t properties; + + private: + + // the current host name parsed + const char* hostName; + // the name of the class of the process + const char* function; + + public : + + // Default constructor. + ProcessFactory(); + + // Copy constructor. + ProcessFactory(const ProcessFactory& rProcessFactory); + + // Destructor. + virtual ~ProcessFactory(); + + // Set the identity of the current process. + void setProcessIdentity(const char* hostName, const char* function); + + // Register an argument of the current process. + void registerProcessArg(const char* arg); + + // Set the property of the current process. + void setProperty(const char* id, const char* value); + + // Return the host name of the current process. + const const char* getHostName(void); + + // Create the current process. + void createProcess(void); + + }; + + // the process factory used by the application handler. + static ProcessFactory* processFactory; + + public: + + // Handle the begining of the parsing of the xml file describing the application. + static void onStartDocument(void); + + // Handle at the end of the parsing. + static void onEndDocument(void); + + // Handle the begining of the parsing of a xml process element. + static void onBeginProcess(void); + + // Handle the parsing of an argument of the current xml process element. + static void onProcessArg(void); + + // Handle the parsing of a property of the currnet xml process element. + static void OnProperty(void); + + // Handle the end of the parsing of a xml process element + static void onEndProcess(void); + }; -} + } // namespace Msg +} // namespace SimGrid #endif // !MSG_APPLICATION_HANDLER_HPP \ No newline at end of file