More methods and waring and exception handling

parent cf38808d
...@@ -51,22 +51,24 @@ if( NOT WIN32 ) ...@@ -51,22 +51,24 @@ if( NOT WIN32 )
set( SHARED_LIBRARY_EXTENSION "so" ) set( SHARED_LIBRARY_EXTENSION "so" )
endif( ) endif( )
# VA shared libs if( BUILD_SHARED_LIBS )
install( FILES "${VABASE_LIBRARY_DIRS}/VABase.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) # VA shared libs
install( FILES "${VANET_LIBRARY_DIRS}/VANet.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) install( FILES "${VABASE_LIBRARY_DIRS}/VABase.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" )
install( FILES "${VANET_LIBRARY_DIRS}/VANet.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" )
# VistaCoreLibs shared libs
foreach( VISTACORELIBS_LIBRARY_DIR ${VISTACORELIBS_LIBRARY_DIRS} ) # VistaCoreLibs shared libs
if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaBase.${SHARED_LIBRARY_EXTENSION}" ) foreach( VISTACORELIBS_LIBRARY_DIR ${VISTACORELIBS_LIBRARY_DIRS} )
install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaBase.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaBase.${SHARED_LIBRARY_EXTENSION}" )
endif( ) install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaBase.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" )
if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaAspects.${SHARED_LIBRARY_EXTENSION}" ) endif( )
install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaAspects.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaAspects.${SHARED_LIBRARY_EXTENSION}" )
endif( ) install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaAspects.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" )
if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaInterProcComm.${SHARED_LIBRARY_EXTENSION}" ) endif( )
install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaInterProcComm.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaInterProcComm.${SHARED_LIBRARY_EXTENSION}" )
endif( ) install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaInterProcComm.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" )
endforeach( ) endif( )
endforeach( )
endif( )
# Tests # Tests
install( DIRECTORY "tests" DESTINATION "python" ) install( DIRECTORY "tests" DESTINATION "python" )
......
...@@ -7,28 +7,37 @@ ...@@ -7,28 +7,37 @@
"# VA notebook test\n", "# VA notebook test\n",
"\n", "\n",
"This is a simple test program that demonstrates the use of the VA Python binding within a jupyter notebook environment.\n", "This is a simple test program that demonstrates the use of the VA Python binding within a jupyter notebook environment.\n",
"Before we start scripting, let's make VA available for us. If it is not available system (or user) wide, this is how you can add the `va` module from the distribution folder structure." "\n",
"## Before we start\n",
"Before we start scripting, let's make VA available for us. If it is not installed and available from everywhere, this is how you can add the `va` module folder:"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"scrolled": false "scrolled": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"import sys\n", "import sys\n",
"sys.path.append( \"../Lib/site-packages\" )\n", "sys.path.append( \"../../dist/Lib/site-packages\" ) # build\n",
"sys.path" "sys.path.append( \"../Lib/site-packages\" ) # deploy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start\n",
"\n",
"We start by making va available for our script"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"import va" "import va"
...@@ -38,7 +47,14 @@ ...@@ -38,7 +47,14 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Ok. Now let's try to connect to a VA server running on the local machine. Remark: this local machine will be the one where this server jupyter notebook is running, not necessarily your PC." "Ok. Now let's try to connect to the VA server that should be running on the same computer where this jupyter notebook is running. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We start by finding out where we are currently working and list the files available, i.e. to identify files that can be used as HRIR, audio file or directivity."
] ]
}, },
{ {
...@@ -47,41 +63,84 @@ ...@@ -47,41 +63,84 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"va.connect( \"localhost\" ) # default port is: 12340; can be given as second parameter" "connection_status = va.connect()"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"We start by finding out where we are currently working and list the files available, i.e. to identify files that can be used as HRIR, audio file or directivity." "We can check the connection by the following line"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"VA connection ready!\n"
]
}
],
"source": [
"connected = va.is_connected()\n",
"if connected :\n",
" print( \"VA connection ready!\" )\n",
"else :\n",
" print( \"Something went wrong.\" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... and also use different server names and ports"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [
"if not connected :\n",
" va.connect( \"localhost\", 12340 ) # these are the default arguments"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"working directory: D:\\Users\\stienen\\dev\\VA\\VAPython\\examples\\jupyter\n"
]
}
],
"source": [ "source": [
"import os\n", "import os\n",
"current_working_dir = os.getcwd()\n", "current_working_dir = os.getcwd()\n",
"print( \"working directory: \" + current_working_dir )\n", "print( \"working directory: \" + current_working_dir )"
"os.listdir( current_working_dir )"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Now lets add this folder to VA. This means that VA can find files that reside in this location, all you have to use is the file name (or a relative path from the base path you have added as search path)." "Now lets add this folder to VA. This means that VA can find files that reside in this location. All you have to do is use a file name or a relative path from this base path. You can add as much folders as you like."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"va.add_search_path( current_working_dir )" "va.add_search_path( current_working_dir )"
......
from distutils.core import setup, Extension
module1 = Extension('va',
define_macros = [('VA_PYTHON_VERSION_MAJOR', '2017'), ('VA_PYTHON_VERSION_MINOR', 'c'), ('VABASE_STATIC', 1), ('VANET_STATIC', 1), ('VISTABASE_STATIC', 1), ('VISTAINTERPROCCOMM_STATIC', 1), ('VISTAASPECTS_STATIC',1)],
include_dirs = ['../VABase/include','../VANet/include'],
libraries = ['VABase', 'VANet', 'VistaBase', 'VistaAspects', 'VistaInterProcComm', 'winmm', 'ws2_32' ],
library_dirs = ['../build_win32-x64.vc14_static/lib','../../ViSTA/build_static_win32-x64.vc14/lib'],
sources = ['src/vasingleton.cpp'] )
setup (name = 'va',
version = '2017.c',
description = 'Virtual Acoustics singleton interface',
author = 'Institute of Technical Acoustics (ITA), RWTH Aachen University',
author_email = 'post@akustik.rwth-aachen.de',
url = 'https://virtualacoustics.org',
long_description = '''
Virtual Acoustics is a real-time auralization framework for Virtual Reality. This module is an interface to interact with a VA server.
''',
license = "Apache License Version 2.0",
ext_modules = [module1])
\ No newline at end of file
...@@ -39,16 +39,16 @@ static PyObject* va_connect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs ...@@ -39,16 +39,16 @@ static PyObject* va_connect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs
if (g_pVANetClient->IsConnected()) if (g_pVANetClient->IsConnected())
{ {
PyErr_Warn(pSelf, "Was still connected, forced disconnect."); PyErr_WarnEx(NULL, "Was still connected, forced disconnect.", 1 );
g_pVANetClient->Disconnect(); g_pVANetClient->Disconnect();
} }
static const char * const _keywords[] = { "server", "port", NULL }; static const char * const _keywords[] = { "server", "port", NULL };
static _PyArg_Parser _parser = { "|si:connect", _keywords, 0 }; static _PyArg_Parser _parser = { "|si:connect", _keywords, 0 };
char* pcServerIP; char* pcServerIP = nullptr;
int iServerPort = 12340; int iServerPort = 12340;
if (!_PyArg_ParseStack(ppArgs, nArgs, pKeywordNames, &_parser, pcServerIP, &iServerPort)) if (!_PyArg_ParseStack(ppArgs, nArgs, pKeywordNames, &_parser, &pcServerIP, &iServerPort))
return NULL; return NULL;
std::string sServerIP = pcServerIP ? std::string(pcServerIP) : "localhost"; std::string sServerIP = pcServerIP ? std::string(pcServerIP) : "localhost";
...@@ -63,10 +63,15 @@ static PyObject* va_connect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs ...@@ -63,10 +63,15 @@ static PyObject* va_connect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs
static PyObject* va_disconnect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) static PyObject* va_disconnect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
{ {
if (g_pVANetClient) if (g_pVANetClient)
g_pVANetClient->Disconnect(); {
else if (g_pVANetClient->IsConnected())
PyErr_Warn(pSelf, "Was not connected, doing nothing. Use is_connected to avoid this message."); {
g_pVANetClient->Disconnect();
return PyBool_FromLong(1);
}
}
PyErr_WarnEx(NULL, "Was not connected, doing nothing. Use is_connected to avoid this message.", 1);
return PyBool_FromLong(1); return PyBool_FromLong(1);
}; };
...@@ -78,7 +83,7 @@ static PyObject* va_is_connected(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t ...@@ -78,7 +83,7 @@ static PyObject* va_is_connected(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t
return PyBool_FromLong(g_pVANetClient->IsConnected()); return PyBool_FromLong(g_pVANetClient->IsConnected());
}; };
static PyObject* va_reset(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) static PyObject* va_reset(PyObject*, PyObject*)
{ {
VAPY_REQUIRE_CONN_TRY; VAPY_REQUIRE_CONN_TRY;
...@@ -91,20 +96,14 @@ static PyObject* va_reset(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, ...@@ -91,20 +96,14 @@ static PyObject* va_reset(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs,
static PyObject* va_enumerate_modules(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) static PyObject* va_enumerate_modules(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
{ {
VAPY_REQUIRE_CONN_TRY; VAPY_REQUIRE_CONN_TRY;
VA_EXCEPT_NOT_IMPLEMENTED; VA_EXCEPT_NOT_IMPLEMENTED;
return NULL;
VAPY_CATCH_RETURN; VAPY_CATCH_RETURN;
}; };
static PyObject* va_call_module(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) static PyObject* va_call_module(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
{ {
VAPY_REQUIRE_CONN_TRY; VAPY_REQUIRE_CONN_TRY;
VA_EXCEPT_NOT_IMPLEMENTED; VA_EXCEPT_NOT_IMPLEMENTED;
return NULL;
VAPY_CATCH_RETURN; VAPY_CATCH_RETURN;
}; };
...@@ -112,8 +111,13 @@ static PyObject* va_add_search_path(PyObject* pSelf, PyObject** ppArgs, Py_ssize ...@@ -112,8 +111,13 @@ static PyObject* va_add_search_path(PyObject* pSelf, PyObject** ppArgs, Py_ssize
{ {
VAPY_REQUIRE_CONN_TRY; VAPY_REQUIRE_CONN_TRY;
VA_EXCEPT_NOT_IMPLEMENTED; static const char * const _keywords[] = { "directory_path", NULL };
return NULL; static _PyArg_Parser _parser = { "s:add_search_path", _keywords, 0 };
char* pcPath = nullptr;
if (!_PyArg_ParseStack(ppArgs, nArgs, pKeywordNames, &_parser, &pcPath))
return NULL;
return PyBool_FromLong(g_pVANetClient->GetCoreInstance()->AddSearchPath(std::string(pcPath)));
VAPY_CATCH_RETURN; VAPY_CATCH_RETURN;
}; };
......
...@@ -7,14 +7,47 @@ sys.path.append( '../Lib/site-packages' ) # deploy structure ...@@ -7,14 +7,47 @@ sys.path.append( '../Lib/site-packages' ) # deploy structure
import va import va
print( "Testing va extension connection." ) print( "Testing va extension connection methods." )
if va.connect( "pc-jst" ) : if va.connect() :
print( "Successfully connected" ) print( "Successfully connected to local server without arguments" )
va.disconnect() # direct disconnect
else : else :
print( "Connection failed" ) print( "Connection failed" )
if va.connect( "localhost" ) :
print( "Successfully connected to local server with localhost argument" )
else :
print( "Connection failed" )
# sensitive disconnect
if va.is_connected() : if va.is_connected() :
va.disconnect() va.disconnect()
if va.connect( "localhost", 12340 ) :
print( "Successfully connected to local server with localhost and port 12340 argument" )
else :
print( "Connection failed" )
print( "Disconnect." )
va.disconnect()
import time
import warnings
with warnings.catch_warnings() :
warnings.simplefilter( "always" )
time.sleep( 1 )
print( "Double disconnect:" )
va.disconnect() # double disconnect should raise warning
va.connect()
time.sleep( 1 )
print( "Double connect:" )
va.connect() # double connect should raise forced disconnection warning
va.disconnect()
print( "Test done." ) print( "Test done." )
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