diff --git a/mediachanger/CMakeLists.txt b/mediachanger/CMakeLists.txt index a0fe0aaacface7bbdea083f1296342d15afecf6c..af98f8fab7f0a364c6b3cc5efec0a7c024855166 100644 --- a/mediachanger/CMakeLists.txt +++ b/mediachanger/CMakeLists.txt @@ -15,7 +15,6 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. cmake_minimum_required (VERSION 2.6) -add_subdirectory(reactor) add_subdirectory(castorrmc) find_package(openssl REQUIRED) find_package(Protobuf3 REQUIRED) diff --git a/mediachanger/reactor/CMakeLists.txt b/mediachanger/reactor/CMakeLists.txt deleted file mode 100644 index ce76e4759c6b9b17ff02e987ef2d3299a1d59ca1..0000000000000000000000000000000000000000 --- a/mediachanger/reactor/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# This file is part of the Castor project. -# See http://castor.web.cern.ch/castor -# -# Copyright (C) 2003 CERN -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# -# -# @author Castor Dev team, castor-dev@cern.ch -# -cmake_minimum_required (VERSION 2.6) - -################################################################################ -# Rules to build the reactor code that is common to both rmcd and tapeserverd -################################################################################ -set (REACTOR_SRC_FILES - ZMQPollEventHandler.cpp - ZMQReactor.cpp) -add_library (ctareactor ${REACTOR_SRC_FILES}) -set_target_properties (ctareactor PROPERTIES - COMPILE_FLAGS -I/usr/include/CDK - COMPILE_DEFINITIONS LINUX) -target_link_libraries (ctareactor ctacommon ctamediachanger zmq) diff --git a/mediachanger/reactor/ZMQPollEventHandler.cpp b/mediachanger/reactor/ZMQPollEventHandler.cpp deleted file mode 100644 index b090c1e9a7132383e70e6f61bd10df7e88241252..0000000000000000000000000000000000000000 --- a/mediachanger/reactor/ZMQPollEventHandler.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/****************************************************************************** - * - * This file is part of the Castor project. - * See http://castor.web.cern.ch/castor - * - * Copyright (C) 2003 CERN - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * @author Castor Dev team, castor-dev@cern.ch - *****************************************************************************/ - -#include "ZMQPollEventHandler.hpp" - -//------------------------------------------------------------------------------ -// destructor -//------------------------------------------------------------------------------ -cta::mediachanger::reactor::ZMQPollEventHandler::~ZMQPollEventHandler() { -} diff --git a/mediachanger/reactor/ZMQPollEventHandler.hpp b/mediachanger/reactor/ZMQPollEventHandler.hpp deleted file mode 100644 index 185e9fb024c1f6e047eee917af197fc75a129dfb..0000000000000000000000000000000000000000 --- a/mediachanger/reactor/ZMQPollEventHandler.hpp +++ /dev/null @@ -1,77 +0,0 @@ -/****************************************************************************** - * - * This file is part of the Castor project. - * See http://castor.web.cern.ch/castor - * - * Copyright (C) 2003 CERN - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * @author Castor Dev team, castor-dev@cern.ch - *****************************************************************************/ - -#pragma once - -#include "common/exception/Exception.hpp" - -#include <zmq.h> - -namespace cta { -namespace mediachanger { -namespace reactor { - -/** - * Handles the events that occur on a poll() file descriptor. - * - * This class is part of an implementation of the Reactor architecture pattern - * described in the following book: - * - * Pattern-Oriented Software Architecture Volume 2 - * Patterns for Concurrent and Networked Objects - * Authors: Schmidt, Stal, Rohnert and Buschmann - * Publication date: 2000 - * ISBN 0-471-60695-2 - */ -class ZMQPollEventHandler { -public: - - /** - * Destructor. - */ - virtual ~ZMQPollEventHandler() = 0; - - /** - * Returns the human-readable name this event handler. - */ - virtual std::string getName() const = 0; - - /** - * Fills the specified poll file-descriptor ready to be used in a call to - * poll(). - */ - virtual void fillPollFd(zmq_pollitem_t &pollitem) =0; - - /** - * Handles the specified event. - * - * @param fd The poll file-descriptor describing the event. - * @return true if the event handler should be removed from and deleted by - * the reactor. - */ - virtual bool handleEvent(const zmq_pollitem_t &fd)=0; - -}; // class ZMQPollEventHandler - -} // namespace reactor -} // namespace tape -} // namespace cta - diff --git a/mediachanger/reactor/ZMQReactor.cpp b/mediachanger/reactor/ZMQReactor.cpp deleted file mode 100644 index 75643ba9face2a4b1ccd54c047b12e3766621629..0000000000000000000000000000000000000000 --- a/mediachanger/reactor/ZMQReactor.cpp +++ /dev/null @@ -1,179 +0,0 @@ -/****************************************************************************** - * - * This file is part of the Castor project. - * See http://castor.web.cern.ch/castor - * - * Copyright (C) 2003 CERN - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * @author Castor Dev team, castor-dev@cern.ch - *****************************************************************************/ - -#include "common/log/log.hpp" -#include "ZMQReactor.hpp" -#include "ZMQPollEventHandler.hpp" -#include "common/utils/utils.hpp" - -#include "common/log/SyslogLogger.hpp" -#include <algorithm> - -namespace{ - bool operator==(const zmq_pollitem_t& a,const zmq_pollitem_t& b){ - if( (a.fd==b.fd && a.fd!= -1 && b.fd != -1) || - (a.socket==b.socket && a.socket!=NULL && b.socket != NULL) ){ - return true; - } - return false; - } -} - -//------------------------------------------------------------------------------ -// constructor -//------------------------------------------------------------------------------ -cta::mediachanger::reactor::ZMQReactor::ZMQReactor(cta::log::Logger &l):m_log(l) { -} -//------------------------------------------------------------------------------ -// destructor -//------------------------------------------------------------------------------ -cta::mediachanger::reactor::ZMQReactor::~ZMQReactor() { - clear(); -} - -//------------------------------------------------------------------------------ -// clear -//------------------------------------------------------------------------------ -void cta::mediachanger::reactor::ZMQReactor::clear() { - for(HandlerMap::iterator it=m_handlers.begin();it!=m_handlers.end();++it){ - delete it->second; - } - m_handlers.clear(); -} - - -//------------------------------------------------------------------------------ -// registerHandler -//------------------------------------------------------------------------------ - - void cta::mediachanger::reactor::ZMQReactor::registerHandler( - ZMQPollEventHandler *const handler) { - zmq_pollitem_t item; - handler->fillPollFd(item); - - std::ostringstream socketInHex; - socketInHex << std::hex << item.socket; - std::list<log::Param> params = {log::Param("fd", item.fd), - log::Param("socket", socketInHex.str())}; - //log::write(LOG_DEBUG, "ZMQReactor registering a new handler", params); - m_log(LOG_DEBUG, "ZMQReactor registering a new handler", params); - - checkDoubleRegistration(item); - m_handlers.push_back(std::make_pair(item,handler)); -} - -//------------------------------------------------------------------------------ -// checkDoubleRegistration -//------------------------------------------------------------------------------ -void cta::mediachanger::reactor::ZMQReactor::checkDoubleRegistration( - const zmq_pollitem_t &item) const { - for(HandlerMap::const_iterator it=m_handlers.begin(); it!=m_handlers.end(); - ++it) { - const std::pair<zmq_pollitem_t, ZMQPollEventHandler*> &maplet = *it; - if(item == maplet.first) { - cta::exception::Exception ex; - ex.getMessage() << "ZMQReactor detected a double registration: fd=" << - item.fd << " socket=" << std::hex << item.socket; - throw ex; - } - } -} - -//------------------------------------------------------------------------------ -// handleEvents -//------------------------------------------------------------------------------ -void cta::mediachanger::reactor::ZMQReactor::handleEvents(const int timeout) { - //it should not bring any copy, thanks to NRVO - std::vector<zmq_pollitem_t> pollFds=buildPollFds(); - - // Please note that we are relying on the fact that the file descriptors of - // the vector are stored contiguously - const int pollRc = zmq_poll(&pollFds[0], pollFds.size(), timeout); - if(0 <= pollRc){ - for(std::vector<zmq_pollitem_t>::const_iterator it=pollFds.begin(); - it!=pollFds.end(); ++it) { - const zmq_pollitem_t &pollFd = *it; - if(0 != pollFd.revents) { - handleEvent(pollFd); - } - } - } else if(0 > pollRc) { - const std::string message = utils::errnoToString(errno); - std::list<log::Param> params = {log::Param("message", message)}; - m_log(LOG_ERR, "Failed to handle I/O event: zmq_poll() failed", params); - } -} - -//------------------------------------------------------------------------------ -// handleEvent -//------------------------------------------------------------------------------ -void cta::mediachanger::reactor::ZMQReactor::handleEvent( - const zmq_pollitem_t &pollFd) { - // Find and dispatch the appropriate handler if there is a pending event - if(pollFd.revents & ZMQ_POLLIN) { - HandlerMap::iterator handlerItor = findHandler(pollFd); - if(handlerItor != m_handlers.end()) { - ZMQPollEventHandler *const handler = handlerItor->second; - const bool removeAndDeleteHandler = handler->handleEvent(pollFd); - if(removeAndDeleteHandler) { - m_handlers.erase(handlerItor); - delete(handler); - } - }else { - std::list<log::Param> params; - params.push_back(log::Param("fd",pollFd.fd)); - params.push_back(log::Param("socket",pollFd.socket)); - m_log(LOG_ERR, "Event on some poll, but no handler to match it", params); - } - } -} - -//------------------------------------------------------------------------------ -// findHandler -//------------------------------------------------------------------------------ - -cta::mediachanger::reactor::ZMQReactor::HandlerMap::iterator - cta::mediachanger::reactor::ZMQReactor::findHandler(const zmq_pollitem_t& pollfd) { - for(HandlerMap::iterator it=m_handlers.begin();it!=m_handlers.end();++it){ - // Use overloaded == to compare zmq_pollitem_t references - if(pollfd==it->first){ - return it; - } - } - return m_handlers.end(); -} - -//------------------------------------------------------------------------------ -// buildPollFds -//------------------------------------------------------------------------------ -std::vector<zmq_pollitem_t> cta::mediachanger::reactor::ZMQReactor::buildPollFds() - const { - std::vector<zmq_pollitem_t> pollFds; - pollFds.reserve(m_handlers.size()); - for(HandlerMap::const_iterator it=m_handlers.begin();it!=m_handlers.end(); - ++it) { - ZMQPollEventHandler *const handler = it->second; - zmq_pollitem_t pollFd; - handler->fillPollFd(pollFd); - pollFds.push_back(pollFd); - } - return pollFds; -} diff --git a/mediachanger/reactor/ZMQReactor.hpp b/mediachanger/reactor/ZMQReactor.hpp deleted file mode 100644 index a06761b95456ede5cd29ce96a1e889dd5163ecec..0000000000000000000000000000000000000000 --- a/mediachanger/reactor/ZMQReactor.hpp +++ /dev/null @@ -1,141 +0,0 @@ -/****************************************************************************** - * - * This file is part of the Castor project. - * See http://castor.web.cern.ch/castor - * - * Copyright (C) 2003 CERN - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * @author Castor Dev team, castor-dev@cern.ch - *****************************************************************************/ - -#pragma once - -#include "ZMQPollEventHandler.hpp" -#include "ZMQReactor.hpp" - -#include "common/log/log.hpp" -#include <utility> -#include <vector> - -namespace cta { -namespace mediachanger { -namespace reactor { - -/** - * This reactor wraps the zmq_poll() function. - * - * This class is part of an implementation of the Reactor architecture pattern - * described in the following book: - * - * Pattern-Oriented Software Architecture Volume 2 - * Patterns for Concurrent and Networked Objects - * Authors: Schmidt, Stal, Rohnert and Buschmann - * Publication date: 2000 - * ISBN 0-471-60695-2 - */ -class ZMQReactor { -public: - - /** - * Constructor. - */ - ZMQReactor(cta::log::Logger &); - /** - * Destructor. - */ - virtual ~ZMQReactor(); - - /** - * Removes and deletes all of the event handlers registered with the reactor. - */ - virtual void clear(); - - /** - * Registers the specified handler. - * - * Please note that the reactor takes ownership of the handler and will - * delete it as appropriate. - * - * @param handler The handler to be registered. Please note that the handler - * MUST be allocated on the heap because the reactor will own the handler - * and therefore delete it as needed. - */ - virtual void registerHandler(ZMQPollEventHandler *const handler); - - /** - * Handles any pending events. - * - * @param timeout Timeout in milliseconds. - */ - virtual void handleEvents(const int timeout); - -private: - - log::Logger &m_log; - - /** - * Type used to map zmq_pollitem_t to event handler. - */ -typedef std::vector<std::pair<zmq_pollitem_t, ZMQPollEventHandler*> > - HandlerMap; - - /** - * Throws a cator::exception::Exception if a handler has already been - * registered for the specified poll item. - * - * @prama item The poll item. - */ - void checkDoubleRegistration(const zmq_pollitem_t &item) const; - - /** - * Builds the vector of file descriptors to be passed to poll(). - * - * Please note that the return type is an std::vector because we can assume - * that its elements are stored contiguously in memory. The address of the - * first element is going to be passed to zmq_poll(). - * - * @return The vector of file descriptors. - */ - std::vector<zmq_pollitem_t> buildPollFds() const; - - /** - * Returns the event handler associated with the specified zmq_pollitem_t. - */ - HandlerMap::iterator findHandler(const zmq_pollitem_t&); - - /** - * Handles the specified ZMQ I/O event. - * - * @param pollFd The file-descriptor representing the I/O event. - */ - void handleEvent(const zmq_pollitem_t &pollFd); - - /** - * Removes the specified handler from the reactor. This method effectively - * does the opposite of registerHandler(). - * - * @param handler The handler to be removed. - */ - void removeHandler(ZMQPollEventHandler *const handler); - - /** - * Map of file descriptor to registered event handler. - */ - HandlerMap m_handlers; - -}; // class ZMQReactor - -} // namespace reactor -} // namespace mediachanger -} // namespace cta