client-cpp  0.10.0
ProfileManager.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef DEFAULTPROFILEMANAGER_HPP_
18 #define DEFAULTPROFILEMANAGER_HPP_
19 
20 #include <memory>
21 
27 #include "kaa/profile/gen/ProfileDefinitions.hpp"
29 
30 namespace kaa {
31 
37 public:
38  ProfileManager(IKaaClientContext &context) : profileContainer_(std::make_shared<DefaultProfileContainer>()), context_(context) { }
39 
44  virtual void setProfileContainer(IProfileContainerPtr container)
45  {
46  if (container) {
47  profileContainer_ = container;
48  }
49  }
50 
58  {
59  static AvroByteArrayConverter<KaaProfile> avroConverter;
60 
61  if (profileContainer_) {
62  return avroConverter.toByteArray(profileContainer_->getProfile());
63  }
64 #if KAA_PROFILE_SCHEMA_VERSION > 0
65  else {
66  throw KaaException("Profile container is not set!");
67  }
68 #endif
69 
70  return avroConverter.toByteArray(KaaProfile());
71  }
72 
77  {
78  SharedDataBuffer serializedProfile = getSerializedProfile();
79  if ((serializedProfile.first.get() && serializedProfile.second > 0)
80  || context_.getStatus().isProfileResyncNeeded()) {
81  transport_->sync();
82  }
83  }
84 
85  virtual bool isInitialized()
86  {
87 #if KAA_PROFILE_SCHEMA_VERSION > 0
88  return profileContainer_.operator bool();
89 #else
90  return true;
91 #endif
92  }
93 
98  virtual void setTransport(IProfileTransportPtr transport)
99  {
100  if (transport) {
101  transport_ = transport;
102  }
103  }
104 
105 private:
106  IProfileTransportPtr transport_;
107  IProfileContainerPtr profileContainer_;
108  IKaaClientContext &context_;
109 };
110 
111 } /* namespace kaa */
112 
113 #endif /* DEFAULTPROFILEMANAGER_HPP_ */
ProfileManager(IKaaClientContext &context)
std::shared_ptr< IProfileContainer > IProfileContainerPtr
virtual bool isInitialized()
SharedDataBuffer toByteArray(const T &datum)
SharedDataBuffer getSerializedProfile()
virtual bool isProfileResyncNeeded() const =0
virtual void setTransport(IProfileTransportPtr transport)
virtual void setProfileContainer(IProfileContainerPtr container)
virtual IKaaClientStateStorage & getStatus()=0
std::pair< boost::shared_array< std::uint8_t >, std::uint32_t > SharedDataBuffer
std::shared_ptr< IProfileTransport > IProfileTransportPtr