Skip to content

Commit

Permalink
Web: support to query values via TCP/IP communication
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Sep 25, 2021
1 parent bd46e97 commit 5387e12
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Mod/Web/App/Server.cpp
Expand Up @@ -112,6 +112,13 @@ const QByteArray& ServerEvent::request() const
AppServer::AppServer(QObject* parent)
: QTcpServer(parent)
{
PyObject* mod = PyImport_ImportModule("__main__");
if (mod) {
module = mod;
}
else {
throw Py::RuntimeError("Cannot load __main__ module");
}
}

void AppServer::incomingConnection(qintptr socket)
Expand Down Expand Up @@ -147,12 +154,35 @@ void AppServer::customEvent(QEvent* e)
QByteArray msg = ev->request();
QTcpSocket* socket = ev->socket();

std::string str = runPython(msg);
std::string str;
if (msg.startsWith("GET ")) {
msg = QByteArray("GET = ") + msg.mid(4);
str = runPython(msg);
if (str == "None") {
str = getRequest(str);
}
}
else {
str = runPython(msg);
}

socket->write(str.c_str());
socket->close();
}

std::string AppServer::getRequest(const std::string& str) const
{
try {
Base::PyGILStateLocker lock;
Py::Object attr = module.getAttr(std::string("GET"));
return attr.as_string();
}
catch (Py::Exception &e) {
e.clear();
return str;
}
}

std::string AppServer::runPython(const QByteArray& msg)
{
std::string str;
Expand Down
2 changes: 2 additions & 0 deletions src/Mod/Web/App/Server.h
Expand Up @@ -89,12 +89,14 @@ class AppServer : public QTcpServer

protected:
void customEvent(QEvent* e);
std::string getRequest(const std::string&) const;

private Q_SLOTS:
void readClient();
void discardClient();

private:
Py::Object module;
};

}
Expand Down

4 comments on commit 5387e12

@dcapeletti
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, thank you for your reply.
Which version of FreeCAD should I download to test this commit?

Thanks

@luzpaz
Copy link
Contributor

@luzpaz luzpaz commented on 5387e12 Oct 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcapeletti You'll have to git checkout this branch and then build from source.

@dcapeletti
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi I downloaded the weekly-builds version and I confirm that it works!!!!

Thanks

@dcapeletti
Copy link

@dcapeletti dcapeletti commented on 5387e12 Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wwmayer I have started FreeCAD in -c mode but apparently the web server does not respond in console mode.
Can you confirm this?

Thanks

Please sign in to comment.