From e133a950865071b1c006387e915d39989a53c8cd Mon Sep 17 00:00:00 2001 From: cherierm Date: Thu, 3 Jul 2008 16:56:29 +0000 Subject: [PATCH] Last Changes of CPP version of Msg git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5853 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/cxx/Application.cxx | 31 ++++-- src/cxx/Application.hpp | 15 +-- src/cxx/ApplicationHandler.cxx | 34 +++++-- src/cxx/ApplicationHandler.hpp | 100 ++++++++++-------- src/cxx/BadAllocException.cxx | 4 +- src/cxx/BadAllocException.hpp | 10 +- src/cxx/Environment.cxx | 22 ++-- src/cxx/Environment.hpp | 16 +-- src/cxx/Exception.cxx | 2 +- src/cxx/Exception.hpp | 4 +- src/cxx/FileNotFoundException.cxx | 1 - src/cxx/FileNotFoundException.hpp | 4 +- src/cxx/Host.cxx | 102 ++++++++++++------- src/cxx/Host.hpp | 72 +++++++------ src/cxx/HostNotFoundException.cxx | 4 +- src/cxx/HostNotFoundException.hpp | 4 +- src/cxx/InvalidArgumentException.cxx | 4 +- src/cxx/InvalidArgumentException.hpp | 4 +- src/cxx/LogicException.cxx | 4 +- src/cxx/LogicException.hpp | 4 +- src/cxx/Msg.cxx | 15 ++- src/cxx/Msg.hpp | 20 ++-- src/cxx/MsgException.cxx | 4 +- src/cxx/MsgException.hpp | 4 +- src/cxx/Object.cxx | 39 ++++--- src/cxx/Process.cxx | 142 +++++++++++++++++++++----- src/cxx/Process.hpp | 43 +++++--- src/cxx/Simulation.cxx | 17 +++- src/cxx/Simulation.hpp | 11 +- src/cxx/Task.cxx | 146 ++++++++++++++++++++------- src/cxx/Task.hpp | 99 ++++++++++++++---- 31 files changed, 676 insertions(+), 305 deletions(-) diff --git a/src/cxx/Application.cxx b/src/cxx/Application.cxx index e11cab9b9a..e326494d26 100644 --- a/src/cxx/Application.cxx +++ b/src/cxx/Application.cxx @@ -14,10 +14,19 @@ */ #include + + +#include +#include +#include +#include #include #include #include +#include + +#include #ifndef S_ISREG #define S_ISREG(__mode) (((__mode) & S_IFMT) == S_IFREG) @@ -34,7 +43,7 @@ namespace SimGrid this->deployed = false; } - Application(const Application& rApplication) + Application::Application(const Application& rApplication) { this->file = rApplication.getFile(); @@ -51,7 +60,7 @@ namespace SimGrid struct stat statBuf = {0}; - if(stat(statBuff, &info) < 0 || !S_ISREG(statBuff.st_mode)) + if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode)) throw FileNotFoundException(file); this->file = file; @@ -63,7 +72,7 @@ namespace SimGrid // NOTHING TODO } - Application::deploy(const char* file) + void Application::deploy(const char* file) throw(NullPointerException, FileNotFoundException, LogicException, MsgException) { // check logic @@ -78,7 +87,7 @@ namespace SimGrid struct stat statBuf = {0}; - if(stat(statBuff, &info) < 0 || !S_ISREG(statBuff.st_mode)) + if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode)) throw FileNotFoundException(file); surf_parse_reset_parser(); @@ -87,13 +96,13 @@ namespace SimGrid surfxml_add_callback(STag_surfxml_process_cb_list, ApplicationHandler::onBeginProcess); // set the process arg handler - surfxml_add_callback(ETag_surfxml_argument_cb_list, ApplicationHandler::onArg); + surfxml_add_callback(ETag_surfxml_argument_cb_list, ApplicationHandler::onProcessArg); // set the properties handler surfxml_add_callback(STag_surfxml_prop_cb_list, ApplicationHandler::OnProperty); // set the end of the xml process element handler - surfxml_add_callback(ETag_surfxml_process_cb_list, ApplicationHandler::OnEndProcess); + surfxml_add_callback(ETag_surfxml_process_cb_list, ApplicationHandler::onEndProcess); surf_parse_open(file); @@ -112,7 +121,7 @@ namespace SimGrid this->deployed = true; } - Application::deploy(void) + void Application::deploy(void) throw(LogicException, MsgException) { // check logic @@ -126,9 +135,9 @@ namespace SimGrid surf_parse_reset_parser(); surfxml_add_callback(STag_surfxml_process_cb_list, ApplicationHandler::onBeginProcess); - surfxml_add_callback(ETag_surfxml_argument_cb_list, ApplicationHandler::onArg); + surfxml_add_callback(ETag_surfxml_argument_cb_list, ApplicationHandler::onProcessArg); surfxml_add_callback(STag_surfxml_prop_cb_list, ApplicationHandler::OnProperty); - surfxml_add_callback(ETag_surfxml_process_cb_list, ApplicationHandler::OnEndProcess); + surfxml_add_callback(ETag_surfxml_process_cb_list, ApplicationHandler::onEndProcess); surf_parse_open(file); @@ -140,7 +149,7 @@ namespace SimGrid this->deployed = true; } - bool Application::isDeployed(void) + bool Application::isDeployed(void) const { return this->deployed; } @@ -160,7 +169,7 @@ namespace SimGrid struct stat statBuf = {0}; - if(stat(statBuff, &info) < 0 || !S_ISREG(statBuff.st_mode)) + if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode)) throw FileNotFoundException("file (file not found)"); this->file = file; diff --git a/src/cxx/Application.hpp b/src/cxx/Application.hpp index 1ca158fe75..7efe7a2073 100644 --- a/src/cxx/Application.hpp +++ b/src/cxx/Application.hpp @@ -19,17 +19,19 @@ #error Application.hpp requires C++ compilation (use a .cxx suffix) #endif -#include -#include -#include -#include +#include namespace SimGrid { namespace Msg { + class NullPointerException; + class FileNotFoundException; + class LogicException; + class MsgException; + // Application wrapper class declaration. - class Application + class SIMGRIDX_EXPORT Application { public: @@ -75,7 +77,7 @@ namespace SimGrid */ void deploy(void) - throw(LogicExeption, MsgException); + throw(LogicException, MsgException); /*! \brief Application::deploy() - Deploy the appliction. * @@ -141,6 +143,7 @@ namespace SimGrid // the xml file which describes the application of the simulation. const char* file; + }; } // namespace Msg diff --git a/src/cxx/ApplicationHandler.cxx b/src/cxx/ApplicationHandler.cxx index f1a8077c04..9be89078f8 100644 --- a/src/cxx/ApplicationHandler.cxx +++ b/src/cxx/ApplicationHandler.cxx @@ -12,15 +12,27 @@ /* ApplicationHandler member functions implementation. */ - + #include +#include +#include +#include +#include +#include + +#include + +#include +#include + + namespace SimGrid { namespace Msg { - ApplicationHandler::ProcessFactory::processFactory = NULL; + ApplicationHandler::ProcessFactory* ApplicationHandler::processFactory = NULL; // Desable the default constructor, the copy constructor , the assignement operator // and the destructor of this class. Assume that this class is static. @@ -59,7 +71,7 @@ namespace SimGrid { // release the handler at the end of the parsing. if(processFactory) - delete processFactroy; + delete processFactory; } void ApplicationHandler::onBeginProcess(void) @@ -91,7 +103,7 @@ namespace SimGrid ApplicationHandler::ProcessFactory::ProcessFactory() { - this->args = xbt_dynar_new(sizeof(char*),ProcessFactory::freeCstr); + this->args = xbt_dynar_new(sizeof(char*),ApplicationHandler::ProcessFactory::freeCstr); this->properties = NULL; // TODO instanciate the dictionary this->hostName = NULL; this->function = NULL; @@ -102,12 +114,14 @@ namespace SimGrid throw (ClassNotFoundException, HostNotFoundException) { Host host; + Class* c; Process* process; // try to dynamicaly create an instance fo the process from its name (which is specified by the element function // in the xml application file. // if this static method fails, it throws an exception of the class ClassNotFoundException - process = (Process*)Class::fromName(this->function); + c = Class::fromName(this->function); + process = reinterpret_cast(c->createObject()); // try to retrieve the host of the process from its name // if this method fails, it throws an exception of the class HostNotFoundException @@ -118,7 +132,7 @@ namespace SimGrid char** argv = (char**)calloc(argc, sizeof(char*)); - for(int i = 0; i < argc; i++) + for(int i = argc -1; i >= 0; i--) xbt_dynar_pop(this->args, &(argv[i])); // finaly create the process (for more detail on the process creation see Process::create() @@ -129,7 +143,7 @@ namespace SimGrid this->properties = new Properties();*/ } - void ApplicationHandler::ProcessFactory::setProcessIdentity(const string& hostName, const string& function) + void ApplicationHandler::ProcessFactory::setProcessIdentity(const char* hostName, const char* function) { this->hostName = hostName; this->function = function; @@ -144,12 +158,12 @@ namespace SimGrid // callback function used by the dynamic array to cleanup all of its elements. void ApplicationHandler::ProcessFactory::freeCstr(void* cstr) { - free(*(void**)str); + free(*(void**)cstr); } void ApplicationHandler::ProcessFactory::registerProcessArg(const char* arg) { - char* cstr = strdup(arg); + char* cstr = _strdup(arg); xbt_dynar_push(this->args, &cstr); } @@ -158,7 +172,7 @@ namespace SimGrid // TODO implement this function; } - const const char* ApplicationHandler::ProcessFactory::getHostName(void) + const char* ApplicationHandler::ProcessFactory::getHostName(void) { return this->hostName; } diff --git a/src/cxx/ApplicationHandler.hpp b/src/cxx/ApplicationHandler.hpp index 37e5bf139a..6c3819b7eb 100644 --- a/src/cxx/ApplicationHandler.hpp +++ b/src/cxx/ApplicationHandler.hpp @@ -20,34 +20,27 @@ #error ApplicationHandler.hpp requires C++ compilation (use a .cxx suffix) #endif +#include +#include -#include -#include +#include namespace SimGrid { namespace Msg { + + class ClassNotFoundException; + class HostNotFoundException; + class Process; + // Declaration of the class ApplicationHandler (Singleton). - class ApplicationHandler + class SIMGRIDX_EXPORT ApplicationHandler { - 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); - + friend Process; + + public: + class ProcessFactory { public: @@ -66,32 +59,51 @@ namespace SimGrid 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) - throw (ClassNotFoundException, HostNotFoundException); + // 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 char* getHostName(void); + + // Create the current process. + void createProcess(void) + throw (ClassNotFoundException, HostNotFoundException); + + static void freeCstr(void* cstr); }; + + 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); // the process factory used by the application handler. static ProcessFactory* processFactory; diff --git a/src/cxx/BadAllocException.cxx b/src/cxx/BadAllocException.cxx index 07df2b614c..d650141a49 100644 --- a/src/cxx/BadAllocException.cxx +++ b/src/cxx/BadAllocException.cxx @@ -1,11 +1,9 @@ -#include "BadAllocException.hpp" +#include #include #include #include -using namespace std; - namespace SimGrid { namespace Msg diff --git a/src/cxx/BadAllocException.hpp b/src/cxx/BadAllocException.hpp index df050b8dea..ac4039fc97 100644 --- a/src/cxx/BadAllocException.hpp +++ b/src/cxx/BadAllocException.hpp @@ -10,17 +10,17 @@ * */ -#ifndef MSG_MSGEXCEPTION_HPP -#define MSG_MSGEXCEPTION_HPP +#ifndef MSG_BADALLOCEXCEPTION_HPP +#define MSG_BADALLOCEXCEPTION_HPP -#include "Exception.hpp" +#include namespace SimGrid { namespace Msg { - class BadAllocException : public Exception + class SIMGRIDX_EXPORT BadAllocException : public Exception { public: @@ -61,4 +61,4 @@ namespace SimGrid }// namespace SimGrid -#endif // !MSG_MSGEXCEPTION_HPP +#endif // !MSG_BADALLOCEXCEPTION_HPP diff --git a/src/cxx/Environment.cxx b/src/cxx/Environment.cxx index 489c8143d0..806e868860 100644 --- a/src/cxx/Environment.cxx +++ b/src/cxx/Environment.cxx @@ -15,9 +15,19 @@ #include +#include +#include +#include +#include +#include + +#include + #include #include +#include + #ifndef S_ISREG #define S_ISREG(__mode) (((__mode) & S_IFMT) == S_IFREG) #endif @@ -32,14 +42,14 @@ namespace SimGrid this->loaded = false; } - Environment::Environment(const Environment& rEnvironment); + Environment::Environment(const Environment& rEnvironment) { this->file = rEnvironment.getFile(); this->loaded = rEnvironment.isLoaded(); } Environment::Environment(const char* file) - throw(NullPointerException, InvalidArgumentException); + throw(NullPointerException, InvalidArgumentException) { // check parameters @@ -48,8 +58,8 @@ namespace SimGrid struct stat statBuf = {0}; - if(stat(statBuff, &info) < 0 || !S_ISREG(statBuff.st_mode)) - throw InvalidParameterException("file (file not found)"); + if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode)) + throw InvalidArgumentException("file (file not found)"); this->file = file; this->loaded = false; @@ -94,7 +104,7 @@ namespace SimGrid struct stat statBuf = {0}; - if(stat(statBuff, &info) < 0 || !S_ISREG(statBuff.st_mode)) + if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode)) throw FileNotFoundException(file); MSG_create_environment(file); @@ -124,7 +134,7 @@ namespace SimGrid struct stat statBuf = {0}; - if(stat(statBuff, &info) < 0 || !S_ISREG(statBuff.st_mode)) + if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode)) throw FileNotFoundException("file (file not found)"); this->file = file; diff --git a/src/cxx/Environment.hpp b/src/cxx/Environment.hpp index cc21c72701..fb2a17b4b2 100644 --- a/src/cxx/Environment.hpp +++ b/src/cxx/Environment.hpp @@ -19,18 +19,20 @@ #error Environment.hpp requires C++ compilation (use a .cxx suffix) #endif -#include -#include -#include -#include - +#include namespace SimGrid { namespace Msg { + class NullPointerException; + class FileNotFoundException; + class InvalidArgumentException; + class LogicException; + class MsgException; + // Environment class wrapper declaration - class Environment + class SIMGRIDX_EXPORT Environment { public: @@ -136,7 +138,7 @@ namespace SimGrid const char* file; // flag : is true the environment of the simulation is loaded. - bool loaded. + bool loaded; }; } // namespace Msg diff --git a/src/cxx/Exception.cxx b/src/cxx/Exception.cxx index 924eab874c..5e54f88b9d 100644 --- a/src/cxx/Exception.cxx +++ b/src/cxx/Exception.cxx @@ -1,4 +1,4 @@ -#include "Exception.hpp" +#include namespace SimGrid { diff --git a/src/cxx/Exception.hpp b/src/cxx/Exception.hpp index ad22e99387..c520ae6970 100644 --- a/src/cxx/Exception.hpp +++ b/src/cxx/Exception.hpp @@ -13,12 +13,14 @@ #ifndef MSG_EXCEPTION_HPP #define MSG_EXCEPTION_HPP +#include + namespace SimGrid { namespace Msg { - class Exception + class SIMGRIDX_EXPORT Exception { public: diff --git a/src/cxx/FileNotFoundException.cxx b/src/cxx/FileNotFoundException.cxx index 713c153ebc..d99519058f 100644 --- a/src/cxx/FileNotFoundException.cxx +++ b/src/cxx/FileNotFoundException.cxx @@ -4,7 +4,6 @@ #include #include -using namespace std; namespace SimGrid { diff --git a/src/cxx/FileNotFoundException.hpp b/src/cxx/FileNotFoundException.hpp index 398f1536e9..639accd7ee 100644 --- a/src/cxx/FileNotFoundException.hpp +++ b/src/cxx/FileNotFoundException.hpp @@ -13,14 +13,14 @@ #ifndef MSG_FILENOTFOUND_HPP #define MSG_FILENOTFOUND_HPP -#include +#include namespace SimGrid { namespace Msg { - class FileNotFoundException : public Exception + class SIMGRIDX_EXPORT FileNotFoundException : public Exception { public: diff --git a/src/cxx/Host.cxx b/src/cxx/Host.cxx index 07ec58c57a..db4722bf45 100644 --- a/src/cxx/Host.cxx +++ b/src/cxx/Host.cxx @@ -14,6 +14,22 @@ */ #include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include + +#include + namespace SimGrid { namespace Msg @@ -37,11 +53,11 @@ namespace SimGrid Host& Host::getByName(const char* hostName) - throw(HostNotFoundException, InvalidParameterException, BadAllocException) + throw(HostNotFoundException, InvalidArgumentException, BadAllocException) { // check the parameters if(!hostName) - throw InvalidParmeterException("hostName"); + throw InvalidArgumentException("hostName"); m_host_t nativeHost = NULL; // native host. Host* host = NULL; // wrapper host. @@ -53,7 +69,7 @@ namespace SimGrid { // native host not associated yet with its wrapper // instanciate a new wrapper - if(!(host = new Host()) + if(!(host = new Host())) throw BadAllocException(hostName); host->nativeHost = nativeHost; @@ -96,19 +112,19 @@ namespace SimGrid } void Host::all(Host*** hosts, int* len) - throw(InvalidParameterException, BadAllocException) + throw(InvalidArgumentException, BadAllocException) { - /* check the parameters */ + // check the parameters if(!hosts) - throw InvalidParameterException("hosts"); + throw InvalidArgumentException("hosts"); if(len < 0) - throw InvalidParameterException("len parameter must be positive"); + throw InvalidArgumentException("len parameter must be positive"); int count = xbt_fifo_size(msg_global->host); if(*len < count) - throw InvalidParameterException("len parameter must be more than the number of installed host\n (use Host::getNumber() to get the number of hosts)"); + throw InvalidArgumentException("len parameter must be more than the number of installed host\n (use Host::getNumber() to get the number of hosts)"); int index; m_host_t nativeHost; @@ -123,7 +139,7 @@ namespace SimGrid if(!host) { - if(!(host = new Host()) + if(!(host = new Host())) { // release all allocated memory. for(int i = 0; i < index; i++) @@ -164,7 +180,7 @@ namespace SimGrid double Host::getSpeed(void) const { - return MSG_get_host_speed(nativeHost->simdata->smx_host); + return MSG_get_host_speed(nativeHost); } bool Host::hasData(void) const @@ -172,31 +188,42 @@ namespace SimGrid return (NULL != this->data); } - bool Host::isAvailable(void) const + int Host::isAvailable(void) const { - return (bool)SIMIX_host_get_state(nativeHost->simdata->smx_host); + return SIMIX_host_get_state(nativeHost->simdata->smx_host); } void Host::put(int channel, const Task& rTask) - throw(MsgException, InvalidParameterException) + throw(MsgException, InvalidArgumentException) { // checks the parameters if(channel < 0) - throw InvalidParameterException("channel (must be more or equal to zero)"); + throw InvalidArgumentException("channel (must be more or equal to zero)"); if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, nativeHost, channel , -1.0)) throw MsgException("MSG_task_put_with_timeout() failed"); } + + void Host::put(int channel, Task* task) + throw(MsgException, InvalidArgumentException) + { + // checks the parameters + if(channel < 0) + throw InvalidArgumentException("channel (must be more or equal to zero)"); + + if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, nativeHost, channel , -1.0)) + throw MsgException("MSG_task_put_with_timeout() failed"); + } void Host::put(int channel, const Task& rTask, double timeout) - throw(MsgException, InvalidParameterException) + throw(MsgException, InvalidArgumentException) { // checks the parameters if(channel < 0) - throw InvalidParameterException("channel (must be more or equal to zero)"); + throw InvalidArgumentException("channel (must be more or equal to zero)"); if(timeout < 0.0 && timeout != -1.0) - throw InvalidParameterException("timeout (must be more or equal to zero or equal to -1.0)"); + throw InvalidArgumentException("timeout (must be more or equal to zero or equal to -1.0)"); if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, nativeHost, channel , timeout)) @@ -204,25 +231,25 @@ namespace SimGrid } void Host::putBounded(int channel, const Task& rTask, double maxRate) - throw(MsgException, InvalidParameterException) + throw(MsgException, InvalidArgumentException) { // checks the parameters if(channel < 0) - throw InvalidParameterException("channel (must be more or equal to zero)"); + throw InvalidArgumentException("channel (must be more or equal to zero)"); if(maxRate < 0.0 && maxRate != -1.0) - throw InvalidParameterException("maxRate (must be more or equal to zero or equal to -1.0)"); + throw InvalidArgumentException("maxRate (must be more or equal to zero or equal to -1.0)"); if(MSG_OK != MSG_task_put_bounded(rTask.nativeTask, nativeHost, channel, maxRate)) throw MsgException("MSG_task_put_bounded() failed"); } void Host::send(const Task& rTask) - throw(NativeException) + throw(MsgException) { MSG_error_t rv; - char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2); + char* alias = (char*)calloc(strlen(this->getName())+ strlen(Process::currentProcess().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -238,25 +265,26 @@ namespace SimGrid } void Host::send(const char* alias, const Task& rTask) - throw(InvalidParameterException, MsgException) + throw(InvalidArgumentException, MsgException) { // check the parameters if(!alias) - throw InvalidParmeterException("alias (must not be NULL)"); + throw InvalidArgumentException("alias (must not be NULL)"); if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, -1.0)) throw MsgException("MSG_task_send_with_timeout() failed"); } void Host::send(const Task& rTask, double timeout) - throw(InvalidParameterException, BadAllocException, MsgException) + throw(InvalidArgumentException, BadAllocException, MsgException) { // check the parameters if(timeout < 0 && timeout != -1.0) - throw InvalidParameterException("timeout (must be positive or equal to zero or equal to -1.0)"); + throw InvalidArgumentException("timeout (must be positive or equal to zero or equal to -1.0)"); + MSG_error_t rv; - char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2); + char* alias = (char*)calloc(strlen(this->getName()) + strlen(Process::currentProcess().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -273,15 +301,15 @@ namespace SimGrid } void Host::send(const char* alias, const Task& rTask, double timeout) - throw(InvalidParameterException, MsgException) + throw(InvalidArgumentException, MsgException) { // check the parameter if(!alias) - throw InvalidParmeterException("alias (must not be NULL)"); + throw InvalidArgumentException("alias (must not be NULL)"); if(timeout < 0 && timeout != -1.0) - throw InvalidParameterException("timeout (must be positive or equal to zero or equal to -1.0)"); + throw InvalidArgumentException("timeout (must be positive or equal to zero or equal to -1.0)"); if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout)) throw MsgException("MSG_task_send_with_timeout() failed"); @@ -289,14 +317,14 @@ namespace SimGrid void Host::sendBounded(const Task& rTask, double maxRate) - throw(InvalidParameterException, BadAllocException, MsgException) + throw(InvalidArgumentException, BadAllocException, MsgException) { if(maxRate < 0 && maxRate != -1.0) - throw InvalidParameterException("maxRate (must be positive or equal to zero or equal to -1.0)"); + throw InvalidArgumentException("maxRate (must be positive or equal to zero or equal to -1.0)"); MSG_error_t rv; - char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2); + char* alias = (char*)calloc(strlen(this->getName()) + strlen(Process::currentProcess().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -312,18 +340,18 @@ namespace SimGrid } void Host::sendBounded(const char* alias, const Task& rTask, double maxRate) - throw(InvalidParameterException, MsgException) + throw(InvalidArgumentException, MsgException) { // check the parameters if(!alias) - throw InvalidParameterException("alias (must not be NULL)"); + throw InvalidArgumentException("alias (must not be NULL)"); if(maxRate < 0 && maxRate != -1) - throw InvalidParameterException("maxRate (must be positive or equal to zero or equal to -1.0)"); + throw InvalidArgumentException("maxRate (must be positive or equal to zero or equal to -1.0)"); if(MSG_OK != MSG_task_send_bounded(rTask.nativeTask, alias, maxRate)) throw MsgException("MSG_task_send_bounded() failed"); - } + } } // namspace Msg } // namespace SimGrid \ No newline at end of file diff --git a/src/cxx/Host.hpp b/src/cxx/Host.hpp index bb219ebacc..881bebbdbe 100644 --- a/src/cxx/Host.hpp +++ b/src/cxx/Host.hpp @@ -52,23 +52,34 @@ catch(HostNotFoundException e) #error Host.hpp requires C++ compilation (use a .cxx suffix) #endif -#include -#include -#include -#include +#include + +#include + // namespace SimGrid::Msg namespace SimGrid { namespace Msg { + class InvalidArgumentException; + class BadAllocException; + class HostNotFoundException; + class MsgException; + + class Task; + class Process; + // Declaration of the class SimGrid::Msg::Host. - class Host // final class. + class SIMGRIDX_EXPORT Host // final class. { + friend Process; + friend Task; + // Desable the default constructor. // The best way to get an host instance is to use the static method Host::getByName(). - private : + public : // Default constructor (desabled). Host(); @@ -97,11 +108,11 @@ namespace SimGrid * * \exception [HostNotFoundException] if no host with the specified name * was found. - * [InvalidParameterException] if the hostName parameter is invalid (NULL). + * [InvalidArgumentException] if the hostName parameter is invalid (NULL). * [BadAllocException] if there is not enough memory to allocate the host. */ static Host& getByName(const char* hostName) - throw(HostNotFoundException, InvalidParameterException, BadAllocException); + throw(HostNotFoundException, InvalidArgumentException, BadAllocException); /*! \brief Host::getNumber() - returns the number of the installed hosts. * @@ -118,7 +129,7 @@ namespace SimGrid * \see Process::currentProcess(). */ static Host& currentHost(void) - throw(InvalidParameterException, BadAllocException); + throw(InvalidArgumentException, BadAllocException); /*! \brief Host::all() - This static method retrieves all of the hosts of the installed platform. * @@ -130,7 +141,7 @@ namespace SimGrid * the parameter len is set with the number of hosts of the platform. * Otherwise the method throw one of the exception described below. * - * \exception [InvalidParameterException] if the parameter hosts is invalid or + * \exception [InvalidArgumentException] if the parameter hosts is invalid or * if the parameter len is negative or * less than the number of hosts installed * on the current platform. @@ -229,9 +240,9 @@ namespace SimGrid /*! \brief Host::isAvailable - tests if an host is availabled. * * \return Is the host is availabled the method returns - * true. Otherwise the method returns false. + * 1. Otherwise the method returns 0. */ - bool isAvailble(void) const; + int isAvailable(void) const; /* ! \brief Host::put() - put a task on the given channel of a host . * @@ -243,11 +254,14 @@ namespace SimGrid * the method throws one of the exceptions described below. * * \exception [MsgException] if an internal error occurs. - * [InvalidParameterException] if the value of the channel specified as + * [InvalidArgumentException] if the value of the channel specified as * parameter is negative. */ void put(int channel, const Task& rTask) - throw(MsgException, InvalidParameterException); + throw(MsgException, InvalidArgumentException); + + void Host::put(int channel, Task* task) + throw(MsgException, InvalidArgumentException); /* ! \brief Host::put() - put a task on the given channel of a host object (waiting at most timeout seconds). * @@ -260,14 +274,14 @@ namespace SimGrid * the method throws one of the exceptions described below. * * \exception [MsgException] if an internal error occurs. - * [InvalidParameterException] if the value of the channel specified as + * [InvalidArgumentException] if the value of the channel specified as * parameter is negative or if the timeout value * is less than zero and différent of -1. * * \remark To specify no timeout set the timeout value with -1.0. */ - void put(int channel, Task task, double timeout) - throw(MsgException, InvalidParameterException); + void put(int channel, const Task& rTask, double timeout) + throw(MsgException, InvalidArgumentException); /* ! \brief Host::putBounded() - put a task on the given channel of a host object (capping the emission rate to maxrate). * @@ -280,14 +294,14 @@ namespace SimGrid * the method throws one of the exceptions described below. * * \exception [MsgException] if an internal error occurs. - * [InvalidParameterException] if the value of the channel specified as + * [InvalidArgumentException] if the value of the channel specified as * parameter is negative or if the maxRate parameter value * is less than zero and différent of -1.0. * * \remark To specify no rate set the maxRate parameter value with -1.0. */ void putBounded(int channel, const Task& rTask, double maxRate) - throw(MsgException, InvalidParameterException); + throw(MsgException, InvalidArgumentException); /* ! brief Host::send() - sends the given task to mailbox identified by the default alias. * @@ -311,13 +325,13 @@ namespace SimGrid * \return If successful the task is sended to the default mailbox. Otherwise the * method throws one of the exceptions described below. * - * \exception [InvalidParameterException] if alias parameter is invalid (NULL). + * \exception [InvalidArgumentException] if alias parameter is invalid (NULL). * [BadAllocException] if there is not enough memory to allocate * the default alias variable. * [MsgException] if an internal error occurs. */ void send(const char* alias, const Task& rTask) - throw(InvalidParameterException, BadAllocException, MsgException); + throw(InvalidArgumentException, BadAllocException, MsgException); /* ! brief Host::send() - sends the given task to mailbox identified by the default alias * (waiting at most timeout seconds). @@ -330,7 +344,7 @@ namespace SimGrid * * \exception [BadAllocException] if there is not enough memory to allocate * the default alias variable. - * [InvalidParameterException] if the timeout value is negative and different of + * [InvalidArgumentException] if the timeout value is negative and different of * -1.0. * [MsgException] if an internal error occurs. * @@ -339,7 +353,7 @@ namespace SimGrid * */ void send(const Task& rTask, double timeout) - throw(NativeException); + throw(MsgException); /* ! brief Host::send() - sends the given task to mailbox identified by the parameter alias * (waiting at most timeout seconds). @@ -351,7 +365,7 @@ namespace SimGrid * \return If successful the task is sended to the default mailbox. Otherwise the * method throws one of the exceptions described below. * - * \exception [InvalidParameterException] if the timeout value is negative and different of + * \exception [InvalidArgumentException] if the timeout value is negative and different of * -1.0 or if the alias parameter is invalid (NULL). * [MsgException] if an internal error occurs. * @@ -360,7 +374,7 @@ namespace SimGrid * */ void send(const char* alias, const Task& rTask, double timeout) - throw(InvalidParameterException, MsgException); + throw(InvalidArgumentException, MsgException); /* ! brief Host::sendBounded() - sends the given task to mailbox associated to the default alias * (capping the emission rate to maxRate). @@ -371,7 +385,7 @@ namespace SimGrid * \return If successful the task is sended to the default mailbox. Otherwise the * method throws one of the exceptions described below. * - * \exception [InvalidParameterException] if the maximum rate value is negative and different of + * \exception [InvalidArgumentException] if the maximum rate value is negative and different of * -1.0. * [MsgException] if an internal error occurs. * [BadAllocException] if there is not enough memory to allocate @@ -381,7 +395,7 @@ namespace SimGrid * */ void sendBounded(const Task& rTask, double maxRate) - throw(InvalidParameterException, BadAllocException, MsgException); + throw(InvalidArgumentException, BadAllocException, MsgException); /* ! brief Host::sendBounded() - sends the given task to mailbox identified by the parameter alias * (capping the emission rate to maxRate). @@ -393,7 +407,7 @@ namespace SimGrid * \return If successful the task is sended to the default mailbox. Otherwise the * method throws one of the exceptions described below. * - * \exception [InvalidParameterException] if the maximum rate value is negative and different of + * \exception [InvalidArgumentException] if the maximum rate value is negative and different of * -1.0 or if the alias parameter is invalid (NULL). * [MsgException] if an internal error occurs. * @@ -401,7 +415,7 @@ namespace SimGrid * */ void sendBounded(const char* alias, const Task& rTask, double maxRate) - throw(InvalidParameterException, MsgException); + throw(InvalidArgumentException, MsgException); protected: // Attributes. diff --git a/src/cxx/HostNotFoundException.cxx b/src/cxx/HostNotFoundException.cxx index c0dac52229..8f0c47674b 100644 --- a/src/cxx/HostNotFoundException.cxx +++ b/src/cxx/HostNotFoundException.cxx @@ -1,11 +1,9 @@ -#include "HostNotFoundException.hpp" +#include #include #include #include -using namespace std; - namespace SimGrid { namespace Msg diff --git a/src/cxx/HostNotFoundException.hpp b/src/cxx/HostNotFoundException.hpp index 25d3330521..17f4603050 100644 --- a/src/cxx/HostNotFoundException.hpp +++ b/src/cxx/HostNotFoundException.hpp @@ -13,14 +13,14 @@ #ifndef MSG_HOSTNOTFOUNDEXCEPTION_HPP #define MSG_HOSTNOTFOUNDEXCEPTION_HPP -#include "Exception.hpp" +#include namespace SimGrid { namespace Msg { - class HostNotFoundException : public Exception + class SIMGRIDX_EXPORT HostNotFoundException : public Exception { public: diff --git a/src/cxx/InvalidArgumentException.cxx b/src/cxx/InvalidArgumentException.cxx index ffc120830a..2500b8317b 100644 --- a/src/cxx/InvalidArgumentException.cxx +++ b/src/cxx/InvalidArgumentException.cxx @@ -1,11 +1,9 @@ -#include "InvalidArgumentException.hpp" +#include #include #include #include -using namespace std; - namespace SimGrid { namespace Msg diff --git a/src/cxx/InvalidArgumentException.hpp b/src/cxx/InvalidArgumentException.hpp index e81fdd406a..6c72f8ebb2 100644 --- a/src/cxx/InvalidArgumentException.hpp +++ b/src/cxx/InvalidArgumentException.hpp @@ -13,14 +13,14 @@ #ifndef MSG_INVALIDARGUMENTEXCEPTION_HPP #define MSG_INVALIDARGUMENTEXCEPTION_HPP -#include "Exception.hpp" +#include namespace SimGrid { namespace Msg { - class InvalidArgumentException : public Exception + class SIMGRIDX_EXPORT InvalidArgumentException : public Exception { public: diff --git a/src/cxx/LogicException.cxx b/src/cxx/LogicException.cxx index a7adcf2fd5..3cd0390e0c 100644 --- a/src/cxx/LogicException.cxx +++ b/src/cxx/LogicException.cxx @@ -1,11 +1,9 @@ -#include "LogicException.hpp" +#include #include #include #include -using namespace std; - namespace SimGrid { namespace Msg diff --git a/src/cxx/LogicException.hpp b/src/cxx/LogicException.hpp index 19d2f40f66..a39d7b9dfb 100644 --- a/src/cxx/LogicException.hpp +++ b/src/cxx/LogicException.hpp @@ -13,14 +13,14 @@ #ifndef MSG_LOGICEXCEPTION_HPP #define MSG_LOGICEXCEPTION_HPP -#include "Exception.hpp" +#include namespace SimGrid { namespace Msg { - class LogicException : public Exception + class SIMGRIDX_EXPORT LogicException : public Exception { public: diff --git a/src/cxx/Msg.cxx b/src/cxx/Msg.cxx index 94c7e8120e..20521cdad7 100644 --- a/src/cxx/Msg.cxx +++ b/src/cxx/Msg.cxx @@ -13,8 +13,15 @@ /* Msg functions implementation. */ + +#include #include +#include +#include + +// XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(SimGridX); + namespace SimGrid { namespace Msg @@ -35,7 +42,13 @@ namespace SimGrid void info(const char* s) { - INFO1("%s",s); + //INFO1("%s",s); + printf("[SimGridX/info] %s\n", s); + } + + double getClock(void) + { + return MSG_get_clock(); } } // namespace Msg diff --git a/src/cxx/Msg.hpp b/src/cxx/Msg.hpp index 61251569c1..3e26478018 100644 --- a/src/cxx/Msg.hpp +++ b/src/cxx/Msg.hpp @@ -20,35 +20,39 @@ #error Msg.hpp requires C++ compilation (use a .cxx suffix) #endif -#include +#include namespace SimGrid { namespace Msg { + class MsgException; /*! \brief init() - Initialize MSG (This function must be called at the begining of each simulation). * * \param argv A list of arguments. * \param argc The number of arguments of the list. */ - static void init(int argc, char** argv); + SIMGRIDX_EXPORT void init(int argc, char** argv); - /* \brief finalize() - Finalize MSG (This function must be called at the end of each simulation). + /*! \brief finalize() - Finalize MSG (This function must be called at the end of each simulation). * * \exception If this function fails, it throws a exception describing the cause of the failure. */ - static void finalize(void) + SIMGRIDX_EXPORT void finalize(void) throw (MsgException); /*! \brief info() - Display information (using xbt log format). * * \param s The information to display. */ - static void info(const char* s); - - - + SIMGRIDX_EXPORT void info(const char* s); + + /*! \brief getClock() - Retrieve the simulation time + * + * \return The current simulation time. + */ + SIMGRIDX_EXPORT double getClock(void); } // namespace Msg } // namespace SimGrid diff --git a/src/cxx/MsgException.cxx b/src/cxx/MsgException.cxx index 5b9bd4645e..cd1830838a 100644 --- a/src/cxx/MsgException.cxx +++ b/src/cxx/MsgException.cxx @@ -1,11 +1,9 @@ -#include "MsgException.hpp" +#include #include #include #include -using namespace std; - namespace SimGrid { namespace Msg diff --git a/src/cxx/MsgException.hpp b/src/cxx/MsgException.hpp index 55d151a1b0..a8f576b05c 100644 --- a/src/cxx/MsgException.hpp +++ b/src/cxx/MsgException.hpp @@ -13,14 +13,14 @@ #ifndef MSG_MSGEXCEPTION_HPP #define MSG_MSGEXCEPTION_HPP -#include "Exception.hpp" +#include namespace SimGrid { namespace Msg { - class MsgException : public Exception + class SIMGRIDX_EXPORT MsgException : public Exception { public: diff --git a/src/cxx/Object.cxx b/src/cxx/Object.cxx index d678d648ce..3d649a19e6 100644 --- a/src/cxx/Object.cxx +++ b/src/cxx/Object.cxx @@ -1,20 +1,24 @@ -#include +#include #include DeclaringClasses* DeclaringClass::declaringClasses = NULL; -namespace msg +namespace SimGrid { - // Generate static object constructor for class registration - void DeclareClass(Class* c) - { - MSG_DELCARING_CLASSES.lock(); - MSG_DELCARING_CLASSES.addHead(c); - MSG_DELCARING_CLASSES.unlock(); - } -} + namespace Msg + { + + // Generate static object constructor for class registration + void DeclareClass(Class* c) + { + MSG_DELCARING_CLASSES.lock(); + MSG_DELCARING_CLASSES.addHead(c); + MSG_DELCARING_CLASSES.unlock(); + } + } // namespace Msg +} // namespace SimGrid ////////////////////////////////////////////////////////////////////////////// // Implémentation des fonctions membre de la classe Class @@ -47,20 +51,18 @@ throw (ClassNotFoundException) for(cur = MSG_DELCARING_CLASSES.getHead(); cur; cur = cur->next) { if(!strcmp(name,cur->name)) - break; + return cur; + } MSG_DELCARING_CLASSES.unlock(); throw ClassNotFoundException(name); } + Object* Class::createObject(const char* name) { Class* c = fromName(name); - - if(NULL == c) - return NULL; - return c->createObject(); } @@ -146,5 +148,12 @@ bool DeclaringClasses::remove(Class* c) return success; } + +bool Object::isInstanceOf(const char* className) +{ + Class* c = Class::fromName(className); + + return this->getClass()->isDerivedFrom(c); +} diff --git a/src/cxx/Process.cxx b/src/cxx/Process.cxx index 0ee57edf9e..bceac3298d 100644 --- a/src/cxx/Process.cxx +++ b/src/cxx/Process.cxx @@ -1,11 +1,33 @@ #include +#include +#include +#include +#include +#include +#include +#include + + +#include +#include +#include + +#include +#include + +#include +#include +#include + + namespace SimGrid { namespace Msg { - Process* Process::currentProcess = NULL; - + + MSG_IMPLEMENT_DYNAMIC(Process, Object); + // Default constructor. Process::Process() { @@ -46,11 +68,8 @@ namespace SimGrid if(!name) throw NullPointerException("name"); - if(!hostName) - throw NullPointerException("hostName"); - if(argc < 0) - throw InvalidArgument("argc (must be positive)"); + throw InvalidArgumentException("argc (must be positive)"); if(!argc && argv) throw LogicException("argv is not NULL but argc is zero"); @@ -61,7 +80,7 @@ namespace SimGrid create(rHost, name, argc, argv); } - Process::Process(const char* hostname, const char* name, int argc, char** argv) + Process::Process(const char* hostName, const char* name, int argc, char** argv) throw(NullPointerException, InvalidArgumentException, LogicException, HostNotFoundException) { // check the parameters @@ -73,7 +92,7 @@ namespace SimGrid throw NullPointerException("hostName"); if(argc < 0) - throw InvalidArgument("argc (must be positive)"); + throw InvalidArgumentException("argc (must be positive)"); if(!argc && argv) throw LogicException("argv is not NULL but argc is zero"); @@ -81,7 +100,7 @@ namespace SimGrid if(argc && !argv) throw LogicException("argv is NULL but argc is not zero"); - Host host = Host::getByName(hostname); + Host host = Host::getByName(hostName); create(host, name, argc, argv); } @@ -105,9 +124,9 @@ namespace SimGrid throw MsgException("MSG_process_resume() failed"); } - bool Process::isSuspended(void) + int Process::isSuspended(void) { - return (bool)MSG_process_is_suspended(nativeProcess); + return MSG_process_is_suspended(nativeProcess); } Host& Process::getHost(void) @@ -187,13 +206,13 @@ namespace SimGrid void Process::migrate(const Host& rHost) throw(MsgException) { - if(MSG_OK != MSG_process_change_host(nativeProcess, rHost.nativeHost)) + if(MSG_OK != MSG_process_change_host(rHost.nativeHost)) throw MsgException("MSG_process_change_host()"); } void Process::sleep(double seconds) - throw(throw(InvalidArgumentException, MsgException)) + throw(InvalidArgumentException, MsgException) { // check the parameters. if(seconds <= 0) @@ -328,7 +347,7 @@ namespace SimGrid void Process::sendTask(const Task& rTask) throw(BadAllocException, MsgException) { - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc( strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -351,7 +370,7 @@ namespace SimGrid if(timeout < 0 && timeout !=-1.0) throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)"); - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2 , sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -387,7 +406,7 @@ namespace SimGrid throw(BadAllocException, MsgException) { - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -436,7 +455,7 @@ namespace SimGrid throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)"); - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -484,7 +503,7 @@ namespace SimGrid if(timeout < 0 && timeout !=-1.0) throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)"); - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -524,7 +543,7 @@ namespace SimGrid Task& Process::receiveTask(const Host& rHost) throw(BadAllocException, MsgException) { - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -543,7 +562,7 @@ namespace SimGrid return (*((Task*)nativeTask->data)); } - void Process::create(const Host& rHost, const char* name, int argc, char** argv) + /*void Process::create(const Host& rHost, const char* name, int argc, char** argv) throw(HostNotFoundException) { smx_process_t nativeCurrentProcess = NULL; @@ -561,7 +580,7 @@ namespace SimGrid throw HostNotFoundException(rHost.getName()); // realloc the list of the argument to add the pointer to this process instance at the end - argv = (char**)realloc(argc + 1, sizeo(char*)); + argv = (char**)realloc(argc + 1, sizeof(char*)); // add the pointer to this instance at the end of the list of the arguments of the process // so the static method Process::run() (passed as argument of the MSG function xbt_context_new()) @@ -580,7 +599,7 @@ namespace SimGrid // create the context of the process. simdata->context = xbt_context_new(name, Process::run, NULL, NULL, simix_global->cleanup_process_function, nativeProcess, simdata->argc, simdata->argv); - /* Process structure */ + // Process structure this->nativeProcess->name = xbt_strdup(name); this->nativeProcess->simdata = simdata; @@ -592,7 +611,7 @@ namespace SimGrid xbt_swag_insert(this->nativeProcess, nativeHost->simdata->process_list); - /* fix current_process, about which xbt_context_start mocks around */ + // fix current_process, about which xbt_context_start mocks around nativeCurrentProcess = simix_global->current_process; xbt_context_start(this->nativeProcess->simdata->context); simix_global->current_process = nativeCurrentProcess; @@ -600,11 +619,79 @@ namespace SimGrid xbt_swag_insert(this->nativeProcess, simix_global->process_list); DEBUG2("Inserting %s(%s) in the to_run list", this->nativeProcess->name, nativeHost->name); xbt_swag_insert(this->nativeProcess, simix_global->process_to_run); + }*/ + + void Process::create(const Host& rHost, const char* name, int argc, char** argv) + throw(InvalidArgumentException) + { + char alias[MAX_ALIAS_NAME + 1] = {0}; + msg_mailbox_t mailbox; + + + // try to retrieve the host where to createt the process from its name + m_host_t nativeHost = rHost.nativeHost; + + if(!nativeHost) + throw InvalidArgumentException("rHost"); + + /* allocate the data of the simulation */ + this->nativeProcess = xbt_new0(s_m_process_t,1); + this->nativeProcess->simdata = xbt_new0(s_simdata_process_t,1); + this->nativeProcess->name = _strdup(name); + this->nativeProcess->simdata->m_host = nativeHost; + this->nativeProcess->simdata->PID = msg_global->PID++; + + // realloc the list of the argument to add the pointer to this process instance at the end + if(argc) + argv = (char**)realloc(argv , (argc + 1) * sizeof(char*)); + else + argv = (char**)calloc(1 ,sizeof(char*)); + + // add the pointer to this instance at the end of the list of the arguments of the process + // so the static method Process::run() (passed as argument of the MSG function xbt_context_new()) + // can retrieve the concerned process object by the run + // so Process::run() can call the method main() of the good process + // for more detail see Process::run() method + argv[argc] = (char*)this; + + this->nativeProcess->simdata->argc = argc; + this->nativeProcess->simdata->argv = argv; + + this->nativeProcess->simdata->s_process = SIMIX_process_create( + this->nativeProcess->name, + Process::run, + (void*)this->nativeProcess, + nativeHost->name, + argc, + argv, + NULL); + + if (SIMIX_process_self()) + {/* someone created me */ + this->nativeProcess->simdata->PPID = MSG_process_get_PID((m_process_t)SIMIX_process_self()->data); + } + else + { + this->nativeProcess->simdata->PPID = -1; + } + + this->nativeProcess->simdata->last_errno = MSG_OK; + + /* add the process to the list of the processes of the simulation */ + xbt_fifo_unshift(msg_global->process_list, this->nativeProcess); + + sprintf(alias,"%s:%s",(this->nativeProcess->simdata->m_host->simdata->smx_host)->name,this->nativeProcess->name); + + mailbox = MSG_mailbox_new(alias); + + MSG_mailbox_set_hostname(mailbox, this->nativeProcess->simdata->m_host->simdata->smx_host->name); + + } - Process& Process::fromNativeProcess(m_process_t nativeProcess) + Process* Process::fromNativeProcess(m_process_t nativeProcess) { - return (*((Process*)(nativeProcess->simdata->arg[nativeProcess->argc]))); + return ((Process*)(nativeProcess->simdata->argv[nativeProcess->simdata->argc])); } int Process::run(int argc, char** argv) @@ -614,6 +701,11 @@ namespace SimGrid // for mor detail see Process::create() method return ((Process*)argv[argc])->main(argc, argv); } + + int Process::main(int argc, char** argv) + { + throw LogicException("Process::main() not implemented"); + } } // namespace Msg diff --git a/src/cxx/Process.hpp b/src/cxx/Process.hpp index 10816ec469..bd45c70113 100644 --- a/src/cxx/Process.hpp +++ b/src/cxx/Process.hpp @@ -18,22 +18,39 @@ #error Process.hpp requires C++ compilation (use a .cxx suffix) #endif +#include + +#include +#include namespace SimGrid { namespace Msg { + class NullPointerException; + class HostNotFoundException; + class ProcessNotFoundException; + class InvalidArgumentException; + class BadAllocException; + class LogicException; + class MsgException; + + class ApplicationHandler; + class Host; + class Task; + // SimGrid::Msg::Process class declaration. - class Process + class SIMGRIDX_EXPORT Process : public Object { - friend ApplicationHandler; - private; + friend ApplicationHandler::ProcessFactory; + MSG_DECLARE_DYNAMIC(Process); + + public: // Disable the default constructor. Process(); - public: /*! \brief Constructs a process from the name of the host and its name. * @@ -143,10 +160,10 @@ namespace SimGrid /*! \brief Process::isSuspend() - Tests if a process is suspended. * - * \return This method returns true is the process is suspended. - * Otherwise the method returns false. + * \return This method returns 1 is the process is suspended. + * Otherwise the method returns 0. */ - bool isSuspended(void); + int isSuspended(void); /*! \brief Process::getHost() - Retrieves the host of a process object. * @@ -333,7 +350,7 @@ namespace SimGrid * \remark Set the timeout with -1.0 to disable it. */ Task& getTask(int channel, double timeout, const Host& rHost) - throw(InvalidArgumentException MsgException); + throw(InvalidArgumentException, MsgException); /*! \brief Process::sendTask() - Sends the given task in the mailbox identified by the specified alias * (waiting at most given time). @@ -498,7 +515,7 @@ namespace SimGrid * * [MsgException] if an internal exception occurs. */ - Task& Process::receiveTask(double timeout, const Host& rHost) + Task& receiveTask(double timeout, const Host& rHost) throw(BadAllocException, InvalidArgumentException, MsgException); /*! \brief Process::receiveTask() - Retrieves a task from the mailbox identified by the alias @@ -555,7 +572,7 @@ namespace SimGrid * * \return The wrapper associated with the native process specified as parameter. */ - static Process& fromNativeProcess(m_process_t nativeProcess); + static Process* fromNativeProcess(m_process_t nativeProcess); public: @@ -572,7 +589,7 @@ namespace SimGrid * * \return The exit code of the main function. */ - virtual int main(int argc, char** argv) = 0; + virtual int main(int argc, char** argv); private: @@ -584,4 +601,6 @@ namespace SimGrid } //namepace Msg -} namespace SimGrid \ No newline at end of file +} // namespace SimGrid + +#endif // !MSG_PROCESS_HPP \ No newline at end of file diff --git a/src/cxx/Simulation.cxx b/src/cxx/Simulation.cxx index b1fd89e32a..34ee0fd20b 100644 --- a/src/cxx/Simulation.cxx +++ b/src/cxx/Simulation.cxx @@ -15,12 +15,20 @@ #include +#include +#include + +#include +#include + +#include + +#include + namespace SimGrid { namespace Msg { - - int Simulation::execute(int argc, char** argv) { if(argc < 3) @@ -55,6 +63,7 @@ namespace SimGrid try { app.deploy(argv[2]); + } catch(FileNotFoundException e) { info(e.toString()); @@ -65,7 +74,7 @@ namespace SimGrid //try to run the simulation the given application on the given environment try { - this->run(); + run(); } catch(MsgException e) { @@ -81,7 +90,7 @@ namespace SimGrid } catch(MsgException e) { - info(e.toString()) + info(e.toString()); return 1; } diff --git a/src/cxx/Simulation.hpp b/src/cxx/Simulation.hpp index 00f4ab1c64..de97d90e0c 100644 --- a/src/cxx/Simulation.hpp +++ b/src/cxx/Simulation.hpp @@ -19,20 +19,25 @@ #error Sumulation.hpp requires C++ compilation (use a .cxx suffix) #endif +#include + namespace SimGrid { namespace Msg { + class MsgException; + class FileNotFoundException; + // Simulation class declaration. - class Simulation + class SIMGRIDX_EXPORT Simulation { public : - Simulation(); + Simulation(){}; Simulation(const Simulation& rSimulation); - virtual ~Simulation(); + virtual ~Simulation(){}; // Operations. diff --git a/src/cxx/Task.cxx b/src/cxx/Task.cxx index c9ef4f0a34..b0c2ccbda3 100644 --- a/src/cxx/Task.cxx +++ b/src/cxx/Task.cxx @@ -1,9 +1,25 @@ #include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include + namespace SimGrid { namespace Msg { + + MSG_IMPLEMENT_DYNAMIC(Task, Object); Task::Task() { @@ -13,11 +29,12 @@ namespace SimGrid Task::Task(const Task& rTask) { + this->nativeTask = rTask.nativeTask; } Task::~Task() - throw(NativeException) + throw(MsgException) { if(NULL != nativeTask) if(MSG_OK != MSG_task_destroy(nativeTask)) @@ -28,7 +45,7 @@ namespace SimGrid Task::Task(const char* name, double computeDuration, double messageSize) throw(InvalidArgumentException, NullPointerException) { - + if(computeDuration < 0) throw InvalidArgumentException("computeDuration"); @@ -47,7 +64,6 @@ namespace SimGrid Task::Task(const char* name, Host* hosts, double* computeDurations, double* messageSizes, int hostCount) throw(NullPointerException, InvalidArgumentException) { - // check the parameters if(!name) @@ -91,7 +107,7 @@ namespace SimGrid - task->data = (void*)this; + this->nativeTask->data = (void*)this; } @@ -102,7 +118,7 @@ namespace SimGrid Process& Task::getSender(void) const { - m_proccess_t nativeProcess = MSG_task_get_sender(nativeTask); + m_process_t nativeProcess = MSG_task_get_sender(nativeTask); return (*((Process*)(nativeProcess->data))); } @@ -135,7 +151,7 @@ namespace SimGrid MSG_task_set_priority(nativeTask, priority); } - Task& Task::get(int channel) + /*Task& Task::get(int channel) throw(InvalidArgumentException, MsgException) { // check the parameters @@ -149,6 +165,22 @@ namespace SimGrid throw MsgException("MSG_task_get_ext() failed"); return (*((Task*)(nativeTask->data))); + }*/ + + Task* Task::get(int channel) + throw(InvalidArgumentException, MsgException) + { + // check the parameters + + if(channel < 0) + throw InvalidArgumentException("channel (must not be negative)"); + + m_task_t nativeTask = NULL; + + if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , -1.0, NULL)) + throw MsgException("MSG_task_get_ext() failed"); + + return ((Task*)(nativeTask->data)); } Task& Task::get(int channel, const Host& rHost) @@ -188,7 +220,7 @@ namespace SimGrid return (*((Task*)(nativeTask->data))); } - bool static Task::probe(int channel) + int Task::probe(int channel) throw(InvalidArgumentException) { // check the parameters @@ -196,7 +228,7 @@ namespace SimGrid if(channel < 0) throw InvalidArgumentException("channel (must not be negative)"); - return (bool)MSG_task_Iprobe(channel); + return MSG_task_Iprobe(channel); } int Task::probe(int channel, const Host& rHost) @@ -207,7 +239,7 @@ namespace SimGrid if(channel < 0) throw InvalidArgumentException("channel (must not be negative)"); - return MSG_task_probe_from_host(chan_id,rHost.nativeHost); + return MSG_task_probe_from_host(channel,rHost.nativeHost); } void Task::execute(void) @@ -227,16 +259,16 @@ namespace SimGrid void Task::send(void) throw(BadAllocException, MsgException) { - char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2); + char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); - sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName()); + sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName()); MSG_error_t rv = MSG_task_send_with_timeout(nativeTask, alias, -1.0); - free(alias) + free(alias); if(MSG_OK != rv) throw MsgException("MSG_task_send_with_timeout() failed"); @@ -255,23 +287,23 @@ namespace SimGrid } void Task::send(double timeout) - throw(BadAllocationException, InvalidArgumentException, MsgException) + throw(BadAllocException, InvalidArgumentException, MsgException) { // check the parameters if(timeout < 0 && timeout != -1.0) throw InvalidArgumentException("timeout (must not be negative and different than -1.0"); - char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2); + char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); - sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName()); + sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName()); MSG_error_t rv = MSG_task_send_with_timeout(nativeTask, alias, timeout); - free(alias) + free(alias); if(MSG_OK != rv) throw MsgException("MSG_task_send_with_timeout() failed"); @@ -301,12 +333,12 @@ namespace SimGrid if(maxRate < 0 && maxRate != -1.0) throw InvalidArgumentException("maxRate (must not be negative and different than -1.0"); - char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2); + char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); - sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName()); + sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName()); MSG_error_t rv = MSG_task_send_bounded(nativeTask, alias, maxRate); @@ -332,15 +364,15 @@ namespace SimGrid throw MsgException("MSG_task_send_bounded() failed"); } - Task& Task::receive(void) - throw(throw(BadAllocException, MsgException)) + /*Task& Task::receive(void) + throw(BadAllocException, MsgException) { - char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2); + char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); - sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName()); + sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName()); m_task_t nativeTask = NULL; @@ -352,9 +384,31 @@ namespace SimGrid throw MsgException("MSG_task_receive_ext() failed"); return (*((Task*)nativeTask->data)); + }*/ + + Task* Task::receive(void) + throw(BadAllocException, MsgException) + { + char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char)); + + if(!alias) + throw BadAllocException("alias"); + + sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName()); + + m_task_t nativeTask = NULL; + + MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL); + + free(alias); + + if(MSG_OK != rv) + throw MsgException("MSG_task_receive_ext() failed"); + + return ((Task*)nativeTask->data); } - Task& Task::receive(const char* alias) + /*Task& Task::receive(const char* alias) throw(NullPointerException, MsgException) { // check the parameters @@ -368,6 +422,22 @@ namespace SimGrid throw MsgException("MSG_task_receive_ext() failed"); return (*((Task*)nativeTask->data)); + }*/ + + Task* Task::receive(const char* alias) + throw(NullPointerException, MsgException) + { + // check the parameters + + if(!alias) + throw NullPointerException("alias"); + + m_task_t nativeTask = NULL; + + if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL)) + throw MsgException("MSG_task_receive_ext() failed"); + + return ((Task*)nativeTask->data); } Task& Task::receive(const char* alias, double timeout) @@ -378,7 +448,7 @@ namespace SimGrid if(!alias) throw NullPointerException("alias"); - if(timeout < 0 && alias != -1.0) + if(timeout < 0 && timeout != -1.0) throw InvalidArgumentException("timeout (must not be negative and differnt than -1.0)"); m_task_t nativeTask = NULL; @@ -413,7 +483,7 @@ namespace SimGrid if(!alias) throw NullPointerException("alias"); - if(timeout < 0 && alias != -1.0) + if(timeout < 0 && timeout != -1.0) throw InvalidArgumentException("timeout (must not be negative and differnt than -1.0)"); m_task_t nativeTask = NULL; @@ -425,24 +495,24 @@ namespace SimGrid return (*((Task*)nativeTask->data)); } - bool Task::listen(void) + int Task::listen(void) throw(BadAllocException) { - char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2); + char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); - sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName()); + sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName()); int rv = MSG_task_listen(alias); free(alias); - return (bool)rv; + return rv; } - bool Task::listen(const char* alias) + int Task::listen(const char* alias) throw(NullPointerException) { // check the parameters @@ -450,18 +520,18 @@ namespace SimGrid if(!alias) throw NullPointerException("alias"); - return (bool)MSG_task_listen(alias); + return MSG_task_listen(alias); } int Task::listenFrom(void) throw(BadAllocException) { - char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2); + char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); - sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName()); + sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName()); int rv = MSG_task_listen_from(alias); @@ -483,12 +553,12 @@ namespace SimGrid int Task::listenFromHost(const Host& rHost) throw(BadAllocException) { - char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2); + char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); - sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName()); + sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName()); int rv = MSG_task_listen_from_host(alias, rHost.nativeHost); @@ -506,6 +576,12 @@ namespace SimGrid return MSG_task_listen_from_host(alias, rHost.nativeHost); } + + const Task& Task::operator = (const Task& rTask) + { + this->nativeTask = rTask.nativeTask; + return *this; + } } // namespace Msg } // namespace SimGrid diff --git a/src/cxx/Task.hpp b/src/cxx/Task.hpp index 143f66834e..a158f4f29e 100644 --- a/src/cxx/Task.hpp +++ b/src/cxx/Task.hpp @@ -17,19 +17,66 @@ // Compilation C++ recquise #ifndef __cplusplus - #error Process.hpp requires C++ compilation (use a .cxx suffix) + #error Task.hpp requires C++ compilation (use a .cxx suffix) #endif +#include + +#include + +#include + namespace SimGrid { namespace Msg { + class MsgException; + class InvalidArgumentException; + class NullPointerException; + class MsgException; + class BadAllocException; + + class Process; + class Host; + // SimGrid::Msg::Task wrapper class declaration. - class Task + class SIMGRIDX_EXPORT Task : public Object { + MSG_DECLARE_DYNAMIC(Task); + + friend Process; + friend Host; + protected: // Default constructor. Task(); + + class Ref + { + public: + Ref(Task* task) + { + count = 1; + this->task = task; + } + + virtual ~Ref(){} + + void operator++(void){ + count++; + + } + + void operator--(void){ + if(--count <= 0) + delete p; + } + + private: + int count; + Task* task; + + }; public: /*! \brief Copy constructor. @@ -144,9 +191,12 @@ namespace SimGrid * * [MsgException] if an internal excpetion occurs. */ - static Task& get(int channel) - throw(InvalidArgumentException, MsgException); - + /*static Task& get(int channel) + throw(InvalidArgumentException, MsgException);*/ + + static Task* get(int channel) + throw(InvalidArgumentException, MsgException); + /*! \brief Task::get() - Gets a task from the given channel number of the given host. * * \param channel The channel number. @@ -184,14 +234,14 @@ namespace SimGrid * * \param channel The channel number. * - * \return If there is a waiting task on the channel the method returns true. Otherwise - * the method returns false. + * \return If there is a waiting task on the channel the method returns 1. Otherwise + * the method returns 0. * * \exception If this method fails, it throws the exception described below: * * [InvalidArgumentException] if the parameter channel is negative. */ - static bool probe(int channel) + static int probe(int channel) throw(InvalidArgumentException); /*! \brief Task::probe() - Counts tasks waiting on the given channel of local host and sent by given host. @@ -266,7 +316,7 @@ namespace SimGrid * [MsgException] if an internal exception occurs. */ void send(double timeout) - throw(BadAllocationException, InvalidArgumentException, MsgException); + throw(BadAllocException, InvalidArgumentException, MsgException); /*! \brief Task::send() - Send the task on the mailbox identified by a given alias * (waiting at most given time). @@ -333,7 +383,10 @@ namespace SimGrid * * [MsgException] if an internal exception occurs. */ - static Task& receive(void) + /*static Task& receive(void) + throw(BadAllocException, MsgException);*/ + + static Task* receive(void) throw(BadAllocException, MsgException); /*! \brief Task::receive() - Receives a task from the mailbox identified by a given alias (located @@ -349,7 +402,9 @@ namespace SimGrid * * [MsgException] if an internal exception occurs. */ - static Task& receive(const char* alias) + /*static Task& receive(const char* alias) + throw(NullPointerException, MsgException);*/ + static Task* receive(const char* alias) throw(NullPointerException, MsgException); /*! \brief Task::receive() - Receives a task from the mailbox identified by a given alias (located @@ -420,7 +475,7 @@ namespace SimGrid * * [BadAllocException] if there is not enough memory to build the default alias. */ - static bool listen(void) + static int listen(void) throw(BadAllocException); /*! \brief Task::listen() - Listen whether there is a waiting task on the mailbox @@ -428,14 +483,14 @@ namespace SimGrid * * \param alias The alias of the mailbox. * - * \return If there is a waiting task on the mailbox the method returns true. - * Otherwise the method returns false. + * \return If there is a waiting task on the mailbox the method returns 1. + * Otherwise the method returns 0. * * \exception If this method fails, it throws one of the exceptions described below: * * [NullPointerException] if the parameter alias is NULL. */ - static bool listen(const char* alias) + static int listen(const char* alias) throw(NullPointerException); /*! \brief Task::listenFrom() - Tests whether there is a pending communication on the mailbox @@ -496,17 +551,23 @@ namespace SimGrid * [BadAllocException] if there is not enough memory to build the default * alias. */ - static bool listenFromHost(const char* alias, const Host& rHost) - throw(NullPointerException, NativeException); + static int listenFromHost(const char* alias, const Host& rHost) + throw(NullPointerException, MsgException); + + virtual const Task& operator= (const Task& rTask); - private: + protected: // Attributes. - m_task_t nativeTask; // the native MSG task. + m_task_t nativeTask; // the native MSG task. + + Ref* ref; }; } // namespace Msg } // namespace SimGrid +typedef Task* TaskPtr; + #endif // §MSG_TASK_HPP \ No newline at end of file -- 2.20.1