vasingletonmethods.hpp 2.16 KB
Newer Older
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
#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 );
}