File size: 6,509 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2014 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <limits>
#include <sstream>
#include <Base/Console.h>
#include <Base/Interpreter.h>
#include <Base/PyObjectBase.h>
#include "Server.h"
// See http://docs.python.org/2/library/socketserver.html
/*
import socket
import threading
ip = "127.0.0.1"
port = 54880
def client(ip, port, message):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
try:
sock.sendall(message)
response = sock.recv(1024)
print ("Received: {}".format(response))
finally:
sock.close()
client(ip, port, b"print ('Hello World')")
client(ip, port, b"import FreeCAD\nFreeCAD.newDocument()")
*/
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."); // register with Python
}
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);
}
} // namespace Web
/* Python entry */
PyMOD_INIT_FUNC(Web)
{
// ADD YOUR CODE HERE
//
//
PyObject* mod = Web::initModule();
Base::Console().log("Loading Web module... done\n");
PyMOD_Return(mod);
}
|