git-subtree-dir: libs/CommService git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
117 lines
2.8 KiB
C++
117 lines
2.8 KiB
C++
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
/**
|
|
* @file
|
|
* @brief implementation file for the testservice subcommand of the bccli application
|
|
* @author Dominik Meyer <dmeyer@hsu-hh.de>
|
|
* @date 2019-01-06
|
|
* @copyright 2019 no yet defined
|
|
*/
|
|
#include <testservice/testservice.hpp>
|
|
#include <BC/BC.hpp>
|
|
#include <BC/SimpleServiceUDP.hpp>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <chrono>
|
|
#include <signal.h>
|
|
#include <iomanip>
|
|
#include <random>
|
|
|
|
#ifdef __MINGW64__
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
/// variable for stopping sending pings
|
|
bool runningTestService=true;
|
|
|
|
|
|
/*
|
|
* signal handler for Ctrl-C
|
|
*/
|
|
#ifdef __MINGW64__
|
|
BOOL WINAPI killHandlerTestservice(DWORD dwType)
|
|
{
|
|
switch(dwType) {
|
|
case CTRL_C_EVENT:
|
|
runningTestService=false;
|
|
break;
|
|
case CTRL_BREAK_EVENT:
|
|
runningTestService=false;
|
|
break;
|
|
default:
|
|
printf("Some other event\n");
|
|
}
|
|
return TRUE;
|
|
}
|
|
#else
|
|
void killHandlerTestservice(int s){
|
|
|
|
if (s == SIGINT)
|
|
{
|
|
runningTestService=false;
|
|
}
|
|
|
|
}
|
|
#endif
|
|
/*
|
|
* message receiver class
|
|
*/
|
|
void BCCLI::testService::receiver::receive(BC::Message *m)
|
|
{
|
|
// print simple information about message
|
|
std::cout << std::setw(10) << m->sourceID;
|
|
std::cout << " ";
|
|
std::cout << std::setw(10) << m->destinationID;
|
|
std::cout << std::setw(20) << m->domain;
|
|
std::cout << " ";
|
|
std::cout << std::setw(20) << m->topic;
|
|
|
|
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
|
|
void BCCLI::testservice(const std::string &broadcastAddress, const std::string &srcAddress, unsigned short port)
|
|
{
|
|
// initialize the signal handler
|
|
#ifdef __MINGW64__
|
|
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)killHandlerTestservice,TRUE)) {
|
|
return;
|
|
}
|
|
#else
|
|
struct sigaction sigIntHandler;
|
|
sigIntHandler.sa_handler = killHandlerTestservice;
|
|
sigemptyset(&sigIntHandler.sa_mask);
|
|
sigIntHandler.sa_flags = 0;
|
|
sigaction(SIGINT, &sigIntHandler, NULL);
|
|
#endif
|
|
|
|
BCCLI::testService::receiver r;
|
|
|
|
// fetch us required random number device
|
|
std::random_device rd;
|
|
std::mt19937 mt(rd());
|
|
|
|
// set the distribution range of the numbers
|
|
std::uniform_int_distribution<uint64_t> dist(UINT16_MAX, UINT32_MAX);
|
|
|
|
BC::DataTypes::deviceIdType id = dist(mt);
|
|
|
|
std::cout << "Starting Test Service at id "<< id << std::endl;
|
|
BC::SimpleServiceUDP service(id, BC::DataTypes::deviceMajorType::UNKNOWN, "bccli test service",port, broadcastAddress, srcAddress);
|
|
service.subscribe(&r,BC::Constants::cAllDomains, BC::Constants::cAllTopics);
|
|
service.connect();
|
|
|
|
std::cout << "Stop service with CTRL-C" << std::endl;
|
|
|
|
while(runningTestService)
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
}
|
|
|
|
service.disconnect();
|
|
std::cout << "Test Service stopped" << std::endl;
|
|
}
|