client-cpp  0.6.1
EndpointRegistrationManager.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 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 ENDPOINTREGISTRATIONMANAGER_HPP_
18 #define ENDPOINTREGISTRATIONMANAGER_HPP_
19 
20 #include "kaa/KaaDefaults.hpp"
21 
22 #ifdef KAA_USE_EVENTS
23 
24 #include <map>
25 #include <list>
26 #include <string>
27 #include <memory>
28 
30 
36 
37 namespace kaa {
38 
39 class ClientStatus;
40 class SyncResponse;
41 class UserAttachRequest;
42 class IAttachedEndpointListListener;
43 
44 class EndpointRegistrationManager : public IEndpointRegistrationManager
45  , public IRegistrationProcessor
46 {
47 public:
48  EndpointRegistrationManager(IKaaClientStateStoragePtr status);
49 
50  virtual void regenerateEndpointAccessToken();
51 
52  virtual const std::string& getEndpointAccessToken() {
53  return endpointAccessToken_;
54  }
55 
56  virtual void attachEndpoint(const std::string& endpointAccessToken
57  , IEndpointAttachStatusListener* listener = nullptr);
58 
59  virtual void detachEndpoint(const std::string& endpointKeyHash
60  , IEndpointAttachStatusListener* listener = nullptr);
61 
62  virtual void detachEndpoint(IEndpointAttachStatusListener* listener = nullptr);
63 
64  virtual void attachUser(const std::string& userExternalId
65  , const std::string& userAccessToken
66  , IEndpointAttachStatusListener* listener = nullptr);
67 
68  virtual const AttachedEndpoints& getAttachedEndpoints();
69 
70  virtual void addAttachedEndpointListListener(IAttachedEndpointListListener *listener);
71 
72  virtual void removeAttachedEndpointListListener(IAttachedEndpointListListener *listener);
73 
74  virtual std::map<std::string, std::string> getEndpointsToAttach();
75  virtual std::map<std::string, std::string> getEndpointsToDetach();
76  virtual UserAttachRequestPtr getUserAttachRequest();
77 
78  virtual void onUserAttach(const UserSyncResponse::userAttachResponse_t& response);
79 
80  virtual void onEndpointsAttach(const std::vector<EndpointAttachResponse>& endpoints);
81  virtual void onEndpointsDetach(const std::vector<EndpointDetachResponse>& endpoints);
82 
83  virtual void onCurrentEndpointAttach(const UserAttachNotification& response);
84  virtual void onCurrentEndpointDetach(const UserDetachNotification& response);
85 
86  virtual bool isCurrentEndpointAttached() {
87  return status_->getEndpointAttachStatus();
88  }
89 
90  virtual void setTransport(UserTransport * transport) {
91  userTransport_ = transport;
92  if (userTransport_ != nullptr && (userAttachRequest_.get() != nullptr || !attachingEndpoints_.empty() || !detachingEndpoints_.empty())) {
93  userTransport_->sync();
94  }
95  }
96 
97  virtual void setAttachStatusListener(IEndpointAttachStatusListener* listener) {
98  if (listener != nullptr) {
99  attachStatusListener_ = listener;
100  }
101  }
102 
103 private:
104  void onEndpointAccessTokenChanged(const std::string& old);
105 
106 private:
107  struct EndpointOperationInfo {
108  std::string endpointData_;/* endpoint's token or hash */
109  IEndpointAttachStatusListener* listener_;
110  };
111 
112 
113 private:
115 
116  std::string endpointAccessToken_;
117  std::string endpointKeyHash_;
118 
119  UserAttachRequestPtr userAttachRequest_;
120 
121  std::map<std::string/*requestId*/, EndpointOperationInfo> attachingEndpoints_;
122  std::map<std::string/*requestId*/, EndpointOperationInfo> detachingEndpoints_;
123  std::map<std::string/*epToken*/, std::string/*epHash*/> attachedEndpoints_;
124  KAA_R_MUTEX_DECLARE(endpointsGuard_);
125 
126  UserTransport * userTransport_;
127 
128  KaaObservable<void (const AttachedEndpoints&), IAttachedEndpointListListener *> attachedEPListListeners_;
129  KAA_R_MUTEX_DECLARE(listenerGuard_);
130 
131  IEndpointAttachStatusListener* attachStatusListener_;
132 };
133 
134 }
135 
136 #endif
137 
138 #endif /* ENDPOINTREGISTRATIONMANAGER_HPP_ */
std::map< std::string, std::string > AttachedEndpoints
#define KAA_R_MUTEX_DECLARE(name)
Definition: KaaThread.hpp:73
std::shared_ptr< IKaaClientStateStorage > IKaaClientStateStoragePtr