/*
 *  ofOscSender.cpp
 *  openFrameworks OSC addon
 *
 *  damian@frey.co.nz
 *
 */

#include "ofOscSender.h"

#include "OscTypes.h"
#include "OscOutboundPacketStream.h"

#include <UdpSocket.h>

#include <assert.h>

ofOscSender::ofOscSender()
{
}

ofOscSender::~ofOscSender()
{
}

void ofOscSender::setup( const char* hostname, int port )
{
	socket = new UdpTransmitSocket( IpEndpointName( hostname, port ) );
}

void ofOscSender::sendMessage( const ofOscMessage& message )
{
	static const int OUTPUT_BUFFER_SIZE = 16384;
	char buffer[OUTPUT_BUFFER_SIZE];
    osc::OutboundPacketStream p( buffer, OUTPUT_BUFFER_SIZE );
    
    p << osc::BeginBundleImmediate << osc::BeginMessage( message.getAddress() );
	for ( int i=0; i< message.getNumArgs(); ++i )
	{
		if ( message.getArgType(i) == OFOSC_TYPE_INT32 )
			p << message.getArgAsInt32( i );
		else if ( message.getArgType( i ) == OFOSC_TYPE_FLOAT )
			p << message.getArgAsFloat( i );
		else if ( message.getArgType( i ) == OFOSC_TYPE_STRING )
			p << message.getArgAsString( i );
		else
		{
			assert( false && "bad argument type" );
		}
	}
	p << osc::EndMessage << osc::EndBundle;
    
	socket->Send( p.Data(), p.Size() );
}
