client-cpp  0.9.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  transport_->sync();
81  }
82  }
83 
84  virtual bool isInitialized()
85  {
86 #if KAA_PROFILE_SCHEMA_VERSION > 0
87  return profileContainer_.operator bool();
88 #else
89  return true;
90 #endif
91  }
92 
97  virtual void setTransport(IProfileTransportPtr transport)
98  {
99  if (transport) {
100  transport_ = transport;
101  }
102  }
103 
104 private:
105  IProfileTransportPtr transport_;
106  IProfileContainerPtr profileContainer_;
107  IKaaClientContext &context_;
108 };
109 
110 } /* namespace kaa */
111 
112 #endif /* DEFAULTPROFILEMANAGER_HPP_ */
ProfileManager(IKaaClientContext &context)
std::shared_ptr< IProfileContainer > IProfileContainerPtr
virtual bool isInitialized()
SharedDataBuffer toByteArray(const T &datum)
SharedDataBuffer getSerializedProfile()
virtual void setTransport(IProfileTransportPtr transport)
virtual void setProfileContainer(IProfileContainerPtr container)
std::pair< boost::shared_array< std::uint8_t >, std::uint32_t > SharedDataBuffer
std::shared_ptr< IProfileTransport > IProfileTransportPtr