mirror of
				https://github.com/hyperion-project/hyperion.ng.git
				synced 2025-03-01 10:33:28 +00:00 
			
		
		
		
	implement json forwarder
Former-commit-id: 5519118304bd5690e6b512481347579339e78ac9
This commit is contained in:
		@@ -9,12 +9,19 @@
 | 
			
		||||
// QT includes
 | 
			
		||||
#include <QList>
 | 
			
		||||
#include <QStringList>
 | 
			
		||||
#include <QHostAddress>
 | 
			
		||||
 | 
			
		||||
// Utils includes
 | 
			
		||||
#include <utils/ColorRgb.h>
 | 
			
		||||
class MessageForwarder
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
	struct JsonSlaveAddress {
 | 
			
		||||
		QHostAddress addr;
 | 
			
		||||
		quint16 port = 19444;
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	MessageForwarder();
 | 
			
		||||
	~MessageForwarder();
 | 
			
		||||
	
 | 
			
		||||
@@ -23,9 +30,9 @@ public:
 | 
			
		||||
	void sendMessage();
 | 
			
		||||
	
 | 
			
		||||
	QStringList getProtoSlaves();
 | 
			
		||||
	QList<MessageForwarder::JsonSlaveAddress> getJsonSlaves();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
	bool _running;
 | 
			
		||||
 | 
			
		||||
	QStringList _protoSlaves;
 | 
			
		||||
	QStringList               _protoSlaves;
 | 
			
		||||
	QList<MessageForwarder::JsonSlaveAddress>   _jsonSlaves;
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -275,13 +275,19 @@ MessageForwarder * Hyperion::createMessageForwarder(const Json::Value & forwarde
 | 
			
		||||
			if ( ! forwarderConfig["json"].isNull() && forwarderConfig["json"].isArray() )
 | 
			
		||||
			{
 | 
			
		||||
				for (const Json::Value& addr : forwarderConfig["json"])
 | 
			
		||||
				{
 | 
			
		||||
					std::cout << "Json forward to " << addr.asString() << std::endl;
 | 
			
		||||
					forwarder->addJsonSlave(addr.asString());
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if ( ! forwarderConfig["proto"].isNull() && forwarderConfig["proto"].isArray() )
 | 
			
		||||
			{
 | 
			
		||||
				for (const Json::Value& addr : forwarderConfig["proto"])
 | 
			
		||||
				{
 | 
			
		||||
					std::cout << "Proto forward to " << addr.asString() << std::endl;
 | 
			
		||||
					forwarder->addProtoSlave(addr.asString());
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,7 @@
 | 
			
		||||
#include <hyperion/MessageForwarder.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
MessageForwarder::MessageForwarder() :
 | 
			
		||||
_running(false)
 | 
			
		||||
MessageForwarder::MessageForwarder()
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -13,7 +12,19 @@ MessageForwarder::~MessageForwarder()
 | 
			
		||||
 | 
			
		||||
void MessageForwarder::addJsonSlave(std::string slave)
 | 
			
		||||
{
 | 
			
		||||
	std::cout << slave << std::endl;
 | 
			
		||||
	QStringList parts = QString(slave.c_str()).split(":");
 | 
			
		||||
	if (parts.size() != 2)
 | 
			
		||||
		throw std::runtime_error(QString("Wrong address: unable to parse address (%1)").arg(slave.c_str()).toStdString());
 | 
			
		||||
 | 
			
		||||
	bool ok;
 | 
			
		||||
	quint16 port = parts[1].toUShort(&ok);
 | 
			
		||||
	if (!ok)
 | 
			
		||||
		throw std::runtime_error(QString("Wrong address: Unable to parse the port number (%1)").arg(parts[1]).toStdString());
 | 
			
		||||
 | 
			
		||||
	JsonSlaveAddress c;
 | 
			
		||||
	c.addr = QHostAddress(parts[0]);
 | 
			
		||||
	c.port = port;
 | 
			
		||||
	_jsonSlaves << c;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MessageForwarder::addProtoSlave(std::string slave)
 | 
			
		||||
@@ -23,12 +34,14 @@ void MessageForwarder::addProtoSlave(std::string slave)
 | 
			
		||||
 | 
			
		||||
void MessageForwarder::sendMessage()
 | 
			
		||||
{
 | 
			
		||||
	if ( ! _running )
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QStringList MessageForwarder::getProtoSlaves()
 | 
			
		||||
{
 | 
			
		||||
	return _protoSlaves;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QList<MessageForwarder::JsonSlaveAddress> MessageForwarder::getJsonSlaves()
 | 
			
		||||
{
 | 
			
		||||
	return _jsonSlaves;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,7 @@
 | 
			
		||||
// hyperion util includes
 | 
			
		||||
#include <hyperion/ImageProcessorFactory.h>
 | 
			
		||||
#include <hyperion/ImageProcessor.h>
 | 
			
		||||
#include <hyperion/MessageForwarder.h>
 | 
			
		||||
#include <hyperion/ColorTransform.h>
 | 
			
		||||
#include <utils/ColorRgb.h>
 | 
			
		||||
 | 
			
		||||
@@ -254,11 +255,16 @@ void JsonClientConnection::handleMessage(const std::string &messageString)
 | 
			
		||||
void JsonClientConnection::forwardJsonMessage(const Json::Value & message)
 | 
			
		||||
{
 | 
			
		||||
	QTcpSocket client;
 | 
			
		||||
	client.connectToHost(QHostAddress("127.0.0.1"), 19444);
 | 
			
		||||
	if ( client.waitForConnected(500) )
 | 
			
		||||
	QList<MessageForwarder::JsonSlaveAddress> list = _hyperion->getForwarder()->getJsonSlaves();
 | 
			
		||||
 | 
			
		||||
	for ( int i=0; i<list.size(); i++ )
 | 
			
		||||
	{
 | 
			
		||||
		sendMessage(message,&client);
 | 
			
		||||
		client.close();
 | 
			
		||||
		client.connectToHost(list.at(i).addr, list.at(i).port);
 | 
			
		||||
		if ( client.waitForConnected(500) )
 | 
			
		||||
		{
 | 
			
		||||
			sendMessage(message,&client);
 | 
			
		||||
			client.close();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,6 @@ ProtoServer::ProtoServer(Hyperion *hyperion, uint16_t port) :
 | 
			
		||||
	QStringList slaves = forwarder->getProtoSlaves();
 | 
			
		||||
 | 
			
		||||
	for (int i = 0; i < slaves.size(); ++i) {
 | 
			
		||||
		std::cout << "Proto forward to " << slaves.at(i).toLocal8Bit().constData() << std::endl;
 | 
			
		||||
		ProtoConnection* p = new ProtoConnection(slaves.at(i).toLocal8Bit().constData());
 | 
			
		||||
		p->setSkipReply(true);
 | 
			
		||||
		_proxy_connections << p;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user