提交 58d22297 编写于 作者: muleisheng's avatar muleisheng 提交者: Calvin Miao

bridge: add test case and clean code

上级 0c170549
load("//tools:cpplint.bzl", "cpplint")
package(default_visibility = ["//visibility:public"])
cc_library(
name = "bridge_lib",
srcs = [
],
copts = ['-DMODULE_NAME=\\"bridge\\"'],
deps = [
"//cyber",
"//external:gflags",
"//modules/common",
"//modules/common/adapters:adapter_gflags",
"//modules/common/monitor_log",
"//modules/common/time",
"//modules/common/util",
"//modules/planning/proto:planning_proto",
"//modules/bridge/common:bridge_gflags",
],
)
cc_library(
name = "udp_bridge_component_lib",
srcs = [
"udp_bridge_component.cc",
],
hdrs = [
"udp_bridge_component.h",
],
copts = ['-DMODULE_NAME=\\"bridge\\"'],
deps = [
":bridge_lib",
],
)
cc_binary(
name = "libudp_bridge_component.so",
linkshared = True,
linkstatic = False,
deps = [":udp_bridge_component_lib"],
)
cc_test(
name = "udp_bridge_component_test",
size = "small",
srcs = [
"udp_bridge_component_test.cc",
],
deps = [
":udp_bridge_component_lib",
"@gtest//:main",
],
)
cpplint()
load("//tools:cpplint.bzl", "cpplint")
package(default_visibility = ["//visibility:public"])
cc_library(
name = "bridge_gflags",
srcs = [
"bridge_gflags.cc",
],
hdrs = [
"bridge_gflags.h",
],
deps = [
],
)
cpplint()
/******************************************************************************
* Copyright 2019 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "modules/bridge/common/bridge_gflags.h"
DEFINE_string(bridge_module_name,
"Bridge",
"Bridge module name");
DEFINE_string(remote_ip,
"127.0.0.1",
"remote ip");
DEFINE_int32(remote_port, 8900,
"remote port");
/******************************************************************************
* Copyright 2019 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#pragma once
#include "gflags/gflags.h"
DECLARE_string(bridge_module_name);
DECLARE_string(remote_ip);
DECLARE_int32(remote_port);
--flagfile=/apollo/modules/common/data/global_flagfile.txt
--remote_ip=127.0.0.1
--remote_port=8900
module_config {
module_library : "/apollo/bazel-bin/modules/bridge/libudp_bridge_component.so"
components {
class_name : "UDPBridgeComponent<planning::ADCTrajectory>"
config {
name: "bridge_ADCTrajectory"
readers {
channel: "/apollo/planning"
}
}
}
components {
class_name : "UDPBridgeComponent<LocalizationEstimate>"
config {
name: "bridge_LocalizationEstimate"
readers {
channel: "/apollo/localization/pose"
}
}
}
}
<cyber>
<module>
<name>bridge</name>
<dag_conf>/apollo/modules/bridge/dag/bridge.dag</dag_conf>
<process_name>udp_bridge</process_name>
</module>
</cyber>
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "modules/bridge/udp_bridge_component.h"
namespace apollo {
namespace bridge {
#define BRIDGE_IMPL(pb_msg) \
template class UDPBridgeComponent<pb_msg>
#define FREE_ARRY(arry) \
if (arry) { \
delete [] arry; \
arry = nullptr; \
}
template<typename T>
bool UDPBridgeComponent<T>::Init() {
AINFO << "UDP bridge init, starting ...";
return true;
}
template<typename T>
bool UDPBridgeComponent<T>::Proc(
const std::shared_ptr<T> &pb_msg) {
if (pb_msg== nullptr) {
AERROR << "proto msg is not ready!";
return false;
}
apollo::cyber::scheduler::Instance()->CreateTask(
[&pb_msg]() {
struct sockaddr_in server_addr;
server_addr.sin_addr.s_addr = inet_addr(FLAGS_remote_ip.c_str());
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons((uint16_t)FLAGS_remote_port);
Session session;
session.Socket(AF_INET, SOCK_DGRAM, 0);
if (session.Connect((struct sockaddr*)&server_addr,
sizeof(server_addr)) < 0) {
std::cout << "connect to server failed, " << strerror(errno)
<< std::endl;
return;
}
unsigned int msg_len = pb_msg->ByteSize();
char *buf = new char[msg_len];
memset(buf, 0, msg_len);
pb_msg->SerializeToArray(buf, msg_len);
if (session.Send(buf, msg_len, 0) < 0) {
std::cout << "send message failed." << std::endl;
FREE_ARRY(buf)
return;
}
FREE_ARRY(buf)
},
"bridge_client");
return true;
}
BRIDGE_IMPL(LocalizationEstimate);
BRIDGE_IMPL(planning::ADCTrajectory);
} // namespace bridge
} // namespace apollo
/******************************************************************************
* Copyright 2017 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#pragma once
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <memory>
#include <string>
#include <iostream>
#include <vector>
#include "cyber/cyber.h"
#include "cyber/init.h"
#include "cyber/io/session.h"
#include "cyber/scheduler/scheduler_factory.h"
#include "cyber/class_loader/class_loader.h"
#include "cyber/component/component.h"
#include "modules/common/monitor_log/monitor_log_buffer.h"
#include "modules/planning/proto/planning.pb.h"
#include "modules/common/util/util.h"
#include "modules/bridge/common/bridge_gflags.h"
namespace apollo {
namespace bridge {
using apollo::cyber::io::Session;
using apollo::localization::LocalizationEstimate;
template<typename T>
class UDPBridgeComponent final
: public cyber::Component<T> {
public:
UDPBridgeComponent()
: monitor_logger_buffer_(common::monitor::MonitorMessageItem::CONTROL) {}
bool Init() override;
bool Proc(const std::shared_ptr<T> &pb_msg) override;
std::string Name() const {
return FLAGS_bridge_module_name;
}
private:
common::monitor::MonitorLogBuffer monitor_logger_buffer_;
};
CYBER_REGISTER_COMPONENT(UDPBridgeComponent<planning::ADCTrajectory>)
CYBER_REGISTER_COMPONENT(UDPBridgeComponent<LocalizationEstimate>)
} // namespace bridge
} // namespace apollo
/******************************************************************************
* Copyright 2019 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "modules/bridge/udp_bridge_component.h"
#include "cyber/init.h"
#include "gtest/gtest.h"
namespace apollo {
namespace bridge {
TEST(PredictionComponentTest, Simple) {
cyber::Init("udp_bridge_component_test");
UDPBridgeComponent<planning::ADCTrajectory> udp_bridge_component;
EXPECT_EQ(udp_bridge_component.Name(), "Bridge");
}
} // namespace bridge
} // namespace apollo
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册