LCOV - code coverage report
Current view: top level - sw/source/uibase/dbui - maildispatcher.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 0 110 0.0 %
Date: 2014-11-03 Functions: 0 25 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 acquired 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             : 
     188           0 : void MailDispatcher::addListener(::rtl::Reference<IMailDispatcherListener> listener)
     189             : {
     190             :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     191             : 
     192           0 :     ::osl::MutexGuard guard(listener_container_mutex_);
     193           0 :     listeners_.push_back(listener);
     194           0 : }
     195             : 
     196           0 : std::list< ::rtl::Reference<IMailDispatcherListener> > MailDispatcher::cloneListener()
     197             : {
     198           0 :     ::osl::MutexGuard guard(listener_container_mutex_);
     199           0 :     return listeners_;
     200             : }
     201             : 
     202           0 : void MailDispatcher::sendMailMessageNotifyListener(uno::Reference<mail::XMailMessage> message)
     203             : {
     204             :     try
     205             :     {
     206           0 :         mailserver_->sendMailMessage(message);
     207           0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     208           0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryNotifier(this, message));
     209             :     }
     210           0 :     catch (const mail::MailException& ex)
     211             :     {
     212           0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     213           0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryErrorNotifier(this, message, ex.Message));
     214             :     }
     215           0 :     catch (const uno::RuntimeException& ex)
     216             :     {
     217           0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     218           0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryErrorNotifier(this, message, ex.Message));
     219             :     }
     220           0 : }
     221             : 
     222           0 : void MailDispatcher::run()
     223             : {
     224           0 :     osl_setThreadName("MailDispatcher");
     225             : 
     226             :     // acquire a self reference in order to avoid race
     227             :     // conditions. The last client of this class must
     228             :     // call shutdown before releasing his last reference
     229             :     // to this class in order to shutdown this thread
     230             :     // which will release his (the very last reference
     231             :     // to the class and so force their destruction
     232           0 :     m_xSelfReference = this;
     233             : 
     234             :     // signal that the mail dispatcher thread is now alive
     235           0 :     mail_dispatcher_active_.set();
     236             : 
     237             :     for(;;)
     238             :     {
     239           0 :         wakening_call_.wait();
     240             : 
     241           0 :         ::osl::ClearableMutexGuard thread_status_guard(thread_status_mutex_);
     242           0 :         if (shutdown_requested_)
     243           0 :            break;
     244             : 
     245           0 :         ::osl::ClearableMutexGuard message_container_guard(message_container_mutex_);
     246             : 
     247           0 :         if (messages_.size())
     248             :         {
     249           0 :             thread_status_guard.clear();
     250           0 :             uno::Reference<mail::XMailMessage> message = messages_.front();
     251           0 :             messages_.pop_front();
     252           0 :             message_container_guard.clear();
     253           0 :             sendMailMessageNotifyListener(message);
     254             :         }
     255             :         else // idle - put ourself to sleep
     256             :         {
     257           0 :             wakening_call_.reset();
     258           0 :             message_container_guard.clear();
     259           0 :             thread_status_guard.clear();
     260           0 :             MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     261           0 :             std::for_each(listeners_cloned.begin(), listeners_cloned.end(), GenericEventNotifier(&IMailDispatcherListener::idle, this));
     262             :         }
     263           0 :     } // end for        SSH ALI
     264           0 : }
     265             : 
     266           0 : void MailDispatcher::onTerminated()
     267             : {
     268             :     //keep the reference until the end of onTerminated() because of the call order in the
     269             :     //_threadFunc() from osl/thread.hxx
     270           0 :     m_xSelfReference = 0;
     271           0 : }
     272             : 
     273             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10