#include "core/internal/pcp_manager.h" #include "core/internal/p2p_cluster_pcp_handler.h" #include "core/internal/p2p_point_to_point_pcp_handler.h" #include "core/internal/p2p_star_pcp_handler.h" namespace location { namespace nearby { namespace connections { template PCPManager::PCPManager( Ptr > medium_manager, Ptr endpoint_channel_manager, Ptr > endpoint_manager, Ptr bandwidth_upgrade_manager) : pcp_handlers_(), current_pcp_handler_() { pcp_handlers_[PCP::P2P_CLUSTER] = MakePtr(new P2PClusterPCPHandler( medium_manager, endpoint_manager, endpoint_channel_manager, bandwidth_upgrade_manager)); pcp_handlers_[PCP::P2P_STAR] = MakePtr(new P2PStarPCPHandler( medium_manager, endpoint_manager, endpoint_channel_manager, bandwidth_upgrade_manager)); pcp_handlers_[PCP::P2P_POINT_TO_POINT] = MakePtr(new P2PPointToPointPCPHandler( medium_manager, endpoint_manager, endpoint_channel_manager, bandwidth_upgrade_manager)); } template PCPManager::~PCPManager() { // TODO(tracyzhou): Add logging. // clear() instead of destroy() because this is just a reference -- the real // object will be destroyed in the loop below. current_pcp_handler_.clear(); for (typename PCPHandlersMap::iterator it = pcp_handlers_.begin(); it != pcp_handlers_.end(); it++) { it->second.destroy(); } pcp_handlers_.clear(); } template Status::Value PCPManager::startAdvertising( Ptr > client_proxy, const string& endpoint_name, const string& service_id, const AdvertisingOptions& advertising_options, Ptr connection_lifecycle_listener) { if (!setCurrentPCPHandler(advertising_options.strategy)) { return Status::ERROR; } return current_pcp_handler_->startAdvertising( client_proxy, service_id, endpoint_name, advertising_options, connection_lifecycle_listener); } template void PCPManager::stopAdvertising( Ptr > client_proxy) { if (!current_pcp_handler_.isNull()) { current_pcp_handler_->stopAdvertising(client_proxy); } } template Status::Value PCPManager::startDiscovery( Ptr > client_proxy, const string& service_id, const DiscoveryOptions& discovery_options, Ptr discovery_listener) { if (!setCurrentPCPHandler(discovery_options.strategy)) { return Status::ERROR; } return current_pcp_handler_->startDiscovery( client_proxy, service_id, discovery_options, discovery_listener); } template void PCPManager::stopDiscovery( Ptr > client_proxy) { if (!current_pcp_handler_.isNull()) { current_pcp_handler_->stopDiscovery(client_proxy); } } template Status::Value PCPManager::requestConnection( Ptr > client_proxy, const string& endpoint_name, const string& endpoint_id, Ptr connection_lifecycle_listener) { if (current_pcp_handler_.isNull()) { return Status::OUT_OF_ORDER_API_CALL; } return current_pcp_handler_->requestConnection( client_proxy, endpoint_name, endpoint_id, connection_lifecycle_listener); } template Status::Value PCPManager::acceptConnection( Ptr > client_proxy, const string& endpoint_id, Ptr payload_listener) { if (current_pcp_handler_.isNull()) { return Status::OUT_OF_ORDER_API_CALL; } return current_pcp_handler_->acceptConnection(client_proxy, endpoint_id, payload_listener); } template Status::Value PCPManager::rejectConnection( Ptr > client_proxy, const string& endpoint_id) { if (current_pcp_handler_.isNull()) { return Status::OUT_OF_ORDER_API_CALL; } return current_pcp_handler_->rejectConnection(client_proxy, endpoint_id); } template proto::connections::Medium PCPManager::getBandwidthUpgradeMedium() { if (current_pcp_handler_.isNull()) { return proto::connections::Medium::UNKNOWN_MEDIUM; } return current_pcp_handler_->getBandwidthUpgradeMedium(); } template bool PCPManager::setCurrentPCPHandler(const Strategy& strategy) { current_pcp_handler_ = getPCPHandler(deducePCP(strategy)); return !current_pcp_handler_.isNull(); } template PCP::Value PCPManager::deducePCP(const Strategy& strategy) { if (Strategy::kP2PCluster == strategy) { return PCP::P2P_CLUSTER; } else if (Strategy::kP2PStar == strategy) { return PCP::P2P_STAR; } else if (Strategy::kP2PPointToPoint == strategy) { return PCP::P2P_POINT_TO_POINT; } else { // TODO(tracyzhou): Add logging. return PCP::UNKNOWN; } } template Ptr > PCPManager::getPCPHandler(PCP::Value pcp) { return pcp_handlers_[pcp]; } } // namespace connections } // namespace nearby } // namespace location