vasingletonmethods.hpp 2.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#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

13 14 15 16 17 18 19
//! Helper for API dev
static PyObject* va_not_implemented( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
	VA_EXCEPT_NOT_IMPLEMENTED;
	return NULL;
}

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
//! 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 );
}

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
static PyObject* va_enumerate_modules( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
	RequireCoreAvailable();

	VA_EXCEPT_NOT_IMPLEMENTED;

	return NULL;
}

static PyObject* va_call_module( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
	RequireCoreAvailable();

	VA_EXCEPT_NOT_IMPLEMENTED;

	return NULL;
}

static PyObject* va_add_search_path( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
	RequireCoreAvailable();

	VA_EXCEPT_NOT_IMPLEMENTED;

	return NULL;
}

static PyObject* va_create_listener( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
99 100 101 102 103 104 105 106
{
	RequireCoreAvailable();

	std::string sName = "PyListener";
	int iID = g_pVANetClient->GetCoreInstance()->CreateListener( sName, IVACore::VA_AURAMODE_ALL );

	return PyLong_FromLong( iID );
}