-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathpublisher.cc
106 lines (89 loc) · 2.81 KB
/
publisher.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* 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.
*
*/
// Example: ./publisher addr1
#include <atomic>
#include <chrono>
#include <csignal>
#include <iostream>
#include <string>
#include <thread>
#include <ignition/msgs.hh>
#include <ignition/transport.hh>
/// \brief Flag used to break the publisher loop and terminate the program.
static std::atomic<bool> g_terminatePub(false);
//////////////////////////////////////////////////
/// \brief Usage function.
void usage()
{
std::cerr << "./publisher <dst_address>" << std::endl;
}
//////////////////////////////////////////////////
/// \brief Function callback executed when a SIGINT or SIGTERM signals are
/// captured. This is used to break the infinite loop that publishes messages
/// and exit the program smoothly.
void signal_handler(int _signal)
{
if (_signal == SIGINT || _signal == SIGTERM)
g_terminatePub = true;
}
//////////////////////////////////////////////////
int main(int argc, char **argv)
{
if (argc != 2)
{
usage();
return -1;
}
// Install a signal handler for SIGINT and SIGTERM.
std::signal(SIGINT, signal_handler);
std::signal(SIGTERM, signal_handler);
// Create a transport node and advertise a topic.
ignition::transport::Node node;
std::string topic = "/broker/msgs";
auto pub = node.Advertise<ignition::msgs::Dataframe>(topic);
if (!pub)
{
std::cerr << "Error advertising topic [" << topic << "]" << std::endl;
return -1;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Prepare the message.
ignition::msgs::Dataframe msg;
msg.set_src_address("addr1");
msg.set_dst_address(argv[1]);
// Publish messages at 1Hz.
int counter = 0;
while (!g_terminatePub)
{
// Prepare the payload.
ignition::msgs::StringMsg payload;
payload.set_data("hello world " + std::to_string(counter));
std::string serializedData;
if (!payload.SerializeToString(&serializedData))
{
std::cerr << "Error serializing message\n"
<< payload.DebugString() << std::endl;
}
msg.set_data(serializedData);
if (!pub.Publish(msg))
break;
++counter;
std::cout << "Publishing hello on topic [" << topic << "]" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
return 0;
}