Commit 22b49903 authored by Pavel Vainerman's avatar Pavel Vainerman

(UHttpServer): initial commit

parent 9fdacb81
......@@ -69,6 +69,13 @@ if test -n "$poco_old"; then
POCO_CFLAGS="${POCO_CFLAGS} -DPOCO_OLD_VERSION"
fi
AC_MSG_CHECKING([check json])
if test -a "/usr/include/json.hpp"; then
AC_MSG_RESULT([ok])
else
AC_MSG_ERROR([json.hpp not found])
fi
#check sqlite support
AC_MSG_CHECKING([sqlite support])
buildsqlite=true
......@@ -95,18 +102,6 @@ test "x$IDL" = "x" && AC_MSG_ERROR([*** omniidl not found.])
#AC_SUBST(UNISET_IDLFLAGS)
#check mysql support
AC_MSG_CHECKING([mysql support])
buildmysql=true
AC_ARG_ENABLE(mysql, AC_HELP_STRING([--disable-mysql], [disable MySQL support]),
[ if test $enableval = yes; then buildmysql=true; else buildmysql=false; fi],[ buildmysql=true; ])
if test ${buildmysql} = true; then
AC_MSG_RESULT([enabled])
AC_CHECK_HEADERS([mysql/mysql.h])
AC_SEARCH_LIBS([mysql_init],mysqlclient,[],[],exit)
else
AC_MSG_RESULT([disabled])
fi
AM_CONDITIONAL(DISABLE_MYSQL, test ${buildmysql} = false)
......@@ -378,6 +373,7 @@ AC_CONFIG_FILES([Makefile
src/Communications/Makefile
src/Communications/Modbus/Makefile
src/Communications/TCP/Makefile
src/Communications/Http/Makefile
src/Interfaces/Makefile
src/ObjectRepository/Makefile
src/Processes/Makefile
......
......@@ -16,6 +16,7 @@ libUniSet2_la_LIBADD = -lm \
$(top_builddir)/src/Communications/libCommunications.la \
$(top_builddir)/src/Communications/Modbus/libModbus.la \
$(top_builddir)/src/Communications/TCP/libTCP.la \
$(top_builddir)/src/Communications/Http/libHttp.la \
$(top_builddir)/src/Interfaces/libInterfaces.la \
$(top_builddir)/src/ObjectRepository/libObjectsRepository.la \
$(top_builddir)/src/Processes/libProcesses.la \
......
############################################################################
# This file is part of the UniSet library #
############################################################################
noinst_LTLIBRARIES = libHttp.la
libHttp_la_SOURCES = UHttpRequestHandler.cc UHttpServer.cc
libHttp_la_CXXFLAGS = $(POCO_CFLAGS)
libHttp_la_LIBADD = $(POCO_LIBS)
include $(top_builddir)/include.mk
/*
* Copyright (c) 2015 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 <ostream>
#include <Poco/URI.h>
#include "UHttpRequestHandler.h"
// -------------------------------------------------------------------------
using namespace Poco::Net;
using namespace UniSetTypes;
// -------------------------------------------------------------------------
UHttpRequestHandler::UHttpRequestHandler( std::shared_ptr<IHttpRequestRegistry> _supplier ):
supplier(_supplier)
{
}
// -------------------------------------------------------------------------
void UHttpRequestHandler::handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp )
{
if( !supplier )
{
resp.setStatus(HTTPResponse::HTTP_NOT_FOUND);
resp.setContentType("text/json");
std::ostream& out = resp.send();
out.flush();
return;
}
Poco::URI uri(req.getURI());
// в текущей версии подразумевается, что запрос идёт в формате
// http://xxx.host:port/ObjectName
// поэтому сразу передаём uri.getQuery() в качестве имени объекта
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("text/json");
std::ostream& out = resp.send();
auto json = supplier->getData(uri.getQuery());
out << json.dump();
out.flush();
}
// -------------------------------------------------------------------------
UHttpRequestHandlerFactory::UHttpRequestHandlerFactory( std::shared_ptr<IHttpRequestRegistry>& _supplier ):
supplier(_supplier)
{
}
// -------------------------------------------------------------------------
HTTPRequestHandler* UHttpRequestHandlerFactory::createRequestHandler( const HTTPServerRequest& req )
{
return new UHttpRequestHandler(supplier);
}
// -------------------------------------------------------------------------
/*
* Copyright (c) 2015 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 <Poco/URI.h>
#include "UHttpServer.h"
// -------------------------------------------------------------------------
using namespace Poco::Net;
using namespace UniSetTypes;
// -------------------------------------------------------------------------
UHttpServer::UHttpServer(std::shared_ptr<IHttpRequestRegistry>& supplier, const std::string _host, int _port ):
sa(_host,_port)
{
/*! \FIXME: доделать конфигурирование параметров */
HTTPServerParams* httpParams = new HTTPServerParams;
httpParams->setMaxQueued(100);
httpParams->setMaxThreads(1);
reqFactory = std::make_shared<UHttpRequestHandlerFactory>(supplier);
http = std::make_shared<Poco::Net::HTTPServer>(reqFactory.get(), ServerSocket(sa), httpParams );
}
// -------------------------------------------------------------------------
UHttpServer::~UHttpServer()
{
}
// -------------------------------------------------------------------------
UHttpServer::UHttpServer()
{
}
// -------------------------------------------------------------------------
std::shared_ptr<DebugStream> UHttpServer::log()
{
return mylog;
}
// -------------------------------------------------------------------------
############################################################################
# This file is part of the UniSet library #
############################################################################
SUBDIRS=TCP Modbus
SUBDIRS=TCP Modbus Http
noinst_LTLIBRARIES = libCommunications.la
libCommunications_la_SOURCES = ComPort.cc ComPort485F.cc
......
......@@ -29,7 +29,8 @@ test_logserver.cc \
test_tcpcheck.cc \
test_utcpsocket.cc \
test_iocontroller_types.cc \
test_debugstream.cc
test_debugstream.cc \
test_uhttp.cc
tests_with_conf_LDADD = $(top_builddir)/lib/libUniSet2.la
tests_with_conf_CPPFLAGS = -I$(top_builddir)/include
......
......@@ -334,6 +334,8 @@ include/USocket.h
include/UDPCore.h
include/WDTInterface.h
include/UHelpers.h
include/UHttpRequestHandler.h
include/UHttpServer.h
lib/Makefile.am
python/examples/test.xml
python/lib/pyUniSet/Makefile.am
......@@ -376,6 +378,7 @@ src/Communications/TCP/UTCPCore.cc
src/Communications/TCP/UTCPStream.cc
src/Communications/TCP/UTCPSocket.cc
src/Communications/TCP/USocket.cc
src/Communications/Http/UHttpRequestHandler.cc
src/Communications/ComPort.cc
src/Communications/ComPort485F.cc
src/Communications/Makefile.am
......@@ -468,6 +471,7 @@ tests/test_uobject.cc
tests/test_utcpsocket.cc
tests/test_iocontroller_types.cc
tests/test_debugstream.cc
tests/test_uhttp.cc
tests/TestUObject.h
tests/tests-junit.xml
tests/tests.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