diff --git a/app/br/br.cpp b/app/br/br.cpp index 0f8ac51..e1894a6 100644 --- a/app/br/br.cpp +++ b/app/br/br.cpp @@ -162,6 +162,9 @@ public: } else if (!strcmp(fun, "version")) { check(parc == 0, "No parameters expected for 'version'."); printf("%s\n", br_version()); + } else if (!strcmp(fun, "web")) { + check(parc == 0, "No parameters expected for 'shell'."); + br_web(); } else if (!strcmp(fun, "shell")) { check(parc == 0, "No parameters expected for 'shell'."); shell = true; diff --git a/openbr/core/web.cpp b/openbr/core/web.cpp new file mode 100644 index 0000000..b7f1b8b --- /dev/null +++ b/openbr/core/web.cpp @@ -0,0 +1,61 @@ +#include + +#include "web.h" + +#ifndef BR_EMBEDDED + +#include +#include +#include + +class WebServices : public QTcpServer +{ + Q_OBJECT + +public: + WebServices() + { + connect(this, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); + } + + void listen() + { + QTcpServer::listen(QHostAddress::Any, 8080); + } + +private slots: + void handleNewConnection() + { + while (hasPendingConnections()) { + QTcpSocket *socket = QTcpServer::nextPendingConnection(); + connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); + socket->write("HTTP/1.1 200 OK\r\n" + "Content-Type: text/html; charset=UTF-8\r\n\r\n" + "Hello World!\r\n"); + socket->disconnectFromHost(); + } + } +}; + +void br::web() +{ + static WebServices webServices; + if (webServices.isListening()) + return; + + webServices.listen(); + qDebug("Listening on %s:%d", qPrintable(webServices.serverAddress().toString()), webServices.serverPort()); + while (true) + QCoreApplication::processEvents(); +} + +#include "web.moc" + +#else // BR_EMBEDDED + +void br::web() +{ + qFatal("Web services not supported in embedded builds."); +} + +#endif // BR_EMBEDDED diff --git a/openbr/core/web.h b/openbr/core/web.h new file mode 100644 index 0000000..7bfc33c --- /dev/null +++ b/openbr/core/web.h @@ -0,0 +1,10 @@ +#ifndef __WEB_H +#define __WEB_H + +namespace br +{ + // Enter web services + void web(); +} // namespace br + +#endif // __WEB_H diff --git a/openbr/openbr.cpp b/openbr/openbr.cpp index b0318bf..909bcd5 100644 --- a/openbr/openbr.cpp +++ b/openbr/openbr.cpp @@ -22,6 +22,7 @@ #include "core/fuse.h" #include "core/plot.h" #include "core/qtutils.h" +#include "core/web.h" using namespace br; @@ -241,3 +242,8 @@ const char *br_version() static QByteArray version = Context::version().toLocal8Bit(); return version.data(); } + +void br_web() +{ + br::web(); +} diff --git a/openbr/openbr.h b/openbr/openbr.h index 627bc65..ce2ffcc 100644 --- a/openbr/openbr.h +++ b/openbr/openbr.h @@ -364,6 +364,11 @@ BR_EXPORT void br_train_n(int num_inputs, const char *inputs[], const char *mode */ BR_EXPORT const char *br_version(); +/*! + * \brief Launches br web services + */ +BR_EXPORT void br_web(); + /*! @}*/ #ifdef __cplusplus