Commit 69a42967 authored by Pavel Vainerman's avatar Pavel Vainerman

[http-resolver]: added to UInterface

parent 3d2b8079
......@@ -27,6 +27,7 @@
<DocDir name="./"/>
<LockDir name="/tmp/"/>
<HttpResolver name="HttpResolver"/>
<LocalIOR name="1"/>
<Services>
<LocalTimeService AskLifeTimeSEC="10" MaxCountTimers="100" name="TimeService"/>
<LocalInfoServer dbrepeat="1" name="InfoServer">
......
......@@ -92,7 +92,6 @@ HttpResolver::HttpResolver( const string& name, int argc, const char* const* arg
httpHost = uniset::getArgParam("--" + prefix + "httpserver-host", argc, argv, "localhost");
httpPort = uniset::getArgInt("--" + prefix + "httpserver-port", argc, argv, "8008");
httpCORS_allow = uniset::getArgParam("--" + prefix + "httpserver-cors-allow", argc, argv, httpCORS_allow);
httpReplyAddr = uniset::getArgParam("--" + prefix + "httpserver-reply-addr", argc, argv, "");
rinfo << myname << "(init): http server parameters " << httpHost << ":" << httpPort << endl;
Poco::Net::SocketAddress sa(httpHost, httpPort);
......@@ -146,7 +145,6 @@ void HttpResolver::help_print()
cout << "--prefix-httpserver-max-queued num - Размер очереди запросов к http серверу. По умолчанию: 100" << endl;
cout << "--prefix-httpserver-max-threads num - Разрешённое количество потоков для http-сервера. По умолчанию: 3" << endl;
cout << "--prefix-httpserver-cors-allow addr - (CORS): Access-Control-Allow-Origin. Default: *" << endl;
cout << "--prefix-httpserver-reply-addr host[:port] - Адрес отдаваемый клиенту для подключения. По умолчанию адрес узла где запущен logdb" << endl;
}
// -----------------------------------------------------------------------------
void HttpResolver::run()
......
......@@ -121,6 +121,7 @@ namespace uniset
const std::string getConfFileName() const noexcept;
std::string getImagesDir() const noexcept;
std::string getNodeIp( uniset::ObjectId node );
timeout_t getHeartBeatTime() const noexcept;
timeout_t getNCReadyTimeout() const noexcept;
......@@ -136,6 +137,7 @@ namespace uniset
bool isLocalIOR() const noexcept;
bool isTransientIOR() const noexcept;
size_t getHttpResovlerPort() const noexcept;
/*! получить значение указанного параметра, или значение по умолчанию */
std::string getArgParam(const std::string& name, const std::string& defval = "") const noexcept;
......@@ -202,6 +204,8 @@ namespace uniset
timeout_t repeatTimeout = { 50 }; /*!< пауза между попытками [мс] */
size_t httpResolverPort = { 8008 };
uniset::ListOfNode lnodes;
// repository
......
#ifndef DISABLE_REST_API
/*
* Copyright (c) 2020 Pavel Vainerman.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 2.1.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// -------------------------------------------------------------------------
#ifndef UHttpClient_H_
#define UHttpClient_H_
// -------------------------------------------------------------------------
#include <string>
#include <Poco/Net/HTTPClientSession.h>
#include "DebugStream.h"
// -------------------------------------------------------------------------
namespace uniset
{
namespace UHttp
{
class UHttpClient
{
public:
UHttpClient();
virtual ~UHttpClient();
// http://site.com/query?params
// \return ""
std::string get( const std::string& host, int port, const std::string& request );
protected:
Poco::Net::HTTPClientSession session;
private:
};
}
}
// -------------------------------------------------------------------------
#endif // UHttpClient_H_
// -------------------------------------------------------------------------
#endif
......@@ -36,6 +36,9 @@
#include "IOController_i.hh"
#include "MessageType.h"
#include "Configuration.h"
#ifndef DISABLE_REST_API
#include "UHttpClient.h"
#endif
// -----------------------------------------------------------------------------------------
namespace uniset
{
......@@ -301,6 +304,7 @@ namespace uniset
protected:
std::string set_err(const std::string& pre, const uniset::ObjectId id, const uniset::ObjectId node) const;
std::string httpResolve( const uniset::ObjectId id, const uniset::ObjectId node ) const;
private:
void init();
......
......@@ -2,7 +2,7 @@
# This file is part of the UniSet library #
############################################################################
noinst_LTLIBRARIES = libHttp.la
libHttp_la_SOURCES = UHttpRequestHandler.cc UHttpServer.cc
libHttp_la_SOURCES = UHttpRequestHandler.cc UHttpServer.cc UHttpClient.cc
libHttp_la_CXXFLAGS = $(POCO_CFLAGS)
libHttp_la_LIBADD = $(POCO_LIBS)
......
#ifndef DISABLE_REST_API
/*
* Copyright (c) 2020 Pavel Vainerman.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 2.1.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// -------------------------------------------------------------------------
#include <sstream>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/StreamCopier.h>
#include "UHttpClient.h"
#include "Exceptions.h"
// -------------------------------------------------------------------------
using namespace Poco::Net;
// -------------------------------------------------------------------------
namespace uniset
{
using namespace UHttp;
// -------------------------------------------------------------------------
UHttpClient::UHttpClient()
{
}
// -------------------------------------------------------------------------
UHttpClient::~UHttpClient()
{
}
// -------------------------------------------------------------------------
std::string UHttpClient::get( const std::string& host, int port, const std::string& query )
{
session.setHost(host);
session.setPort(port);
HTTPRequest request(HTTPRequest::HTTP_GET, query, HTTPMessage::HTTP_1_1);
session.sendRequest(request);
HTTPResponse response;
std::istream& rs = session.receiveResponse(response);
if( response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK )
return "";
std::stringstream ret;
Poco::StreamCopier::copyStream(rs, ret);
return ret.str();
}
// -------------------------------------------------------------------------
} // end of namespace uniset
// -------------------------------------------------------------------------
#endif // #ifndef DISABLE_REST_API
......@@ -305,6 +305,7 @@ namespace uniset
// transientIOR
int tior = getArgInt("--transientIOR");
if( tior )
transientIOR = tior;
......@@ -602,6 +603,10 @@ namespace uniset
else if( name == "LocalIOR" )
{
localIOR = it.getIntProp("name");
httpResolverPort = it.getIntProp("httpResolverPort");
if( httpResolverPort <= 0 )
httpResolverPort = 8008;
}
else if( name == "TransientIOR" )
{
......@@ -1113,7 +1118,7 @@ namespace uniset
}
// -------------------------------------------------------------------------
std::pair<string,xmlNode*> Configuration::getRepSectionName( const string& sec )
std::pair<string, xmlNode*> Configuration::getRepSectionName( const string& sec )
{
xmlNode* secnode = unixml->findNode(unixml->getFirstNode(), sec);
......@@ -1323,6 +1328,20 @@ namespace uniset
return transientIOR;
}
size_t Configuration::getHttpResovlerPort() const noexcept
{
return httpResolverPort;
}
std::string Configuration::getNodeIp( uniset::ObjectId node )
{
UniXML::iterator nIt = getXMLObjectNode(node);
if( !nIt )
return "";
return nIt.getProp("ip");
}
// -------------------------------------------------------------------------
xmlNode* Configuration::getXMLSensorsSection() noexcept
{
......
......@@ -874,9 +874,9 @@ namespace uniset
}
// ------------------------------------------------------------------------------------------------------------
uniset::ObjectPtr UInterface::resolve( const uniset::ObjectId rid , const uniset::ObjectId node ) const
uniset::ObjectPtr UInterface::resolve( const uniset::ObjectId rid, const uniset::ObjectId node ) const
{
if ( rid == uniset::DefaultObjectId )
if( rid == uniset::DefaultObjectId )
throw uniset::ResolveNameError("UI(resolve): ID=uniset::DefaultObjectId");
if( node == uniset::DefaultObjectId )
......@@ -896,7 +896,12 @@ namespace uniset
if( CORBA::is_nil(orb) )
orb = uconf->getORB();
string sior(uconf->iorfile->getIOR(rid));
string sior;
if( node == uconf->getLocalNode() )
sior = uconf->iorfile->getIOR(rid);
else
sior = httpResolve(rid, node);
if( !sior.empty() )
{
......@@ -904,14 +909,12 @@ namespace uniset
rcache.cache(rid, node, nso); // заносим в кэш
return nso._retn();
}
else
{
// если NameService недоступен то,
// сразу выдаём ошибку
uwarn << "not found IOR-file for " << uconf->oind->getNameById(rid) << endl;
uwarn << "not found IOR-file for " << uconf->oind->getNameById(rid)
<< " node=" << uconf->oind->getNameById(node)
<< endl;
throw uniset::ResolveNameError();
}
}
if( node != uconf->getLocalNode() )
{
......@@ -1019,6 +1022,36 @@ namespace uniset
}
// -------------------------------------------------------------------------------------------
std::string UInterface::httpResolve( const uniset::ObjectId id, const uniset::ObjectId node ) const
{
#ifndef DISABLE_REST_API
size_t port = uconf->getHttpResovlerPort();
if( port == 0 )
return "";
const std::string host = uconf->getNodeIp(node);
if( host.empty() )
return "";
string ret;
const string query = "api/v01/resolve/text?" + std::to_string(id);
for( size_t i = 0; i < uconf->getRepeatCount(); i++ )
{
ret = resolver.get(host, port, query);
if( !ret.empty() )
return ret;
msleep(uconf->getRepeatTimeout());
}
#endif
return "";
}
// -------------------------------------------------------------------------------------------
void UInterface::send( const uniset::ObjectId name, const uniset::TransportMessage& msg, const uniset::ObjectId node )
{
if ( name == uniset::DefaultObjectId )
......
......@@ -24,6 +24,11 @@
./extensions/DBServer-SQLite/SQLiteInterface.cc
./extensions/DBServer-SQLite/SQLiteInterface.h
./extensions/DBServer-SQLite/test.cc
./extensions/HttpResolver/HttpResolver.cc
./extensions/HttpResolver/HttpResolver.h
./extensions/HttpResolver/HttpResolverSugar.h
./extensions/HttpResolver/main.cc
./extensions/HttpResolver/Makefile.am
./extensions/include/Calibration.h
./extensions/include/ComediInterface.h
./extensions/include/DigitalFilter.h
......@@ -185,43 +190,6 @@
./extensions/SMViewer/Makefile.am
./extensions/SMViewer/SMViewer.cc
./extensions/SMViewer/SMViewer.h
./extensions/tests1/Makefile.am
./extensions/tests1/MBSlaveTest/Makefile.am
./extensions/tests1/MBSlaveTest/mbslave-test.cc
./extensions/tests1/MBSlaveTest/TestProc.cc
./extensions/tests1/MBSlaveTest/TestProc.h
./extensions/tests1/MBSlaveTest/testproc.src.xml
./extensions/tests1/MQPerfTest/Makefile.am
./extensions/tests1/MQPerfTest/mq-test.cc
./extensions/tests1/MQPerfTest/TestProc.cc
./extensions/tests1/MQPerfTest/TestProc.h
./extensions/tests1/MQPerfTest/testproc.src.xml
./extensions/tests1/r/t.cc
./extensions/tests1/SMemoryTest/LostPassiveTestProc.cc
./extensions/tests1/SMemoryTest/LostPassiveTestProc.h
./extensions/tests1/SMemoryTest/LostTestProc.cc
./extensions/tests1/SMemoryTest/LostTestProc.h
./extensions/tests1/SMemoryTest/losttestproc.src.xml
./extensions/tests1/SMemoryTest/Makefile.am
./extensions/tests1/SMemoryTest/smemory-test.cc
./extensions/tests1/SMemoryTest/sm-lostmessage-test.cc
./extensions/tests1/SMemoryTest/TestProc.cc
./extensions/tests1/SMemoryTest/TestProc.h
./extensions/tests1/SMemoryTest/testproc.src.xml
./extensions/tests1/sm_perf_test2.cc
./extensions/tests1/sm_perf_test.cc
./extensions/tests1/sz.cc
./extensions/tests1/test_calibration.cc
./extensions/tests1/test_digitalfilter.cc
./extensions/tests1/test_iobase.cc
./extensions/tests1/test_iobase_with_sm.cc
./extensions/tests1/test_restapi_uniset.cc
./extensions/tests1/tests.cc
./extensions/tests1/tests_with_conf.cc
./extensions/tests1/tests_with_sm.cc
./extensions/tests1/tests_with_sm.h
./extensions/tests1/test_ui.cc
./extensions/tests1/test_vtypes.cc
./extensions/tests/Makefile.am
./extensions/tests/MBSlaveTest/Makefile.am
./extensions/tests/MBSlaveTest/mbslave-test.cc
......@@ -264,7 +232,6 @@
./extensions/UNetUDP/tests/Makefile.am
./extensions/UNetUDP/tests/tests_individual_process.cc
./extensions/UNetUDP/tests/tests_with_sm.cc
./extensions/UNetUDP/tests/test_unetudp1.cc
./extensions/UNetUDP/tests/test_unetudp.cc
./extensions/UNetUDP/tests/u.cc
./extensions/UNetUDP/tests/urecv_perf_test.cc
......@@ -358,6 +325,7 @@
./include/UA.h
./include/UDPCore.h
./include/UHelpers.h
./include/UHttpClient.h
./include/UHttpRequestHandler.h
./include/UHttpServer.h
./include/UInterface.h
......@@ -368,6 +336,7 @@
./include/unisetstd.h
./include/UniSetTypes.h
./include/UniXML.h
./include/UResolver.h
./include/USocket.h
./include/UTCPCore.h
./include/UTCPSocket.h
......@@ -380,6 +349,7 @@
./src/Communications/ComPort485F.cc
./src/Communications/ComPort.cc
./src/Communications/Http/Makefile.am
./src/Communications/Http/UHttpClient.cc
./src/Communications/Http/UHttpRequestHandler.cc
./src/Communications/Http/UHttpServer.cc
./src/Communications/Makefile.am
......@@ -416,6 +386,8 @@
./src/Core/UniSetObject_iSK.cc
./src/Core/UniSetTypes.cc
./src/Core/UniSetTypes_iSK.cc
./src/Core/UResolver.cc
./src/Core/UResolver_iSK.cc
./src/Log/Debug.cc
./src/Log/DebugExtBuf.h
./src/Log/DebugStream.cc
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment