Skip to content

Commit 67678ff

Browse files
committed
Address reivew comments
Signed-off-by: Barry Xu <[email protected]>
1 parent 3705273 commit 67678ff

File tree

15 files changed

+107
-55
lines changed

15 files changed

+107
-55
lines changed

docs/message_definition_encoding.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This set of definitions with all field types recursively included can be called
1616

1717
## `ros2msg` encoding
1818

19-
This encoding consists of definitions in [.msg](https://docs.ros.org/en/rolling/Concepts/About-ROS-Interfaces.html#message-description-specification) and [.srv](https://docs.ros.org/en/rolling/Concepts/Basic/About-Interfaces.html#services) format, concatenated together in human-readable form with
19+
This encoding consists of definitions in [.msg](https://docs.ros.org/en/rolling/Concepts/Basic/About-Interfaces.html#messages) and [.srv](https://docs.ros.org/en/rolling/Concepts/Basic/About-Interfaces.html#services) format, concatenated together in human-readable form with
2020
a delimiter.
2121

2222
The top-level message definition is present first, with no delimiter. All dependent .msg definitions are preceded by a two-line delimiter:

ros2bag/ros2bag/api/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ def _split_lines(self, text, width):
5555

5656

5757
def print_error(string: str) -> str:
58-
return '[ERROR] [ros2bag]: {}'.format(string)
58+
return _print_base('ERROR', string)
59+
60+
61+
def print_warn(string: str) -> str:
62+
return _print_base('WARN', string)
63+
64+
65+
def _print_base(print_type: str, string: str) -> str:
66+
return '[{}] [ros2bag]: {}'.format(print_type, string)
5967

6068

6169
def dict_to_duration(time_dict: Optional[Dict[str, int]]) -> Duration:
@@ -200,3 +208,16 @@ def add_writer_storage_plugin_extensions(parser: ArgumentParser) -> None:
200208
'Settings in this profile can still be overridden by other explicit options '
201209
'and --storage-config-file. Profiles:\n' +
202210
'\n'.join([f'{preset[0]}: {preset[1]}' for preset in preset_profiles]))
211+
212+
213+
def convert_service_to_service_event_topic(services):
214+
services_event_topics = []
215+
216+
if not services:
217+
return services_event_topics
218+
219+
for service in services:
220+
name = '/' + service if service[0] != '/' else service
221+
services_event_topics.append(name + '/_service_event')
222+
223+
return services_event_topics

ros2bag/ros2bag/verb/burst.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ros2bag.api import add_standard_reader_args
1919
from ros2bag.api import check_not_negative_int
2020
from ros2bag.api import check_positive_float
21+
from ros2bag.api import convert_service_to_service_event_topic
2122
from ros2bag.api import convert_yaml_to_qos_profile
2223
from ros2bag.api import print_error
2324
from ros2bag.verb import VerbExtension
@@ -95,12 +96,7 @@ def main(self, *, args): # noqa: D102
9596
play_options.rate = 1.0
9697
play_options.topics_to_filter = args.topics
9798
# Convert service name to service event topic name
98-
services = []
99-
if args.services and len(args.services) != 0:
100-
for s in args.services:
101-
name = '/' + s if s[0] != '/' else s
102-
services.append(name + '/_service_event')
103-
play_options.services_to_filter = services
99+
play_options.services_to_filter = convert_service_to_service_event_topic(args.services)
104100
play_options.topic_qos_profile_overrides = qos_profile_overrides
105101
play_options.loop = False
106102
play_options.topic_remapping_options = topic_remapping

ros2bag/ros2bag/verb/play.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
from ros2bag.api import add_standard_reader_args
1919
from ros2bag.api import check_not_negative_int
2020
from ros2bag.api import check_positive_float
21+
from ros2bag.api import convert_service_to_service_event_topic
2122
from ros2bag.api import convert_yaml_to_qos_profile
2223
from ros2bag.api import print_error
24+
from ros2bag.api import print_warn
2325
from ros2bag.verb import VerbExtension
2426
from ros2cli.node import NODE_NAME_PREFIX
2527
from rosbag2_py import Player
@@ -54,12 +56,17 @@ def add_arguments(self, parser, cli_name): # noqa: D102
5456
'replayed.')
5557
parser.add_argument(
5658
'--services', type=str, default=[], nargs='+',
57-
help='services to replay, separated by space. At least one service needs to be '
58-
'specified.')
59+
help='services to replay, separated by space. if none specified, all services will '
60+
'be played.')
5961
parser.add_argument(
6062
'-e', '--regex', default='',
6163
help='filter topics by regular expression to replay, separated by space. If none '
6264
'specified, all topics will be replayed.')
65+
parser.add_argument(
66+
'-x', '--exclude', default='',
67+
help='regular expressions to exclude topics from replay, separated by space. If none '
68+
'specified, all topics will be replayed. This argument is deprecated and please '
69+
'use --exclude-topics.')
6370
parser.add_argument(
6471
'--exclude-topics', default='',
6572
help='regular expressions to exclude topics from replay, separated by space. If none '
@@ -172,6 +179,10 @@ def main(self, *, args): # noqa: D102
172179
except (InvalidQoSProfileException, ValueError) as e:
173180
return print_error(str(e))
174181

182+
if args.exclude and args.exclude_topics:
183+
return print_error(str('-x/--exclude and --exclude_topics cannot be used at the '
184+
'same time.'))
185+
175186
storage_config_file = ''
176187
if args.storage_config_file:
177188
storage_config_file = args.storage_config_file.name
@@ -193,15 +204,17 @@ def main(self, *, args): # noqa: D102
193204
play_options.topics_to_filter = args.topics
194205

195206
# Convert service name to service event topic name
196-
services = []
197-
if args.services and len(args.services) != 0:
198-
for s in args.services:
199-
name = '/' + s if s[0] != '/' else s
200-
services.append(name + '/_service_event')
201-
play_options.services_to_filter = services
207+
play_options.services_to_filter = convert_service_to_service_event_topic(args.services)
202208

203209
play_options.regex_to_filter = args.regex
204-
play_options.topics_regex_to_exclude = args.exclude_topics
210+
211+
if args.exclude:
212+
print(print_warn(str('-x/--exclude argument is deprecated. Please use '
213+
'--exclude-topics.')))
214+
play_options.topics_regex_to_exclude = args.exclude
215+
else:
216+
play_options.topics_regex_to_exclude = args.exclude_topics
217+
205218
if args.exclude_services:
206219
play_options.services_regex_to_exclude = args.exclude_services + '/_service_event'
207220
else:

ros2bag/ros2bag/verb/record.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from rclpy.qos import InvalidQoSProfileException
2020
from ros2bag.api import add_writer_storage_plugin_extensions
21+
from ros2bag.api import convert_service_to_service_event_topic
2122
from ros2bag.api import convert_yaml_to_qos_profile
2223
from ros2bag.api import print_error
2324
from ros2bag.api import SplitLineFormatter
@@ -183,11 +184,17 @@ def add_arguments(self, parser, cli_name): # noqa: D102
183184
help='Choose the compression format/algorithm. '
184185
'Has no effect if no compression mode is chosen. Default: %(default)s.')
185186

