Squashed 'libs/CommService/' content from commit 7ccc0fc

git-subtree-dir: libs/CommService
git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
This commit is contained in:
Henry Winkel
2022-09-15 09:53:53 +02:00
commit cc67e4840f
799 changed files with 179487 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
/* 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;
}

View File

@@ -0,0 +1,34 @@
#ifndef __BCCLI_TESTSERVICE_HPP__
#define __BCCLI_TESTSERVICE_HPP__
/* 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 header file for the testservice subcommand of the bccli application
* @author Dominik Meyer <dmeyer@hsu-hh.de>
* @date 2019-02-06
* @copyright 2019 no yet defined
*/
#include <string>
#include <BC/receiveable.hpp>
namespace BCCLI
{
void testservice(const std::string &broadcastAddress, const std::string &srcAddress, unsigned short port);
namespace testService
{
class receiver : public BC::receiveable
{
public:
void receive(BC::Message *m) override;
};
}; // namespace testService
};
#endif