1 /* 2 * Copyright (c) 2024 Huawei Device 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 * Description: channel manager 15 * Author: sunhong 16 * Create: 2022-01-19 17 */ 18 19 #ifndef CHANNEL_MANAGER_H 20 #define CHANNEL_MANAGER_H 21 22 #include <map> 23 #include <mutex> 24 #include <string> 25 #include <memory> 26 #include "channel_request.h" 27 #include "connection.h" 28 #include "channel_listener.h" 29 #include "channel_manager_listener.h" 30 #include "channel_info.h" 31 32 namespace OHOS { 33 namespace CastEngine { 34 namespace CastEngineService { 35 class ChannelManager { 36 public: 37 ChannelManager(const int sessionIndex, std::shared_ptr<IChannelManagerListener> channelManagerListener); 38 ~ChannelManager(); 39 40 int CreateChannel(ChannelRequest &request, std::shared_ptr<IChannelListener> channelListener); 41 int CreateChannel(ChannelRequest &request, std::shared_ptr<IChannelListener> channelListener, 42 ChannelFileSchema &fileSchema); 43 bool IsAllChannelOpened() const; 44 bool DestroyChannel(const Channel &channel); 45 bool DestroyChannel(ModuleType moduleType); 46 void DestroyAllChannels(); 47 48 private: 49 class ConnectionListenerInner : public ConnectionListener { 50 public: ConnectionListenerInner(std::shared_ptr<IChannelManagerListener> listener)51 explicit ConnectionListenerInner(std::shared_ptr<IChannelManagerListener> listener) 52 : channelManagerListenerInner_(listener) {}; 53 ~ConnectionListenerInner()54 ~ConnectionListenerInner() {}; 55 56 private: 57 std::shared_ptr<IChannelManagerListener> channelManagerListenerInner_; 58 OnConnectionOpened(std::shared_ptr<Channel> channel)59 bool OnConnectionOpened(std::shared_ptr<Channel> channel) override 60 { 61 channelManagerListenerInner_->OnChannelCreated(channel); 62 return true; 63 } 64 OnConnectionConnectFailed(ChannelRequest & channelRequest,int errorCode)65 void OnConnectionConnectFailed(ChannelRequest &channelRequest, int errorCode) override 66 { 67 channelManagerListenerInner_->OnChannelOpenFailed(channelRequest, errorCode); 68 } 69 OnConnectionClosed(std::shared_ptr<Channel> channel)70 void OnConnectionClosed(std::shared_ptr<Channel> channel) override 71 { 72 channelManagerListenerInner_->OnChannelRemoved(channel); 73 } 74 OnConnectionError(std::shared_ptr<Channel> channel,int errorCode)75 void OnConnectionError(std::shared_ptr<Channel> channel, int errorCode) override 76 { 77 channelManagerListenerInner_->OnChannelError(channel, errorCode); 78 } 79 }; 80 81 bool IsRequestValid(const ChannelRequest &request) const; 82 std::shared_ptr<Connection> GetConnection(ChannelLinkType linkType); 83 84 static const int RET_ERR = -1; 85 int sessionId_{ -1 }; 86 int sessionIndex_{ -1 }; 87 int connectionNum_{ 0 }; 88 std::map<ChannelRequest, std::shared_ptr<Connection>> connectionMap_; 89 std::mutex connectionMapMtx_; 90 std::shared_ptr<IChannelManagerListener> channelManagerListener_; 91 std::shared_ptr<ConnectionListener> connectionListener_; 92 }; 93 } // namespace CastEngineService 94 } // namespace CastEngine 95 } // namespace OHOS 96 97 98 #endif // CHANNEL_MANAGER_H 99