Initial commit
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
|||||||
|
*.o.*
|
||||||
|
*.d.*
|
||||||
|
*.a.*
|
||||||
|
.*.swp
|
||||||
|
.AppleDouble
|
||||||
|
.debug
|
||||||
|
VStarCamFinder*
|
||||||
|
!VStarCamFinder.cc
|
||||||
|
!VStarCamFinder.cpp
|
||||||
|
!VStarCamFinder.h
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "Makefiles"]
|
||||||
|
path = Makefiles
|
||||||
|
url = https://gogs.dierkse.nl/JDierkse/Makefiles.git
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../Makefile
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
#include "VStarCam/Finder.h"
|
||||||
|
#include <Logging.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Logging::OpenLog();
|
||||||
|
Logging::SetLogMask(Logging::Severity::Info);
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Usage: " << argv[0] << " <ipaddress>";
|
||||||
|
Logging::Log(Logging::Severity::Error, ss.str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ipAddress(argv[1]);
|
||||||
|
|
||||||
|
int port = 0;
|
||||||
|
|
||||||
|
VStarCam::Finder finder("192.168.101.100");
|
||||||
|
port = finder.GetPort(ipAddress);
|
||||||
|
|
||||||
|
if (port != 0)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << port;
|
||||||
|
Logging::Log(Logging::Severity::Error, ss.str());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "ERROR: " << e.what() << std::endl;
|
||||||
|
|
||||||
|
Logging::Log(Logging::Severity::Error, ss.str());
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../../Libraries/Logging
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../../Libraries/Network
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../../Libraries/Utilities
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../../Libraries/asio
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../../Libraries/clipp
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#
|
||||||
|
# Makefile.conf
|
||||||
|
#
|
||||||
|
|
||||||
|
LFLAGS += -lNetwork
|
||||||
|
LFLAGS += -L$(ROOTPATH)/Libraries/Network/lib/$(ARCH)
|
||||||
|
CFLAGS += -I$(ROOTPATH)/Libraries/Network/include
|
||||||
|
|
||||||
|
LFLAGS += -lLogging
|
||||||
|
LFLAGS += -L$(ROOTPATH)/Libraries/Logging/lib/$(ARCH)
|
||||||
|
CFLAGS += -I$(ROOTPATH)/Libraries/Logging/include
|
||||||
|
|
||||||
|
LFLAGS += -lUtilities
|
||||||
|
LFLAGS += -L$(ROOTPATH)/Libraries/Utilities/lib/$(ARCH)
|
||||||
|
CFLAGS += -I$(ROOTPATH)/Libraries/Utilities/include
|
||||||
|
|
||||||
|
CFLAGS += -I$(ROOTPATH)/Libraries/asio/asio/include
|
||||||
|
CFLAGS += -I$(ROOTPATH)/Libraries/clipp
|
||||||
|
|
||||||
|
LFLAGS += -pthread -lm
|
||||||
|
CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/Libraries
|
||||||
|
DEBUGDIR := .debug
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#
|
||||||
|
# Makefile.target
|
||||||
|
#
|
||||||
|
|
||||||
|
VStarCamFinder.$(ARCH): $(OBJECTS) Application/VStarCamFinder.o.$(ARCH)
|
||||||
|
$(call build_target_arch,$@,$^)
|
||||||
|
|
||||||
|
VStarCamFinder:
|
||||||
|
$(call build_target,$@)
|
||||||
|
|
||||||
|
.DEFAULT_GOAL := VStarCamFinder
|
||||||
|
|
||||||
|
TARGETS += VStarCamFinder
|
||||||
Submodule
+1
Submodule Makefiles added at cd9ef1a1b0
@@ -0,0 +1,142 @@
|
|||||||
|
#include "Finder.h"
|
||||||
|
#include <BroadcastClient.h>
|
||||||
|
#include <BroadcastServer.h>
|
||||||
|
#include <Logging.h>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
|
||||||
|
namespace VStarCam {
|
||||||
|
|
||||||
|
Finder::Finder(const std::string& sourceAddress) :
|
||||||
|
m_startTime(asio::system_timer::time_point(std::chrono::system_clock::duration::max())),
|
||||||
|
m_timeout(std::chrono::seconds(5)),
|
||||||
|
m_sourceAddress(sourceAddress),
|
||||||
|
m_sourcePort(8600),
|
||||||
|
m_targetPort(9600),
|
||||||
|
m_port(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Finder::~Finder()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int Finder::GetPort(const std::string& ipAddress)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Retrieving port for " << ipAddress << std::endl;
|
||||||
|
Logging::Log(Logging::Severity::Info, ss.str());
|
||||||
|
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
|
m_port = 0;
|
||||||
|
m_targetAddress = ipAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
Network::BroadcastClient client(m_sourceAddress, m_targetPort, std::bind(&Finder::ProcessMessage, this, std::placeholders::_1));
|
||||||
|
Network::BroadcastServer server(m_sourceAddress, m_sourcePort, std::function<void(const std::string&)>());
|
||||||
|
|
||||||
|
m_startTime = std::chrono::system_clock::now();
|
||||||
|
std::string message("\x44\x48\x01\x01\x0a");
|
||||||
|
server.Broadcast(message, m_targetPort);
|
||||||
|
|
||||||
|
while (!PortFound() && !Timeout())
|
||||||
|
server.Broadcast(message, m_targetPort);
|
||||||
|
|
||||||
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
|
int port = m_port;
|
||||||
|
m_port = 0;
|
||||||
|
|
||||||
|
ss.str("");
|
||||||
|
ss << "New port for " << ipAddress << ": " << port << std::endl;
|
||||||
|
Logging::Log(Logging::Severity::Info, ss.str());
|
||||||
|
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Finder::Timeout() const
|
||||||
|
{
|
||||||
|
return m_startTime + m_timeout <= std::chrono::system_clock::now();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Finder::PortFound()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
|
return m_condition.wait_for(lock, std::chrono::milliseconds(1000)) == std::cv_status::no_timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Finder::ProcessMessage(const std::string& message)
|
||||||
|
{
|
||||||
|
std::string::const_iterator it = message.begin();
|
||||||
|
|
||||||
|
// Skip Header
|
||||||
|
it += 4;
|
||||||
|
|
||||||
|
std::string ipAddress = GetString(it);
|
||||||
|
std::string subnetMask = GetString(it);
|
||||||
|
std::string gateway = GetString(it);
|
||||||
|
std::string dns1 = GetString(it);
|
||||||
|
std::string dns2 = GetString(it);
|
||||||
|
|
||||||
|
// Skip Header
|
||||||
|
it += 6;
|
||||||
|
|
||||||
|
// Get Port
|
||||||
|
unsigned int port;
|
||||||
|
|
||||||
|
unsigned char low = *it++;
|
||||||
|
unsigned char high = *it++;
|
||||||
|
|
||||||
|
std::string hexPort;
|
||||||
|
hexPort += high;
|
||||||
|
hexPort += low;
|
||||||
|
hexPort = StringToHex(hexPort);
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << std::hex << hexPort;
|
||||||
|
ss >> port;
|
||||||
|
|
||||||
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
|
if (ipAddress == m_targetAddress)
|
||||||
|
{
|
||||||
|
m_port = port;
|
||||||
|
lock.unlock();
|
||||||
|
m_condition.notify_one();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Finder::GetString(std::string::const_iterator& it) const
|
||||||
|
{
|
||||||
|
std::string output;
|
||||||
|
while (static_cast<unsigned char>(*it) != 0)
|
||||||
|
output += *it++;
|
||||||
|
|
||||||
|
Advance(it);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Finder::Advance(std::string::const_iterator& it) const
|
||||||
|
{
|
||||||
|
while (static_cast<unsigned char>(*it) == 0)
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Finder::StringToHex(const std::string& input) const
|
||||||
|
{
|
||||||
|
static const char* const lut = "0123456789ABCDEF";
|
||||||
|
size_t len = input.length();
|
||||||
|
|
||||||
|
std::string output;
|
||||||
|
output.reserve(2 * len);
|
||||||
|
for (size_t i = 0; i < len; ++i)
|
||||||
|
{
|
||||||
|
const unsigned char c = input[i];
|
||||||
|
output.push_back(lut[c >> 4]);
|
||||||
|
output.push_back(lut[c & 15]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace VStarCam
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef VSTARCAM_FINDER_H
|
||||||
|
#define VSTARCAM_FINDER_H
|
||||||
|
|
||||||
|
#include <asio.hpp>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
|
namespace VStarCam {
|
||||||
|
|
||||||
|
class Finder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit Finder(const std::string& ipAddress);
|
||||||
|
~Finder();
|
||||||
|
|
||||||
|
int GetPort(const std::string& ipAddress);
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool Timeout() const;
|
||||||
|
bool PortFound();
|
||||||
|
void ProcessMessage(const std::string& message);
|
||||||
|
std::string GetString(std::string::const_iterator& it) const;
|
||||||
|
void Advance(std::string::const_iterator& it) const;
|
||||||
|
std::string StringToHex(const std::string& input) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
asio::system_timer::time_point m_startTime;
|
||||||
|
asio::system_timer::duration m_timeout;
|
||||||
|
|
||||||
|
mutable std::mutex m_mutex;
|
||||||
|
std::condition_variable m_condition;
|
||||||
|
|
||||||
|
std::string m_sourceAddress;
|
||||||
|
std::string m_targetAddress;
|
||||||
|
|
||||||
|
int m_sourcePort;
|
||||||
|
int m_targetPort;
|
||||||
|
|
||||||
|
int m_port;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace VStarCam
|
||||||
|
|
||||||
|
#endif // UTIL_VSTARCAMFINDER_H
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../Makefile
|
||||||
Reference in New Issue
Block a user