startLocalServer.cpp

00001 
00002 /* Copyright (c) 2007-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #include "server.h"
00019 
00020 #include "global.h"
00021 #include "loader.h"
00022 
00023 #include <eq/net/node.h>
00024 #include <eq/net/pairConnection.h>
00025 
00026 using namespace eq::server;
00027 using namespace eq::base;
00028 using namespace std;
00029 
00030 #define CONFIG "server{ config{ appNode{ pipe { window { viewport [ .25 .25 .5 .5 ] channel { name \"channel\" }}}} compound { channel \"channel\" wall { bottom_left [ -.8 -.5 -1 ] bottom_right [  .8 -.5 -1 ] top_left [ -.8  .5 -1 ] }}}}"
00031 
00032 namespace
00033 {
00034 class ServerThread : public eq::base::Thread
00035 {
00036 public:
00037     ServerThread() {}
00038     virtual ~ServerThread() {}
00039 
00040     bool start( ServerPtr server )
00041         {
00042             EQASSERT( !_server );
00043             _server = server;
00044             return Thread::start();
00045         }
00046     
00047 protected:
00048 
00049     virtual void* run()
00050         {
00051             const bool ret = _server->run();
00052             EQASSERT( ret );
00053 
00054             _server->stopListening();
00055 
00056             EQINFO << "Server thread done, ref count " 
00057                    << _server->getRefCount() - 1 << endl;
00058             EQASSERTINFO( _server->getRefCount() == 1, _server->getRefCount( ));
00059 
00060             _server = 0;
00061             Global::clear();
00062 
00063             return reinterpret_cast< void* >( static_cast< size_t >(
00064                 ret ? EXIT_SUCCESS : EXIT_FAILURE ));
00065         }
00066 
00067 private:
00068     ServerPtr _server;    
00069 };
00070 
00071 static ServerThread _serverThread;
00072 }
00073 
00074 EQSERVER_EXPORT eq::net::ConnectionPtr eqsStartLocalServer( 
00075     const std::string& file )
00076 {
00077     if( _serverThread.isRunning( ))
00078     {
00079         EQWARN << "Only one app-local per process server allowed" << endl;
00080         return 0;
00081     }
00082 
00083     Loader    loader;
00084     ServerPtr server;
00085 
00086     if( !file.empty( ))
00087         server = loader.loadFile( file );
00088     if( !server )
00089         server = loader.parseServer( CONFIG );
00090     if( !server )
00091     {
00092         EQERROR << "Failed to load configuration" << endl;
00093         return 0;
00094     }
00095 
00096     if( !server->listen( ))
00097     {
00098         EQERROR << "Failed to setup server listener" << endl;
00099         return 0;
00100     }
00101 
00102     Loader::addOutputCompounds( server );
00103     Loader::addDestinationViews( server );
00104     Loader::addDefaultObserver( server );
00105 
00106     eq::net::ConnectionDescriptionPtr desc = new ConnectionDescription;
00107     desc->type = eq::net::CONNECTIONTYPE_PIPE;
00108 
00109     // Do not use RefPtr for easier handling
00110     eq::net::PairConnection* connection = new eq::net::PairConnection( 
00111         eq::net::Connection::create( desc ),
00112         eq::net::Connection::create( desc ));
00113 
00114     // Wrap in one RefPtr to do correct reference counting and avoid deletion
00115     eq::net::ConnectionPtr  conn = connection;
00116 
00117     if( !connection->connect( ))
00118     {
00119         EQERROR << "Failed to connect server connection" << endl;
00120         return 0;
00121     }
00122 
00123     eq::net::ConnectionPtr sibling = connection->getSibling();
00124     server->_addConnection( sibling );
00125 
00126     if( !_serverThread.start( server ))
00127     {
00128         EQERROR << "Failed to start server thread" << endl;
00129         return 0;
00130     }
00131 
00132     return conn;
00133 }
00134 
00135 EQSERVER_EXPORT void eqsJoinLocalServer()
00136 {
00137     _serverThread.join();
00138 }
Generated on Mon Aug 10 18:58:41 2009 for Equalizer 0.9 by  doxygen 1.5.8