Defining a name for a Connection

This commit is contained in:
brunoherbelin
2020-10-23 21:54:45 +02:00
parent 509416d5a0
commit db0892d25b
5 changed files with 53 additions and 20 deletions

View File

@@ -10,7 +10,6 @@
#include "Connection.h" #include "Connection.h"
#include "Log.h" #include "Log.h"
#ifndef NDEBUG #ifndef NDEBUG
#define CONNECTION_DEBUG #define CONNECTION_DEBUG
#endif #endif
@@ -39,6 +38,9 @@ bool Connection::init()
// through exception runtime if fails // through exception runtime if fails
receiver_ = new UdpListeningReceiveSocket( IpEndpointName( IpEndpointName::ANY_ADDRESS, receiver_ = new UdpListeningReceiveSocket( IpEndpointName( IpEndpointName::ANY_ADDRESS,
connections_[0].port_handshake ), &listener_ ); connections_[0].port_handshake ), &listener_ );
// validate hostname
connections_[0].name = APP_NAME "@" + NetworkToolkit::hostname() +
"." + std::to_string(connections_[0].port_handshake-HANDSHAKE_PORT);
// all good // all good
trial = MAX_HANDSHAKE; trial = MAX_HANDSHAKE;
} }
@@ -87,11 +89,26 @@ ConnectionInfo Connection::info(int index)
} }
void Connection::print() struct hasName: public std::unary_function<ConnectionInfo, bool>
{ {
for(int i = 0; i<connections_.size(); i++) { inline bool operator()(const ConnectionInfo elem) const {
Log::Info(" - %s:%d", connections_[i].address_.c_str(), connections_[i].port_handshake); return (elem.name.compare(_a) == 0);
} }
hasName(std::string a) : _a(a) { }
private:
std::string _a;
};
int Connection::index(const std::string &name) const
{
int id = -1;
std::vector<ConnectionInfo>::const_iterator p = std::find_if(connections_.begin(), connections_.end(), hasName(name));
if (p != connections_.end())
id = std::distance(connections_.begin(), p);
return id;
} }
int Connection::index(ConnectionInfo i) const int Connection::index(ConnectionInfo i) const
@@ -105,6 +122,13 @@ int Connection::index(ConnectionInfo i) const
return id; return id;
} }
void Connection::print()
{
for(int i = 0; i<connections_.size(); i++) {
Log::Info(" - %s %s:%d", connections_[i].name.c_str(), connections_[i].address.c_str(), connections_[i].port_handshake);
}
}
void Connection::listen() void Connection::listen()
{ {
#ifdef CONNECTION_DEBUG #ifdef CONNECTION_DEBUG
@@ -187,6 +211,7 @@ void ConnectionRequestListener::ProcessMessage( const osc::ReceivedMessage& m,
osc::OutboundPacketStream p( buffer, IP_MTU_SIZE ); osc::OutboundPacketStream p( buffer, IP_MTU_SIZE );
p.Clear(); p.Clear();
p << osc::BeginMessage( OSC_PREFIX OSC_PONG ); p << osc::BeginMessage( OSC_PREFIX OSC_PONG );
p << Connection::manager().connections_[0].name.c_str();
p << Connection::manager().connections_[0].port_handshake; p << Connection::manager().connections_[0].port_handshake;
p << Connection::manager().connections_[0].port_stream_send; p << Connection::manager().connections_[0].port_stream_send;
p << Connection::manager().connections_[0].port_stream_receive; p << Connection::manager().connections_[0].port_stream_receive;
@@ -203,10 +228,11 @@ void ConnectionRequestListener::ProcessMessage( const osc::ReceivedMessage& m,
// create info struct // create info struct
ConnectionInfo info; ConnectionInfo info;
info.address_ = remote_ip; info.address = remote_ip;
// add all ports info // add all ports info
osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin(); osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
info.name = std::string( (arg++)->AsString() );
info.port_handshake = (arg++)->AsInt32(); info.port_handshake = (arg++)->AsInt32();
info.port_stream_send = (arg++)->AsInt32(); info.port_stream_send = (arg++)->AsInt32();
info.port_stream_receive = (arg++)->AsInt32(); info.port_stream_receive = (arg++)->AsInt32();

View File

@@ -18,34 +18,37 @@ protected:
struct ConnectionInfo { struct ConnectionInfo {
std::string address_; std::string address;
int port_handshake; int port_handshake;
int port_stream_send; int port_stream_send;
int port_stream_receive; int port_stream_receive;
std::string name;
int alive; int alive;
ConnectionInfo () { ConnectionInfo () {
address_ = "localhost"; address = "127.0.0.1";
port_handshake = HANDSHAKE_PORT; port_handshake = HANDSHAKE_PORT;
port_stream_send = STREAM_REQUEST_PORT; port_stream_send = STREAM_REQUEST_PORT;
port_stream_receive = STREAM_RESPONSE_PORT; port_stream_receive = STREAM_RESPONSE_PORT;
name = "user@localhost";
alive = ALIVE; alive = ALIVE;
} }
inline ConnectionInfo& operator = (const ConnectionInfo& o) inline ConnectionInfo& operator = (const ConnectionInfo& o)
{ {
if (this != &o) { if (this != &o) {
this->address_ = o.address_; this->address = o.address;
this->port_handshake = o.port_handshake; this->port_handshake = o.port_handshake;
this->port_stream_send = o.port_stream_send; this->port_stream_send = o.port_stream_send;
this->port_stream_receive = o.port_stream_receive; this->port_stream_receive = o.port_stream_receive;
this->name = o.name;
} }
return *this; return *this;
} }
inline bool operator == (const ConnectionInfo& o) const inline bool operator == (const ConnectionInfo& o) const
{ {
return this->address_.compare(o.address_) == 0 return this->address.compare(o.address) == 0
&& this->port_handshake == o.port_handshake; && this->port_handshake == o.port_handshake;
} }
@@ -73,6 +76,7 @@ public:
int numHosts () const; int numHosts () const;
int index(ConnectionInfo i) const; int index(ConnectionInfo i) const;
int index(const std::string &name) const;
ConnectionInfo info(int index = 0); // index 0 for self ConnectionInfo info(int index = 0); // index 0 for self
private: private:

View File

@@ -156,3 +156,12 @@ std::string NetworkToolkit::closest_host_ip(const std::string &ip)
return address; return address;
} }
std::string NetworkToolkit::hostname()
{
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
return std::string(hostname);
}

View File

@@ -38,6 +38,8 @@ std::vector<std::string> host_ips();
bool is_host_ip(const std::string &ip); bool is_host_ip(const std::string &ip);
std::string closest_host_ip(const std::string &ip); std::string closest_host_ip(const std::string &ip);
std::string hostname();
} }
#endif // NETWORKTOOLKIT_H #endif // NETWORKTOOLKIT_H

View File

@@ -39,6 +39,7 @@ using namespace std;
#include "Log.h" #include "Log.h"
#include "SystemToolkit.h" #include "SystemToolkit.h"
#include "RenderingManager.h" #include "RenderingManager.h"
#include "Connection.h"
#include "ActionManager.h" #include "ActionManager.h"
#include "Resource.h" #include "Resource.h"
#include "FileDialog.h" #include "FileDialog.h"
@@ -2311,21 +2312,12 @@ void Navigator::RenderNewPannel()
new_source_preview_.setSource( Mixer::manager().createSourceDevice(namedev), namedev); new_source_preview_.setSource( Mixer::manager().createSourceDevice(namedev), namedev);
} }
} }
for (int d = 1; d < Connection::manager().numHosts(); ++d){
// for (uint n = 0; n < NetworkToolkit::DEFAULT; ++n){ std::string namehost = Connection::manager().info(d).name;
// if (ImGui::Selectable( NetworkToolkit::protocol_name[n] )) {
// new_source_preview_.setSource( Mixer::manager().createSourceNetwork("192.168.0.30") );
// }
// }
for (int d = 0; d < NetworkHosts::manager().numHosts(); ++d){
std::string namehost = NetworkHosts::manager().name(d);
if (ImGui::Selectable( namehost.c_str() )) { if (ImGui::Selectable( namehost.c_str() )) {
new_source_preview_.setSource( Mixer::manager().createSourceNetwork(namehost), namehost); new_source_preview_.setSource( Mixer::manager().createSourceNetwork(namehost), namehost);
} }
} }
ImGui::EndCombo(); ImGui::EndCombo();
} }