main.cpp 4.33 KB
// 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;
}