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 <rtl/ustring.hxx>
21 #include <sal/log.hxx>
22 #include <osl/time.h>
23 #include <osl/thread.hxx>
24 #include "SerfSession.hxx"
25 #include "SerfLockStore.hxx"
26 
27 using namespace http_dav_ucp;
28 
29 namespace http_dav_ucp {
30 
31 class TickerThread : public osl::Thread
32 {
33     bool m_bFinish;
34     SerfLockStore & m_rLockStore;
35 
36 public:
37 
TickerThread(SerfLockStore & rLockStore)38     explicit TickerThread( SerfLockStore & rLockStore )
39         : osl::Thread(), m_bFinish( false ), m_rLockStore( rLockStore ) {}
40 
finish()41     void finish() { m_bFinish = true; }
42 
43 protected:
44 
45     virtual void SAL_CALL run() override;
46 };
47 
48 } // namespace http_dav_ucp
49 
50 
run()51 void TickerThread::run()
52 {
53     osl_setThreadName("http_dav_ucp::TickerThread");
54 
55     SAL_INFO("ucb.ucp.webdav",  "TickerThread: start." );
56 
57     // we have to go through the loop more often to be able to finish ~quickly
58     const int nNth = 25;
59 
60     int nCount = nNth;
61     while ( !m_bFinish )
62     {
63         if ( nCount-- <= 0 )
64         {
65             m_rLockStore.refreshLocks();
66             nCount = nNth;
67         }
68 
69         TimeValue aTV;
70         aTV.Seconds = 0;
71         aTV.Nanosec = 1000000000 / nNth;
72         wait( aTV );
73     }
74 
75     SAL_INFO("ucb.ucp.webdav",  "TickerThread: stop." );
76 }
77 
78 
SerfLockStore()79 SerfLockStore::SerfLockStore()
80     : m_pTickerThread( nullptr )
81     , m_bFinishing( false )
82 {
83 }
84 
85 
~SerfLockStore()86 SerfLockStore::~SerfLockStore()
87 {
88     stopTicker();
89     m_bFinishing = true;
90 
91     // release active locks, if any.
92     SAL_WARN_IF( !m_aLockInfoMap.empty(), "ucb.ucp.webdav",
93                 "SerfLockStore::~SerfLockStore - Releasing active locks!" );
94 
95     for ( auto& rLockInfo : m_aLockInfoMap )
96     {
97         rLockInfo.second.m_xSession->UNLOCK( rLockInfo.first );
98     }
99 }
100 
finishing() const101 bool SerfLockStore::finishing() const
102 {
103     return m_bFinishing;
104 }
105 
startTicker()106 void SerfLockStore::startTicker()
107 {
108     osl::MutexGuard aGuard( m_aMutex );
109 
110     if ( !m_pTickerThread )
111     {
112         m_pTickerThread = new TickerThread( *this );
113         m_pTickerThread->create();
114     }
115 }
116 
117 
stopTicker()118 void SerfLockStore::stopTicker()
119 {
120     osl::MutexGuard aGuard( m_aMutex );
121 
122     if ( m_pTickerThread )
123     {
124         m_pTickerThread->finish();
125         m_pTickerThread->join();
126         delete m_pTickerThread;
127         m_pTickerThread = nullptr;
128     }
129 }
130 
getLockToken(const OUString & rLock)131 OUString SerfLockStore::getLockToken( const OUString& rLock )
132 {
133     osl::MutexGuard aGuard( m_aMutex );
134 
135     LockInfoMap::const_iterator it( m_aLockInfoMap.find( rLock ) );
136     if ( it != m_aLockInfoMap.end() )
137         return (*it).second.m_sToken;
138 
139     SAL_WARN("ucb.ucp.webdav", "SerfLockStore::getLockToken: lock not found!" );
140     return OUString();
141 }
142 
addLock(const OUString & rLock,const OUString & sToken,rtl::Reference<SerfSession> const & xSession,sal_Int32 nLastChanceToSendRefreshRequest)143 void SerfLockStore::addLock( const OUString& rLock,
144                              const OUString& sToken,
145                              rtl::Reference< SerfSession > const & xSession,
146                              sal_Int32 nLastChanceToSendRefreshRequest )
147 {
148     osl::MutexGuard aGuard( m_aMutex );
149 
150     m_aLockInfoMap[ rLock ]
151         = LockInfo( sToken, xSession, nLastChanceToSendRefreshRequest );
152 
153     startTicker();
154 }
155 
156 
updateLock(const OUString & rLock,sal_Int32 nLastChanceToSendRefreshRequest)157 void SerfLockStore::updateLock( const OUString& rLock,
158                                 sal_Int32 nLastChanceToSendRefreshRequest )
159 {
160     osl::MutexGuard aGuard( m_aMutex );
161 
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!" );
165 
166     if ( it != m_aLockInfoMap.end() )
167     {
168         (*it).second.m_nLastChanceToSendRefreshRequest
169             = nLastChanceToSendRefreshRequest;
170     }
171 }
172 
173 
removeLock(const OUString & rLock)174 void SerfLockStore::removeLock( const OUString& rLock )
175 {
176     osl::MutexGuard aGuard( m_aMutex );
177 
178     m_aLockInfoMap.erase( rLock );
179 
180     if ( m_aLockInfoMap.empty() )
181         stopTicker();
182 }
183 
184 
refreshLocks()185 void SerfLockStore::refreshLocks()
186 {
187     osl::MutexGuard aGuard( m_aMutex );
188 
189     for ( auto& rLockInfo : m_aLockInfoMap )
190     {
191         LockInfo & rInfo = rLockInfo.second;
192         if ( rInfo.m_nLastChanceToSendRefreshRequest != -1 )
193         {
194             // 30 seconds or less remaining until lock expires?
195             TimeValue t1;
196             osl_getSystemTime( &t1 );
197             if ( rInfo.m_nLastChanceToSendRefreshRequest - 30
198                      <= sal_Int32( t1.Seconds ) )
199             {
200                 // refresh the lock.
201                 sal_Int32 nlastChanceToSendRefreshRequest = -1;
202                 if ( rInfo.m_xSession->LOCK(
203                          rLockInfo.first, &nlastChanceToSendRefreshRequest ) )
204                 {
205                     rInfo.m_nLastChanceToSendRefreshRequest
206                         = nlastChanceToSendRefreshRequest;
207                 }
208                 else
209                 {
210                     // refresh failed. stop auto-refresh.
211                     rInfo.m_nLastChanceToSendRefreshRequest = -1;
212                 }
213             }
214         }
215     }
216 }
217 
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
219