From e3291ac14eb4c7c8a7fc3db17f6bc0916e160916 Mon Sep 17 00:00:00 2001 From: cherierm Date: Fri, 16 May 2008 09:52:21 +0000 Subject: [PATCH 1/1] Add comment and exception mechanism to the Host class. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5432 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/cxx/Host.cxx | 580 +++++++++++++++++++++++++---------------------- src/cxx/Host.hpp | 210 +++++++++++++++-- 2 files changed, 500 insertions(+), 290 deletions(-) diff --git a/src/cxx/Host.cxx b/src/cxx/Host.cxx index 8cb4a08e35..07ec58c57a 100644 --- a/src/cxx/Host.cxx +++ b/src/cxx/Host.cxx @@ -1,289 +1,329 @@ +/* + * Host.cxx + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + *(GNU LGPL) which comes with this package. + * + */ + + /* Host class member functions implementation. + */ #include namespace SimGrid { -namespace Msg -{ - - -Host::Host() -{ - nativeHost = NULL; - data = NULL; -} - -Host::Host(const Host& rHost) -{ - this->nativeHost = rHost.nativeHost; - this->data = rHost.getData(); -} - -Host::~Host() -{ - // NOTHING TODO -} - - -Host& Host::getByName(const char* hostName) -throw(HostNotFoundException, InvalidParameterException, BadAllocException) -{ - // check the parameters - if(!hostName) - throw InvalidParmeterException("hostName"); + namespace Msg + { + Host::Host() + { + nativeHost = NULL; + data = NULL; + } - m_host_t nativeHost = NULL; // native host. - Host* host = NULL; // wrapper host. - - if(!(nativeHost = MSG_get_host_by_name(hostName))) - throw HostNotFoundException(hostName); - - if(!nativeHost->data) - { // native host not associated yet with its wrapper - - // instanciate a new wrapper - if(!(host = new Host()) - throw BadAllocException(hostName); + Host::Host(const Host& rHost) + { + this->nativeHost = rHost.nativeHost; + this->data = rHost.getData(); + } - host->nativeHost = nativeHost; - - // the native host data field is set with its wrapper returned - nativeHost->data = (void*)host; - } - - // return the reference to cxx wrapper - return *((Host*)nativeHost->data); -} - -int Host::getNumber(void) -{ - return MSG_get_host_number(); -} - -Host& Host::currentHost(void) -{ - Host* host = NULL; - m_host_t nativeHost = MSG_host_self(); - - if(!nativeHost->data) - { - // the native host not yet associated with its wrapper - - // instanciate a new wrapper - host = new Host(); - - host->nativeHost = nativeHost; - - nativeHost->data = (void*)host; - } - else - { - host = (Host*)nativeHost->data; - } - - return *host; -} - -void Host::all(Host*** hosts, int* len) -throw(InvalidParameterException, BadAllocException) -{ - /* check the parameters */ - if(!hosts) - throw InvalidParameterException("hosts"); + Host::~Host() + { + // NOTHING TODO + } - if(len < 0) - throw InvalidParameterException("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)"); - - int index; - m_host_t nativeHost; - Host* host; - - m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host); - - for(index = 0; index < count; index++) - { - nativeHost = table[index]; - host = (Host*)(nativeHost->data); - - if(!host) + + Host& Host::getByName(const char* hostName) + throw(HostNotFoundException, InvalidParameterException, BadAllocException) { - if(!(host = new Host()) - { - // release all allocated memory. - for(int i = 0; i < index; i++) - delete (*(hosts)[i]); + // check the parameters + if(!hostName) + throw InvalidParmeterException("hostName"); + + m_host_t nativeHost = NULL; // native host. + Host* host = NULL; // wrapper host. + + if(!(nativeHost = MSG_get_host_by_name(hostName))) + throw HostNotFoundException(hostName); + + if(!nativeHost->data) + { // native host not associated yet with its wrapper + + // instanciate a new wrapper + if(!(host = new Host()) + throw BadAllocException(hostName); - throw BadAllocException("to fill the table of the hosts installed on your platform"); + host->nativeHost = nativeHost; + + // the native host data field is set with its wrapper returned + nativeHost->data = (void*)host; } - host->nativeHost = nativeHost; - nativeHost->data = (void*)host; + // return the reference to cxx wrapper + return *((Host*)nativeHost->data); } - (*hosts)[index] = host; - } - - *len = count; -} - - -const char* Host::getName(void) const -{ - return nativeHost->name; -} - - -void Host::setData(void* data) -{ - this->data = data; -} - -// Getters/setters - -void* Host::getData(void) const -{ - return this->data; -} - -int Host::getRunningTaskNumber(void) const -{ - return MSG_get_host_msgload(nativeHost); -} - -double Host::getSpeed(void) const -{ - return MSG_get_host_speed(nativeHost->simdata->smx_host); -} - -bool Host::hasData(void) const -{ - return (NULL != this->data); -} - -bool Host::isAvailable(void) const -{ - return (bool)SIMIX_host_get_state(nativeHost->simdata->smx_host); -} - -void Host::put(int channel, const Task& rTask) -throw(NativeException) -{ - if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, nativeHost, channel , -1.0)) - { - // TODO throw NativeException - } -} - -void Host::put(int channel, const Task& rTask, double timeout) -throw(NativeException) -{ - if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, nativeHost, channel , timeout)) - { - // TODO throw NativeException - } -} - -void Host::putBounded(int channel, const Task& rTask, double maxRate) -throw(NativeException) -{ - MsgNative.hostPutBounded(this, channel, task, maxrate); - - if(MSG_OK != MSG_task_put_bounded(rTask.nativeTask, nativeHost, channel, maxRate)) - { - // TODO throw NativeException - } -} - -void Host::send(const Task& rTask) -throw(NativeException) -{ - MSG_error_t rv; - - char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2); - sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName()); + int Host::getNumber(void) + { + return MSG_get_host_number(); + } + Host& Host::currentHost(void) + { + Host* host = NULL; + m_host_t nativeHost = MSG_host_self(); + + if(!nativeHost->data) + { + // the native host not yet associated with its wrapper + + // instanciate a new wrapper + host = new Host(); + + host->nativeHost = nativeHost; + + nativeHost->data = (void*)host; + } + else + { + host = (Host*)nativeHost->data; + } + + return *host; + } - rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, -1.0); - - free(alias); - - if(MSG_OK != rv) - { - // TODO throw NativeException - } -} - -void Host::send(const char* alias, const Task& rTask) -throw(NativeException) -{ - - if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, -1.0)) - { - // TODO throw NativeException - } -} - -void Host::send(const Task& rTask, double timeout) -throw(NativeException) -{ - MSG_error_t rv; - - char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2); - sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName()); + void Host::all(Host*** hosts, int* len) + throw(InvalidParameterException, BadAllocException) + { + /* check the parameters */ + if(!hosts) + throw InvalidParameterException("hosts"); + + if(len < 0) + throw InvalidParameterException("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)"); + + int index; + m_host_t nativeHost; + Host* host; + m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host); + + for(index = 0; index < count; index++) + { + nativeHost = table[index]; + host = (Host*)(nativeHost->data); + + if(!host) + { + if(!(host = new Host()) + { + // release all allocated memory. + for(int i = 0; i < index; i++) + delete (*(hosts)[i]); + + throw BadAllocException("to fill the table of the hosts installed on your platform"); + } + + host->nativeHost = nativeHost; + nativeHost->data = (void*)host; + } + + (*hosts)[index] = host; + } - rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout); - - free(alias); - - if(MSG_OK != rv) - { - // TODO throw NativeException - } -} - -void Host::send(const char* alias, const Task& rTask, double timeout) -throw(NativeException) -{ - if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout)) - { - // TODO throw NativeException - } -} - - -void Host::sendBounded(const Task& rTask, double maxRate) -throw(NativeException) -{ - - MSG_error_t rv; - - char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2); - sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName()); + *len = count; + } - rv = MSG_task_send_bounded(rTask.nativeTask, alias, maxRate); - - free(alias); - - if(MSG_OK != rv) - { - // TODO throw NativeException - } -} - -void Host::sendBounded(const char* alias, const Task& rTask, double maxRate) -throw(NativeException) -{ - if(MSG_OK != MSG_task_send_bounded(rTask.nativeTask, alias, maxRate)) - { - // TODO throw NativeException - } -} - - -} // namspace Msg + const char* Host::getName(void) const + { + return nativeHost->name; + } + + void Host::setData(void* data) + { + this->data = data; + } + + void* Host::getData(void) const + { + return this->data; + } + + int Host::getRunningTaskNumber(void) const + { + return MSG_get_host_msgload(nativeHost); + } + + double Host::getSpeed(void) const + { + return MSG_get_host_speed(nativeHost->simdata->smx_host); + } + + bool Host::hasData(void) const + { + return (NULL != this->data); + } + + bool Host::isAvailable(void) const + { + return (bool)SIMIX_host_get_state(nativeHost->simdata->smx_host); + } + + void Host::put(int channel, const Task& rTask) + throw(MsgException, InvalidParameterException) + { + // checks the parameters + if(channel < 0) + throw InvalidParameterException("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, const Task& rTask, double timeout) + throw(MsgException, InvalidParameterException) + { + // checks the parameters + if(channel < 0) + throw InvalidParameterException("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)"); + + + if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, nativeHost, channel , timeout)) + throw MsgException("MSG_task_put_with_timeout() failed"); + } + + void Host::putBounded(int channel, const Task& rTask, double maxRate) + throw(MsgException, InvalidParameterException) + { + // checks the parameters + if(channel < 0) + throw InvalidParameterException("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)"); + + 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) + { + MSG_error_t rv; + + char* alias = (char*)calloc(strlen(this->getName() + strlen(Process::currentProcess().getName()) + 2); + + if(!alias) + throw BadAllocException("alias"); + + sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName()); + + rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, -1.0); + + free(alias); + + if(MSG_OK != rv) + throw MsgException("MSG_task_send_with_timeout() failed"); + } + + void Host::send(const char* alias, const Task& rTask) + throw(InvalidParameterException, MsgException) + { + // check the parameters + if(!alias) + throw InvalidParmeterException("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) + { + // check the parameters + if(timeout < 0 && timeout != -1.0) + throw InvalidParameterException("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); + + if(!alias) + throw BadAllocException("alias"); + + sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName()); + + + rv = MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout); + + free(alias); + + if(MSG_OK != rv) + throw MsgException("MSG_task_send_with_timeout() failed"); + } + + void Host::send(const char* alias, const Task& rTask, double timeout) + throw(InvalidParameterException, MsgException) + { + // check the parameter + + if(!alias) + throw InvalidParmeterException("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)"); + + if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias, timeout)) + throw MsgException("MSG_task_send_with_timeout() failed"); + } + + + void Host::sendBounded(const Task& rTask, double maxRate) + throw(InvalidParameterException, BadAllocException, MsgException) + { + if(maxRate < 0 && maxRate != -1.0) + throw InvalidParameterException("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); + + if(!alias) + throw BadAllocException("alias"); + + sprintf(alias,"%s:%s", this->getName(),Process::currentProcess().getName()); + + rv = MSG_task_send_bounded(rTask.nativeTask, alias, maxRate); + + free(alias); + + if(MSG_OK != rv) + throw MsgException("MSG_task_send_bounded() failed"); + } + + void Host::sendBounded(const char* alias, const Task& rTask, double maxRate) + throw(InvalidParameterException, MsgException) + { + // check the parameters + if(!alias) + throw InvalidParameterException("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)"); + + 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 e8b2854e54..53ca4df5a3 100644 --- a/src/cxx/Host.hpp +++ b/src/cxx/Host.hpp @@ -55,6 +55,7 @@ catch(HostNotFoundException e) #include #include #include +#include // namespace SimGrid::Msg namespace SimGrid @@ -62,7 +63,7 @@ namespace SimGrid namespace Msg { // Declaration of the class SimGrid::Msg::Host. - class Host + class Host // final class. { // Desable the default constructor. // The best way to get an host instance is to use the static method Host::getByName(). @@ -185,53 +186,222 @@ namespace SimGrid */ static void all(Host*** hosts /*in|out*/, int* len /*in|out*/); - + /*! \brief Host::getName() - This method return the name of the Msg host object. + * + * \return The name of the host object. + */ const char* getName(void) const; + /*! \brief Host::setData() - Set the user data of an host object. + * + * \param data The user data to set. + */ void setData(void* data); - // Getters/setters - + /*! \brief Host::getData() - Get the user data of a host object. + * + * \return The user data of the host object. + */ void* getData(void) const; - int Host::getRunningTaskNumber(void) const; + /*! \brief Host::hasData() - Test if an host object has some data. + * + * \return This method returns true if the host object has some user data. + * Otherwise the method returns false. + */ + bool hasData(void) const; - double getSpeed(void) const; + /*! \brief Host::getRunningTaskNumber() - returns the number of tasks currently running on a host. + * + * \return The number of task currently running of the host object. + * + * \remark The external load is not taken in account. + */ + int getRunningTaskNumber(void) const; - bool hasData(void) const; + /*! \brief Host::getSpeed() - returns the speed of the processor of a host, + * regardless of the current load of the machine. + * + * \return The speed of the processor of the host in flops. + */ + double getSpeed(void) const; + /*! \brief Host::isAvailable - tests if an host is availabled. + * + * \return Is the host is availabled the method returns + * true. Otherwise the method returns false. + */ bool isAvailble(void) const; + /* ! \brief Host::put() - put a task on the given channel of a host . + * + * \param channel The channel where to put the task. + * \param rTask A refercence to the task object containing the native task to + * put on the channel specified by the parameter channel. + * + * \return If successful the task is puted on the specified channel. Otherwise + * 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 + * parameter is negative. + */ void put(int channel, const Task& rTask) - throw(NativeException); + throw(MsgException, InvalidParameterException); + /* ! \brief Host::put() - put a task on the given channel of a host object (waiting at most timeout seconds). + * + * \param channel The channel where to put the task. + * \param rTask A refercence to the task object containing the native task to + * put on the channel specified by the parameter channel. + * \param timeout The timeout in seconds. + * + * \return If successful the task is puted on the specified channel. Otherwise + * 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 + * 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(NativeException); + throw(MsgException, InvalidParameterException); + /* ! \brief Host::putBounded() - put a task on the given channel of a host object (capping the emission rate to maxrate). + * + * \param channel The channel where to put the task. + * \param rTask A refercence to the task object containing the native task to + * put on the channel specified by the parameter channel. + * \param maxRate The maximum rate. + * + * \return If successful the task is puted on the specified channel. Otherwise + * 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 + * 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 Host::putBounded(int channel, const Task& rTask, double maxRate) - throw(NativeException); - + throw(MsgException, InvalidParameterException); + /* ! brief Host::send() - sends the given task to mailbox identified by the default alias. + * + * \param rTask A reference to the task object containing the native msg task to send. + * + * \return If successful the task is sended to the default mailbox. Otherwise the + * method throws one of the exceptions described below. + * + * \exception [BadAllocException] if there is not enough memory to allocate + * the default alias variable. + * [MsgException] if an internal error occurs. + */ void send(const Task& rTask) - throw(NativeException); + throw(BadAllocException, MsgException); + /* ! brief Host::send() - sends the given task to mailbox identified by the specified alias parameter. + * + * \param rTask A reference to the task object containing the native msg task to send. + * \param alias The alias of the mailbox where to send the task. + * + * \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). + * [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(NativeException); + throw(InvalidParameterException, BadAllocException, MsgException); + /* ! brief Host::send() - sends the given task to mailbox identified by the default alias + * (waiting at most timeout seconds). + * + * \param rTask A reference to the task object containing the native msg task to send. + * \param timeout The timeout value to wait for. + * + * \return If successful the task is sended to the default mailbox. Otherwise the + * method throws one of the exceptions described below. + * + * \exception [BadAllocException] if there is not enough memory to allocate + * the default alias variable. + * [InvalidParameterException] if the timeout value is negative and different of + * -1.0. + * [MsgException] if an internal error occurs. + * + * \remark To specify no timeout set the timeout value with -1.0 or use the previous + * version of this method. + * + */ void send(const Task& rTask, double timeout) throw(NativeException); + /* ! brief Host::send() - sends the given task to mailbox identified by the parameter alias + * (waiting at most timeout seconds). + * + * \param alias The alias of the mailbox to send the task. + * \param rTask A reference to the task object containing the native msg task to send. + * \param timeout The timeout value to wait for. + * + * \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 + * -1.0 or if the alias parameter is invalid (NULL). + * [MsgException] if an internal error occurs. + * + * \remark To specify no timeout set the timeout value with -1.0 or use the previous + * version of this method. + * + */ void send(const char* alias, const Task& rTask, double timeout) - throw(NativeException); + throw(InvalidParameterException, MsgException); + /* ! brief Host::sendBounded() - sends the given task to mailbox associated to the default alias + * (capping the emission rate to maxRate). + * + * \param rTask A reference to the task object containing the native msg task to send. + * \param maxRate The maximum rate value. + * + * \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 + * -1.0. + * [MsgException] if an internal error occurs. + * [BadAllocException] if there is not enough memory to allocate + * the default alias variable. + * + * \remark To specify no rate set its value with -1.0. + * + */ void sendBounded(const Task& rTask, double maxRate) - throw(NativeException); + throw(InvalidParameterException, BadAllocException, MsgException); + /* ! brief Host::sendBounded() - sends the given task to mailbox identified by the parameter alias + * (capping the emission rate to maxRate). + * + * \param alias The alias of the mailbox where to send the task. + * \param rTask A reference to the task object containing the native msg task to send. + * \param maxRate The maximum rate value. + * + * \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 + * -1.0 or if the alias parameter is invalid (NULL). + * [MsgException] if an internal error occurs. + * + * \remark To specify no rate set its value with -1.0. + * + */ void sendBounded(const char* alias, const Task& rTask, double maxRate) - throw(NativeException); - - - + throw(InvalidParameterException, MsgException); protected: // Attributes. @@ -241,7 +411,7 @@ namespace SimGrid * It is set automaticatly during the call of the static * method Host::getByName(). * - * @see Host::getByName(). + * \see Host::getByName(). */ m_host_t nativeHost; -- 2.20.1