diff --git a/src/impl/generic/external_commit_helper.cpp b/src/impl/generic/external_commit_helper.cpp new file mode 100644 index 00000000..66417642 --- /dev/null +++ b/src/impl/generic/external_commit_helper.cpp @@ -0,0 +1,46 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2015 Realm Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////// + +#include "external_commit_helper.hpp" + +#include "realm_coordinator.hpp" + +#include +#include + +using namespace realm; +using namespace realm::_impl; + +ExternalCommitHelper::ExternalCommitHelper(RealmCoordinator& parent) +: m_parent(parent) +, m_history(realm::make_client_history(parent.get_path(), parent.get_encryption_key().data())) +, m_sg(*m_history, parent.is_in_memory() ? SharedGroup::durability_MemOnly : SharedGroup::durability_Full, + parent.get_encryption_key().data()) +, m_thread(std::async(std::launch::async, [=] { + while (m_sg.wait_for_change()) { + m_parent.on_change(); + } +})) +{ +} + +ExternalCommitHelper::~ExternalCommitHelper() +{ + m_sg.wait_for_change_release(); + m_thread.wait(); // Wait for the thread to exit +} diff --git a/src/impl/generic/external_commit_helper.hpp b/src/impl/generic/external_commit_helper.hpp new file mode 100644 index 00000000..3249430e --- /dev/null +++ b/src/impl/generic/external_commit_helper.hpp @@ -0,0 +1,54 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2015 Realm Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////// + +#ifndef REALM_EXTERNAL_COMMIT_HELPER_HPP +#define REALM_EXTERNAL_COMMIT_HELPER_HPP + +#include + +#include + +namespace realm { +class ClientHistory; + +namespace _impl { +class RealmCoordinator; + +class ExternalCommitHelper { +public: + ExternalCommitHelper(RealmCoordinator& parent); + ~ExternalCommitHelper(); + + // A no-op in this version, but needed for the Apple version + void notify_others() { } + +private: + RealmCoordinator& m_parent; + + // The listener thread + std::future m_thread; + + // A shared group used to listen for changes + std::unique_ptr m_history; + SharedGroup m_sg; +}; + +} // namespace _impl +} // namespace realm + +#endif /* REALM_EXTERNAL_COMMIT_HELPER_HPP */ diff --git a/src/impl/realm_coordinator.hpp b/src/impl/realm_coordinator.hpp index ac889412..46cfcec3 100644 --- a/src/impl/realm_coordinator.hpp +++ b/src/impl/realm_coordinator.hpp @@ -45,6 +45,8 @@ public: const Schema* get_schema() const noexcept; uint64_t get_schema_version() const noexcept { return m_config.schema_version; } const std::string& get_path() const noexcept { return m_config.path; } + const std::vector& get_encryption_key() const noexcept { return m_config.encryption_key; } + bool is_in_memory() const noexcept { return m_config.in_memory; } // Asyncronously call notify() on every Realm instance for this coordinator's // path, including those in other processes