non/nonlib/OSC/Endpoint.C

228 lines
6.1 KiB
C++
Raw Normal View History

2010-01-21 01:33:02 +01:00
/*******************************************************************************/
/* Copyright (C) 2010 Jonathan Moore Liles */
/* */
/* This program is free software; you can redistribute it and/or modify it */
/* under the terms of the GNU General Public License as published by the */
/* Free Software Foundation; either version 2 of the License, or (at your */
/* option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
/* more details. */
/* */
/* You should have received a copy of the GNU General Public License along */
/* with This program; see the file COPYING. If not,write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*******************************************************************************/
#include <lo/lo.h>
#include "debug.h"
#include <stdlib.h>
#include <stdio.h>
#include "Endpoint.H"
namespace OSC
{
void
Endpoint::error_handler(int num, const char *msg, const char *path)
{
WARNING( "LibLO server error %d in path %s: %s\n", num, path, msg);
}
Endpoint::Endpoint ( const char *port )
{
DMESSAGE( "Creating OSC server" );
// _st = lo_server_thread_new( s, error_handler );
// _server = lo_server_thread_get_server( _st );
_server = lo_server_new( port, error_handler );
if ( ! _server )
FATAL( "Error creating OSC server" );
char *url = lo_server_get_url(_server);
printf("OSC: %s\n",url);
free(url);
}
Endpoint::~Endpoint ( )
{
// lo_server_thread_free( _st );
lo_server_free( _server );
}
void
Endpoint::add_method ( const char *path, const char *typespec, lo_method_handler handler, void *user_data )
{
lo_server_add_method( _server, path, typespec, handler, user_data );
}
void
Endpoint::del_method ( const char *path, const char *typespec )
{
lo_server_del_method( _server, path, typespec );
}
/* void * */
/* Endpoint::osc_thread ( void * arg ) */
/* { */
/* ((Endpoint*)arg)->osc_thread(); */
/* return NULL; */
/* } */
/* void */
/* Endpoint::osc_thread ( void ) */
/* { */
/* _thread.name( "OSC" ); */
/* DMESSAGE( "OSC Thread running" ); */
/* for ( ;; ) */
/* { */
/* lo_server_recv( _sever ); */
/* } */
/* } */
void
Endpoint::start ( void )
{
/* if ( !_thread.clone( &Endpoint::osc_thread, this ) ) */
/* FATAL( "Could not create OSC thread" ); */
/* lo_server_thread_start( _st ); */
}
void
Endpoint::stop ( void )
{
// lo_server_thread_stop( _st );
}
int
Endpoint::port ( void ) const
{
return lo_server_get_port( _server );
}
char *
Endpoint::url ( void ) const
{
return lo_server_get_url( _server );
}
/** Process any waiting events and return immediately */
void
Endpoint::check ( void ) const
{
wait( 0 );
}
/** Process any waiting events and return immediately */
void
Endpoint::wait ( int timeout ) const
{
lo_server_recv_noblock( _server, timeout );
}
/** Process events forever */
void
Endpoint::run ( void ) const
{
for ( ;; )
{
lo_server_recv( _server );
}
}
int
Endpoint::send ( lo_address to, const char *path, std::list< OSC_Value > values )
{
lo_message m = lo_message_new();
for ( std::list< OSC_Value >::const_iterator i = values.begin();
i != values.end();
++i )
{
const OSC_Value *ov = &(*i);
switch ( ov->type() )
{
case 'f':
lo_message_add_float( m, ((OSC_Float*)ov)->value() );
break;
case 'i':
lo_message_add_int32( m, ((OSC_Int*)ov)->value() );
break;
case 's':
DMESSAGE( "Adding string %s", ((OSC_String*)ov)->value() );
lo_message_add_string( m, ((OSC_String*)ov)->value() );
break;
default:
FATAL( "Unknown format: %c", ov->type() );
break;
}
}
DMESSAGE( "Path: %s", path );
lo_bundle b = lo_bundle_new( LO_TT_IMMEDIATE );
lo_bundle_add_message(b, path, m );
int r = lo_send_bundle_from( to, _server, b );
// int r = lo_send_message_from( to, _server, path, m );
// lo_message_free( m );
return r;
}
int
Endpoint::send ( lo_address to, const char *path )
{
return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "" );
}
int
Endpoint::send ( lo_address to, const char *path, int v )
{
return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "i", v );
}
int
Endpoint::send ( lo_address to, const char *path, float v )
{
return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "f", v );
}
int
Endpoint::send ( lo_address to, const char *path, double v )
{
return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "d", v );
}
int
Endpoint::send ( lo_address to, const char *path, const char * v )
{
return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "s", v );
}
int
Endpoint::send ( lo_address to, const char *path, const char * v1, const char *v2 )
{
return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "ss", v1, v2 );
}
}