ADD: Message container and Join Message

This commit is contained in:
Henry Winkel
2022-11-11 14:36:57 +01:00
parent 2d077f4ff4
commit 3006f79883
13 changed files with 545 additions and 23 deletions

View File

@@ -0,0 +1,24 @@
#pragma once
#include <WHISPER/Messages/Message.hpp>
#include <WHISPER/Messages/Protos/join.pb.h>
#include <string>
#include <loguru.hpp>
namespace WHISPER {
class Join : public Message
{
private:
messages::join::Join joinMessage;
public:
std::uint32_t port;
std::string sourceAddr;
Join(std::string receivedMessage);
Join(std::uint32_t deviceID,std::uint32_t topic, MsgType Type, SourceType src,std::uint32_t port, std::string addr);
};
}

View File

@@ -0,0 +1,88 @@
#pragma once
#include "google/protobuf/any.pb.h"
#include "google/protobuf/message.h"
#include <WHISPER/Messages/Protos/message.pb.h>
#include <memory>
#include <string>
#include <loguru.hpp>
namespace WHISPER {
enum MsgType : int32_t
{
/// message to shutdown all participants
SHUTDOWN = 0,
/// on participant is joined
JOIN=1,
/// participant is leaving
LEAVE,
/// owntrack informaton
OWN_TRACK,
/// raw track message
RAW_TRACK,
/// simple data
SIMPLE
}; // enum class EventType
enum SourceType : int32_t
{
SIMCOMTROLER,
///
SHIP,
///
SENSOR,
///
EFEKTOR,
///
GATEWAY,
///
}; // enum class EventType
class Message{
private:
std::string payloadString_;
std::shared_ptr<google::protobuf::Any> payload_;
public:
Message()=default;
Message(std::int32_t deviceId, std::uint32_t topic, MsgType Type,SourceType src);
Message(std::string msg);
std::string getPayloadString();
std::uint32_t topic_;
std::int32_t msgType_;
std::int32_t sourceType_;
std::int32_t deviceId_;
void addPayLoad(std::shared_ptr<google::protobuf::Any> any);
void addPayLoad(std::string any);
std::string serialize();
protected:
messages::header::Message msg;
};
}

View File

@@ -3,14 +3,13 @@
syntax = "proto3";
package messages.join;
import "google/protobuf/timestamp.proto";
// import "google/protobuf/timestamp.proto";
// [END declaration]
// [START messages]
message Join {
int32 id = 1;
int32 port = 2;
string address = 3;
uint32 port = 1;
string srcAddress = 2;
}

View File

@@ -0,0 +1,19 @@
syntax = "proto3";
import "google/protobuf/any.proto";
package messages.header;
// [START messages]
message Message {
int32 topic = 1;
int32 msgType = 2;
int32 sourceType = 3;
int32 sourceID = 4;
repeated google.protobuf.Any payload = 5;
}

View File

@@ -0,0 +1,69 @@
#pragma once
/* 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
* @copyright MPLv2
*/
#include <mutex>
#include <queue>
#include <memory>
#include <condition_variable>
#include <string>
namespace WHISPER{
/**
* @class BasicMessageQueue
*
* This class encapsulates shared storage which is a queue and protect it
* with a mutex. In Addition there is a condition variable to notify other
* threads if there is new data in the queue.
*/
template<class T>
class threadSafeQueue
{
private:
std::queue<std::unique_ptr<T>> q;
std::mutex mx;
std::condition_variable condVar;
public:
/**
* @brief default constructor for class BasicMessageQueue
*/
threadSafeQueue();
/**
* @brief appends given message to queue
*
* The storage is protected by a mutex. Makes a notify_one call to inform
* in waiting thread that there is a new message in the queue.
*
* @param msg - incoming message
*/
void addElement( std::unique_ptr<T> msg );
/**
* @brief gets the fron message from the queue
*
* This method gets the front message in the queue and deletes it from the
* queue. The storage is protected by a mutex. If the queue is empty the
* function throws an exception.
*
* @result msg - returns the front message from the queue
* @throws length_error - if queue is empty this method throws length_error exception
*/
std::unique_ptr<T> getElement();
/**
* @brief method size
*
* @result size - current size of the message queue
*/
unsigned int size();
}; //BasicMessageQueue
}; // namespace BC

View File

@@ -10,8 +10,21 @@
* @copyright 2022 MPLv2
*/
#include <cstddef>
#include <cstdint>
#include <memory>
#define ZMQ_BUILD_DRAFT_API 1
#include <zmq.hpp>
#include <string>
#include <vector>
#include <atomic>
#include <thread>
#include <loguru.hpp>
#include <WHISPER/threadSafeQueue.hpp>
#include <WHISPER/Messages/Message.hpp>
/**
* @brief namespace for all whisper-com related components
@@ -20,7 +33,57 @@ namespace WHISPER
{
// Add datatypes here
class whispercomm{
private:
/// device ID
std::uint32_t ownID_;
/// device Type
SourceType ownDeviceType_;
/// all topics this service subscribed
std::vector<std::string> subscribedTopics;
/// show if the service is connected or not
std::atomic<bool> connected;
/// attribute identifying this service as a gateway and all packets should be forwarded
bool gateway = false;
/// variable for holding the receive thread identifier
std::thread receiveThread;
/// variable indicating if the receiveThread should be stopped
std::atomic<bool> stopReceiveThread;
std::shared_ptr<threadSafeQueue<std::string>> receiveQueue = nullptr;
void receive();
public:
whispercomm(std::uint32_t id, SourceType owndevicetype):ownID_(id),ownDeviceType_(owndevicetype)
{
receiveQueue = std::make_shared<threadSafeQueue<std::string>>();
};
void connect(std::shared_ptr<threadSafeQueue<std::string>> receiver);
void publish(std::string msg);
void disconnect();
void subscribe(std::string topic);
void unsubscribe(std::string topic);
protected:
void addMsgToReceiverQueue(std::string);
virtual void derivedConnect() = 0;
virtual void derivedDisconnect() = 0;
virtual void derivedPublish(std::string msg) = 0;
virtual void derivedReceive() = 0;