OpenTracker

An Open Architecture for Reconfigurable Tracking based on XML | Contact

VRPNModule.cxx

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (c) 2006,
00003  * Institute for Computer Graphics and Vision
00004  * Graz University of Technology
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are
00009  * met:
00010  *
00011  * Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the following disclaimer.
00013  *
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the distribution.
00017  *
00018  * Neither the name of the Graz University of Technology nor the names of
00019  * its contributors may be used to endorse or promote products derived from
00020  * this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00023  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00024  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00026  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  * ========================================================================
00034  * PROJECT: OpenTracker
00035  * ======================================================================== */
00042 /* ======================================================================= */
00043 
00044 #include "VRPNModule.h"
00045 #include "VRPNSource.h"
00046 #include "VRPNSink.h"
00047 
00048 #ifdef USE_VRPN
00049 
00050 #ifdef WIN32
00051 #pragma comment(lib,"vrpn.lib")
00052 #endif
00053 
00054 // #define ACE_NTRACE 0
00055 
00056 #include <ace/Log_Msg.h>
00057 
00058 #include <vrpn_Connection.h>
00059 
00060 using namespace std;
00061 using namespace ot;
00062 
00063 VRPNModule::VRPNModule() : 
00064     Module(), 
00065     NodeFactory()
00066 {
00067 }
00068 
00069 VRPNModule::~VRPNModule()
00070 {
00071     VRPNSourceVector::iterator it;
00072     for( it = sources.begin(); it != sources.end(); it++ )
00073     {
00074         delete (*it);
00075     }
00076     sources.clear();
00077     VRPNSinkVector::iterator it2;
00078     for( it2 = sinks.begin(); it2 != sinks.end(); it2++ )
00079     {
00080         delete (*it2);
00081     }
00082     sinks.clear();
00083 }
00084 
00085 void VRPNModule::init(StringTable & attributes,  ConfigNode * localTree)
00086 {
00087     Module::init( attributes, localTree );
00088     if( attributes.get("port", &port ) != 1 )
00089         port = vrpn_DEFAULT_LISTEN_PORT_NO;
00090     ip = attributes.get("interface");
00091 }
00092 
00093 // called to construct a new Node.
00094 
00095 ot::Node * VRPNModule::createNode( const std::string& name, StringTable& attributes)
00096 {
00097     ACE_TRACE(ACE_TEXT("VRPNModule::createNode"));
00098     if( name.compare("VRPNSource") == 0 )
00099     {
00100         const string & nameVal = attributes.get("name");
00101         VRPNSource::Type typeVal = VRPNSource::TRACKER;;
00102         if( attributes.get("type").compare("button") == 0 )
00103             typeVal = VRPNSource::BUTTON;
00104         int stationVal = 0;
00105         attributes.get("station", &stationVal);
00106         VRPNSource * source = new VRPNSource;
00107         source->name = nameVal;
00108         source->type = typeVal;
00109         source->station = stationVal;
00110         sources.push_back( source );
00111         return source;
00112     } 
00113     else if( name.compare("VRPNSink") == 0 )  
00114     {
00115         const string & nameVal = attributes.get("name");
00116         VRPNSink::Type typeVal = VRPNSink::TRACKER;
00117         if( attributes.get("type").compare("button") == 0 )
00118             typeVal = VRPNSink::BUTTON;
00119         int stationVal = 0;
00120         attributes.get("station", &stationVal);
00121         VRPNSink * sink = new VRPNSink;
00122         sink->name = nameVal;
00123         sink->type = typeVal;
00124         sink->station = stationVal;
00125         sinks.push_back( sink );
00126         return sink;
00127     }
00128     return NULL;
00129 }
00130 
00131 void VRPNModule::start()
00132 {
00133     ACE_TRACE(ACE_TEXT("VRPNModule::start"));
00134     VRPNSourceVector::iterator it;
00135     for( it = sources.begin(); it != sources.end(); it++ )
00136     {
00137         (*it)->start();
00138     }
00139 
00140     if( ip.compare("") == 0 )
00141         connection = new vrpn_Connection( port );
00142     else
00143         connection = new vrpn_Connection( port, NULL, NULL, ip.c_str() );
00144 
00145     VRPNSinkVector::iterator it2;
00146     for( it2 = sinks.begin(); it2 != sinks.end(); it2++ )
00147     {
00148         (*it2)->start(connection);
00149     }
00150 }
00151 
00152 void VRPNModule::pushEvent()
00153 {
00154     VRPNSourceVector::iterator it;
00155     for( it = sources.begin(); it != sources.end(); it++ )
00156     {
00157         (*it)->mainloop();
00158     }
00159 }
00160 
00161 void VRPNModule::pullEvent()
00162 {
00163     ACE_TRACE(ACE_TEXT("VRPNModule::pullEvent"));
00164     VRPNSinkVector::iterator it;
00165     for( it = sinks.begin(); it != sinks.end(); it++ )
00166     {
00167         (*it)->mainloop();
00168     }
00169     connection->mainloop();
00170 }
00171 
00172 #else
00173 #pragma message(">>> no VRPN support")
00174 #endif
00175 
00176 /* 
00177  * ------------------------------------------------------------
00178  *   End of VRPNModule.cxx
00179  * ------------------------------------------------------------
00180  *   Automatic Emacs configuration follows.
00181  *   Local Variables:
00182  *   mode:c++
00183  *   c-basic-offset: 4
00184  *   eval: (c-set-offset 'substatement-open 0)
00185  *   eval: (c-set-offset 'case-label '+)
00186  *   eval: (c-set-offset 'statement 'c-lineup-runin-statements)
00187  *   eval: (setq indent-tabs-mode nil)
00188  *   End:
00189  * ------------------------------------------------------------ 
00190  */

copyright (c) 2006 Graz University of Technology