git-subtree-dir: libs/CommService git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
138 lines
4.1 KiB
C++
138 lines
4.1 KiB
C++
#ifndef __LIBBC_BC_SIMPLESERVICE_UDP_HPP__
|
|
#define __LIBBC_BC_SIMPLESERVICE_UDP_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 main header file for the simpleservice udp class
|
|
* @author Dominik Meyer <dmeyer@hsu-hh.de>
|
|
* @date 2019-02-01
|
|
* @copyright 2019 no yet defined
|
|
*/
|
|
#include <thread>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <chrono>
|
|
#include <list>
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
#include <BC/BC.hpp>
|
|
#include <BC/BasicService.hpp>
|
|
#include <BC/Message.hpp>
|
|
|
|
|
|
namespace BC {
|
|
class SimpleServiceUDPremoteClient
|
|
{
|
|
public:
|
|
unsigned short port;
|
|
BC::DataTypes::deviceIdType id;
|
|
};
|
|
|
|
/**
|
|
* @brief SimpleServiceUDP uses simple UDP broadcasts for distributing messages
|
|
*
|
|
* The SimpleServiceUDP is an implementation of a PubSub Service. It extends the BasicService
|
|
* and uses UDP broadcasts to distribute data. The constructor of the class
|
|
*
|
|
* This Service is only useful for debugging purposes running your network in a local LAN.
|
|
*/
|
|
class SimpleServiceUDP : public BC::BasicService
|
|
{
|
|
private:
|
|
/// the IP Address to send messages to
|
|
std::string dstIp;
|
|
|
|
/// the local source IP Address
|
|
std::string srcIp;
|
|
|
|
/// the udp port on which to broadcast and receive messages
|
|
unsigned short broadcastPort = 0;
|
|
|
|
/// the port we are listing on for messages, should normally be broadcastPort, but...
|
|
unsigned short localPort = 0;
|
|
|
|
/// the network socket to work on
|
|
int inetSocket = -1;
|
|
|
|
/// vector of local client ports we forward messages to
|
|
std::vector<SimpleServiceUDPremoteClient> clients;
|
|
|
|
/// mutex for exclusive access to the clients vector
|
|
std::mutex mutexClients;
|
|
|
|
|
|
protected:
|
|
/**
|
|
* @brief called from the parent class when the connect method is called
|
|
*
|
|
* Method is responsible for setting up the connection to the underlying
|
|
* transport layer protocol
|
|
*
|
|
*/
|
|
void derivedConnect() override;
|
|
|
|
/**
|
|
* @brief called from the parent class when the disconnect method is called
|
|
*
|
|
* Method is responsible for tearing down the connection to the underlying
|
|
* transport layer protocol
|
|
*
|
|
*/
|
|
void derivedDisconnect() override;
|
|
|
|
/**
|
|
* @brief called from the parent class in the receive thread
|
|
*
|
|
* Method is responsible for receiving data frames from the transport layer
|
|
*
|
|
* @see BasicService::receive
|
|
*/
|
|
void derivedReceive() override;
|
|
|
|
/**
|
|
* @brief called from the parent class after constructing a correct CMS::PubSub::Message
|
|
*
|
|
* Method is responsible for processing Service specific messages
|
|
*
|
|
* @param m - the received message
|
|
*
|
|
* @return true|false true = message has been processed, false = message has not been processed
|
|
*/
|
|
bool process(const BC::Message *m) override;
|
|
|
|
/**
|
|
* @brief called from the parent class to actually publish the message
|
|
*
|
|
* @param m - the messahe to publish
|
|
*/
|
|
void derivedPublish(BC::Message &m) override;
|
|
|
|
public:
|
|
/**
|
|
* @brief constructor for the SimpleServiceUDP class
|
|
*
|
|
* Constructor initializes the class
|
|
*
|
|
* @param myID - the id of the device connected to the network
|
|
* @param majorT - the majorType of device
|
|
* @param minorT - the minorType of the device
|
|
* @param port - the UDP port on which to transmit and receive broadcast messages
|
|
* @param dst - the broadcast IP address to send messages to
|
|
* @param src - the local IP Address we listen on, has to be on the same network of the broadcast address
|
|
*/
|
|
SimpleServiceUDP(const BC::DataTypes::deviceIdType &myID, const BC::DataTypes::deviceMajorType &majorT,
|
|
const std::string &minorT, const unsigned short &port, const std::string &dst,
|
|
const std::string &src) : BasicService(myID,majorT,minorT),
|
|
dstIp(dst), srcIp(src), broadcastPort(port){}
|
|
};
|
|
}; // namespace BC
|
|
|
|
#endif
|