implemented local client join functionality
This commit is contained in:
@@ -98,6 +98,18 @@ target_include_directories(whisper-com PUBLIC
|
|||||||
loguru
|
loguru
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(mainRcv
|
||||||
|
|
||||||
|
src/mainRecv.cpp
|
||||||
|
|
||||||
|
|
||||||
|
)
|
||||||
|
target_link_libraries(mainRcv
|
||||||
|
loguru
|
||||||
|
whisper-com
|
||||||
|
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Everything TEST related
|
# Everything TEST related
|
||||||
|
|||||||
@@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#include "WHISPER/whisper.hpp"
|
#include "WHISPER/whisper.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#define ZMQ_BUILD_DRAFT_API 1
|
#define ZMQ_BUILD_DRAFT_API 1
|
||||||
#include <zmq.hpp>
|
#include <zmq.hpp>
|
||||||
@@ -11,17 +13,31 @@
|
|||||||
|
|
||||||
namespace WHISPER {
|
namespace WHISPER {
|
||||||
|
|
||||||
|
|
||||||
|
struct localClient{
|
||||||
|
short port;
|
||||||
|
zmq::socket_t clientSocket;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class InternalUDPService : public whispercomm {
|
class InternalUDPService : public whispercomm {
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string address_;
|
std::string destinationAdress_;
|
||||||
|
std::string myAdress_;
|
||||||
std::uint16_t port_;
|
std::uint16_t port_;
|
||||||
std::uint16_t ownReceivingPort_;
|
std::uint16_t ownReceivingPort_;
|
||||||
|
|
||||||
zmq::context_t ctx;
|
zmq::context_t ctx;
|
||||||
zmq::socket_t sender;
|
zmq::socket_t sender;
|
||||||
std::shared_ptr<zmq::socket_t> receiver;
|
std::shared_ptr<zmq::socket_t> receiver = nullptr;
|
||||||
|
std::shared_ptr<zmq::socket_t> loopbackSocket = nullptr;
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<localClient>> localclients;
|
||||||
|
|
||||||
std::uint16_t checkPort(std::uint16_t port);
|
std::uint16_t checkPort(std::uint16_t port);
|
||||||
|
|
||||||
@@ -33,8 +49,11 @@ namespace WHISPER {
|
|||||||
void derivedUnsubscribe(std::string topic) override;
|
void derivedUnsubscribe(std::string topic) override;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
InternalUDPService(std::uint32_t id, SourceType owndevicetype,std::uint16_t port, std::string address);
|
InternalUDPService(std::uint32_t id, SourceType owndevicetype,std::uint16_t port, std::string destinationAdress, std::string myAdress);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ namespace WHISPER {
|
|||||||
JOIN=1,
|
JOIN=1,
|
||||||
/// participant is leaving
|
/// participant is leaving
|
||||||
LEAVE,
|
LEAVE,
|
||||||
|
/// PING
|
||||||
|
PING,
|
||||||
|
/// PONG
|
||||||
|
PONG,
|
||||||
/// owntrack informaton
|
/// owntrack informaton
|
||||||
OWN_TRACK,
|
OWN_TRACK,
|
||||||
/// raw track message
|
/// raw track message
|
||||||
|
|||||||
@@ -70,6 +70,8 @@ namespace WHISPER
|
|||||||
void subscribe(std::string topic);
|
void subscribe(std::string topic);
|
||||||
void unsubscribe(std::string topic);
|
void unsubscribe(std::string topic);
|
||||||
|
|
||||||
|
std::uint32_t getOwnID();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void addMsgToReceiverQueue(WHISPER::Message);
|
void addMsgToReceiverQueue(WHISPER::Message);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "WHISPER/InternalUDPService.hpp"
|
#include "WHISPER/InternalUDPService.hpp"
|
||||||
|
#include "WHISPER/Messages/Join.hpp"
|
||||||
#include "WHISPER/Messages/Message.hpp"
|
#include "WHISPER/Messages/Message.hpp"
|
||||||
#include "WHISPER/whisper.hpp"
|
#include "WHISPER/whisper.hpp"
|
||||||
#include "zmq.hpp"
|
#include "zmq.hpp"
|
||||||
@@ -17,8 +18,8 @@
|
|||||||
namespace WHISPER {
|
namespace WHISPER {
|
||||||
|
|
||||||
|
|
||||||
InternalUDPService::InternalUDPService(std::uint32_t id, SourceType owndevicetype,std::uint16_t port, std::string address):
|
InternalUDPService::InternalUDPService(std::uint32_t id, SourceType owndevicetype,std::uint16_t port, std::string destinationAdress,std::string myAdress):
|
||||||
whispercomm(id, owndevicetype),port_(port),address_(address)
|
whispercomm(id, owndevicetype),port_(port),destinationAdress_(destinationAdress),myAdress_(myAdress)
|
||||||
{
|
{
|
||||||
ctx = zmq::context_t(2);
|
ctx = zmq::context_t(2);
|
||||||
sender = zmq::socket_t(ctx,zmq::socket_type::radio);
|
sender = zmq::socket_t(ctx,zmq::socket_type::radio);
|
||||||
@@ -30,7 +31,7 @@ namespace WHISPER {
|
|||||||
void InternalUDPService::derivedConnect()
|
void InternalUDPService::derivedConnect()
|
||||||
{
|
{
|
||||||
ownReceivingPort_ = checkPort(port_);
|
ownReceivingPort_ = checkPort(port_);
|
||||||
LOG_S(INFO)<< "could bind to port:" << ownReceivingPort_;
|
std::string sendingPort = std::to_string(port_);
|
||||||
|
|
||||||
|
|
||||||
if(ownReceivingPort_ == port_){
|
if(ownReceivingPort_ == port_){
|
||||||
@@ -39,17 +40,24 @@ namespace WHISPER {
|
|||||||
}else if (ownReceivingPort_ == 0) {
|
}else if (ownReceivingPort_ == 0) {
|
||||||
throw std::invalid_argument( " receiver cant bind to port " );
|
throw std::invalid_argument( " receiver cant bind to port " );
|
||||||
}else {
|
}else {
|
||||||
|
LOG_S(INFO)<< "new port to bind: " << ownReceivingPort_;
|
||||||
|
|
||||||
|
loopbackSocket = std::make_shared<zmq::socket_t>(ctx,zmq::socket_type::radio);
|
||||||
|
loopbackSocket->connect("udp://127.0.0.255:"+sendingPort);
|
||||||
|
|
||||||
std::string portAsString = std::to_string(ownReceivingPort_);
|
std::string portAsString = std::to_string(ownReceivingPort_);
|
||||||
|
|
||||||
receiver->bind("udp://*:"+portAsString);
|
receiver->bind("udp://*:"+portAsString);
|
||||||
}
|
}
|
||||||
|
|
||||||
receiver->join("management");
|
receiver->join("management");
|
||||||
|
|
||||||
|
|
||||||
std::string sendingPort = std::to_string(port_);
|
|
||||||
LOG_S(INFO)<< sendingPort;
|
LOG_S(INFO)<< sendingPort;
|
||||||
|
|
||||||
sender.connect("udp://"+address_+":"+sendingPort);
|
sender.connect("udp://"+destinationAdress_+":"+sendingPort);
|
||||||
|
// sender.set(zmq::sockopt::multicast_loop ,1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -60,36 +68,81 @@ namespace WHISPER {
|
|||||||
}
|
}
|
||||||
void InternalUDPService::derivedPublish(std::string msg,std::string topic)
|
void InternalUDPService::derivedPublish(std::string msg,std::string topic)
|
||||||
{
|
{
|
||||||
zmq::message_t tmpmsg(msg.size());
|
zmq::message_t tmpmsg(msg.begin(),msg.end());
|
||||||
memcpy (tmpmsg.data (), msg.data(), msg.size());
|
|
||||||
const char *tmp = topic.c_str();
|
|
||||||
tmpmsg.set_group(topic.c_str());
|
tmpmsg.set_group(topic.c_str());
|
||||||
sender.send(tmpmsg,zmq::send_flags::none);
|
sender.send(tmpmsg,zmq::send_flags::none);
|
||||||
|
if (loopbackSocket != nullptr) {
|
||||||
|
LOG_S(INFO)<<"loop back send";
|
||||||
|
zmq::message_t tmpmsg(msg.begin(),msg.end());
|
||||||
|
tmpmsg.set_group(topic.c_str());
|
||||||
|
loopbackSocket->send(tmpmsg,zmq::send_flags::none);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
void InternalUDPService::derivedReceive()
|
void InternalUDPService::derivedReceive()
|
||||||
{
|
{
|
||||||
|
|
||||||
LOG_S(INFO)<<"zmq receiving funk";
|
|
||||||
zmq::message_t msg;
|
zmq::message_t msg;
|
||||||
|
|
||||||
|
|
||||||
receiver->recv(msg,zmq::recv_flags::none);
|
receiver->recv(msg,zmq::recv_flags::none);
|
||||||
// LOG_S(INFO)<<result.value();
|
|
||||||
std::string data;
|
std::string message = msg.to_string();
|
||||||
memcpy (data.data (), msg.data(), msg.size());
|
|
||||||
addMsgToReceiverQueue(WHISPER::Message(data));
|
WHISPER::Message receivedMessage(message);
|
||||||
|
|
||||||
|
if (receivedMessage.deviceId_ != getOwnID()) {
|
||||||
|
|
||||||
|
int msgType = receivedMessage.msgType_;
|
||||||
|
if (msgType == WHISPER::JOIN) {
|
||||||
|
|
||||||
|
WHISPER::Join join(message);
|
||||||
|
if (this->myAdress_ == join.sourceAddr || join.sourceAddr == "127.0.0.1") {
|
||||||
|
|
||||||
|
bool clientAllreadyIn = false;
|
||||||
|
if (localclients.size() > 0) {
|
||||||
|
for (std::vector<std::shared_ptr<localClient>>::iterator it = localclients.begin(); it != localclients.end();it++)
|
||||||
|
{
|
||||||
|
if (it->get()->port == join.port) {
|
||||||
|
clientAllreadyIn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (clientAllreadyIn == false) {
|
||||||
|
auto client = std::make_shared<localClient>();
|
||||||
|
client->port = join.port;
|
||||||
|
client->clientSocket = zmq::socket_t(ctx,zmq::socket_type::radio);
|
||||||
|
client->clientSocket.connect("udp://"+join.sourceAddr+":" + std::to_string(join.port));
|
||||||
|
localclients.emplace_back(client);
|
||||||
|
setGateway(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}else if(msgType == WHISPER::LEAVE)
|
||||||
|
{
|
||||||
|
|
||||||
|
}else if(msgType == WHISPER::PING)
|
||||||
|
{
|
||||||
|
|
||||||
|
}else if(msgType == WHISPER::PONG)
|
||||||
|
{
|
||||||
|
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
addMsgToReceiverQueue(WHISPER::Message(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InternalUDPService::derivedSubscribe(std::string topic)
|
void InternalUDPService::derivedSubscribe(std::string topic)
|
||||||
{
|
{
|
||||||
// receiver.join(topic.c_str());
|
receiver->join(topic.c_str());
|
||||||
}
|
}
|
||||||
void InternalUDPService::derivedUnsubscribe(std::string topic)
|
void InternalUDPService::derivedUnsubscribe(std::string topic)
|
||||||
{
|
{
|
||||||
// receiver.leave(topic.c_str());
|
receiver->leave(topic.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::uint16_t InternalUDPService::checkPort(std::uint16_t port)
|
std::uint16_t InternalUDPService::checkPort(std::uint16_t port)
|
||||||
@@ -157,6 +210,7 @@ namespace WHISPER {
|
|||||||
|
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
|
||||||
|
LOG_S(WARNING)<< "new assignet port is" << localPort;
|
||||||
|
|
||||||
return localPort;
|
return localPort;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ namespace WHISPER {
|
|||||||
msg = messages::header::Message();
|
msg = messages::header::Message();
|
||||||
try {
|
try {
|
||||||
msg.ParseFromString(stringMessage);
|
msg.ParseFromString(stringMessage);
|
||||||
|
|
||||||
|
deviceId_ = msg.sourceid();
|
||||||
topic_ = msg.topic();
|
topic_ = msg.topic();
|
||||||
sourceType_ = msg.sourcetype();
|
sourceType_ = msg.sourcetype();
|
||||||
msgType_ = msg.msgtype();
|
msgType_ = msg.msgtype();
|
||||||
@@ -60,7 +62,6 @@ namespace WHISPER {
|
|||||||
std::string Message::serialize(){
|
std::string Message::serialize(){
|
||||||
|
|
||||||
std::string serializedMessage;
|
std::string serializedMessage;
|
||||||
LOG_S(INFO)<<msg.ByteSizeLong();
|
|
||||||
if (msg.IsInitialized()) {
|
if (msg.IsInitialized()) {
|
||||||
serializedMessage = msg.SerializeAsString();
|
serializedMessage = msg.SerializeAsString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,10 @@
|
|||||||
namespace WHISPER
|
namespace WHISPER
|
||||||
{
|
{
|
||||||
|
|
||||||
|
std::uint32_t whispercomm::getOwnID()
|
||||||
|
{
|
||||||
|
return ownID_;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void whispercomm::connect(std::shared_ptr<threadSafeQueue<WHISPER::Message>> receiver)
|
void whispercomm::connect(std::shared_ptr<threadSafeQueue<WHISPER::Message>> receiver)
|
||||||
|
|||||||
66
src/main.cpp
66
src/main.cpp
@@ -1,5 +1,6 @@
|
|||||||
#include "WHISPER/InternalUDPService.hpp"
|
#include "WHISPER/InternalUDPService.hpp"
|
||||||
#include "WHISPER/Messages/Message.hpp"
|
#include "WHISPER/Messages/Message.hpp"
|
||||||
|
#include "zmq.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <loguru.hpp>
|
#include <loguru.hpp>
|
||||||
|
|
||||||
@@ -26,33 +27,66 @@ void killHandlerPing(int s) {
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
// GOOGLE_PROTOBUF_VERIFY_VERSION;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
WHISPER::Join join(1,1,WHISPER::MsgType::JOIN,WHISPER::SourceType::SHIP,8000,"127.0.0.1");
|
WHISPER::Join join(1,1,WHISPER::MsgType::JOIN,WHISPER::SourceType::SHIP,8000,"192.168.0.19");
|
||||||
std::string msg = join.serialize();
|
// std::string msg = join.serialize();
|
||||||
LOG_S(INFO)<<" serialized Message is "<<msg.size();
|
// LOG_S(INFO)<<" serialized Message is "<<msg.size();
|
||||||
LOG_S(INFO)<<msg;
|
// LOG_S(INFO)<<msg;
|
||||||
|
|
||||||
messages::header::Message proto;
|
// messages::header::Message proto;
|
||||||
proto.ParseFromString(msg);
|
// proto.ParseFromString(msg);
|
||||||
|
|
||||||
WHISPER::Message receivedMessage(msg);
|
// WHISPER::Message receivedMessage(msg);
|
||||||
|
|
||||||
LOG_S(INFO)<<receivedMessage.msgType_;
|
// LOG_S(INFO)<<receivedMessage.msgType_;
|
||||||
|
|
||||||
switch (receivedMessage.msgType_) {
|
// switch (receivedMessage.msgType_) {
|
||||||
case WHISPER::MsgType::JOIN:
|
// case WHISPER::MsgType::JOIN:
|
||||||
WHISPER::Join receivedJoin(msg);
|
// WHISPER::Join receivedJoin(msg);
|
||||||
LOG_S(INFO)<< "join message data afer reception "<< receivedJoin.port;
|
// LOG_S(INFO)<< "join message data afer reception "<< receivedJoin.port;
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
auto receiver = std::make_shared<WHISPER::threadSafeQueue<WHISPER::Message>>();
|
auto receiver = std::make_shared<WHISPER::threadSafeQueue<WHISPER::Message>>();
|
||||||
WHISPER::InternalUDPService service(1,WHISPER::SHIP,8000,"127.0.0.1");
|
WHISPER::InternalUDPService service(1,WHISPER::SHIP,8000,"192.168.0.255","192.168.0.19");
|
||||||
service.connect(receiver);
|
service.connect(receiver);
|
||||||
|
service.publish(join.serialize(), "management");
|
||||||
|
service.subscribe("data");
|
||||||
|
|
||||||
|
|
||||||
|
// zmq::context_t ctx(2);
|
||||||
|
// zmq::socket_t sock(ctx,zmq::socket_type::radio);
|
||||||
|
// sock.connect("udp://127.0.0.255:8000");
|
||||||
|
|
||||||
|
|
||||||
|
// std::string string = "hello world form 2";
|
||||||
|
// zmq::message_t msg(string.begin(),string.end());
|
||||||
|
// // memcpy (msg.data (), string.data(), string.size());
|
||||||
|
// LOG_S(INFO)<<"message contains "<<msg.str();
|
||||||
|
|
||||||
|
// zmq::context_t ctx(2);
|
||||||
|
// zmq::socket_t sock(ctx,zmq::socket_type::dish);
|
||||||
|
// sock.bind("udp://*:8000");
|
||||||
|
// zmq::message_t temp;
|
||||||
|
// sock.join("data");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int size = 0;
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
|
// zmq::message_t msg(string.begin(),string.end());
|
||||||
|
// msg.set_group("data");
|
||||||
|
// sock.send(msg,zmq::send_flags::none);
|
||||||
|
// service.publish(join.serialize(), "management");
|
||||||
|
|
||||||
|
if (size != receiver->size()) {
|
||||||
|
LOG_S(INFO)<<"received messages " << size;
|
||||||
|
size = receiver->size();
|
||||||
|
}
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
61
src/mainRecv.cpp
Normal file
61
src/mainRecv.cpp
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
#include "zmq.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <loguru.hpp>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include <WHISPER/InternalUDPService.hpp>
|
||||||
|
#include <WHISPER/Messages/Join.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
/// variable for stopping the application
|
||||||
|
bool running = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief killhandler to set running to false on CTRL-C
|
||||||
|
*
|
||||||
|
* @param s - the signal to manage
|
||||||
|
*/
|
||||||
|
void killHandlerPing(int s) {
|
||||||
|
|
||||||
|
if (s == SIGINT) {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// zmq::context_t ctx(2);
|
||||||
|
// zmq::socket_t sock(ctx,zmq::socket_type::dish);
|
||||||
|
// sock.bind("udp://*:8000");
|
||||||
|
// zmq::message_t temp;
|
||||||
|
// sock.join("data");
|
||||||
|
|
||||||
|
WHISPER::Join join(2,1,WHISPER::MsgType::JOIN,WHISPER::SourceType::SHIP,8000,"192.168.0.19");
|
||||||
|
|
||||||
|
auto receiver = std::make_shared<WHISPER::threadSafeQueue<WHISPER::Message>>();
|
||||||
|
WHISPER::InternalUDPService service(1,WHISPER::SHIP,8000,"192.168.0.255","192.168.0.255");
|
||||||
|
service.connect(receiver);
|
||||||
|
service.publish(join.serialize(), "management");
|
||||||
|
|
||||||
|
// zmq::context_t ctx(2);
|
||||||
|
// zmq::socket_t sock(ctx,zmq::socket_type::radio);
|
||||||
|
// sock.connect("udp://127.0.0.1:8000");
|
||||||
|
// std::string string = "hello world form 2";
|
||||||
|
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
// LOG_S(INFO)<<"received messages " << receiver->size();
|
||||||
|
service.publish(join.serialize(), "management");
|
||||||
|
// zmq::message_t msg(string.begin(),string.end());
|
||||||
|
// msg.set_group("management");
|
||||||
|
// sock.send(msg,zmq::send_flags::none);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user