VSomeip 源码分析 1 offer_service
1、hello_world_service 构造函数
// Get the vSomeIP runtime and
// create a application via the runtime, we could pass the application name
// here otherwise the name supplied via the VSOMEIP_APPLICATION_NAME
// environment variable is used
hello_world_service() :
rtm_(vsomeip::runtime::get()), //获取runtime,静态类
app_(rtm_->create_application()),//创建application
stop_(false),
stop_thread_(std::bind(&hello_world_service::stop, this))//开启stop线程
{
}
2、init
2.1 加载配置文件xxx.json
加载.so和.json
auto its_plugin = plugin_manager::get()->get_plugin(
plugin_type_e::CONFIGURATION_PLUGIN, VSOMEIP_CFG_LIBRARY);
if (its_plugin) {
auto its_configuration_plugin
= std::dynamic_pointer_cast<configuration_plugin>(its_plugin);
if (its_configuration_plugin) {
configuration_ = its_configuration_plugin->get_configuration(name_);
VSOMEIP_INFO << "Configuration module loaded.";
} else {
std::cerr << "Invalid configuration module!" << std::endl;
std::exit(EXIT_FAILURE);
}
} else {
std::cerr << "Configuration module could not be loaded!" << std::endl;
std::exit(EXIT_FAILURE);
}
解析.json
// Override with path from environment (if existing)
const char *its_env = getenv(VSOMEIP_ENV_CONFIGURATION);
if (nullptr != its_env) {
if (utility::is_file(its_env)) {
its_file = its_env;
its_folder = "";
} else if (utility::is_folder(its_env)) {
its_folder = its_env;
its_file = "";
}
}
初始化log模块
logger::logger_impl::init(shared_from_this());
获取.json中的service id , hello_world 中为4444
{
"name":"hello_world_service",
"id":"0x4444"
},
client_ = its_configuration->get_id(name_);
2.2 创建 routing_
routing_ = std::make_shared<routing_manager_impl>(this);
//routing_manager_impl 构造中
ep_mgr_impl_(std::make_shared<endpoint_manager_impl>(this, io_, configuration_)),
初始化,这里的client_ 为上文 4444
routing_->set_client(client_);
routing_->init();
2.3 创建 routing_ ->init
routing_的init,创建了一个stub_<routing_manager_stub>->host_ = this< routing_manager_impl >
routing_manager_base::init(ep_mgr_impl_);
stub_ = std::make_shared<routing_manager_stub>(this, configuration_);
stub_->init();
stub_的init,创建endpoint_ ,其中
endpoint_host_= this <endpoint_manager_impl>
routing_host_= routing_manager_stub
local_ = VSOMEIP_ROUTING_CLIENT[0]
2.4 server 注册 cbk
填充的是 members_[_service][_instance][_method] = _handler;
// register a message handler callback for messages sent to our service
app_->register_message_handler(service_id, service_instance_id,
service_method_id,
std::bind(&hello_world_service::on_message_cbk, this,
std::placeholders::_1));
handler_ = _handler;
在on_state中调用
app_->register_state_handler(
std::bind(&hello_world_service::on_state_cbk, this,
std::placeholders::_1));
3、application_impl::start()
创建线程数量:默认 2
const int io_thread_nice_level = configuration_->get_io_thread_nice_level(name_);
创建dispatch线程
auto its_main_dispatcher = std::make_shared<std::thread>(
std::bind(&application_impl::main_dispatch, shared_from_this()));
创建shutdown线程
stop_thread_= std::thread(&application_impl::shutdown, shared_from_this());
3.1 routing_->start()
if (routing_)
routing_->start();
注册一个on_client_id_timer_expired 定时器回调,检测是否有系统error
client_id_timer_.async_wait(
std::bind(
&routing_manager_stub::on_client_id_timer_expired,
std::dynamic_pointer_cast<routing_manager_stub>(shared_from_this()),
std::placeholders::_1));
启动 endpoint_ ,即 进入 async_accept,等待client 的connect
if (endpoint_) {
endpoint_->start();
}
创建 client_registration_func 线程,监听 client的注册:当有client向 stub 注册时,会创建一个local_client_endpoint_impl去connect
client [5555] ,存入 local_endpoints_ [ _client ] = its_endpoint;
client_registration_thread_ = std::make_shared<std::thread>(
std::bind(&routing_manager_stub::client_registration_func, this));
inline std::shared_ptr<endpoint> find_or_create_local(
client_t _client) {
return ep_mgr_->find_or_create_local(_client);
}
3.2 host_->on_state
host_->on_state(state_type_e::ST_REGISTERED);
执行 offer_service
void on_state_cbk(vsomeip::state_type_e _state)
{
if(_state == vsomeip::state_type_e::ST_REGISTERED)
{
// we are registered at the runtime and can offer our service
app_->offer_service(service_id, service_instance_id);
}
}
4 、offer_service( service_id, service_instance_id)
这里的 client_ 即上文 4444
routing_->offer_service(client_, _service, _instance, _major, _minor);
bool routing_manager_impl::offer_service(client_t _client,
service_t _service, instance_t _instance,
major_version_t _major, minor_version_t _minor) {
return offer_service(_client, _service, _instance, _major, _minor, true);
}
先去 local_services_ 里查找 , 没有,填充 routing_manager_base 的
services_[_service][instance] = its_info; 和
填充local_services [_service][_instance] = std::make_tuple(_major,_minor, _client);
if (!handle_local_offer_service(_client, _service, _instance, _major, _minor)) {
erase_offer_command(_service, _instance);
return false;
}
创建 local_receiver_ <local_server_endpoint_impl > 【4444】 Listening at /tmp/vsomeip-4444
并启动 start
stub_->on_offer_service(_client, _service, _instance, _major, _minor);
if (_client == host_->get_client()) {
create_local_receiver(); //创建 local_receiver_
}
routing_info_[_client].second[_service][_instance] = std::make_pair(_major, _minor); // 填充 广播 表
填充 application_impl 的 available_
available_[_service][_instance][_major] = _minor;
on_availability(_service, _instance, true, _major, _minor);
其中 availability_ 为 application_impl::register_availability_handler 注册的cbk集合 ,为空。因此什么都不执行。