diff --git a/mediachanger/AcsProxyZmq.cpp b/mediachanger/AcsProxyZmq.cpp index 20748dd70ebdf3b0fa17cb9b0466584bce57ef62..cc398f29a175301857e1677768de517862e4f067 100644 --- a/mediachanger/AcsProxyZmq.cpp +++ b/mediachanger/AcsProxyZmq.cpp @@ -169,9 +169,8 @@ Frame createAcsForceDismountTapeFrame(const std::string &vid, const AcsLibrarySl // constructor //------------------------------------------------------------------------------ AcsProxyZmq::AcsProxyZmq(void *const zmqContext, const unsigned short serverPort) throw(): - m_serverPort(serverPort), - m_serverSocket(zmqContext, ZMQ_REQ) { - connectZmqSocketToLocalhost(m_serverSocket, serverPort); + m_zmqContext(zmqContext), + m_serverPort(serverPort) { } //------------------------------------------------------------------------------ @@ -182,10 +181,10 @@ void AcsProxyZmq::mountTapeReadOnly(const std::string &vid, const LibrarySlot &l try { const Frame rqst = createAcsMountTapeReadOnlyFrame(vid, dynamic_cast<const AcsLibrarySlot&>(librarySlot)); - sendFrame(m_serverSocket, rqst); + sendFrame(serverSocketInstance(), rqst); MediaChangerReturnValue reply; - recvTapeReplyOrEx(m_serverSocket, reply); + recvTapeReplyOrEx(serverSocketInstance(), reply); if(0 != reply.value()) { // Should never get here cta::exception::Exception ex; @@ -210,10 +209,10 @@ void AcsProxyZmq::mountTapeReadWrite(const std::string &vid, const LibrarySlot & try { const Frame rqst = createAcsMountTapeReadWriteFrame(vid, dynamic_cast<const AcsLibrarySlot&>(librarySlot)); - sendFrame(m_serverSocket, rqst); + sendFrame(serverSocketInstance(), rqst); MediaChangerReturnValue reply; - recvTapeReplyOrEx(m_serverSocket, reply); + recvTapeReplyOrEx(serverSocketInstance(), reply); if(0 != reply.value()) { // Should never get here cta::exception::Exception ex; @@ -238,10 +237,10 @@ void AcsProxyZmq::dismountTape(const std::string &vid, const LibrarySlot &librar try { const Frame rqst = createAcsDismountTapeFrame(vid, dynamic_cast<const AcsLibrarySlot&>(librarySlot)); - sendFrame(m_serverSocket, rqst); + sendFrame(serverSocketInstance(), rqst); MediaChangerReturnValue reply; - recvTapeReplyOrEx(m_serverSocket, reply); + recvTapeReplyOrEx(serverSocketInstance(), reply); if(0 != reply.value()) { // Should never get here cta::exception::Exception ex; @@ -266,10 +265,10 @@ void AcsProxyZmq::forceDismountTape(const std::string &vid, const LibrarySlot &l try { const Frame rqst = createAcsForceDismountTapeFrame(vid, dynamic_cast<const AcsLibrarySlot&>(librarySlot)); - sendFrame(m_serverSocket, rqst); + sendFrame(serverSocketInstance(), rqst); MediaChangerReturnValue reply; - recvTapeReplyOrEx(m_serverSocket, reply); + recvTapeReplyOrEx(serverSocketInstance(), reply); if(0 != reply.value()) { // Should never get here cta::exception::Exception ex; @@ -286,5 +285,16 @@ void AcsProxyZmq::forceDismountTape(const std::string &vid, const LibrarySlot &l } } +//------------------------------------------------------------------------------ +// serverSocketInstance +//------------------------------------------------------------------------------ +ZmqSocketMT &AcsProxyZmq::serverSocketInstance() { + if(nullptr == m_serverSocket) { + m_serverSocket.reset(new ZmqSocketMT(m_zmqContext, ZMQ_REQ)); + connectZmqSocketToLocalhost(*m_serverSocket, m_serverPort); + } + return *m_serverSocket; +} + } // namespace mediachanger } // namespace cta diff --git a/mediachanger/AcsProxyZmq.hpp b/mediachanger/AcsProxyZmq.hpp index 8b8ff4d4539f7226d3d6565b1b2e66c66c6d7ba8..d0c24e8a6348271dc6051943f265ffc59cfd656b 100644 --- a/mediachanger/AcsProxyZmq.hpp +++ b/mediachanger/AcsProxyZmq.hpp @@ -22,6 +22,7 @@ #include "mediachanger/MediaChangerProxy.hpp" #include "mediachanger/ZmqSocketMT.hpp" +#include <memory> #include <mutex> namespace cta { @@ -80,6 +81,11 @@ public: void forceDismountTape(const std::string &vid, const LibrarySlot &librarySlot) override; private: + + /** + * The ZMQ context. + */ + void *m_zmqContext; /** * Mutex used to implement a critical section around the enclosed @@ -94,9 +100,21 @@ private: const unsigned short m_serverPort; /** - * Socket connecting this proxy the daemon it represents. + * Socket connecting this proxy to the daemon it represents. + */ + std::unique_ptr<ZmqSocketMT> m_serverSocket; + + /** + * Returns the socket instance connecting this proxy to the daemon it + * represents. This method instantiates a socket and connects it if + * a socket does not already exist. + * + * Please note that a lock MUST be taken on m_mutex before calling this + * method. + * + * @return The socket connecting this proxy the daemon it represents. */ - ZmqSocketMT m_serverSocket; + ZmqSocketMT &serverSocketInstance(); }; // class AcsProxyZmq