RemotePathChecker.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright (c) 2014 ownCloud GmbH. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; version 2.1 of the License
  7. *
  8. * This library is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  11. * details.
  12. */
  13. #ifndef PATHCHECKER_H
  14. #define PATHCHECKER_H
  15. #include <string>
  16. #include <vector>
  17. #include <unordered_map>
  18. #include <queue>
  19. #include <thread>
  20. #include <memory>
  21. #include <mutex>
  22. #include <atomic>
  23. #include <condition_variable>
  24. #pragma once
  25. class RemotePathChecker {
  26. public:
  27. enum FileState {
  28. // Order synced with NCOverlay
  29. StateError = 0,
  30. StateOk, StateOkSWM,
  31. StateSync,
  32. StateWarning,
  33. StateNone
  34. };
  35. RemotePathChecker();
  36. ~RemotePathChecker();
  37. std::shared_ptr<const std::vector<std::wstring>> WatchedDirectories() const;
  38. bool IsMonitoredPath(const wchar_t* filePath, int* state);
  39. private:
  40. FileState _StrToFileState(const std::wstring &str);
  41. std::mutex _mutex;
  42. std::atomic<bool> _stop;
  43. // Everything here is protected by the _mutex
  44. /** The list of paths we need to query. The main thread fill this, and the worker thread
  45. * send that to the socket. */
  46. std::queue<std::wstring> _pending;
  47. std::unordered_map<std::wstring, FileState> _cache;
  48. // The vector is const since it will be accessed from multiple threads through NCOverlay::IsMemberOf.
  49. // Each modification needs to be made onto a copy and then atomically replaced in the shared_ptr.
  50. std::shared_ptr<const std::vector<std::wstring>> _watchedDirectories;
  51. bool _connected;
  52. // The main thread notifies when there are new items in _pending
  53. //std::condition_variable _newQueries;
  54. HANDLE _newQueries;
  55. std::thread _thread;
  56. void workerThreadLoop();
  57. };
  58. #endif