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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.
/*!
* @file testmain.cxx
* This file acts as a main entry point to the application.
*
* This file was generated by the tool fastddsgen.
*/
#include <csignal>
#include <cstring>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <thread>
#include <chrono>
#include "test.hpp"
#include "manager/IPC.h"
using eprosima::fastdds::dds::Log;
volatile sig_atomic_t keep_running = 1;
std::function<void(int)> stop_handler;
void signal_handler(
int signum)
{
keep_running = 0;
stop_handler(signum);
}
std::string parse_signal(
const int& signum)
{
switch (signum)
{
case SIGINT:
return "SIGINT";
case SIGTERM:
return "SIGTERM";
#ifndef _WIN32
case SIGQUIT:
return "SIGQUIT";
case SIGHUP:
return "SIGHUP";
#endif // _WIN32
default:
return "UNKNOWN SIGNAL";
}
}
int main(
int argc,
char** argv)
{
eprosima::fastdds::dds::Log::SetVerbosity(eprosima::fastdds::dds::Log::Info);
// 获取日志输出的文件句柄,如果需要的话
// eprosima::fastdds::dds::Log::SetCategoryFilter(eprosima::fastdds::dds::Log::Kind::Info, "my_log_file.txt");
//EPROSIMA_LOG_INFO(TEST, "LOG INFO start 1 ...........");
auto ret = EXIT_SUCCESS;
int domain_id = 0;
if (argc != 2 || (strcmp(argv[1], "p") != 0 && strcmp(argv[1], "s") != 0))
{
std::cout << "Error: Incorrect arguments." << std::endl;
std::cout << "Usage: " << std::endl << std::endl;
std::cout << argv[0] << " publisher|subscriber" << std::endl << std::endl;
ret = EXIT_FAILURE;
}
else
{
// 发布者
if(strcmp(argv[1], "p") == 0) {
IPCPublisherInstance.createPublisher<HelloSecurity>("Topic1", 10);
IPCPublisherInstance.createPublisher<OneMessage>("Topic2", 10);
}
// 观察者
if(strcmp(argv[1], "s") == 0) {
auto commandSubscriber = IPCSubscriberInstance.createSubscriber<HelloSecurity>("Topic1", 10);
commandSubscriber->setDataHandler([](const HelloSecurity& sample){
cout << "---------------recv message: "<< sample.securityData() << endl;
});
auto commandSubscriber1 = IPCSubscriberInstance.createSubscriber<OneMessage>("Topic2", 0);
commandSubscriber1->setDataHandler([](const OneMessage& sample){
cout << "-------------recv message: a = " << sample.a() << " b = " << sample.b() <<
" --- vector size = " << sample.sequenceTest().size() << endl;
});
}
stop_handler = [&](int signum)
{
std::cout << "\n" << parse_signal(signum) << " received, stopping " << argv[1]
<< " execution." << std::endl;
IPCPublisherInstance.stopAll();
IPCSubscriberInstance.stopAll();
};
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGHUP, signal_handler);
//thread.join();
}
while (keep_running)
{
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
if(strcmp(argv[1], "p") == 0) {
HelloSecurity msg;
msg.securityData("123456");
IPCPublisherInstance.getPublisherByTopic<HelloSecurity>("Topic1")->publish(msg);
OneMessage msg1;
msg1.a(1);
msg1.b(2);
std::vector<int16_t> vec{1,2,3,4,5};
msg1.sequenceTest(vec);
IPCPublisherInstance.getPublisherByTopic<OneMessage>("Topic2")->publish(msg1);
}
}
Log::Reset();
return ret;
}