LCOV - code coverage report
Current view: top level - libreoffice/sw/source/ui/dbui - maildispatcher.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 111 0.0 %
Date: 2012-12-27 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             : using ::rtl::OUString;
      27             : 
      28             : typedef std::list< ::rtl::Reference<IMailDispatcherListener> > MailDispatcherListenerContainer_t;
      29             : 
      30             : namespace /* private */
      31             : {
      32             :     /* Generic event notifier for started,
      33             :        stopped, and idle events which are
      34             :        very similary */
      35           0 :     class GenericEventNotifier
      36             :     {
      37             :     public:
      38             :         // pointer to virtual function typedef
      39             :         typedef void (IMailDispatcherListener::*GenericNotificationFunc_t)(::rtl::Reference<MailDispatcher>);
      40             : 
      41           0 :         GenericEventNotifier(
      42             :             GenericNotificationFunc_t notification_function,
      43             :             ::rtl::Reference<MailDispatcher> mail_dispatcher) :
      44             :             notification_function_(notification_function),
      45           0 :             mail_dispatcher_(mail_dispatcher)
      46           0 :         {}
      47             : 
      48           0 :         void operator() (::rtl::Reference<IMailDispatcherListener> listener) const
      49           0 :         { (listener.get()->*notification_function_)(mail_dispatcher_); }
      50             : 
      51             :     private:
      52             :         GenericNotificationFunc_t notification_function_;
      53             :         ::rtl::Reference<MailDispatcher> mail_dispatcher_;
      54             :     };
      55             : 
      56           0 :     class MailDeliveryNotifier
      57             :     {
      58             :     public:
      59           0 :         MailDeliveryNotifier(::rtl::Reference<MailDispatcher> xMailDispatcher, uno::Reference<mail::XMailMessage> message) :
      60             :             mail_dispatcher_(xMailDispatcher),
      61           0 :             message_(message)
      62           0 :         {}
      63             : 
      64           0 :         void operator() (::rtl::Reference<IMailDispatcherListener> listener) const
      65           0 :         { listener->mailDelivered(mail_dispatcher_, message_); }
      66             : 
      67             :     private:
      68             :         ::rtl::Reference<MailDispatcher> mail_dispatcher_;
      69             :         uno::Reference<mail::XMailMessage> message_;
      70             :     };
      71             : 
      72           0 :     class MailDeliveryErrorNotifier
      73             :     {
      74             :     public:
      75           0 :         MailDeliveryErrorNotifier(
      76             :             ::rtl::Reference<MailDispatcher> xMailDispatcher,
      77             :             uno::Reference<mail::XMailMessage> message,
      78             :             const ::rtl::OUString& error_message) :
      79             :             mail_dispatcher_(xMailDispatcher),
      80             :             message_(message),
      81           0 :             error_message_(error_message)
      82           0 :         {}
      83             : 
      84           0 :         void operator() (::rtl::Reference<IMailDispatcherListener> listener) const
      85           0 :         { listener->mailDeliveryError(mail_dispatcher_, message_, error_message_); }
      86             : 
      87             :     private:
      88             :         ::rtl::Reference<MailDispatcher> mail_dispatcher_;
      89             :         uno::Reference<mail::XMailMessage> message_;
      90             :         ::rtl::OUString error_message_;
      91             :     };
      92             : 
      93             : } // namespace private
      94             : 
      95           0 : MailDispatcher::MailDispatcher(uno::Reference<mail::XSmtpService> mailserver) :
      96             :     mailserver_ (mailserver),
      97             :     run_(false),
      98           0 :     shutdown_requested_(false)
      99             : {
     100           0 :     wakening_call_.reset();
     101           0 :     mail_dispatcher_active_.reset();
     102             : 
     103           0 :     if (!create())
     104           0 :         throw uno::RuntimeException();
     105             : 
     106             :     // wait until the mail dispatcher thread is really alive
     107             :     // and has aquired a reference to this instance of the
     108             :     // class
     109           0 :     mail_dispatcher_active_.wait();
     110           0 : }
     111             : 
     112           0 : MailDispatcher::~MailDispatcher()
     113             : {
     114           0 : }
     115             : 
     116           0 : void MailDispatcher::enqueueMailMessage(uno::Reference<mail::XMailMessage> message)
     117             : {
     118           0 :     ::osl::MutexGuard thread_status_guard(thread_status_mutex_);
     119           0 :     ::osl::MutexGuard message_container_guard(message_container_mutex_);
     120             : 
     121             :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     122             : 
     123           0 :     messages_.push_back(message);
     124           0 :     if (run_)
     125           0 :         wakening_call_.set();
     126           0 : }
     127             : 
     128           0 : uno::Reference<mail::XMailMessage> MailDispatcher::dequeueMailMessage()
     129             : {
     130           0 :     ::osl::MutexGuard guard(message_container_mutex_);
     131           0 :     uno::Reference<mail::XMailMessage> message;
     132           0 :     if(!messages_.empty())
     133             :     {
     134           0 :         message = messages_.front();
     135           0 :         messages_.pop_front();
     136             :     }
     137           0 :     return message;
     138             : }
     139             : 
     140           0 : void MailDispatcher::start()
     141             : {
     142             :     OSL_PRECOND(!isStarted(), "MailDispatcher is already started!");
     143             : 
     144           0 :     ::osl::ClearableMutexGuard thread_status_guard(thread_status_mutex_);
     145             : 
     146             :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     147             : 
     148           0 :     if (!shutdown_requested_)
     149             :     {
     150           0 :         run_ = true;
     151           0 :         wakening_call_.set();
     152           0 :         thread_status_guard.clear();
     153             : 
     154           0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     155           0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), GenericEventNotifier(&IMailDispatcherListener::started, this));
     156           0 :     }
     157           0 : }
     158             : 
     159           0 : void MailDispatcher::stop()
     160             : {
     161             :     OSL_PRECOND(isStarted(), "MailDispatcher not started!");
     162             : 
     163           0 :     ::osl::ClearableMutexGuard thread_status_guard(thread_status_mutex_);
     164             : 
     165             :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     166             : 
     167           0 :     if (!shutdown_requested_)
     168             :     {
     169           0 :         run_ = false;
     170           0 :         wakening_call_.reset();
     171           0 :         thread_status_guard.clear();
     172             : 
     173           0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     174           0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), GenericEventNotifier(&IMailDispatcherListener::stopped, this));
     175           0 :     }
     176           0 : }
     177             : 
     178           0 : void MailDispatcher::shutdown()
     179             : {
     180           0 :     ::osl::MutexGuard thread_status_guard(thread_status_mutex_);
     181             : 
     182             :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     183             : 
     184           0 :     shutdown_requested_ = true;
     185           0 :     wakening_call_.set();
     186           0 : }
     187             : 
     188           0 : bool MailDispatcher::isStarted() const
     189             : {
     190           0 :     return run_;
     191             : }
     192             : 
     193           0 : void MailDispatcher::addListener(::rtl::Reference<IMailDispatcherListener> listener)
     194             : {
     195             :     OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
     196             : 
     197           0 :     ::osl::MutexGuard guard(listener_container_mutex_);
     198           0 :     listeners_.push_back(listener);
     199           0 : }
     200             : 
     201           0 : std::list< ::rtl::Reference<IMailDispatcherListener> > MailDispatcher::cloneListener()
     202             : {
     203           0 :     ::osl::MutexGuard guard(listener_container_mutex_);
     204           0 :     return listeners_;
     205             : }
     206             : 
     207           0 : void MailDispatcher::sendMailMessageNotifyListener(uno::Reference<mail::XMailMessage> message)
     208             : {
     209             :     try
     210             :     {
     211           0 :         mailserver_->sendMailMessage(message);
     212           0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     213           0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryNotifier(this, message));
     214             :     }
     215           0 :     catch (const mail::MailException& 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 :     catch (const uno::RuntimeException& ex)
     221             :     {
     222           0 :         MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     223           0 :         std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryErrorNotifier(this, message, ex.Message));
     224             :     }
     225           0 : }
     226             : 
     227           0 : void MailDispatcher::run()
     228             : {
     229             :     // aquire a self reference in order to avoid race
     230             :     // conditions. The last client of this class must
     231             :     // call shutdown before releasing his last reference
     232             :     // to this class in order to shutdown this thread
     233             :     // which will release his (the very last reference
     234             :     // to the class and so force their destruction
     235           0 :     m_xSelfReference = this;
     236             : 
     237             :     // signal that the mail dispatcher thread is now alive
     238           0 :     mail_dispatcher_active_.set();
     239             : 
     240           0 :     for(;;)
     241             :     {
     242           0 :         wakening_call_.wait();
     243             : 
     244           0 :         ::osl::ClearableMutexGuard thread_status_guard(thread_status_mutex_);
     245           0 :         if (shutdown_requested_)
     246             :            break;
     247             : 
     248           0 :         ::osl::ClearableMutexGuard message_container_guard(message_container_mutex_);
     249             : 
     250           0 :         if (messages_.size())
     251             :         {
     252           0 :             thread_status_guard.clear();
     253           0 :             uno::Reference<mail::XMailMessage> message = messages_.front();
     254           0 :             messages_.pop_front();
     255           0 :             message_container_guard.clear();
     256           0 :             sendMailMessageNotifyListener(message);
     257             :         }
     258             :         else // idle - put ourself to sleep
     259             :         {
     260           0 :             wakening_call_.reset();
     261           0 :             message_container_guard.clear();
     262           0 :             thread_status_guard.clear();
     263           0 :             MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
     264           0 :             std::for_each(listeners_cloned.begin(), listeners_cloned.end(), GenericEventNotifier(&IMailDispatcherListener::idle, this));
     265             :         }
     266           0 :     } // end for        SSH ALI
     267           0 : }
     268             : 
     269           0 : void MailDispatcher::onTerminated()
     270             : {
     271             :     //keep the reference until the end of onTerminated() because of the call order in the
     272             :     //_threadFunc() from osl/thread.hxx
     273           0 :     m_xSelfReference = 0;
     274           0 : }
     275             : 
     276             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10