LibreOffice Module ucb (master)  1
SerfLockStore.cxx
Go to the documentation of this file.
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;
35 
36 public:
37 
38  explicit TickerThread( SerfLockStore & rLockStore )
39  : osl::Thread(), m_bFinish( false ), m_rLockStore( rLockStore ) {}
40 
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 
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  {
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 
80  : m_pTickerThread( nullptr )
81  , m_bFinishing( false )
82 {
83 }
84 
85 
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 
102 {
103  return m_bFinishing;
104 }
105 
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 
119 {
120  osl::MutexGuard aGuard( m_aMutex );
121 
122  if ( m_pTickerThread )
123  {
125  m_pTickerThread->join();
126  delete m_pTickerThread;
127  m_pTickerThread = nullptr;
128  }
129 }
130 
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 
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 
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 
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 
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  {
206  = nlastChanceToSendRefreshRequest;
207  }
208  else
209  {
210  // refresh failed. stop auto-refresh.
212  }
213  }
214  }
215  }
216 }
217 
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void addLock(const OUString &rLock, const OUString &sToken, rtl::Reference< SerfSession > const &xSession, sal_Int32 nLastChanceToSendRefreshRequest)
TickerThread(SerfLockStore &rLockStore)
void updateLock(const OUString &rLock, sal_Int32 nLastChanceToSendRefreshRequest)
OUString getLockToken(const OUString &rLock)
sal_Int32 m_nLastChanceToSendRefreshRequest
virtual void SAL_CALL run() override
void removeLock(const OUString &rLock)
SerfLockStore & m_rLockStore
#define SAL_WARN_IF(condition, area, stream)
#define SAL_INFO(area, stream)
#define SAL_WARN(area, stream)
rtl::Reference< SerfSession > m_xSession