Make modpython work with server-dependent caps

This commit is contained in:
Alexey Sokolov
2024-01-14 11:12:32 +00:00
parent 4b1a524a72
commit efd136c01c
6 changed files with 195 additions and 55 deletions
+43
View File
@@ -478,6 +478,49 @@ CPySocket::~CPySocket() {
Py_CLEAR(m_pyObj);
}
CPyCapability::CPyCapability(PyObject* serverCb, PyObject* clientCb)
: m_serverCb(serverCb), m_clientCb(clientCb) {
Py_INCREF(serverCb);
Py_INCREF(clientCb);
}
CPyCapability::~CPyCapability() {
Py_CLEAR(m_serverCb);
Py_CLEAR(m_clientCb);
}
void CPyCapability::OnServerChangedSupport(CIRCNetwork* pNetwork, bool bState) {
PyObject* pyArg_Network =
SWIG_NewInstanceObj(pNetwork, SWIG_TypeQuery("CIRCNetwork*"), 0);
PyObject* pyArg_bState = Py_BuildValue("l", (long int)bState);
PyObject* pyRes = PyObject_CallFunctionObjArgs(m_serverCb, pyArg_Network,
pyArg_bState, nullptr);
if (!pyRes) {
CString sPyErr = ((CPyModule*)GetModule())->GetPyExceptionStr();
DEBUG("modpython: " << GetModule()->GetModName()
<< "/OnServerChangedSupport failed: " << sPyErr);
}
Py_CLEAR(pyRes);
Py_CLEAR(pyArg_bState);
Py_CLEAR(pyArg_Network);
}
void CPyCapability::OnClientChangedSupport(CClient* pClient, bool bState) {
PyObject* pyArg_Client =
SWIG_NewInstanceObj(pClient, SWIG_TypeQuery("CClient*"), 0);
PyObject* pyArg_bState = Py_BuildValue("l", (long int)bState);
PyObject* pyRes = PyObject_CallFunctionObjArgs(m_clientCb, pyArg_Client,
pyArg_bState, nullptr);
if (!pyRes) {
CString sPyErr = ((CPyModule*)GetModule())->GetPyExceptionStr();
DEBUG("modpython: " << GetModule()->GetModName()
<< "/OnClientChangedSupport failed: " << sPyErr);
}
Py_CLEAR(pyRes);
Py_CLEAR(pyArg_bState);
Py_CLEAR(pyArg_Client);
}
CPyModule* CPyModCommand::GetModule() {
return this->m_pModule;
}
+4
View File
@@ -241,6 +241,10 @@ class CPyRetBool {
bool ExistsNV(const CString& sName) {
return $self->EndNV() != $self->FindNV(sName);
}
void AddServerDependentCapability(const CString& sName, PyObject* serverCb,
PyObject* clientCb) {
$self->AddServerDependentCapability(sName, std::make_unique<CPyCapability>(serverCb, clientCb));
}
}
%extend CModules {
+14
View File
@@ -369,3 +369,17 @@ inline CPyModCommand* CreatePyModCommand(CPyModule* pModule,
PyObject* pyObj) {
return new CPyModCommand(pModule, sCmd, sArgs, sDesc, pyObj);
}
class ZNC_EXPORT_LIB_EXPORT CPyCapability : public CCapability {
public:
CPyCapability(PyObject* serverCb, PyObject* clientCb);
~CPyCapability();
void OnServerChangedSupport(CIRCNetwork* pNetwork, bool bState) override;
void OnClientChangedSupport(CClient* pClient, bool bState) override;
private:
PyObject* m_serverCb;
PyObject* m_clientCb;
};