vasingletonmethods.hpp 3.69 KB
Newer Older
1 2 3 4 5 6
#include <Python.h>
#include <VANetClient.h>
#include <VACore.h>
#include <VAException.h>
#include <string.h>

7
// If you want to extend the va Python pSelf interface, also add
8 9 10 11
// 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
12
static PyObject* g_pVAError = nullptr; //!< Static pointer to error instance
13

14
//! Helper for API dev
15
static PyObject* va_not_implemented( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
16 17 18 19 20
{
	VA_EXCEPT_NOT_IMPLEMENTED;
	return NULL;
}

21 22 23 24 25 26 27 28 29 30
//! 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." );
}

31
static PyObject* va_connect( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
32 33 34 35 36
{
	if( !g_pVANetClient )
		g_pVANetClient = IVANetClient::Create();

	if( g_pVANetClient->IsConnected() )
37 38
	{
		PyErr_Warn( pSelf, "Was still connected, forced disconnect." );
39
		g_pVANetClient->Disconnect();
40 41 42 43 44 45 46 47 48 49
	}

	static const char * const _keywords[] = { "server", "port", NULL };
	static _PyArg_Parser _parser = { "Oi:connect", _keywords, 0 };
	PyObject* pServer;
	PyObject* pPort;

	if( !_PyArg_ParseStack( ppArgs, nArgs, pKeywordNames, &_parser, &pServer, &pPort ) )
		return PyBool_FromLong( 0 );

50
	std::string sServerIP = "localhost";
51 52
	char* pcServerIP = nullptr;
	if( PyArg_ParseTuple( pPort, "i", &pcServerIP ) )
53
		sServerIP = std::string( pcServerIP );
54

55 56 57 58 59
	int iServerPort = 12340;
	int* piServerPort = nullptr;
	if( PyArg_ParseTuple( pPort, "i", &piServerPort ) )
		iServerPort = *piServerPort;

60
	if( IVANetClient::VA_NO_ERROR == g_pVANetClient->Initialize( sServerIP, iServerPort ) )
61
		return PyBool_FromLong( 1 );
62

63 64 65
	PyErr_SetString( PyExc_ConnectionError, std::string( "Could not connect to " + sServerIP + " on " + std::to_string( ( long ) iServerPort ) ).c_str() );

	return NULL;
66 67
}

68
static PyObject* va_disconnect( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
69 70 71 72 73 74
{
	if( g_pVANetClient )
		g_pVANetClient->Disconnect();

	return PyBool_FromLong( 1 );
}
75
static PyObject* va_is_connected( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
76 77 78 79 80 81
{
	if( !g_pVANetClient )
		return PyBool_FromLong( 0 );
	else
		return PyBool_FromLong( g_pVANetClient->IsConnected() );
}
82

83
static PyObject* va_reset( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
84 85 86 87 88 89 90 91
{
	RequireCoreAvailable();

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

	return PyBool_FromLong( 1 );
}

92
static PyObject* va_enumerate_modules( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
93 94 95 96 97 98 99 100
{
	RequireCoreAvailable();

	VA_EXCEPT_NOT_IMPLEMENTED;

	return NULL;
}

101
static PyObject* va_call_module( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
102 103 104 105 106 107 108 109
{
	RequireCoreAvailable();

	VA_EXCEPT_NOT_IMPLEMENTED;

	return NULL;
}

110
static PyObject* va_add_search_path( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
111 112 113 114 115 116 117 118
{
	RequireCoreAvailable();

	VA_EXCEPT_NOT_IMPLEMENTED;

	return NULL;
}

119
static PyObject* va_create_listener( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
120 121 122 123 124 125 126 127
{
	RequireCoreAvailable();

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

	return PyLong_FromLong( iID );
}