#ifndef __JRUNCONTROL__JCLIENTLIST__ #define __JRUNCONTROL__JCLIENTLIST__ #include #include #include "chsm.h" #include "JRuncontrol/JClient.hh" /** * \author mdejong */ namespace KM3NETDAQ { /** * List of clients. */ class JClientList : public std::vector { protected: /** * Auxiliary class for comparing clients. */ class JComparator { public: /** * Less than operator. * The full names of the clients are used for the comparison. * * \param first first client * \param second second client * \return true is first less than second; else false */ inline bool operator()(const JClient& first, const JClient& second) const { return first.getFullName() < second.getFullName(); } /** * Less than operator. * The full names of the client is used for the comparison. * * \param client client * \param full_name full name * \return true is client's full name less than full name; else false */ inline bool operator()(const JClient& client, const std::string& full_name) const { return client.getFullName() < full_name; } }; const JComparator comparator; public: /** * Default constructor. */ JClientList() : std::vector(), comparator() {} /** * Insert client. * * \param client client * \return iterator to client and true if inserted; else false */ std::pair insert(const JClient& client) { iterator i = std::lower_bound(this->begin(), this->end(), client, comparator); if (i == this->end() || comparator(client, *i)) { i = std::vector::insert(i, client); return std::make_pair(i, true); } else { return std::make_pair(i, false); } } /** * Find client. * * \param client client * \return iterator to client */ iterator find(const JClient& client) { iterator i = std::lower_bound(this->begin(), this->end(), client, comparator); if (i != this->end() && !comparator(*i, client)) return i; else return this->end(); } /** * Find client by its full name. * * \param buffer full name of client (possibly followed by more text) * \return position of client (or end) */ iterator find(const std::string& buffer) { const std::string full_name = getFullName(buffer); iterator i = std::lower_bound(this->begin(), this->end(), full_name, comparator); if (i != this->end() && !comparator(*i, full_name)) return i; else return this->end(); } /** * Start processes. */ void start() { for (iterator i = this->begin(); i != this->end(); ++i) { if (i->getMode() == JClient::ACTIVE) { i->start(); } } } /** * Stop processes. * * \param signal signal */ void stop(const int signal = -9) { for (iterator i = this->begin(); i != this->end(); ++i) { if (i->getBorn() > i->getDied()) { i->stop(signal); } } } /** * Get number of clients with given mode. * * \param mode mode * \return number of clients */ unsigned int count(const JClient::JMode mode) const { unsigned int n = 0; for (const_iterator i = this->begin(); i != this->end(); ++i) { if (i->getMode() == mode) { ++n; } } return n; } /** * Get number of active clients with born count exceeding died count. * * \return number of clients */ unsigned int count() const { unsigned int n = 0; for (const_iterator i = this->begin(); i != this->end(); ++i) { if (i->getMode() == JClient::ACTIVE && i->getBorn() > i->getDied()) { ++n; } } return n; } /** * Get number of active clients in given state. * * \param state state * \return number of clients */ unsigned int count(const CHSM::state& state) const { unsigned int n = 0; for (const_iterator i = this->begin(); i != this->end(); ++i) { if (i->getMode() == JClient::ACTIVE && i->getStatename() == getStateName(state.name())) { ++n; } } return n; } }; } #endif