www.pudn.com > reacTIVision-1.3.rar > TuioServer.cpp


/*  reacTIVision fiducial tracking framework
    TuioServer.cpp
    Copyright (C) 2006 Martin Kaltenbrunner 

    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; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "TuioServer.h"
#include 
#include 
#ifdef WIN32
#include 
#else
#include 
#endif

TuioServer::TuioServer(char* address, int port)
{
	//InitializeNetworking();
	long unsigned int ip = GetHostByName(address);
	transmitSocket = new UdpTransmitSocket(IpEndpointName(ip, port));
	
	objBuffer = new char[IP_MTU_SIZE];
	objPacket = new osc::OutboundPacketStream(objBuffer,IP_MTU_SIZE);
	(*objPacket) << osc::BeginBundleImmediate;
	(*objPacket) << osc::BeginMessage( "/tuio/2Dobj") << "source" << "vision" << osc::EndMessage;


	curBuffer = new char[IP_MTU_SIZE];
	curPacket = new osc::OutboundPacketStream(curBuffer,IP_MTU_SIZE);
	(*curPacket) << osc::BeginBundleImmediate;
	(*curPacket) << osc::BeginMessage( "/tuio/2Dcur") << "source" << "vision" << osc::EndMessage;


	objMessages=0;
	curMessages=0;
}

bool  TuioServer::freeObjSpace() {
	return (((*objPacket).Capacity()-(*objPacket).Size())>=OBJ_MESSAGE_SIZE);
}

void TuioServer::addObjSeq(long fseq) {
	(*objPacket) << osc::BeginMessage( "/tuio/2Dobj") << "fseq"
		     << fseq << osc::EndMessage;
}

void TuioServer::addObjSet(long s_id, int f_id, float x, float y, float a, float X, float Y, float A, float m, float r) {

	if(invert_x) x=1-x;
	if(invert_y) y=1-y;
	if(invert_a) a=TWO_PI-a;

	(*objPacket) << osc::BeginMessage( "/tuio/2Dobj") << "set" 
		     << s_id << f_id << x << y << a << X << Y << A << m << r
	             << osc::EndMessage;
	objMessages++;
}

void TuioServer::addObjAlive(long *id, int size) {
	(*objPacket) << osc::BeginMessage( "/tuio/2Dobj") << "alive";
	for (int i=0;i0) {
		(*objPacket) << osc::EndBundle;

		transmitSocket->Send( objPacket->Data(), objPacket->Size() );
		objPacket->Clear();
		(*objPacket) << osc::BeginBundleImmediate;
		(*objPacket) << osc::BeginMessage( "/tuio/2Dobj") << "source" << "vision" << osc::EndMessage;

		objMessages = 0;
	}
}

bool  TuioServer::freeCurSpace() {
	return ((curPacket->Capacity()-curPacket->Size())>=CUR_MESSAGE_SIZE);
}

void TuioServer::addCurSeq(long fseq) {
	(*curPacket) << osc::BeginMessage( "/tuio/2Dcur") << "fseq"
		     << fseq << osc::EndMessage;
}

void TuioServer::addCurSet(long s_id, float x, float y, float X, float Y, float m) {

	if(invert_x) x=1-x;
	if(invert_y) y=1-y;

	(*curPacket) << osc::BeginMessage( "/tuio/2Dcur") << "set" 
		     << s_id << x << y << X << Y << m
	             << osc::EndMessage;
	curMessages++;
}

void TuioServer::addCurAlive(long *id, int size) {
	(*curPacket) << osc::BeginMessage( "/tuio/2Dcur") << "alive";
	for (int i=0;i0) {
		(*curPacket) << osc::EndBundle;

		transmitSocket->Send( curPacket->Data(), curPacket->Size() );
		curPacket->Clear();
		(*curPacket) << osc::BeginBundleImmediate;
		(*curPacket) << osc::BeginMessage( "/tuio/2Dcur") << "source" << "vision" << osc::EndMessage;

		curMessages = 0;
	}
}

TuioServer::~TuioServer()
{
	delete objPacket;
	delete curPacket;
	delete []objBuffer;
	delete []curBuffer;
	
	delete transmitSocket;
	//TerminateNetworking();
}