186-
def main(self, *, args): # noqa: D102
187+
def _check_necessary_argument(self, args):
187188
# One options out of --all, --all-topics, --all-services, --services, topics or --regex
188189
# must be used
189190
if not (args.all or args.all_topics or args.all_services or
190191
args.services or (args.topics and len(args.topics) > 0) or args.regex):
192+
return False
193+
return True
194+
195+
def main(self, *, args): # noqa: D102
196+
197+
if not self._check_necessary_argument(args):
191198
return print_error('Must specify only one option out of --all, --all-topics, '
192199
'--all-services, --services, topics and --regex')
193200

@@ -286,12 +293,7 @@ def main(self, *, args): # noqa: D102
286293
record_options.all_services = args.all_services or args.all
287294

288295
# Convert service name to service event topic name
289-
services = []
290-
if args.services and len(args.services) != 0:
291-
for s in args.services:
292-
name = '/' + s if s[0] != '/' else s
293-
services.append(name + '/_service_event')
294-
record_options.services = services
296+
record_options.services = convert_service_to_service_event_topic(args.services)
295297

296298
recorder = Recorder()
297299

rosbag2_cpp/include/rosbag2_cpp/info.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
namespace rosbag2_cpp
2727
{
2828

29-
typedef struct
29+
typedef ROSBAG2_CPP_PUBLIC_TYPE struct rosbag2_service_info_t
3030
{
3131
std::string name;
3232
std::string type;
3333
std::string serialization_format;
3434
size_t request_count;
3535
size_t response_count;
36-
} service_info;
36+
} rosbag2_service_info_t;
3737

3838
class ROSBAG2_CPP_PUBLIC Info
3939
{
@@ -43,7 +43,7 @@ class ROSBAG2_CPP_PUBLIC Info
4343
virtual rosbag2_storage::BagMetadata read_metadata(
4444
const std::string & uri, const std::string & storage_id = "");
4545

46-
virtual std::vector<std::shared_ptr<service_info>> read_service_info(
46+
virtual std::vector<std::shared_ptr<rosbag2_service_info_t>> read_service_info(
4747
const std::string & uri, const std::string & storage_id = "");
4848
};
4949

rosbag2_cpp/include/rosbag2_cpp/service_utils.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,25 @@
1717

1818
#include <string>
1919

20+
#include "rosbag2_cpp/visibility_control.hpp"
21+
2022
namespace rosbag2_cpp
2123
{
22-
bool is_service_event_topic(const std::string & topic, const std::string & topic_type);
24+
ROSBAG2_CPP_PUBLIC
25+
bool
26+
is_service_event_topic(const std::string & topic, const std::string & topic_type);
2327

24-
std::string service_event_topic_name_to_service_name(const std::string & topic_name);
28+
ROSBAG2_CPP_PUBLIC
29+
std::string
30+
service_event_topic_name_to_service_name(const std::string & topic_name);
2531

26-
std::string service_event_topic_type_to_service_type(const std::string & topic_type);
32+
ROSBAG2_CPP_PUBLIC
33+
std::string
34+
service_event_topic_type_to_service_type(const std::string & topic_type);
2735

28-
size_t get_serialization_size_for_service_metadata_event();
29-
}
36+
ROSBAG2_CPP_PUBLIC
37+
size_t
38+
get_serialization_size_for_service_metadata_event();
39+
} // namespace rosbag2_cpp
3040

