فهرست منبع

Initial commit

JDierkse 5 سال پیش
کامیت
2945e8f206
16فایلهای تغییر یافته به همراه294 افزوده شده و 0 حذف شده
  1. 10 0
      .gitignore
  2. 3 0
      .gitmodules
  3. 1 0
      Application/Makefile
  4. 48 0
      Application/VStarCamFinder.cc
  5. 1 0
      Libraries/Logging
  6. 1 0
      Libraries/Network
  7. 1 0
      Libraries/Utilities
  8. 1 0
      Libraries/asio
  9. 1 0
      Libraries/clipp
  10. 1 0
      Makefile
  11. 22 0
      Makefile.conf
  12. 13 0
      Makefile.target
  13. 1 0
      Makefiles
  14. 142 0
      VStarCam/Finder.cpp
  15. 47 0
      VStarCam/Finder.h
  16. 1 0
      VStarCam/Makefile

+ 10 - 0
.gitignore

@@ -0,0 +1,10 @@
+*.o.*
+*.d.*
+*.a.*
+.*.swp
+.AppleDouble
+.debug
+VStarCamFinder*
+!VStarCamFinder.cc
+!VStarCamFinder.cpp
+!VStarCamFinder.h

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "Makefiles"]
+	path = Makefiles
+	url = https://gogs.dierkse.nl/JDierkse/Makefiles.git

+ 1 - 0
Application/Makefile

@@ -0,0 +1 @@
+../Makefile

+ 48 - 0
Application/VStarCamFinder.cc

@@ -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;
+	}
+}

+ 1 - 0
Libraries/Logging

@@ -0,0 +1 @@
+../../Libraries/Logging

+ 1 - 0
Libraries/Network

@@ -0,0 +1 @@
+../../Libraries/Network

+ 1 - 0
Libraries/Utilities

@@ -0,0 +1 @@
+../../Libraries/Utilities

+ 1 - 0
Libraries/asio

@@ -0,0 +1 @@
+../../Libraries/asio

+ 1 - 0
Libraries/clipp

@@ -0,0 +1 @@
+../../Libraries/clipp

+ 1 - 0
Makefile

@@ -0,0 +1 @@
+Makefiles/Makefile

+ 22 - 0
Makefile.conf

@@ -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

+ 13 - 0
Makefile.target

@@ -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

+ 1 - 0
Makefiles

@@ -0,0 +1 @@
+Subproject commit cd9ef1a1b0f7b88f36b0c51b4209d1859aca83aa

+ 142 - 0
VStarCam/Finder.cpp

@@ -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

+ 47 - 0
VStarCam/Finder.h

@@ -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

+ 1 - 0
VStarCam/Makefile

@@ -0,0 +1 @@
+../Makefile