SPIN Framework

include/spinBaseContext.h

00001 // -----------------------------------------------------------------------------
00002 // |    ___  ___  _  _ _     ___                                        _      |
00003 // |   / __>| . \| || \ |   | __>_ _  ___ ._ _ _  ___  _ _ _  ___  _ _ | |__   |
00004 // |   \__ \|  _/| ||   |   | _>| '_><_> || ' ' |/ ._>| | | |/ . \| '_>| / /   |
00005 // |   <___/|_|  |_||_\_|   |_| |_|  <___||_|_|_|\___.|__/_/ \___/|_|  |_\_\   |
00006 // |                                                                           |
00007 // |---------------------------------------------------------------------------|
00008 //
00009 // http://spinframework.sourceforge.net
00010 // Copyright (C) 2009 Mike Wozniewski, Zack Settel
00011 //
00012 // Developed/Maintained by:
00013 //    Mike Wozniewski (http://www.mikewoz.com)
00014 //    Zack Settel (http://www.sheefa.net/zack)
00015 //
00016 // Principle Partners:
00017 //    Shared Reality Lab, McGill University (http://www.cim.mcgill.ca/sre)
00018 //    La Societe des Arts Technologiques (http://www.sat.qc.ca)
00019 //
00020 // Funding by:
00021 //    NSERC/Canada Council for the Arts - New Media Initiative
00022 //    Heritage Canada
00023 //    Ministere du Developpement economique, de l'Innovation et de l'Exportation
00024 //
00025 // -----------------------------------------------------------------------------
00026 //  This file is part of the SPIN Framework.
00027 //
00028 //  SPIN Framework is free software: you can redistribute it and/or modify
00029 //  it under the terms of the GNU Lesser General Public License as published by
00030 //  the Free Software Foundation, either version 3 of the License, or
00031 //  (at your option) any later version.
00032 //
00033 //  SPIN Framework is distributed in the hope that it will be useful,
00034 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00035 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00036 //  GNU Lesser General Public License for more details.
00037 //
00038 //  You should have received a copy of the GNU Lesser General Public License
00039 //  along with SPIN Framework. If not, see <http://www.gnu.org/licenses/>.
00040 // -----------------------------------------------------------------------------
00041 
00042 #ifndef __spinBaseContext_H
00043 #define __spinBaseContext_H
00044 
00045 #include <vector>
00046 #include <string>
00047 #include <lo/lo_types.h>
00048 #include "EventTypes.h"
00049 #include <osg/ArgumentParser>
00050 
00051 namespace spin
00052 {
00053 
00054 // Forward declarations
00055 class spinLog;
00056 class EventHandler;
00057 
00076 class spinBaseContext
00077 {
00078     public:
00084         spinBaseContext();
00085 
00091         virtual ~spinBaseContext();
00092 
00093         static void sigHandler(int signum);
00094 
00102         static volatile bool signalStop;
00103 
00104         enum SpinContextMode { SERVER_MODE, CLIENT_MODE };
00105 
00106         bool isServer() { return mode==SERVER_MODE; }
00107 
00108         virtual bool start() = 0;
00109 
00110         virtual void debugPrint();
00111 
00112         virtual void addCommandLineOptions(osg::ArgumentParser *arguments);
00113         virtual int parseCommandLineOptions(osg::ArgumentParser *arguments);
00114 
00121         bool startThread( void *(*threadFunction) (void*) );
00122 
00126         void stop();
00127 
00128         bool isRunning() { return running; }
00129 
00130 
00135         void addInfoHandler(EventHandler *obs);
00139         void removeInfoHandler(EventHandler *obs);
00140 
00141         void removeHandlerForAllEvents(EventHandler *obs);
00142 
00143 
00149         bool canAutoAssignPorts() { return autoPorts_; }
00150         
00155         void setTTL(int ttl);
00156 
00164         std::vector<lo_address> lo_txAddrs_;
00165 
00169         std::vector<lo_address> lo_rxAddrs_;
00170         
00174         std::vector<lo_server> lo_rxServs_;
00175 
00179         lo_address lo_infoAddr;
00180         
00184         lo_address lo_syncAddr;
00185         lo_server lo_infoServ_;
00186        
00187         std::string tcpPort_; 
00188         lo_server lo_tcpRxServer_;
00189 
00190         static int connectionCallback(const char *path, const char *types, lo_arg **argv, 
00191                 int argc, void *data, void *user_data);
00208         static int nodeCallback(const char *path, const char *types, lo_arg **argv, 
00209                 int argc, void *data, void *user_data);
00237         static int sceneCallback(const char *path, const char *types, lo_arg **argv, 
00238                 int argc, void *data, void *user_data);
00239         static int logCallback(const char *path, const char *types, lo_arg **argv, 
00240                 int argc, void *data, void *user_data);
00241         static int debugCallback(const char *path, const char *types, lo_arg **argv, 
00242                 int argc, void *data, void *user_data);
00243 
00244         static void oscParser_error(int num, const char *msg, const char *path);
00245 
00254         bool doDiscovery_;
00255 
00256     protected:
00257         bool running;
00258         bool autoPorts_;
00259         SpinContextMode mode;
00260         void setLog(spinLog &log);
00261 
00262         std::vector<EventHandler*> infoHandlers;
00263         std::vector<EventHandler*> nodeHandlers;
00264         std::vector<EventHandler*> sceneHandlers;
00265 
00269         virtual void createServers() = 0;
00270 
00271     private:
00272         // pthread stuff
00273         pthread_t pthreadID; // id of child thread
00274         pthread_attr_t pthreadAttr;
00275 };
00276 
00277 } // end of namespace spin
00278 
00279 #endif
00280