3141
#endif // ROSBAG2_CPP__SERVICE_UTILS_HPP_

rosbag2_cpp/src/rosbag2_cpp/info.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <map>
15+
#include "rosbag2_cpp/info.hpp"
16+
1617
#include <unordered_map>
1718
#include <unordered_set>
1819
#include <stdexcept>
@@ -22,7 +23,6 @@
2223
#include "rmw/rmw.h"
2324

2425
#include "rcpputils/filesystem_helper.hpp"
25-
#include "rosbag2_cpp/info.hpp"
2626
#include "rosbag2_cpp/service_utils.hpp"
2727
#include "rosbag2_storage/logging.hpp"
2828
#include "rosbag2_storage/metadata_io.hpp"
@@ -84,7 +84,7 @@ struct service_req_resp_info
8484
};
8585
} // namespace
8686

87-
std::vector<std::shared_ptr<service_info>> Info::read_service_info(
87+
std::vector<std::shared_ptr<rosbag2_service_info_t>> Info::read_service_info(
8888
const std::string & uri, const std::string & storage_id)
8989
{
9090
rosbag2_storage::StorageFactory factory;
@@ -96,13 +96,13 @@ std::vector<std::shared_ptr<service_info>> Info::read_service_info(
9696
using service_analysis =
9797
std::unordered_map<std::string, std::shared_ptr<service_req_resp_info>>;
9898

99-
std::unordered_map<std::string, std::shared_ptr<service_info>> all_service_info;
99+
std::unordered_map<std::string, std::shared_ptr<rosbag2_service_info_t>> all_service_info;
100100
service_analysis service_process_info;
101101

102102
auto all_topics_types = storage->get_all_topics_and_types();
103103
for (auto & t : all_topics_types) {
104104
if (is_service_event_topic(t.name, t.type)) {
105-
auto service_info = std::make_shared<rosbag2_cpp::service_info>();
105+
auto service_info = std::make_shared<rosbag2_cpp::rosbag2_service_info_t>();
106106
service_info->name = service_event_topic_name_to_service_name(t.name);
107107
service_info->type = service_event_topic_type_to_service_type(t.type);
108108
service_info->serialization_format = t.serialization_format;
@@ -111,7 +111,7 @@ std::vector<std::shared_ptr<service_info>> Info::read_service_info(
111111
}
112112
}
113113

114-
std::vector<std::shared_ptr<service_info>> ret_service_info;
114+
std::vector<std::shared_ptr<rosbag2_service_info_t>> ret_service_info;
115115

116116
if (!all_service_info.empty()) {
117117
auto msg = service_msgs::msg::ServiceEventInfo();

rosbag2_cpp/src/rosbag2_cpp/service_utils.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ bool is_service_event_topic(const std::string & topic, const std::string & topic
4646
return false;
4747
}
4848

49-
std::string end_topic_type = topic_type.substr(
50-
topic_type.length() - std::strlen(service_event_topic_type_postfix));
49+
if (topic_type.length() <= std::strlen(service_event_topic_type_postfix)) {
50+
return false;
51+
}
5152

52-
return end_topic_name == RCL_SERVICE_INTROSPECTION_TOPIC_POSTFIX &&
53-
end_topic_type == service_event_topic_type_postfix;
53+
return (topic_type.compare(
54+
topic_type.length() - std::strlen(service_event_topic_type_postfix),
55+
std::strlen(service_event_topic_type_postfix),
56+
service_event_topic_type_postfix) == 0) &&
57+
(end_topic_name == RCL_SERVICE_INTROSPECTION_TOPIC_POSTFIX);
5458
}
5559

5660
std::string service_event_topic_name_to_service_name(const std::string & topic_name)

rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ void SequentialWriter::create_topic(const rosbag2_storage::TopicMetadata & topic
200200
std::string topic_type;
201201
if (is_service_event_topic(topic_with_type.name, topic_with_type.type)) {
202202
// change service event type to service type for next step to get message definition
203-
topic_type = topic_with_type.type.substr(
204-
0,
205-
topic_with_type.type.length() - strlen("_Event"));
203+
topic_type = service_event_topic_type_to_service_type(topic_with_type.type);
206204
} else {
207205
topic_type = topic_with_type.type;
208206
}

0 commit comments

Comments
 (0)