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