vasingletonmethods.hpp 3.13 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
//! 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." );
}

30
static PyObject* va_connect( PyObject* self, PyObject* args )
31 32 33 34 35 36
{
	if( !g_pVANetClient )
		g_pVANetClient = IVANetClient::Create();

	if( g_pVANetClient->IsConnected() )
		g_pVANetClient->Disconnect();
37 38 39 40 41
	
	const char* pcServerIP = nullptr;
	std::string sServerIP = "localhost";
	if( PyArg_ParseTuple( args, "s", &pcServerIP ) )
		sServerIP = std::string( pcServerIP );
42

43 44 45 46 47
	int iServerPort = -1;
	if( !PyArg_ParseTuple( args, "i", &iServerPort ))
		iServerPort = 12340;
	
	if( IVANetClient::VA_NO_ERROR == g_pVANetClient->Initialize( sServerIP, iServerPort ) )
48
		return PyBool_FromLong( 1 );
49 50 51 52
	
	PyErr_SetString( PyExc_ConnectionError, std::string( "Could not connect to " + sServerIP + " on " + std::to_string( ( long ) iServerPort ) ).c_str() );

	return NULL;
53 54 55 56 57 58 59 60 61
}

static PyObject* va_disconnect( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
	if( g_pVANetClient )
		g_pVANetClient->Disconnect();

	return PyBool_FromLong( 1 );
}
62 63 64 65 66 67 68
static PyObject* va_is_connected( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
	if( !g_pVANetClient )
		return PyBool_FromLong( 0 );
	else
		return PyBool_FromLong( g_pVANetClient->IsConnected() );
}
69 70 71 72 73 74 75 76 77 78

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

	g_pVANetClient->GetCoreInstance()->Reset();

	return PyBool_FromLong( 1 );
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
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 )
107 108 109 110 111 112 113 114
{
	RequireCoreAvailable();

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

	return PyLong_FromLong( iID );
}