Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : *
6 : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : *
8 : * OpenOffice.org - a multi-platform office productivity suite
9 : *
10 : * This file is part of OpenOffice.org.
11 : *
12 : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : * it under the terms of the GNU Lesser General Public License version 3
14 : * only, as published by the Free Software Foundation.
15 : *
16 : * OpenOffice.org is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : * GNU Lesser General Public License version 3 for more details
20 : * (a copy is included in the LICENSE file that accompanied this code).
21 : *
22 : * You should have received a copy of the GNU Lesser General Public License
23 : * version 3 along with OpenOffice.org. If not, see
24 : * <http://www.openoffice.org/license.html>
25 : * for a copy of the LGPLv3 License.
26 : *
27 : ************************************************************************/
28 :
29 :
30 : #include "warnings_guard_ne_locks.h"
31 : #include <ne_uri.h>
32 : #include "rtl/ustring.hxx"
33 : #include "osl/time.h"
34 : #include "osl/thread.hxx"
35 : #include "salhelper/thread.hxx"
36 : #include "NeonSession.hxx"
37 : #include "NeonLockStore.hxx"
38 :
39 : using namespace webdav_ucp;
40 :
41 : namespace webdav_ucp {
42 :
43 0 : class TickerThread : public salhelper::Thread
44 : {
45 : bool m_bFinish;
46 : NeonLockStore & m_rLockStore;
47 :
48 : public:
49 :
50 0 : TickerThread( NeonLockStore & rLockStore )
51 : : Thread( "NeonTickerThread" ), m_bFinish( false ),
52 0 : m_rLockStore( rLockStore ) {}
53 :
54 0 : void finish() { m_bFinish = true; }
55 :
56 : private:
57 :
58 : virtual void execute();
59 : };
60 :
61 : } // namespace webdav_ucp
62 :
63 : // -------------------------------------------------------------------
64 0 : void TickerThread::execute()
65 : {
66 : OSL_TRACE( "TickerThread: start." );
67 :
68 : // we have to go through the loop more often to be able to finish ~quickly
69 0 : const int nNth = 25;
70 :
71 0 : int nCount = nNth;
72 0 : while ( !m_bFinish )
73 : {
74 0 : if ( nCount-- <= 0 )
75 : {
76 0 : m_rLockStore.refreshLocks();
77 0 : nCount = nNth;
78 : }
79 :
80 : TimeValue aTV;
81 0 : aTV.Seconds = 0;
82 0 : aTV.Nanosec = 1000000000 / nNth;
83 0 : salhelper::Thread::wait( aTV );
84 : }
85 :
86 : OSL_TRACE( "TickerThread: stop." );
87 0 : }
88 :
89 : // -------------------------------------------------------------------
90 0 : NeonLockStore::NeonLockStore()
91 0 : : m_pNeonLockStore( ne_lockstore_create() )
92 : {
93 : OSL_ENSURE( m_pNeonLockStore, "Unable to create neon lock store!" );
94 0 : }
95 :
96 : // -------------------------------------------------------------------
97 0 : NeonLockStore::~NeonLockStore()
98 : {
99 0 : stopTicker();
100 :
101 : // release active locks, if any.
102 : OSL_ENSURE( m_aLockInfoMap.empty(),
103 : "NeonLockStore::~NeonLockStore - Releasing active locks!" );
104 :
105 0 : LockInfoMap::const_iterator it( m_aLockInfoMap.begin() );
106 0 : const LockInfoMap::const_iterator end( m_aLockInfoMap.end() );
107 0 : while ( it != end )
108 : {
109 0 : NeonLock * pLock = (*it).first;
110 0 : (*it).second.xSession->UNLOCK( pLock );
111 :
112 0 : ne_lockstore_remove( m_pNeonLockStore, pLock );
113 0 : ne_lock_destroy( pLock );
114 :
115 0 : ++it;
116 : }
117 :
118 0 : ne_lockstore_destroy( m_pNeonLockStore );
119 0 : }
120 :
121 : // -------------------------------------------------------------------
122 0 : void NeonLockStore::startTicker()
123 : {
124 0 : osl::MutexGuard aGuard( m_aMutex );
125 :
126 0 : if ( !m_pTickerThread.is() )
127 : {
128 0 : m_pTickerThread = new TickerThread( *this );
129 0 : m_pTickerThread->launch();
130 0 : }
131 0 : }
132 :
133 : // -------------------------------------------------------------------
134 0 : void NeonLockStore::stopTicker()
135 : {
136 0 : osl::MutexGuard aGuard( m_aMutex );
137 :
138 0 : if ( m_pTickerThread.is() )
139 : {
140 0 : m_pTickerThread->finish();
141 0 : m_pTickerThread->join();
142 0 : m_pTickerThread.clear();
143 0 : }
144 0 : }
145 :
146 : // -------------------------------------------------------------------
147 0 : void NeonLockStore::registerSession( HttpSession * pHttpSession )
148 : {
149 0 : osl::MutexGuard aGuard( m_aMutex );
150 :
151 0 : ne_lockstore_register( m_pNeonLockStore, pHttpSession );
152 0 : }
153 :
154 : // -------------------------------------------------------------------
155 0 : NeonLock * NeonLockStore::findByUri( rtl::OUString const & rUri )
156 : {
157 0 : osl::MutexGuard aGuard( m_aMutex );
158 :
159 : ne_uri aUri;
160 : ne_uri_parse( rtl::OUStringToOString(
161 0 : rUri, RTL_TEXTENCODING_UTF8 ).getStr(), &aUri );
162 0 : return ne_lockstore_findbyuri( m_pNeonLockStore, &aUri );
163 : }
164 :
165 : // -------------------------------------------------------------------
166 0 : void NeonLockStore::addLock( NeonLock * pLock,
167 : rtl::Reference< NeonSession > const & xSession,
168 : sal_Int32 nLastChanceToSendRefreshRequest )
169 : {
170 0 : osl::MutexGuard aGuard( m_aMutex );
171 :
172 0 : ne_lockstore_add( m_pNeonLockStore, pLock );
173 0 : m_aLockInfoMap[ pLock ]
174 0 : = LockInfo( xSession, nLastChanceToSendRefreshRequest );
175 :
176 0 : startTicker();
177 0 : }
178 :
179 : // -------------------------------------------------------------------
180 0 : void NeonLockStore::updateLock( NeonLock * pLock,
181 : sal_Int32 nLastChanceToSendRefreshRequest )
182 : {
183 0 : osl::MutexGuard aGuard( m_aMutex );
184 :
185 0 : LockInfoMap::iterator it( m_aLockInfoMap.find( pLock ) );
186 : OSL_ENSURE( it != m_aLockInfoMap.end(),
187 : "NeonLockStore::updateLock: lock not found!" );
188 :
189 0 : if ( it != m_aLockInfoMap.end() )
190 : {
191 0 : (*it).second.nLastChanceToSendRefreshRequest
192 0 : = nLastChanceToSendRefreshRequest;
193 0 : }
194 0 : }
195 :
196 : // -------------------------------------------------------------------
197 0 : void NeonLockStore::removeLock( NeonLock * pLock )
198 : {
199 0 : osl::MutexGuard aGuard( m_aMutex );
200 :
201 0 : m_aLockInfoMap.erase( pLock );
202 0 : ne_lockstore_remove( m_pNeonLockStore, pLock );
203 :
204 0 : if ( m_aLockInfoMap.empty() )
205 0 : stopTicker();
206 0 : }
207 :
208 : // -------------------------------------------------------------------
209 0 : void NeonLockStore::refreshLocks()
210 : {
211 0 : osl::MutexGuard aGuard( m_aMutex );
212 :
213 0 : LockInfoMap::iterator it( m_aLockInfoMap.begin() );
214 0 : const LockInfoMap::const_iterator end( m_aLockInfoMap.end() );
215 0 : while ( it != end )
216 : {
217 0 : LockInfo & rInfo = (*it).second;
218 0 : if ( rInfo.nLastChanceToSendRefreshRequest != -1 )
219 : {
220 : // 30 seconds or less remaining until lock expires?
221 : TimeValue t1;
222 0 : osl_getSystemTime( &t1 );
223 0 : if ( rInfo.nLastChanceToSendRefreshRequest - 30
224 : <= sal_Int32( t1.Seconds ) )
225 : {
226 : // refresh the lock.
227 0 : sal_Int32 nlastChanceToSendRefreshRequest = -1;
228 0 : if ( rInfo.xSession->LOCK(
229 0 : (*it).first,
230 0 : /* out param */ nlastChanceToSendRefreshRequest ) )
231 : {
232 : rInfo.nLastChanceToSendRefreshRequest
233 0 : = nlastChanceToSendRefreshRequest;
234 : }
235 : else
236 : {
237 : // refresh failed. stop auto-refresh.
238 0 : rInfo.nLastChanceToSendRefreshRequest = -1;
239 : }
240 : }
241 : }
242 0 : ++it;
243 0 : }
244 0 : }
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|