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>
21 #include <sal/log.hxx>
23 #include <osl/thread.hxx>
24 #include "SerfSession.hxx"
25 #include "SerfLockStore.hxx"
27 using namespace http_dav_ucp
;
29 namespace http_dav_ucp
{
31 class TickerThread
: public osl::Thread
34 SerfLockStore
& m_rLockStore
;
38 explicit TickerThread( SerfLockStore
& rLockStore
)
39 : osl::Thread(), m_bFinish( false ), m_rLockStore( rLockStore
) {}
41 void finish() { m_bFinish
= true; }
45 virtual void SAL_CALL
run() override
;
48 } // namespace http_dav_ucp
51 void TickerThread::run()
53 osl_setThreadName("http_dav_ucp::TickerThread");
55 SAL_INFO("ucb.ucp.webdav", "TickerThread: start." );
57 // we have to go through the loop more often to be able to finish ~quickly
65 m_rLockStore
.refreshLocks();
71 aTV
.Nanosec
= 1000000000 / nNth
;
75 SAL_INFO("ucb.ucp.webdav", "TickerThread: stop." );
79 SerfLockStore::SerfLockStore()
80 : m_pTickerThread( nullptr )
81 , m_bFinishing( false )
86 SerfLockStore::~SerfLockStore()
91 // release active locks, if any.
92 SAL_WARN_IF( !m_aLockInfoMap
.empty(), "ucb.ucp.webdav",
93 "SerfLockStore::~SerfLockStore - Releasing active locks!" );
95 for ( auto& rLockInfo
: m_aLockInfoMap
)
97 rLockInfo
.second
.m_xSession
->UNLOCK( rLockInfo
.first
);
101 bool SerfLockStore::finishing() const
106 void SerfLockStore::startTicker()
108 osl::MutexGuard
aGuard( m_aMutex
);
110 if ( !m_pTickerThread
)
112 m_pTickerThread
= new TickerThread( *this );
113 m_pTickerThread
->create();
118 void SerfLockStore::stopTicker()
120 osl::MutexGuard
aGuard( m_aMutex
);
122 if ( m_pTickerThread
)
124 m_pTickerThread
->finish();
125 m_pTickerThread
->join();
126 delete m_pTickerThread
;
127 m_pTickerThread
= nullptr;
131 OUString
SerfLockStore::getLockToken( const OUString
& rLock
)
133 osl::MutexGuard
aGuard( m_aMutex
);
135 LockInfoMap::const_iterator
it( m_aLockInfoMap
.find( rLock
) );
136 if ( it
!= m_aLockInfoMap
.end() )
137 return (*it
).second
.m_sToken
;
139 SAL_WARN("ucb.ucp.webdav", "SerfLockStore::getLockToken: lock not found!" );
143 void SerfLockStore::addLock( const OUString
& rLock
,
144 const OUString
& sToken
,
145 rtl::Reference
< SerfSession
> const & xSession
,
146 sal_Int32 nLastChanceToSendRefreshRequest
)
148 osl::MutexGuard
aGuard( m_aMutex
);
150 m_aLockInfoMap
[ rLock
]
151 = LockInfo( sToken
, xSession
, nLastChanceToSendRefreshRequest
);
157 void SerfLockStore::updateLock( const OUString
& rLock
,
158 sal_Int32 nLastChanceToSendRefreshRequest
)
160 osl::MutexGuard
aGuard( m_aMutex
);
162 LockInfoMap::iterator
it( m_aLockInfoMap
.find( rLock
) );
163 SAL_WARN_IF( it
== m_aLockInfoMap
.end(), "ucb.ucp.webdav",
164 "SerfLockStore::updateLock: lock not found!" );
166 if ( it
!= m_aLockInfoMap
.end() )
168 (*it
).second
.m_nLastChanceToSendRefreshRequest
169 = nLastChanceToSendRefreshRequest
;
174 void SerfLockStore::removeLock( const OUString
& rLock
)
176 osl::MutexGuard
aGuard( m_aMutex
);
178 m_aLockInfoMap
.erase( rLock
);
180 if ( m_aLockInfoMap
.empty() )
185 void SerfLockStore::refreshLocks()
187 osl::MutexGuard
aGuard( m_aMutex
);
189 for ( auto& rLockInfo
: m_aLockInfoMap
)
191 LockInfo
& rInfo
= rLockInfo
.second
;
192 if ( rInfo
.m_nLastChanceToSendRefreshRequest
!= -1 )
194 // 30 seconds or less remaining until lock expires?
196 osl_getSystemTime( &t1
);
197 if ( rInfo
.m_nLastChanceToSendRefreshRequest
- 30
198 <= sal_Int32( t1
.Seconds
) )
201 sal_Int32 nlastChanceToSendRefreshRequest
= -1;
202 if ( rInfo
.m_xSession
->LOCK(
203 rLockInfo
.first
, &nlastChanceToSendRefreshRequest
) )
205 rInfo
.m_nLastChanceToSendRefreshRequest
206 = nlastChanceToSendRefreshRequest
;
210 // refresh failed. stop auto-refresh.
211 rInfo
.m_nLastChanceToSendRefreshRequest
= -1;
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */