| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BLIMP_HELIUM_OWNED_REGISTER_H_ | |
| 6 #define BLIMP_HELIUM_OWNED_REGISTER_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "blimp/helium/blimp_helium_export.h" | |
| 12 #include "blimp/helium/result.h" | |
| 13 #include "blimp/helium/revision_generator.h" | |
| 14 #include "blimp/helium/serializable_struct.h" | |
| 15 #include "blimp/helium/stream_helpers.h" | |
| 16 #include "blimp/helium/syncable.h" | |
| 17 #include "blimp/helium/syncable_common.h" | |
| 18 #include "blimp/helium/version_vector.h" | |
| 19 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 20 | |
| 21 namespace blimp { | |
| 22 namespace helium { | |
| 23 | |
| 24 template <class RegisterType> | |
| 25 struct OwnedRegisterChangeset : public SerializableStruct { | |
| 26 OwnedRegisterChangeset() : last_modified(this), value(this) {} | |
| 27 ~OwnedRegisterChangeset() override = default; | |
| 28 | |
| 29 Field<Revision> last_modified; | |
| 30 Field<RegisterType> value; | |
| 31 }; | |
| 32 | |
| 33 // A Syncable that gives write permissions only to the "owner" Peer, | |
| 34 // while the other Peer only receives and applies updates. | |
| 35 template <class RegisterType> | |
| 36 class OwnedRegister : public Syncable<OwnedRegisterChangeset<RegisterType>> { | |
| 37 public: | |
| 38 using ChangesetType = OwnedRegisterChangeset<RegisterType>; | |
| 39 | |
| 40 OwnedRegister(Peer running_as, Peer owner); | |
| 41 ~OwnedRegister() = default; | |
| 42 | |
| 43 void Set(const RegisterType& value); | |
| 44 const RegisterType& Get() const; | |
| 45 | |
| 46 // Syncable implementation. | |
| 47 std::unique_ptr<ChangesetType> CreateChangeset(Revision from) const override; | |
| 48 void ApplyChangeset(const ChangesetType& changeset) override; | |
| 49 void ReleaseBefore(Revision checkpoint) override; | |
| 50 bool ValidateChangeset(const ChangesetType& changeset) const override; | |
| 51 Revision GetRevision() const override; | |
| 52 void SetLocalUpdateCallback( | |
| 53 const base::Closure& local_update_callback) override; | |
| 54 | |
| 55 private: | |
| 56 base::Closure local_update_callback_; | |
| 57 Revision last_modified_ = 0; | |
| 58 Bias bias_; | |
| 59 RegisterType value_ = {}; | |
| 60 | |
| 61 DISALLOW_COPY_AND_ASSIGN(OwnedRegister); | |
| 62 }; | |
| 63 | |
| 64 template <class RegisterType> | |
| 65 OwnedRegister<RegisterType>::OwnedRegister(Peer owner, Peer running_as) | |
| 66 : bias_(ComputeBias(owner, running_as)) {} | |
| 67 | |
| 68 template <class RegisterType> | |
| 69 void OwnedRegister<RegisterType>::SetLocalUpdateCallback( | |
| 70 const base::Closure& local_update_callback) { | |
| 71 local_update_callback_ = local_update_callback; | |
| 72 } | |
| 73 | |
| 74 template <class RegisterType> | |
| 75 void OwnedRegister<RegisterType>::Set(const RegisterType& value) { | |
| 76 DCHECK(bias_ == Bias::LOCAL); | |
| 77 | |
| 78 value_ = value; | |
| 79 last_modified_ = GetNextRevision(); | |
| 80 | |
| 81 local_update_callback_.Run(); | |
| 82 } | |
| 83 | |
| 84 template <class RegisterType> | |
| 85 const RegisterType& OwnedRegister<RegisterType>::Get() const { | |
| 86 return value_; | |
| 87 } | |
| 88 | |
| 89 template <class RegisterType> | |
| 90 Revision OwnedRegister<RegisterType>::GetRevision() const { | |
| 91 if (bias_ == Bias::LOCAL) { | |
| 92 return last_modified_; | |
| 93 } else { | |
| 94 return 0; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 template <class RegisterType> | |
| 99 std::unique_ptr<typename OwnedRegister<RegisterType>::ChangesetType> | |
| 100 OwnedRegister<RegisterType>::CreateChangeset(Revision from) const { | |
| 101 DCHECK(bias_ == Bias::LOCAL); | |
| 102 | |
| 103 std::unique_ptr<ChangesetType> changeset = base::MakeUnique<ChangesetType>(); | |
| 104 changeset->last_modified.Set(last_modified_); | |
| 105 changeset->value.Set(value_); | |
| 106 return changeset; | |
| 107 } | |
| 108 | |
| 109 template <class RegisterType> | |
| 110 void OwnedRegister<RegisterType>::ApplyChangeset( | |
| 111 const ChangesetType& changeset) { | |
| 112 if (last_modified_ < changeset.last_modified()) { | |
| 113 value_ = changeset.value(); | |
| 114 last_modified_ = changeset.last_modified(); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 template <class RegisterType> | |
| 119 void OwnedRegister<RegisterType>::ReleaseBefore(Revision checkpoint) { | |
| 120 // no-op | |
| 121 } | |
| 122 | |
| 123 template <class RegisterType> | |
| 124 bool OwnedRegister<RegisterType>::ValidateChangeset( | |
| 125 const ChangesetType& changeset) const { | |
| 126 if (bias_ == Bias::LOCAL) { | |
| 127 return false; | |
| 128 } | |
| 129 | |
| 130 if (last_modified_ > changeset.last_modified()) { | |
| 131 return false; | |
| 132 } | |
| 133 | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 } // namespace helium | |
| 138 } // namespace blimp | |
| 139 | |
| 140 #endif // BLIMP_HELIUM_OWNED_REGISTER_H_ | |
| OLD | NEW |