implemented local client join functionality
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
|
||||
|
||||
#include "WHISPER/InternalUDPService.hpp"
|
||||
#include "WHISPER/Messages/Join.hpp"
|
||||
#include "WHISPER/Messages/Message.hpp"
|
||||
#include "WHISPER/whisper.hpp"
|
||||
#include "zmq.hpp"
|
||||
@@ -17,8 +18,8 @@
|
||||
namespace WHISPER {
|
||||
|
||||
|
||||
InternalUDPService::InternalUDPService(std::uint32_t id, SourceType owndevicetype,std::uint16_t port, std::string address):
|
||||
whispercomm(id, owndevicetype),port_(port),address_(address)
|
||||
InternalUDPService::InternalUDPService(std::uint32_t id, SourceType owndevicetype,std::uint16_t port, std::string destinationAdress,std::string myAdress):
|
||||
whispercomm(id, owndevicetype),port_(port),destinationAdress_(destinationAdress),myAdress_(myAdress)
|
||||
{
|
||||
ctx = zmq::context_t(2);
|
||||
sender = zmq::socket_t(ctx,zmq::socket_type::radio);
|
||||
@@ -30,7 +31,7 @@ namespace WHISPER {
|
||||
void InternalUDPService::derivedConnect()
|
||||
{
|
||||
ownReceivingPort_ = checkPort(port_);
|
||||
LOG_S(INFO)<< "could bind to port:" << ownReceivingPort_;
|
||||
std::string sendingPort = std::to_string(port_);
|
||||
|
||||
|
||||
if(ownReceivingPort_ == port_){
|
||||
@@ -39,17 +40,24 @@ namespace WHISPER {
|
||||
}else if (ownReceivingPort_ == 0) {
|
||||
throw std::invalid_argument( " receiver cant bind to port " );
|
||||
}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_);
|
||||
|
||||
receiver->bind("udp://*:"+portAsString);
|
||||
}
|
||||
|
||||
receiver->join("management");
|
||||
|
||||
|
||||
std::string sendingPort = std::to_string(port_);
|
||||
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)
|
||||
{
|
||||
zmq::message_t tmpmsg(msg.size());
|
||||
memcpy (tmpmsg.data (), msg.data(), msg.size());
|
||||
const char *tmp = topic.c_str();
|
||||
zmq::message_t tmpmsg(msg.begin(),msg.end());
|
||||
|
||||
tmpmsg.set_group(topic.c_str());
|
||||
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()
|
||||
{
|
||||
|
||||
LOG_S(INFO)<<"zmq receiving funk";
|
||||
zmq::message_t msg;
|
||||
|
||||
|
||||
zmq::message_t msg;
|
||||
receiver->recv(msg,zmq::recv_flags::none);
|
||||
// LOG_S(INFO)<<result.value();
|
||||
std::string data;
|
||||
memcpy (data.data (), msg.data(), msg.size());
|
||||
addMsgToReceiverQueue(WHISPER::Message(data));
|
||||
|
||||
|
||||
std::string message = msg.to_string();
|
||||
|
||||
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)
|
||||
{
|
||||
// receiver.join(topic.c_str());
|
||||
receiver->join(topic.c_str());
|
||||
}
|
||||
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)
|
||||
@@ -156,7 +209,8 @@ namespace WHISPER {
|
||||
|
||||
|
||||
close(sockfd);
|
||||
|
||||
|
||||
LOG_S(WARNING)<< "new assignet port is" << localPort;
|
||||
|
||||
return localPort;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user