| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include <limits> |
| | #include <sstream> |
| |
|
| | #include <Base/Console.h> |
| | #include <Base/Interpreter.h> |
| | #include <Base/PyObjectBase.h> |
| |
|
| | #include "Server.h" |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | namespace Web |
| | { |
| | class Module: public Py::ExtensionModule<Module> |
| | { |
| | public: |
| | Module() |
| | : Py::ExtensionModule<Module>("Web") |
| | { |
| | add_varargs_method( |
| | "startServer", |
| | &Module::startServer, |
| | "startServer(address=127.0.0.1,port=0) -- Start a server." |
| | ); |
| | add_varargs_method( |
| | "waitForConnection", |
| | &Module::waitForConnection, |
| | "waitForConnection(address=127.0.0.1,port=0,timeout=0)\n" |
| | "Start a server, wait for connection and close server.\n" |
| | "Its use is disadvised in a the GUI version, since it will\n" |
| | "stop responding until the function returns." |
| | ); |
| | add_varargs_method( |
| | "registerServerFirewall", |
| | &Module::registerServerFirewall, |
| | "registerServerFirewall(callable(string)) -- Register a firewall." |
| | ); |
| | initialize("This module is the Web module."); |
| | } |
| |
|
| | private: |
| | Py::Object startServer(const Py::Tuple& args) |
| | { |
| | const char* addr = "127.0.0.1"; |
| | int port = 0; |
| | if (!PyArg_ParseTuple(args.ptr(), "|si", &addr, &port)) { |
| | throw Py::Exception(); |
| | } |
| | if (port > std::numeric_limits<unsigned short>::max()) { |
| | throw Py::OverflowError("port number is greater than maximum"); |
| | } |
| | else if (port < 0) { |
| | throw Py::OverflowError("port number is lower than 0"); |
| | } |
| |
|
| | AppServer* server = new AppServer(); |
| | if (server->listen(QHostAddress(QString::fromLatin1(addr)), port)) { |
| | QString a = server->serverAddress().toString(); |
| | quint16 p = server->serverPort(); |
| | Py::Tuple t(2); |
| | t.setItem(0, Py::String((const char*)a.toLatin1())); |
| | t.setItem(1, Py::Long(p)); |
| | return t; |
| | } |
| | else { |
| | server->deleteLater(); |
| | std::stringstream out; |
| | out << "Server failed to listen at address " << addr << " and port " << port; |
| | throw Py::RuntimeError(out.str()); |
| | } |
| | } |
| |
|
| | Py::Object waitForConnection(const Py::Tuple& args) |
| | { |
| | const char* addr = "127.0.0.1"; |
| | int port = 0; |
| | int timeout = 0; |
| | if (!PyArg_ParseTuple(args.ptr(), "|sii", &addr, &port, &timeout)) { |
| | throw Py::Exception(); |
| | } |
| | if (port > std::numeric_limits<unsigned short>::max()) { |
| | throw Py::OverflowError("port number is greater than maximum"); |
| | } |
| | else if (port < 0) { |
| | throw Py::OverflowError("port number is lower than 0"); |
| | } |
| |
|
| | try { |
| | AppServer server(true); |
| | if (server.listen(QHostAddress(QString::fromLatin1(addr)), port)) { |
| | bool ok = server.waitForNewConnection(timeout); |
| | QTcpSocket* socket = server.nextPendingConnection(); |
| | if (socket) { |
| | socket->waitForReadyRead(); |
| | } |
| |
|
| | server.close(); |
| | return Py::Boolean(ok); |
| | } |
| | else { |
| | std::stringstream out; |
| | out << "Server failed to listen at address " << addr << " and port " << port; |
| | throw Py::RuntimeError(out.str()); |
| | } |
| | } |
| | catch (const Base::SystemExitException& e) { |
| | throw Py::RuntimeError(e.what()); |
| | } |
| | } |
| |
|
| | Py::Object registerServerFirewall(const Py::Tuple& args) |
| | { |
| | PyObject* obj; |
| | if (!PyArg_ParseTuple(args.ptr(), "O", &obj)) { |
| | throw Py::Exception(); |
| | } |
| |
|
| | Py::Object pyobj(obj); |
| | if (pyobj.isNone()) { |
| | Web::Firewall::setInstance(nullptr); |
| | } |
| | else { |
| | Web::Firewall::setInstance(new Web::FirewallPython(pyobj)); |
| | } |
| |
|
| | return Py::None(); |
| | } |
| | }; |
| |
|
| | PyObject* initModule() |
| | { |
| | return Base::Interpreter().addModule(new Module); |
| | } |
| |
|
| | } |
| |
|
| |
|
| | |
| | PyMOD_INIT_FUNC(Web) |
| | { |
| |
|
| | |
| | |
| | |
| | PyObject* mod = Web::initModule(); |
| | Base::Console().log("Loading Web module... done\n"); |
| | PyMOD_Return(mod); |
| | } |
| |
|