车载信息服务软件开发包使用指南(11)

  • 4.4 数据
  • 4.4.9. 添加一个软件桥接器并启用其管理
  • 4.4.10. 删除软件桥及其管理



4.4 数据

4.4.9. 添加一个软件桥接器并启用其管理

这个示例应用程序演示了如何添加软件桥接并启用其管理。

  1. 实现初始化回调,获取数据工厂实例
    也可以使用get管理器实例提供初始化回调。数据工厂将在管理器初始化完成时调用回调。
auto initCb = [&](telux::common::ServiceStatus status) {
	std::lock_guard<std::mutex> lock(mtx);
	status_ = status;
	initCv.notify_all();
};
auto &dataFactory = telux::data::DataFactory::getInstance();
  1. 获取桥管理器实例
~~~~~~{.cpp}
std::unique_lock<std::mutex> lck(mtx);
auto dataBridgeMgr = dataFactory.getBridgeManager(initCb);
~~~~~~
  1. 等待桥管理器初始化完成
initCv.wait(lck);
  1. 检查桥管理器的初始化状态
    桥管理器需要准备删除软件桥并禁用系统中的软件桥管理。如果桥管理器初始化失败,则可以通过调用步骤2来完成新的初始化尝试。如果桥管理器初始化成功,请继续执行步骤5。
if (status_ == telux::common::ServiceStatus::SERVICE_AVAILABLE) {
// Go to step 4
}
else {
//Go to step 2 for another initialization attempt
}
  1. 实现回调,添加软件桥,使其管理
auto respCbAdd = [](telux::common::ErrorCode error) {
	std::cout << "CALLBACK: "
			<< "Add software bridge request"
			<< (error == telux::common::ErrorCode::SUCCESS ? " is successful" : " failed")
			<< ". ErrorCode: " << static_cast<int>(error)
			<< ", description: " << Utils::getErrorCodeAsString(error) << std::endl;
};
auto respCbEnable = [](telux::common::ErrorCode error) {
	std::cout << "CALLBACK: "
			<< "Enable software bridge management request is"
			<< (error == telux::common::ErrorCode::SUCCESS ? " successful" : " failed")
			<< ". ErrorCode: " << static_cast<int>(error)
			<< ", description: " << Utils::getErrorCodeAsString(error) << std::endl;
};
  1. 根据接口名称、接口类型和所需带宽添加软件桥接
telux::data::net::BridgeInfo config;
config.ifaceName = infName;
config.ifaceType = infType;
config.bandwidth = bridgeBw;
dataBridgeMgr->addBridge(config, respCbAdd);

现在,响应回调将与附加桥响应一起调用。

  1. 如果尚未启用,则可启用软件桥接管理
bool enable = true;
dataBridgeMgr->enableBridge(enable, respCbEnable);

现在,响应回调将与启用桥响应一起调用。

4.4.10. 删除软件桥及其管理

这个示例应用程序演示了如何删除软件桥及其管理。

  1. 实现初始化回调,获取数据工厂实例
    也可以使用get管理器实例提供初始化回调。数据工厂将在管理器初始化完成时调用回调。
auto initCb = [&](telux::common::ServiceStatus status) {
	std::lock_guard<std::mutex> lock(mtx);
	status_ = status;
	initCv.notify_all();
};
auto &dataFactory = telux::data::DataFactory::getInstance();
  1. 获取桥管理器实例
~~~~~~{.cpp}
std::unique_lock<std::mutex> lck(mtx);
auto dataBridgeMgr = dataFactory.getBridgeManager(initCb);
~~~~~~
  1. 等待桥管理器初始化完成
initCv.wait(lck);
  1. 检查桥管理器的初始化状态
    桥管理器需要准备删除软件桥并禁用系统中的软件桥管理。如果桥管理器初始化失败,则可以通过调用步骤2来完成新的初始化尝试。如果桥管理器初始化成功,请继续执行步骤5。
if (status_ == telux::common::ServiceStatus::SERVICE_AVAILABLE) {
	// Go to step 5
}
else {
	//Go to step 2 for another initialization attempt
}
  1. 实现回调获取、删除软件桥、禁用软件桥管理
auto respCbGet = [](const std::vector<BridgeInfo> &configs, telux::common::ErrorCode error) {
	std::cout << "CALLBACK: "
		<< "Get software bridge info request"
		<< (error == telux::common::ErrorCode::SUCCESS ? " is successful" : " failed")
		<< ". ErrorCode: " << static_cast<int>(error)
		<< ", description: " << Utils::getErrorCodeAsString(error) << std::endl;
		for (auto c : configs) {
		std::cout << "Iface name: " << c.ifaceName << ", ifaceType: " << (int)c.ifaceType
		<< ", bandwidth: " << c.bandwidth << std::endl;
	}
};
auto respCbRemove = [](telux::common::ErrorCode error) {
	std::cout << "CALLBACK: "
		<< "Remove software bridge request"
		<< (error == telux::common::ErrorCode::SUCCESS ? " is successful" : " failed")
		<< ". ErrorCode: " << static_cast<int>(error)
		<< ", description: " << Utils::getErrorCodeAsString(error) << std::endl;
};
auto respCbDisable = [](telux::common::ErrorCode error) {
	std::cout << "CALLBACK: "
		<< "Disable software bridge management request is"
		<< (error == telux::common::ErrorCode::SUCCESS ? " successful" : " failed")
		<< ". ErrorCode: " << static_cast<int>(error)
		<< ", description: " << Utils::getErrorCodeAsString(error) << std::endl;
};
  1. 获取在系统中配置的软件桥接器的列表
dataBridgeMgr->requestBridgeInfo(respCbGet);

现在,响应回调将通过软件桥的列表而被调用。
7. 根据接口名称从配置的桥中删除软件桥

dataBridgeMgr->removeBridge(ifaceName, respCbRemove);

现在,响应回调将与远程桥响应一起调用。

  1. 禁用软件桥接管理(如果需要)
bool enable = false;
dataBridgeMgr->enableBridge(enable, respCbDisable);

现在,响应回调将与启用桥响应一起调用