• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_SHARING_CONTEXT_H
17 #define OHOS_SHARING_CONTEXT_H
18 
19 #include <unordered_map>
20 #include "agent/agent.h"
21 #include "common/identifier.h"
22 #include "event/handle_event_base.h"
23 
24 namespace OHOS {
25 namespace Sharing {
26 
27 class Context : public EventEmitter,
28                 public IdentifierInfo,
29                 public HandleEventBase,
30                 public IAgentListener,
31                 public std::enable_shared_from_this<Context> {
32 public:
33     using Ptr = std::shared_ptr<Context>;
34 
35     Context();
36     virtual ~Context();
37 
38     bool IsEmptyAgent();
39     bool IsEmptySrcAgent(uint32_t sinkAgentId);
40     int32_t GetSrcAgentSize();
41     int32_t GetSinkAgentSize();
42     int32_t HandleEvent(SharingEvent &event) override;
43 
44     void Release();
45     void OnAgentNotify(AgentStatusMsg::Ptr &statusMsg) override;
46     void SendInteractionEvent(EventType eventType, ContextEventMsg::Ptr &eventMsg);
47 
48 private:
49     void RemoveAgent(uint32_t agentId);
50     void DistributeEvent(SharingEvent &event);
51     void CheckNeedDestroySink(uint32_t sinkAgentId);
52     Agent::Ptr GetAgentById(uint32_t agentId);
53 
54     void HandleAgentDestroy(SharingEvent &event);
55     void HandleStateDestroyAgent(SharingEvent &event);
56     uint32_t HandleCreateAgent(const std::string &designation, AgentType agentType, uint32_t sinkAgentId);
57 
58 private:
59     uint32_t interactionId_ = INVALID_ID;
60 
61     std::mutex mutex_;
62     std::atomic<bool> destroying_ = false;
63     std::unordered_map<int32_t, Agent::Ptr> agents_;
64 };
65 
66 } // namespace Sharing
67 } // namespace OHOS
68 #endif
69