LCOV - code coverage report
Current view: top level - sw/source/core/uibase/dbui - maildispatcher.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 111 0.0 %
Date: 2014-04-14 Functions: 0 26 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10