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 : #include <osl/diagnose.h>
26 :
27 : using namespace ::com::sun::star;
28 :
29 : typedef std::list< ::rtl::Reference<IMailDispatcherListener> > MailDispatcherListenerContainer_t;
30 :
31 : namespace /* private */
32 : {
33 : /* Generic event notifier for started,
34 : stopped, and idle events which are
35 : very similary */
36 0 : class GenericEventNotifier
37 : {
38 : public:
39 : // pointer to virtual function typedef
40 : typedef void (IMailDispatcherListener::*GenericNotificationFunc_t)(::rtl::Reference<MailDispatcher>);
41 :
42 0 : GenericEventNotifier(
43 : GenericNotificationFunc_t notification_function,
44 : ::rtl::Reference<MailDispatcher> mail_dispatcher) :
45 : notification_function_(notification_function),
46 0 : mail_dispatcher_(mail_dispatcher)
47 0 : {}
48 :
49 0 : void operator() (::rtl::Reference<IMailDispatcherListener> listener) const
50 0 : { (listener.get()->*notification_function_)(mail_dispatcher_); }
51 :
52 : private:
53 : GenericNotificationFunc_t notification_function_;
54 : ::rtl::Reference<MailDispatcher> mail_dispatcher_;
55 : };
56 :
57 0 : class MailDeliveryNotifier
58 : {
59 : public:
60 0 : MailDeliveryNotifier(::rtl::Reference<MailDispatcher> xMailDispatcher, uno::Reference<mail::XMailMessage> message) :
61 : mail_dispatcher_(xMailDispatcher),
62 0 : message_(message)
63 0 : {}
64 :
65 0 : void operator() (::rtl::Reference<IMailDispatcherListener> listener) const
66 0 : { listener->mailDelivered(mail_dispatcher_, message_); }
67 :
68 : private:
69 : ::rtl::Reference<MailDispatcher> mail_dispatcher_;
70 : uno::Reference<mail::XMailMessage> message_;
71 : };
72 :
73 0 : class MailDeliveryErrorNotifier
74 : {
75 : public:
76 0 : MailDeliveryErrorNotifier(
77 : ::rtl::Reference<MailDispatcher> xMailDispatcher,
78 : uno::Reference<mail::XMailMessage> message,
79 : const OUString& error_message) :
80 : mail_dispatcher_(xMailDispatcher),
81 : message_(message),
82 0 : error_message_(error_message)
83 0 : {}
84 :
85 0 : void operator() (::rtl::Reference<IMailDispatcherListener> listener) const
86 0 : { listener->mailDeliveryError(mail_dispatcher_, message_, error_message_); }
87 :
88 : private:
89 : ::rtl::Reference<MailDispatcher> mail_dispatcher_;
90 : uno::Reference<mail::XMailMessage> message_;
91 : OUString error_message_;
92 : };
93 :
94 : } // namespace private
95 :
96 0 : MailDispatcher::MailDispatcher(uno::Reference<mail::XSmtpService> mailserver) :
97 : mailserver_ (mailserver),
98 : run_(false),
99 0 : shutdown_requested_(false)
100 : {
101 0 : wakening_call_.reset();
102 0 : mail_dispatcher_active_.reset();
103 :
104 0 : if (!create())
105 0 : throw uno::RuntimeException();
106 :
107 : // wait until the mail dispatcher thread is really alive
108 : // and has acquired a reference to this instance of the
109 : // class
110 0 : mail_dispatcher_active_.wait();
111 0 : }
112 :
113 0 : MailDispatcher::~MailDispatcher()
114 : {
115 0 : }
116 :
117 0 : void MailDispatcher::enqueueMailMessage(uno::Reference<mail::XMailMessage> message)
118 : {
119 0 : ::osl::MutexGuard thread_status_guard(thread_status_mutex_);
120 0 : ::osl::MutexGuard message_container_guard(message_container_mutex_);
121 :
122 : OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
123 :
124 0 : messages_.push_back(message);
125 0 : if (run_)
126 0 : wakening_call_.set();
127 0 : }
128 :
129 0 : uno::Reference<mail::XMailMessage> MailDispatcher::dequeueMailMessage()
130 : {
131 0 : ::osl::MutexGuard guard(message_container_mutex_);
132 0 : uno::Reference<mail::XMailMessage> message;
133 0 : if(!messages_.empty())
134 : {
135 0 : message = messages_.front();
136 0 : messages_.pop_front();
137 : }
138 0 : return message;
139 : }
140 :
141 0 : void MailDispatcher::start()
142 : {
143 : OSL_PRECOND(!isStarted(), "MailDispatcher is already started!");
144 :
145 0 : ::osl::ClearableMutexGuard thread_status_guard(thread_status_mutex_);
146 :
147 : OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
148 :
149 0 : if (!shutdown_requested_)
150 : {
151 0 : run_ = true;
152 0 : wakening_call_.set();
153 0 : thread_status_guard.clear();
154 :
155 0 : MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
156 0 : std::for_each(listeners_cloned.begin(), listeners_cloned.end(), GenericEventNotifier(&IMailDispatcherListener::started, this));
157 0 : }
158 0 : }
159 :
160 0 : void MailDispatcher::stop()
161 : {
162 : OSL_PRECOND(isStarted(), "MailDispatcher not started!");
163 :
164 0 : ::osl::ClearableMutexGuard thread_status_guard(thread_status_mutex_);
165 :
166 : OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
167 :
168 0 : if (!shutdown_requested_)
169 : {
170 0 : run_ = false;
171 0 : wakening_call_.reset();
172 0 : thread_status_guard.clear();
173 :
174 0 : MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
175 0 : std::for_each(listeners_cloned.begin(), listeners_cloned.end(), GenericEventNotifier(&IMailDispatcherListener::stopped, this));
176 0 : }
177 0 : }
178 :
179 0 : void MailDispatcher::shutdown()
180 : {
181 0 : ::osl::MutexGuard thread_status_guard(thread_status_mutex_);
182 :
183 : OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
184 :
185 0 : shutdown_requested_ = true;
186 0 : wakening_call_.set();
187 0 : }
188 :
189 :
190 0 : void MailDispatcher::addListener(::rtl::Reference<IMailDispatcherListener> listener)
191 : {
192 : OSL_PRECOND(!shutdown_requested_, "MailDispatcher thread is shuting down already");
193 :
194 0 : ::osl::MutexGuard guard(listener_container_mutex_);
195 0 : listeners_.push_back(listener);
196 0 : }
197 :
198 0 : std::list< ::rtl::Reference<IMailDispatcherListener> > MailDispatcher::cloneListener()
199 : {
200 0 : ::osl::MutexGuard guard(listener_container_mutex_);
201 0 : return listeners_;
202 : }
203 :
204 0 : void MailDispatcher::sendMailMessageNotifyListener(uno::Reference<mail::XMailMessage> message)
205 : {
206 : try
207 : {
208 0 : mailserver_->sendMailMessage(message);
209 0 : MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
210 0 : std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryNotifier(this, message));
211 : }
212 0 : catch (const mail::MailException& ex)
213 : {
214 0 : MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
215 0 : std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryErrorNotifier(this, message, ex.Message));
216 : }
217 0 : catch (const uno::RuntimeException& ex)
218 : {
219 0 : MailDispatcherListenerContainer_t listeners_cloned(cloneListener());
220 0 : std::for_each(listeners_cloned.begin(), listeners_cloned.end(), MailDeliveryErrorNotifier(this, message, ex.Message));
221 : }
222 0 : }
223 :
224 0 : void MailDispatcher::run()
225 : {
226 0 : osl_setThreadName("MailDispatcher");
227 :
228 : // acquire 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: */
|