LCOV - code coverage report
Current view: top level - sw/source/ui/dbui - maildispatcher.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 111 0.0 %
Date: 2012-08-25 Functions: 0 26 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 245 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #include "maildispatcher.hxx"
      30                 :            : #include "imaildsplistener.hxx"
      31                 :            : 
      32                 :            : #include <algorithm>
      33                 :            : 
      34                 :            : using namespace ::com::sun::star;
      35                 :            : using ::rtl::OUString;
      36                 :            : 
      37                 :            : typedef std::list< ::rtl::Reference<IMailDispatcherListener> > MailDispatcherListenerContainer_t;
      38                 :            : 
      39                 :            : namespace /* private */
      40                 :            : {
      41                 :            :     /* Generic event notifier for started,
      42                 :            :        stopped, and idle events which are
      43                 :            :        very similary */
      44                 :          0 :     class GenericEventNotifier
      45                 :            :     {
      46                 :            :     public:
      47                 :            :         // pointer to virtual function typedef
      48                 :            :         typedef void (IMailDispatcherListener::*GenericNotificationFunc_t)(::rtl::Reference<MailDispatcher>);
      49                 :            : 
      50                 :          0 :         GenericEventNotifier(
      51                 :            :             GenericNotificationFunc_t notification_function,
      52                 :            :             ::rtl::Reference<MailDispatcher> mail_dispatcher) :
      53                 :            :             notification_function_(notification_function),
      54                 :          0 :             mail_dispatcher_(mail_dispatcher)
      55                 :          0 :         {}
      56                 :            : 
      57                 :          0 :         void operator() (::rtl::Reference<IMailDispatcherListener> listener) const
      58 [ #  # ][ #  # ]:          0 :         { (listener.get()->*notification_function_)(mail_dispatcher_); }
      59                 :            : 
      60                 :            :     private:
      61                 :            :         GenericNotificationFunc_t notification_function_;
      62                 :            :         ::rtl::Reference<MailDispatcher> mail_dispatcher_;
      63                 :            :     };
      64                 :            : 
      65                 :          0 :     class MailDeliveryNotifier
      66                 :            :     {
      67                 :            :     public:
      68                 :          0 :         MailDeliveryNotifier(::rtl::Reference<MailDispatcher> xMailDispatcher, uno::Reference<mail::XMailMessage> message) :
      69                 :            :             mail_dispatcher_(xMailDispatcher),
      70                 :          0 :             message_(message)
      71                 :          0 :         {}
      72                 :            : 
      73                 :          0 :         void operator() (::rtl::Reference<IMailDispatcherListener> listener) const
      74 [ #  # ][ #  # ]:          0 :         { listener->mailDelivered(mail_dispatcher_, message_); }
                 [ #  # ]
      75                 :            : 
      76                 :            :     private:
      77                 :            :         ::rtl::Reference<MailDispatcher> mail_dispatcher_;
      78                 :            :         uno::Reference<mail::XMailMessage> message_;
      79                 :            :     };
      80                 :            : 
      81                 :          0 :     class MailDeliveryErrorNotifier
      82                 :            :     {
      83                 :            :     public:
      84                 :          0 :         MailDeliveryErrorNotifier(
      85                 :            :             ::rtl::Reference<MailDispatcher> xMailDispatcher,
      86                 :            :             uno::Reference<mail::XMailMessage> message,
      87                 :            :             const ::rtl::OUString& error_message) :
      88                 :            :             mail_dispatcher_(xMailDispatcher),
      89                 :            :             message_(message),
      90                 :          0 :             error_message_(error_message)
      91                 :          0 :         {}
      92                 :            : 
      93                 :          0 :         void operator() (::rtl::Reference<IMailDispatcherListener> listener) const
      94 [ #  # ][ #  # ]:          0 :         { listener->mailDeliveryError(mail_dispatcher_, message_, error_message_); }
                 [ #  # ]
      95                 :            : 
      96                 :            :     private:
      97                 :            :         ::rtl::Reference<MailDispatcher> mail_dispatcher_;
      98                 :            :         uno::Reference<mail::XMailMessage> message_;
      99                 :            :         ::rtl::OUString error_message_;
     100                 :            :     };
     101                 :            : 
     102                 :            : } // namespace private
     103                 :            : 
     104                 :          0 : MailDispatcher::MailDispatcher(uno::Reference<mail::XSmtpService> mailserver) :
     105                 :            :     mailserver_ (mailserver),
     106                 :            :     run_(false),
     107 [ #  # ][ #  # ]:          0 :     shutdown_requested_(false)
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     108                 :            : {
     109         [ #  # ]:          0 :     wakening_call_.reset();
     110         [ #  # ]:          0 :     mail_dispatcher_active_.reset();
     111                 :            : 
     112 [ #  # ][ #  # ]:          0 :     if (!create())
     113         [ #  # ]:          0 :         throw uno::RuntimeException();
     114                 :            : 
     115                 :            :     // wait until the mail dispatcher thread is really alive
     116                 :            :     // and has aquired a reference to this instance of the
     117                 :            :     // class
     118         [ #  # ]:          0 :     mail_dispatcher_active_.wait();
     119                 :          0 : }
     120                 :            : 
     121 [ #  # ][ #  # ]:          0 : MailDispatcher::~MailDispatcher()
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     122                 :            : {
     123         [ #  # ]:          0 : }
     124                 :            : 
     125                 :          0 : void MailDispatcher::enqueueMailMessage(uno::Reference<mail::XMailMessage> message)
     126                 :            : {
     127         [ #  # ]:          0 :     ::osl::MutexGuard thread_status_guard(thread_status_mutex_);
     128         [ #  # ]:          0 :     ::osl::MutexGuard message_container_guard(message_container_mutex_);
     129                 :            : 
     130                 :            :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     131                 :            : 
     132         [ #  # ]:          0 :     messages_.push_back(message);
     133         [ #  # ]:          0 :     if (run_)
     134 [ #  # ][ #  # ]:          0 :         wakening_call_.set();
                 [ #  # ]
     135                 :          0 : }
     136                 :            : 
     137                 :          0 : uno::Reference<mail::XMailMessage> MailDispatcher::dequeueMailMessage()
     138                 :            : {
     139         [ #  # ]:          0 :     ::osl::MutexGuard guard(message_container_mutex_);
     140                 :          0 :     uno::Reference<mail::XMailMessage> message;
     141         [ #  # ]:          0 :     if(!messages_.empty())
     142                 :            :     {
     143 [ #  # ][ #  # ]:          0 :         message = messages_.front();
     144         [ #  # ]:          0 :         messages_.pop_front();
     145                 :            :     }
     146         [ #  # ]:          0 :     return message;
     147                 :            : }
     148                 :            : 
     149                 :          0 : void MailDispatcher::start()
     150                 :            : {
     151                 :            :     OSL_PRECOND(!isStarted(), "MailDispatcher is already started!");
     152                 :            : 
     153         [ #  # ]:          0 :     ::osl::ClearableMutexGuard thread_status_guard(thread_status_mutex_);
     154                 :            : 
     155                 :            :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     156                 :            : 
     157         [ #  # ]:          0 :     if (!shutdown_requested_)
     158                 :            :     {
     159                 :          0 :         run_ = true;
     160         [ #  # ]:          0 :         wakening_call_.set();
     161         [ #  # ]:          0 :         thread_status_guard.clear();
     162                 :            : 
     163         [ #  # ]:          0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     164 [ #  # ][ #  # ]:          0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), GenericEventNotifier(&IMailDispatcherListener::started, this));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     165         [ #  # ]:          0 :     }
     166                 :          0 : }
     167                 :            : 
     168                 :          0 : void MailDispatcher::stop()
     169                 :            : {
     170                 :            :     OSL_PRECOND(isStarted(), "MailDispatcher not started!");
     171                 :            : 
     172         [ #  # ]:          0 :     ::osl::ClearableMutexGuard thread_status_guard(thread_status_mutex_);
     173                 :            : 
     174                 :            :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     175                 :            : 
     176         [ #  # ]:          0 :     if (!shutdown_requested_)
     177                 :            :     {
     178                 :          0 :         run_ = false;
     179         [ #  # ]:          0 :         wakening_call_.reset();
     180         [ #  # ]:          0 :         thread_status_guard.clear();
     181                 :            : 
     182         [ #  # ]:          0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     183 [ #  # ][ #  # ]:          0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), GenericEventNotifier(&IMailDispatcherListener::stopped, this));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     184         [ #  # ]:          0 :     }
     185                 :          0 : }
     186                 :            : 
     187                 :          0 : void MailDispatcher::shutdown()
     188                 :            : {
     189         [ #  # ]:          0 :     ::osl::MutexGuard thread_status_guard(thread_status_mutex_);
     190                 :            : 
     191                 :            :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     192                 :            : 
     193                 :          0 :     shutdown_requested_ = true;
     194 [ #  # ][ #  # ]:          0 :     wakening_call_.set();
     195                 :          0 : }
     196                 :            : 
     197                 :          0 : bool MailDispatcher::isStarted() const
     198                 :            : {
     199                 :          0 :     return run_;
     200                 :            : }
     201                 :            : 
     202                 :          0 : void MailDispatcher::addListener(::rtl::Reference<IMailDispatcherListener> listener)
     203                 :            : {
     204                 :            :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     205                 :            : 
     206         [ #  # ]:          0 :     ::osl::MutexGuard guard(listener_container_mutex_);
     207 [ #  # ][ #  # ]:          0 :     listeners_.push_back(listener);
     208                 :          0 : }
     209                 :            : 
     210                 :          0 : std::list< ::rtl::Reference<IMailDispatcherListener> > MailDispatcher::cloneListener()
     211                 :            : {
     212         [ #  # ]:          0 :     ::osl::MutexGuard guard(listener_container_mutex_);
     213 [ #  # ][ #  # ]:          0 :     return listeners_;
     214                 :            : }
     215                 :            : 
     216                 :          0 : void MailDispatcher::sendMailMessageNotifyListener(uno::Reference<mail::XMailMessage> message)
     217                 :            : {
     218                 :            :     try
     219                 :            :     {
     220 [ #  # ][ #  # ]:          0 :         mailserver_->sendMailMessage(message);
     221         [ #  # ]:          0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     222 [ #  # ][ #  # ]:          0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryNotifier(this, message));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     223                 :            :     }
     224                 :          0 :     catch (const mail::MailException& ex)
     225                 :            :     {
     226         [ #  # ]:          0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     227   [ #  #  #  #  :          0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryErrorNotifier(this, message, ex.Message));
          #  #  #  #  #  
                #  #  # ]
     228                 :            :     }
     229      [ #  #  # ]:          0 :     catch (const uno::RuntimeException& ex)
     230                 :            :     {
     231         [ #  # ]:          0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     232   [ #  #  #  #  :          0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryErrorNotifier(this, message, ex.Message));
          #  #  #  #  #  
                #  #  # ]
     233                 :            :     }
     234                 :          0 : }
     235                 :            : 
     236                 :          0 : void MailDispatcher::run()
     237                 :            : {
     238                 :            :     // aquire a self reference in order to avoid race
     239                 :            :     // conditions. The last client of this class must
     240                 :            :     // call shutdown before releasing his last reference
     241                 :            :     // to this class in order to shutdown this thread
     242                 :            :     // which will release his (the very last reference
     243                 :            :     // to the class and so force their destruction
     244                 :          0 :     m_xSelfReference = this;
     245                 :            : 
     246                 :            :     // signal that the mail dispatcher thread is now alive
     247                 :          0 :     mail_dispatcher_active_.set();
     248                 :            : 
     249                 :          0 :     for(;;)
     250                 :            :     {
     251         [ #  # ]:          0 :         wakening_call_.wait();
     252                 :            : 
     253         [ #  # ]:          0 :         ::osl::ClearableMutexGuard thread_status_guard(thread_status_mutex_);
     254         [ #  # ]:          0 :         if (shutdown_requested_)
     255                 :            :            break;
     256                 :            : 
     257         [ #  # ]:          0 :         ::osl::ClearableMutexGuard message_container_guard(message_container_mutex_);
     258                 :            : 
     259         [ #  # ]:          0 :         if (messages_.size())
     260                 :            :         {
     261         [ #  # ]:          0 :             thread_status_guard.clear();
     262         [ #  # ]:          0 :             uno::Reference<mail::XMailMessage> message = messages_.front();
     263         [ #  # ]:          0 :             messages_.pop_front();
     264         [ #  # ]:          0 :             message_container_guard.clear();
     265         [ #  # ]:          0 :             sendMailMessageNotifyListener(message);
     266                 :            :         }
     267                 :            :         else // idle - put ourself to sleep
     268                 :            :         {
     269         [ #  # ]:          0 :             wakening_call_.reset();
     270         [ #  # ]:          0 :             message_container_guard.clear();
     271         [ #  # ]:          0 :             thread_status_guard.clear();
     272         [ #  # ]:          0 :             MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     273 [ #  # ][ #  # ]:          0 :             std::for_each(listeners_cloned.begin(), listeners_cloned.end(), GenericEventNotifier(&IMailDispatcherListener::idle, this));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     274                 :            :         }
     275 [ #  # ][ #  # ]:          0 :     } // end for        SSH ALI
                 [ #  # ]
     276                 :          0 : }
     277                 :            : 
     278                 :          0 : void MailDispatcher::onTerminated()
     279                 :            : {
     280                 :            :     //keep the reference until the end of onTerminated() because of the call order in the
     281                 :            :     //_threadFunc() from osl/thread.hxx
     282                 :          0 :     m_xSelfReference = 0;
     283                 :          0 : }
     284                 :            : 
     285                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10