Bump for 3.6-28
[LibreOffice.git] / ucb / source / ucp / webdav / NeonLockStore.cxx
blob77e62589f26ff3dc34d06a43db818899ba11df3e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
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.
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).
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.
27 ************************************************************************/
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"
39 using namespace webdav_ucp;
41 namespace webdav_ucp {
43 class TickerThread : public salhelper::Thread
45 bool m_bFinish;
46 NeonLockStore & m_rLockStore;
48 public:
50 TickerThread( NeonLockStore & rLockStore )
51 : Thread( "NeonTickerThread" ), m_bFinish( false ),
52 m_rLockStore( rLockStore ) {}
54 void finish() { m_bFinish = true; }
56 private:
58 virtual void execute();
61 } // namespace webdav_ucp
63 // -------------------------------------------------------------------
64 void TickerThread::execute()
66 OSL_TRACE( "TickerThread: start." );
68 // we have to go through the loop more often to be able to finish ~quickly
69 const int nNth = 25;
71 int nCount = nNth;
72 while ( !m_bFinish )
74 if ( nCount-- <= 0 )
76 m_rLockStore.refreshLocks();
77 nCount = nNth;
80 TimeValue aTV;
81 aTV.Seconds = 0;
82 aTV.Nanosec = 1000000000 / nNth;
83 salhelper::Thread::wait( aTV );
86 OSL_TRACE( "TickerThread: stop." );
89 // -------------------------------------------------------------------
90 NeonLockStore::NeonLockStore()
91 : m_pNeonLockStore( ne_lockstore_create() )
93 OSL_ENSURE( m_pNeonLockStore, "Unable to create neon lock store!" );
96 // -------------------------------------------------------------------
97 NeonLockStore::~NeonLockStore()
99 stopTicker();
101 // release active locks, if any.
102 OSL_ENSURE( m_aLockInfoMap.empty(),
103 "NeonLockStore::~NeonLockStore - Releasing active locks!" );
105 LockInfoMap::const_iterator it( m_aLockInfoMap.begin() );
106 const LockInfoMap::const_iterator end( m_aLockInfoMap.end() );
107 while ( it != end )
109 NeonLock * pLock = (*it).first;
110 (*it).second.xSession->UNLOCK( pLock );
112 ne_lockstore_remove( m_pNeonLockStore, pLock );
113 ne_lock_destroy( pLock );
115 ++it;
118 ne_lockstore_destroy( m_pNeonLockStore );
121 // -------------------------------------------------------------------
122 void NeonLockStore::startTicker()
124 osl::MutexGuard aGuard( m_aMutex );
126 if ( !m_pTickerThread.is() )
128 m_pTickerThread = new TickerThread( *this );
129 m_pTickerThread->launch();
133 // -------------------------------------------------------------------
134 void NeonLockStore::stopTicker()
136 osl::MutexGuard aGuard( m_aMutex );
138 if ( m_pTickerThread.is() )
140 m_pTickerThread->finish();
141 m_pTickerThread->join();
142 m_pTickerThread.clear();
146 // -------------------------------------------------------------------
147 void NeonLockStore::registerSession( HttpSession * pHttpSession )
149 osl::MutexGuard aGuard( m_aMutex );
151 ne_lockstore_register( m_pNeonLockStore, pHttpSession );
154 // -------------------------------------------------------------------
155 NeonLock * NeonLockStore::findByUri( rtl::OUString const & rUri )
157 osl::MutexGuard aGuard( m_aMutex );
159 ne_uri aUri;
160 ne_uri_parse( rtl::OUStringToOString(
161 rUri, RTL_TEXTENCODING_UTF8 ).getStr(), &aUri );
162 return ne_lockstore_findbyuri( m_pNeonLockStore, &aUri );
165 // -------------------------------------------------------------------
166 void NeonLockStore::addLock( NeonLock * pLock,
167 rtl::Reference< NeonSession > const & xSession,
168 sal_Int32 nLastChanceToSendRefreshRequest )
170 osl::MutexGuard aGuard( m_aMutex );
172 ne_lockstore_add( m_pNeonLockStore, pLock );
173 m_aLockInfoMap[ pLock ]
174 = LockInfo( xSession, nLastChanceToSendRefreshRequest );
176 startTicker();
179 // -------------------------------------------------------------------
180 void NeonLockStore::updateLock( NeonLock * pLock,
181 sal_Int32 nLastChanceToSendRefreshRequest )
183 osl::MutexGuard aGuard( m_aMutex );
185 LockInfoMap::iterator it( m_aLockInfoMap.find( pLock ) );
186 OSL_ENSURE( it != m_aLockInfoMap.end(),
187 "NeonLockStore::updateLock: lock not found!" );
189 if ( it != m_aLockInfoMap.end() )
191 (*it).second.nLastChanceToSendRefreshRequest
192 = nLastChanceToSendRefreshRequest;
196 // -------------------------------------------------------------------
197 void NeonLockStore::removeLock( NeonLock * pLock )
199 osl::MutexGuard aGuard( m_aMutex );
201 m_aLockInfoMap.erase( pLock );
202 ne_lockstore_remove( m_pNeonLockStore, pLock );
204 if ( m_aLockInfoMap.empty() )
205 stopTicker();
208 // -------------------------------------------------------------------
209 void NeonLockStore::refreshLocks()
211 osl::MutexGuard aGuard( m_aMutex );
213 LockInfoMap::iterator it( m_aLockInfoMap.begin() );
214 const LockInfoMap::const_iterator end( m_aLockInfoMap.end() );
215 while ( it != end )
217 LockInfo & rInfo = (*it).second;
218 if ( rInfo.nLastChanceToSendRefreshRequest != -1 )
220 // 30 seconds or less remaining until lock expires?
221 TimeValue t1;
222 osl_getSystemTime( &t1 );
223 if ( rInfo.nLastChanceToSendRefreshRequest - 30
224 <= sal_Int32( t1.Seconds ) )
226 // refresh the lock.
227 sal_Int32 nlastChanceToSendRefreshRequest = -1;
228 if ( rInfo.xSession->LOCK(
229 (*it).first,
230 /* out param */ nlastChanceToSendRefreshRequest ) )
232 rInfo.nLastChanceToSendRefreshRequest
233 = nlastChanceToSendRefreshRequest;
235 else
237 // refresh failed. stop auto-refresh.
238 rInfo.nLastChanceToSendRefreshRequest = -1;
242 ++it;
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */