connectorthread.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // -*- C++ -*-
  2. // Copyright (C) 2013-2015, Vaclav Zeman. All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without modifica-
  5. // tion, are permitted provided that the following conditions are met:
  6. //
  7. // 1. Redistributions of source code must retain the above copyright notice,
  8. // this list of conditions and the following disclaimer.
  9. //
  10. // 2. Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. //
  14. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  15. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  17. // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  18. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  19. // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  20. // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. #ifndef LOG4CPLUS_HELPERS_CONNECTORTHREAD_H
  25. #define LOG4CPLUS_HELPERS_CONNECTORTHREAD_H
  26. #include <log4cplus/config.hxx>
  27. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  28. #pragma once
  29. #endif
  30. #include <log4cplus/thread/syncprims.h>
  31. #include <log4cplus/thread/threads.h>
  32. #include <log4cplus/helpers/socket.h>
  33. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  34. namespace log4cplus { namespace helpers {
  35. class LOG4CPLUS_EXPORT ConnectorThread;
  36. //! Interface implemented by users of ConnectorThread.
  37. class LOG4CPLUS_EXPORT IConnectorThreadClient
  38. {
  39. protected:
  40. virtual ~IConnectorThreadClient ();
  41. //! \return Mutex for synchronization between ConnectorThread and
  42. //! its client object. This is usually SharedObject::access_mutex.
  43. virtual thread::Mutex const & ctcGetAccessMutex () const = 0;
  44. //! \return Socket variable in ConnectorThread client to maintain.
  45. virtual helpers::Socket & ctcGetSocket () = 0;
  46. //! \return ConnectorThread client's function returning connected
  47. //! socket.
  48. virtual helpers::Socket ctcConnect () = 0;
  49. //! Sets connected flag to true in ConnectorThread's client.
  50. virtual void ctcSetConnected () = 0;
  51. friend class LOG4CPLUS_EXPORT ConnectorThread;
  52. };
  53. //! This class is used by SocketAppender and (remote) SysLogAppender
  54. //! to provide asynchronous re-connection.
  55. class LOG4CPLUS_EXPORT ConnectorThread
  56. : public thread::AbstractThread
  57. {
  58. public:
  59. //! \param client reference to ConnectorThread's client object
  60. ConnectorThread (IConnectorThreadClient & client);
  61. virtual ~ConnectorThread ();
  62. virtual void run();
  63. //! Call this function to terminate ConnectorThread. The function
  64. //! sets `exit_flag` and then triggers `trigger_ev` to wake up the
  65. //! ConnectorThread.
  66. void terminate ();
  67. //! This function triggers (`trigger_ev`) connection check and
  68. //! attempt to re-connect a broken connection, when necessary.
  69. void trigger ();
  70. protected:
  71. //! reference to ConnectorThread's client
  72. IConnectorThreadClient & ctc;
  73. //! This event is the re-connection trigger.
  74. thread::ManualResetEvent trigger_ev;
  75. //! When this variable set to true when ConnectorThread is signaled to
  76. bool exit_flag;
  77. };
  78. } } // namespace log4cplus { namespace helpers {
  79. #endif // ! defined (LOG4CPLUS_SINGLE_THREADED)
  80. #endif // LOG4CPLUS_HELPERS_CONNECTORTHREAD_H