1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/ustring.hxx>
22 #include <osl/thread.hxx>
23 #include "SerfSession.hxx"
24 #include "SerfLockStore.hxx"
26 using namespace http_dav_ucp
;
28 namespace http_dav_ucp
{
30 class TickerThread
: public osl::Thread
33 SerfLockStore
& m_rLockStore
;
37 TickerThread( SerfLockStore
& rLockStore
)
38 : osl::Thread(), m_bFinish( false ), m_rLockStore( rLockStore
) {}
40 void finish() { m_bFinish
= true; }
44 virtual void SAL_CALL
run() SAL_OVERRIDE
;
47 } // namespace http_dav_ucp
50 void TickerThread::run()
52 osl_setThreadName("http_dav_ucp::TickerThread");
54 SAL_INFO("ucb.ucp.webdav", "TickerThread: start." );
56 // we have to go through the loop more often to be able to finish ~quickly
64 m_rLockStore
.refreshLocks();
70 aTV
.Nanosec
= 1000000000 / nNth
;
74 SAL_INFO("ucb.ucp.webdav", "TickerThread: stop." );
78 SerfLockStore::SerfLockStore()
79 : m_pTickerThread( 0 )
80 , m_bFinishing( false )
85 SerfLockStore::~SerfLockStore()
90 // release active locks, if any.
91 SAL_WARN_IF( !m_aLockInfoMap
.empty(), "ucb.ucp.webdav",
92 "SerfLockStore::~SerfLockStore - Releasing active locks!" );
94 LockInfoMap::const_iterator
it( m_aLockInfoMap
.begin() );
95 const LockInfoMap::const_iterator
end( m_aLockInfoMap
.end() );
98 (*it
).second
.m_xSession
->UNLOCK( (*it
).first
);
103 bool SerfLockStore::finishing() const
108 void SerfLockStore::startTicker()
110 osl::MutexGuard
aGuard( m_aMutex
);
112 if ( !m_pTickerThread
)
114 m_pTickerThread
= new TickerThread( *this );
115 m_pTickerThread
->create();
120 void SerfLockStore::stopTicker()
122 osl::MutexGuard
aGuard( m_aMutex
);
124 if ( m_pTickerThread
)
126 m_pTickerThread
->finish();
127 m_pTickerThread
->join();
128 delete m_pTickerThread
;
133 OUString
SerfLockStore::getLockToken( const OUString
& rLock
)
135 osl::MutexGuard
aGuard( m_aMutex
);
137 LockInfoMap::const_iterator
it( m_aLockInfoMap
.find( rLock
) );
138 if ( it
!= m_aLockInfoMap
.end() )
139 return (*it
).second
.m_sToken
;
141 SAL_WARN("ucb.ucp.webdav", "SerfLockStore::getLockToken: lock not found!" );
145 void SerfLockStore::addLock( const OUString
& rLock
,
146 const OUString
& sToken
,
147 rtl::Reference
< SerfSession
> const & xSession
,
148 sal_Int32 nLastChanceToSendRefreshRequest
)
150 osl::MutexGuard
aGuard( m_aMutex
);
152 m_aLockInfoMap
[ rLock
]
153 = LockInfo( sToken
, xSession
, nLastChanceToSendRefreshRequest
);
159 void SerfLockStore::updateLock( const OUString
& rLock
,
160 sal_Int32 nLastChanceToSendRefreshRequest
)
162 osl::MutexGuard
aGuard( m_aMutex
);
164 LockInfoMap::iterator
it( m_aLockInfoMap
.find( rLock
) );
165 SAL_WARN_IF( it
== m_aLockInfoMap
.end(), "ucb.ucp.webdav",
166 "SerfLockStore::updateLock: lock not found!" );
168 if ( it
!= m_aLockInfoMap
.end() )
170 (*it
).second
.m_nLastChanceToSendRefreshRequest
171 = nLastChanceToSendRefreshRequest
;
176 void SerfLockStore::removeLock( const OUString
& rLock
)
178 osl::MutexGuard
aGuard( m_aMutex
);
180 m_aLockInfoMap
.erase( rLock
);
182 if ( m_aLockInfoMap
.empty() )
187 void SerfLockStore::refreshLocks()
189 osl::MutexGuard
aGuard( m_aMutex
);
191 LockInfoMap::iterator
it( m_aLockInfoMap
.begin() );
192 const LockInfoMap::const_iterator
end( m_aLockInfoMap
.end() );
195 LockInfo
& rInfo
= (*it
).second
;
196 if ( rInfo
.m_nLastChanceToSendRefreshRequest
!= -1 )
198 // 30 seconds or less remaining until lock expires?
200 osl_getSystemTime( &t1
);
201 if ( rInfo
.m_nLastChanceToSendRefreshRequest
- 30
202 <= sal_Int32( t1
.Seconds
) )
205 sal_Int32 nlastChanceToSendRefreshRequest
= -1;
206 if ( rInfo
.m_xSession
->LOCK(
207 (*it
).first
, &nlastChanceToSendRefreshRequest
) )
209 rInfo
.m_nLastChanceToSendRefreshRequest
210 = nlastChanceToSendRefreshRequest
;
214 // refresh failed. stop auto-refresh.
215 rInfo
.m_nLastChanceToSendRefreshRequest
= -1;
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */