ADD: Message container and Join Message
This commit is contained in:
53
src/WHISPER/Messages/Join.cpp
Normal file
53
src/WHISPER/Messages/Join.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "WHISPER/Messages/Message.hpp"
|
||||
#include "WHISPER/Messages/Protos/join.pb.h"
|
||||
#include <WHISPER/Messages/Join.hpp>
|
||||
#include <memory>
|
||||
|
||||
|
||||
|
||||
|
||||
namespace WHISPER {
|
||||
|
||||
Join::Join(std::string receivedMessage){
|
||||
msg = messages::header::Message();
|
||||
try {
|
||||
msg.ParseFromString(receivedMessage);
|
||||
topic_ = msg.topic();
|
||||
sourceType_ = msg.sourcetype();
|
||||
msgType_ = msg.msgtype();
|
||||
joinMessage = messages::join::Join();
|
||||
if ( msg.payload_size()) {
|
||||
if (msg.payload().begin()->Is<messages::join::Join>()) {
|
||||
msg.payload().begin()->UnpackTo(&joinMessage);
|
||||
}
|
||||
}
|
||||
port = joinMessage.port();
|
||||
sourceAddr = joinMessage.srcaddress();
|
||||
|
||||
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
LOG_S(ERROR)<<e.what();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
WHISPER::Join::Join(std::uint32_t deviceID,std::uint32_t topic, MsgType Type, SourceType src,std::uint32_t port, std::string addr):
|
||||
Message(deviceID,topic,Type,src),port(port),sourceAddr(addr)
|
||||
{
|
||||
joinMessage = messages::join::Join();
|
||||
joinMessage.set_port(port);
|
||||
joinMessage.set_srcaddress(sourceAddr);
|
||||
|
||||
LOG_S(INFO)<< "join message befor packing:" << joinMessage.ByteSizeLong();
|
||||
auto test = std::make_shared<google::protobuf::Any>();
|
||||
test->PackFrom(joinMessage);
|
||||
addPayLoad(test);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
72
src/WHISPER/Messages/Message.cpp
Normal file
72
src/WHISPER/Messages/Message.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
|
||||
|
||||
#include "WHISPER/Messages/Protos/join.pb.h"
|
||||
#include <WHISPER/Messages/Message.hpp>
|
||||
#include <memory>
|
||||
|
||||
|
||||
|
||||
|
||||
namespace WHISPER {
|
||||
Message::Message(std::string stringMessage)
|
||||
{
|
||||
msg = messages::header::Message();
|
||||
try {
|
||||
msg.ParseFromString(stringMessage);
|
||||
topic_ = msg.topic();
|
||||
sourceType_ = msg.sourcetype();
|
||||
msgType_ = msg.msgtype();
|
||||
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
LOG_S(ERROR)<<e.what();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Message::Message(std::int32_t deviceId,std::uint32_t topic, MsgType Type,SourceType src):topic_(topic),sourceType_(src),msgType_(Type){
|
||||
msg = messages::header::Message();
|
||||
|
||||
if(msg.IsInitialized())
|
||||
{
|
||||
msg.set_sourceid(deviceId);
|
||||
msg.set_topic(topic);
|
||||
msg.set_sourcetype(sourceType_);
|
||||
msg.set_msgtype(msgType_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string Message::getPayloadString(){
|
||||
return payloadString_;
|
||||
}
|
||||
|
||||
|
||||
void Message::addPayLoad(std::shared_ptr<google::protobuf::Any> payload){
|
||||
payload_ = payload;
|
||||
LOG_S(INFO)<< "pack any size in message class "<<payload_->ByteSizeLong();
|
||||
|
||||
msg.add_payload()->CopyFrom(*payload_);
|
||||
|
||||
LOG_S(INFO)<< "pack any size in message class "<<payload_->ByteSizeLong();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string Message::serialize(){
|
||||
|
||||
std::string serializedMessage;
|
||||
LOG_S(INFO)<<msg.ByteSizeLong();
|
||||
if (msg.IsInitialized()) {
|
||||
serializedMessage = msg.SerializeAsString();
|
||||
}
|
||||
|
||||
return serializedMessage;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
60
src/WHISPER/threadSafeQueue.cpp
Normal file
60
src/WHISPER/threadSafeQueue.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 BasicMessageQueue.cpp
|
||||
* @brief class which encapsulates queue
|
||||
* @author Christina Sander <christina.sander@hsu-hh.de>
|
||||
* @date 25.06.2020
|
||||
* @copyright MPLv2
|
||||
*/
|
||||
#include <WHISPER/threadSafeQueue.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
template<class T>
|
||||
WHISPER::threadSafeQueue<T> threadSafeQueue(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// appends the given message to the message queue
|
||||
template<class T>
|
||||
void WHISPER::threadSafeQueue< T>::addElement(std::unique_ptr<T> elem)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mx);
|
||||
q.push( std::move(elem) );
|
||||
lk.unlock();
|
||||
condVar.notify_one();
|
||||
}
|
||||
|
||||
// gets a message from the queue
|
||||
template<class T>
|
||||
std::unique_ptr<T> WHISPER::threadSafeQueue<T>::getElement()
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mx);
|
||||
if( 0 == q.size() )
|
||||
{
|
||||
lk.unlock();
|
||||
throw std::length_error("Empty Queue\n");
|
||||
}
|
||||
std::unique_ptr<std::string> elem = std::move( q.front() );
|
||||
q.pop();
|
||||
lk.unlock();
|
||||
return std::move(elem);
|
||||
}
|
||||
|
||||
|
||||
//returns the size of the message queue
|
||||
template<class T>
|
||||
unsigned int WHISPER::threadSafeQueue<T>::size()
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mx);
|
||||
unsigned int size = q.size();
|
||||
lk.unlock();
|
||||
return size;
|
||||
}
|
||||
@@ -15,5 +15,59 @@
|
||||
*/
|
||||
namespace WHISPER
|
||||
{
|
||||
|
||||
|
||||
|
||||
void whispercomm::connect(std::shared_ptr<threadSafeQueue<std::string>> receiver)
|
||||
{
|
||||
this->receiveQueue = receiver;
|
||||
this->derivedConnect();
|
||||
receiveThread = std::thread(&WHISPER::whispercomm::receive,this);
|
||||
|
||||
}
|
||||
|
||||
void whispercomm::disconnect()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void whispercomm::publish(std::string msg){
|
||||
this->derivedPublish(msg);
|
||||
}
|
||||
|
||||
void whispercomm::receive(){
|
||||
this->derivedReceive();
|
||||
connected = true;
|
||||
|
||||
while(!stopReceiveThread)
|
||||
{
|
||||
derivedReceive();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void whispercomm::subscribe(std::string topic)
|
||||
{
|
||||
this->subscribedTopics.push_back(topic);
|
||||
}
|
||||
|
||||
void whispercomm::unsubscribe(std::string topic)
|
||||
{
|
||||
for (std::vector<std::string>::iterator it = subscribedTopics.begin(); it != subscribedTopics.end();it++)
|
||||
{
|
||||
if (*it == topic)
|
||||
{
|
||||
it = subscribedTopics.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void whispercomm::addMsgToReceiverQueue(std::string)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Add datatypes here
|
||||
} // namespace WHISPER
|
||||
26
src/main.cpp
26
src/main.cpp
@@ -1,12 +1,36 @@
|
||||
#include "WHISPER/Messages/Message.hpp"
|
||||
#include <iostream>
|
||||
#include <loguru.hpp>
|
||||
|
||||
#include <WHISPER/Messages/Join.hpp>
|
||||
#include <WHISPER/Messages/Protos/message.pb.h>
|
||||
|
||||
#include "../include/WHISPER/Messages/Protos/join.pb.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
||||
|
||||
|
||||
|
||||
WHISPER::Join join(1,1,WHISPER::MsgType::JOIN,WHISPER::SourceType::SHIP,8000,"127.0.0.1");
|
||||
std::string msg = join.serialize();
|
||||
LOG_S(INFO)<<" serialized Message is "<<msg.size();
|
||||
LOG_S(INFO)<<msg;
|
||||
|
||||
messages::header::Message proto;
|
||||
proto.ParseFromString(msg);
|
||||
|
||||
WHISPER::Message receivedMessage(msg);
|
||||
|
||||
LOG_S(INFO)<<receivedMessage.msgType_;
|
||||
|
||||
switch (receivedMessage.msgType_) {
|
||||
case WHISPER::MsgType::JOIN:
|
||||
WHISPER::Join receivedJoin(msg);
|
||||
LOG_S(INFO)<< "join message data afer reception "<< receivedJoin.port;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
|
||||
// [START declaration]
|
||||
syntax = "proto3";
|
||||
package messages.join;
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
// [END declaration]
|
||||
|
||||
|
||||
// [START messages]
|
||||
message Join {
|
||||
int32 id = 1;
|
||||
int32 port = 2;
|
||||
string address = 3;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user