Commit afadc703118d27e7cf8831cb610946e9f99c3132
1 parent
1b385524
added web hello world
Showing
5 changed files
with
85 additions
and
0 deletions
app/br/br.cpp
| @@ -162,6 +162,9 @@ public: | @@ -162,6 +162,9 @@ public: | ||
| 162 | } else if (!strcmp(fun, "version")) { | 162 | } else if (!strcmp(fun, "version")) { |
| 163 | check(parc == 0, "No parameters expected for 'version'."); | 163 | check(parc == 0, "No parameters expected for 'version'."); |
| 164 | printf("%s\n", br_version()); | 164 | printf("%s\n", br_version()); |
| 165 | + } else if (!strcmp(fun, "web")) { | ||
| 166 | + check(parc == 0, "No parameters expected for 'shell'."); | ||
| 167 | + br_web(); | ||
| 165 | } else if (!strcmp(fun, "shell")) { | 168 | } else if (!strcmp(fun, "shell")) { |
| 166 | check(parc == 0, "No parameters expected for 'shell'."); | 169 | check(parc == 0, "No parameters expected for 'shell'."); |
| 167 | shell = true; | 170 | shell = true; |
openbr/core/web.cpp
0 → 100644
| 1 | +#include <openbr/openbr_plugin.h> | ||
| 2 | + | ||
| 3 | +#include "web.h" | ||
| 4 | + | ||
| 5 | +#ifndef BR_EMBEDDED | ||
| 6 | + | ||
| 7 | +#include <QCoreApplication> | ||
| 8 | +#include <QTcpServer> | ||
| 9 | +#include <QTcpSocket> | ||
| 10 | + | ||
| 11 | +class WebServices : public QTcpServer | ||
| 12 | +{ | ||
| 13 | + Q_OBJECT | ||
| 14 | + | ||
| 15 | +public: | ||
| 16 | + WebServices() | ||
| 17 | + { | ||
| 18 | + connect(this, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + void listen() | ||
| 22 | + { | ||
| 23 | + QTcpServer::listen(QHostAddress::Any, 8080); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | +private slots: | ||
| 27 | + void handleNewConnection() | ||
| 28 | + { | ||
| 29 | + while (hasPendingConnections()) { | ||
| 30 | + QTcpSocket *socket = QTcpServer::nextPendingConnection(); | ||
| 31 | + connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); | ||
| 32 | + socket->write("HTTP/1.1 200 OK\r\n" | ||
| 33 | + "Content-Type: text/html; charset=UTF-8\r\n\r\n" | ||
| 34 | + "Hello World!\r\n"); | ||
| 35 | + socket->disconnectFromHost(); | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | +}; | ||
| 39 | + | ||
| 40 | +void br::web() | ||
| 41 | +{ | ||
| 42 | + static WebServices webServices; | ||
| 43 | + if (webServices.isListening()) | ||
| 44 | + return; | ||
| 45 | + | ||
| 46 | + webServices.listen(); | ||
| 47 | + qDebug("Listening on %s:%d", qPrintable(webServices.serverAddress().toString()), webServices.serverPort()); | ||
| 48 | + while (true) | ||
| 49 | + QCoreApplication::processEvents(); | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +#include "web.moc" | ||
| 53 | + | ||
| 54 | +#else // BR_EMBEDDED | ||
| 55 | + | ||
| 56 | +void br::web() | ||
| 57 | +{ | ||
| 58 | + qFatal("Web services not supported in embedded builds."); | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +#endif // BR_EMBEDDED |
openbr/core/web.h
0 → 100644
openbr/openbr.cpp
| @@ -22,6 +22,7 @@ | @@ -22,6 +22,7 @@ | ||
| 22 | #include "core/fuse.h" | 22 | #include "core/fuse.h" |
| 23 | #include "core/plot.h" | 23 | #include "core/plot.h" |
| 24 | #include "core/qtutils.h" | 24 | #include "core/qtutils.h" |
| 25 | +#include "core/web.h" | ||
| 25 | 26 | ||
| 26 | using namespace br; | 27 | using namespace br; |
| 27 | 28 | ||
| @@ -241,3 +242,8 @@ const char *br_version() | @@ -241,3 +242,8 @@ const char *br_version() | ||
| 241 | static QByteArray version = Context::version().toLocal8Bit(); | 242 | static QByteArray version = Context::version().toLocal8Bit(); |
| 242 | return version.data(); | 243 | return version.data(); |
| 243 | } | 244 | } |
| 245 | + | ||
| 246 | +void br_web() | ||
| 247 | +{ | ||
| 248 | + br::web(); | ||
| 249 | +} |
openbr/openbr.h
| @@ -364,6 +364,11 @@ BR_EXPORT void br_train_n(int num_inputs, const char *inputs[], const char *mode | @@ -364,6 +364,11 @@ BR_EXPORT void br_train_n(int num_inputs, const char *inputs[], const char *mode | ||
| 364 | */ | 364 | */ |
| 365 | BR_EXPORT const char *br_version(); | 365 | BR_EXPORT const char *br_version(); |
| 366 | 366 | ||
| 367 | +/*! | ||
| 368 | + * \brief Launches br web services | ||
| 369 | + */ | ||
| 370 | +BR_EXPORT void br_web(); | ||
| 371 | + | ||
| 367 | /*! @}*/ | 372 | /*! @}*/ |
| 368 | 373 | ||
| 369 | #ifdef __cplusplus | 374 | #ifdef __cplusplus |