车载信息服务软件开发包使用指南(12)
4.4.11 创建VLAN并绑定PDN
此示例应用程序演示了如何创建 VLAN 并将其绑定到 PDN。
- 实现初始化回调,获取DataFactory实例
可选地,初始化回调可以与获取管理器实例一起提供。 管理器初始化完成后,数据工厂将调用回调。
auto initCb = [&](telux::common::ServiceStatus status) {
std::lock_guard<std::mutex> lock(mtx);
status_ = status;
initCv.notify_all();
};
auto &dataFactory = telux::data::DataFactory::getInstance();
- 获取VlanManager实例
std::unique_lock<std::mutex> lck(mtx);
auto dataVlanMgr = dataFactory.getVlanManager(opType, initCb);
- 等待VlanManager初始化完成
initCv.wait(lck);
3.1 查看VlanManager初始化状态
如果 VlanManager 初始化失败,可以通过调用步骤 2 来完成新的初始化尝试。如果 VlanManager 初始化成功,则继续执行步骤 4。
if (status_ == telux::common::ServiceStatus::SERVICE_AVAILABLE) {
// Go to step 4
}
else {
//Go to step 2 for another initialization attempt
}
- 实现创建VLAN的回调
auto respCbCreate = [](bool isAccelerated, telux::common::ErrorCode error) {
std::cout << std::endl << std::endl;
std::cout << "CALLBACK: "
<< "createVlan Response"
<< (error == telux::common::ErrorCode::SUCCESS ? " is successful" : " failed")
<< ". ErrorCode: " << static_cast<int>(error);
std::cout << " Acceleration " << (isAccelerated ? "is allowed" : "is not allowed") << "\n";
};
- 根据接口类型、加速、分配的id创建Vlan
telux::data::VlanConfig config;
config.iface = infType;
config.vlanId = vlanId;
config.isAccelerated = isAccelerated;
dataVlanMgr->createVlan(config, respCbCreate);
现在,将为 createVlan 响应调用响应回调。
- 实现 bindWithprofile 响应的回调
auto respCbBind = [](telux::common::ErrorCode error) {
std::cout << std::endl << std::endl;
std::cout << "CALLBACK: "
<< "bindWithProfile Response"
<< (error == telux::common::ErrorCode::SUCCESS ? " is successful" : " failed")
<< ". ErrorCode: " << static_cast<int>(error) << std::endl;
};
- 将创建的 Vlan 与用户提供的配置文件 ID 绑定
dataVlanMgr->bindWithProfile(profileId, vlanId, respCbBind);
现在,将为 bindWithProfile 响应调用响应回调。
4.4.12 创建静态NAT表项
此示例应用程序演示了如何创建静态 NAT 条目。
- 实现初始化回调,获取DataFactory实例
可选地,初始化回调可以与获取管理器实例一起提供。管理器初始化完成后,数据工厂将调用回调。
auto initCb = [&](telux::common::ServiceStatus status) {
std::lock_guard<std::mutex> lock(mtx);
status_ = status;
initCv.notify_all();
};
auto &dataFactory = telux::data::DataFactory::getInstance();
- 获取 NatManager 实例
std::unique_lock<std::mutex> lck(mtx);
auto dataSnatMgr = dataFactory.getNatManager(opType);
- 等待NatManager初始化完成
initCv.wait(lck);
3.1 查看NatManager初始化状态
如果 NatManager 初始化失败,可以通过调用步骤 2 来完成新的初始化尝试。如果 NatManager 初始化成功,则继续执行步骤 4。
if (status_ == telux::common::ServiceStatus::SERVICE_AVAILABLE) {
// Go to step 4
}
else {
//Go to step 2 for another initialization attempt
}
- 实现创建SNAT入口的回调
auto respCb = [](telux::common::ErrorCode error) {
std::cout << std::endl << std::endl;
std::cout << "CALLBACK: "
<< "addStaticNatEntry"
<< (error == telux::common::ErrorCode::SUCCESS ? " is successful" : " failed");
};
- 根据profile id、本地ip、本地端口、全局端口、协议创建Snat入口
natConfig.addr = ipAddr;
natConfig.port = (uint16_t)localIpPort;
natConfig.globalPort = (uint16_t)globalIpPort;
natConfig.proto = (uint8_t)proto;
dataSnatMgr->addStaticNatEntry(profileId, natConfig, respCb);