Restructuring Python extension sources

parent 7e772904
......@@ -19,7 +19,7 @@ endif( )
add_definitions( -DPy_NO_ENABLE_SHARED )
# Only add library here to check if it compiles. Use python distutils to actually compile the extension.
add_library( VAPython STATIC "vasingleton.cpp" )
add_library( VAPython STATIC "src/vasingleton.cpp" "src/vasingletondoc.hpp" "src/vasingletonmethods.hpp" )
target_link_libraries( VAPython ${VISTA_USE_PACKAGE_LIBRARIES} )
......
#include <Python.h>
#include "vasingletondoc.hpp"
#include "vasingletonmethods.hpp"
// VA methods that will appear in Python if they are added to the following table
// It's corresponding C++ functions are implemented here: vasingletonmethods.hpp
static struct PyMethodDef va_methods[] =
{
{ "connect", ( PyCFunction ) va_connect, METH_FASTCALL, va_connect_doc },
{ "disconnect", ( PyCFunction ) va_disconnect, METH_FASTCALL, va_no_doc },
{ "reset", ( PyCFunction ) va_reset, METH_FASTCALL, va_no_doc },
{ "add_listener", ( PyCFunction ) va_add_listener, METH_FASTCALL, va_no_doc },
{ NULL, NULL }
};
static struct PyModuleDef vamoduledef =
{
PyModuleDef_HEAD_INIT,
"va",
va_module_doc,
-1,
va_methods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC PyInit_va( void )
{
PyObject* dict;
PyObject* module = PyModule_Create( &vamoduledef );
return module;
}
#include <Python.h>
PyDoc_STRVAR( va_module_doc,
"connect(server, port) - connect to a VA server at given server and listening port\n"
"disconnect() - disconnect from VA server." );
PyDoc_STRVAR( va_no_doc,
"For this method no dedicated documentation is available. Please read the C++ API documentation\n"
"of this method for further information." );
PyDoc_STRVAR( va_connect_doc,
"Connect($module, /, server, port)\n"
"--\n"
"\n"
"Connect to a VA server.\n"
"\n"
" server\n"
" Remote server IP.\n"
" port\n"
" TCP/IP listening port, usually 12340." );
#include <Python.h>
#include <VANetClient.h>
#include <VACore.h>
#include <VAException.h>
#include <string.h>
// If you want to extend the va Python module interface, also add
// the function to the va_methods table in vasingleton.cpp - otherwise they will not show up.
// Documentation goes into vasingletondoc.hpp
static IVANetClient* g_pVANetClient = nullptr; //!< Static pointer to VANetClient instance
//! Raises an exception if core is not available
static void RequireCoreAvailable()
{
if( !g_pVANetClient )
VA_EXCEPT2( CVAException::NETWORK_ERROR, "VA client not available, please connect first" );
if( !g_pVANetClient->GetCoreInstance() )
VA_EXCEPT2( CVAException::NETWORK_ERROR, "VA client available, but access to VA interface failed. Please reconnect." );
}
static PyObject* va_connect( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
if( !g_pVANetClient )
g_pVANetClient = IVANetClient::Create();
if( g_pVANetClient->IsConnected() )
g_pVANetClient->Disconnect();
static const char * const _keywords[] = { "server", "port", NULL };
static _PyArg_Parser _parser = { "Oi:connect", _keywords, 0 };
PyObject* server;
PyObject* port;
if( !_PyArg_ParseStack( args, nargs, kwnames, &_parser, &server, &port ) )
return PyBool_FromLong( 0 );
std::string sServer = "localhost";
int iPort = 12340;
if( IVANetClient::VA_NO_ERROR == g_pVANetClient->Initialize( sServer, iPort ) )
return PyBool_FromLong( 1 );
else
return PyBool_FromLong( 1 );
}
static PyObject* va_disconnect( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
if( g_pVANetClient )
g_pVANetClient->Disconnect();
return PyBool_FromLong( 1 );
}
static PyObject* va_reset( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
RequireCoreAvailable();
g_pVANetClient->GetCoreInstance()->Reset();
return PyBool_FromLong( 1 );
}
static PyObject* va_add_listener( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
RequireCoreAvailable();
std::string sName = "PyListener";
int iID = g_pVANetClient->GetCoreInstance()->CreateListener( sName, IVACore::VA_AURAMODE_ALL );
return PyLong_FromLong( iID );
}
# VA is used as a singleton.
# You can access va in every script, function and method.
print( "Testing va extension." )
import va
if( va.connect( "localhost", 12340 ) )
print( "Successfully connected" )
else
print( "Connection failed" )
if( va.is_connected() )
va.disconnect()
print( "Test done." )
#include <Python.h>
#include <VANetClient.h>
#include <string.h>
static int g_iVal = 1;
static IVANetClient* g_pVANetClient = nullptr;
PyDoc_STRVAR(va_module_doc,
"connect(server, port) - connect to a VA server at given server and listening port\n"
"disconnect() - disconnect from VA server.");
PyDoc_STRVAR(va_connect__doc__,
"Connect($module, /, server, port)\n"
"--\n"
"\n"
"Connect to a VA server.\n"
"\n"
" server\n"
" Remote server IP.\n"
" port\n"
" TCP/IP listening port, usually 12340.");
static PyObject* va_disconnect_impl(PyObject*)
{
g_iVal++;
PyObject* pRetVal = PyTuple_New(3);
PyTuple_SetItem(pRetVal, 0, PyLong_FromLong(g_iVal));
PyTuple_SetItem(pRetVal, 1, PyFloat_FromDouble(.4f));
PyTuple_SetItem(pRetVal, 2, PyUnicode_FromString("singed off"));
return pRetVal;
};
static PyObject* va_connect(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
{
if (!g_pVANetClient)
g_pVANetClient = IVANetClient::Create();
g_pVANetClient->Disconnect();
static const char * const _keywords[] = { "server", "port", NULL };
static _PyArg_Parser _parser = { "Oi:connect", _keywords, 0 };
PyObject* server;
PyObject* port;
if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser, &server, &port))
{
return PyBool_FromLong(0);
}
std::string sServer = "localhost";
int iPort = 12340;
if(IVANetClient::VA_NO_ERROR == g_pVANetClient->Initialize(sServer, iPort) )
return PyBool_FromLong(1);
else
return PyBool_FromLong(1);
}
static PyObject* va_disconnect(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
{
return va_disconnect_impl(module);
}
static struct PyMethodDef va_methods[] =
{
{ "connect", (PyCFunction)va_connect, METH_FASTCALL, va_connect__doc__ },
{ "disconnect", (PyCFunction)va_disconnect, METH_FASTCALL, va_connect__doc__ },
{ NULL, NULL }
};
static struct PyModuleDef vamoduledef = {
PyModuleDef_HEAD_INIT,
"va",
va_module_doc,
-1,
va_methods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC PyInit_va(void)
{
PyObject *dict;
PyObject *module = PyModule_Create(&vamoduledef);
if (module == NULL)
return NULL;
dict = PyModule_GetDict(module);
/*
ADD_DEFINE(SND_ASYNC);
ADD_DEFINE(SND_NODEFAULT);
ADD_DEFINE(SND_NOSTOP);
ADD_DEFINE(SND_NOWAIT);
ADD_DEFINE(SND_ALIAS);
ADD_DEFINE(SND_FILENAME);
ADD_DEFINE(SND_MEMORY);
ADD_DEFINE(SND_PURGE);
ADD_DEFINE(SND_LOOP);
ADD_DEFINE(SND_APPLICATION);
ADD_DEFINE(MB_OK);
ADD_DEFINE(MB_ICONASTERISK);
ADD_DEFINE(MB_ICONEXCLAMATION);
ADD_DEFINE(MB_ICONHAND);
ADD_DEFINE(MB_ICONQUESTION);
*/
return module;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment