examples/eqNBody/client.cpp

00001 /*
00002  * Copyright (c) 2009, Philippe Robert <probert@eyescale.ch> 
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 "client.h"
00019 
00020 #include "config.h"
00021 #include "localInitData.h"
00022 
00023 #include <stdlib.h>
00024 
00025 using namespace std;
00026 
00027 namespace eqNbody
00028 {
00029     
00030     Client::Client( const LocalInitData& initData ) : _initData( initData )
00031     {
00032         config = NULL;
00033     }
00034     
00035     int Client::init()
00036     {
00037         EQASSERT(config == NULL);
00038         
00039         // 1. connect to server
00040         server = new eq::Server;
00041         if( !connectServer( server ))
00042         {
00043             EQERROR << "Can't open server" << endl;
00044             return EXIT_FAILURE;
00045         }
00046         
00047         // 2. choose config
00048         eq::ConfigParams configParams;
00049         config = static_cast<Config*>(server->chooseConfig( configParams ));
00050         
00051         if( !config )
00052         {
00053             EQERROR << "No matching config on server" << endl;
00054             disconnectServer( server );
00055             return EXIT_FAILURE;
00056         }
00057         
00058         // 3. init config
00059         config->setInitData( _initData );
00060         if( !config->init() )
00061         {
00062             EQERROR << "Config initialization failed: " << config->getErrorMessage() << endl;
00063             server->releaseConfig( config );
00064             disconnectServer( server );
00065             return EXIT_FAILURE;
00066         }
00067 
00068         return EXIT_SUCCESS;
00069     }
00070     
00071     int Client::exit()
00072     {
00073         EQASSERT(config != NULL);
00074 
00075         // Exit config
00076         config->exit();
00077         
00078         // Cleanup
00079         server->releaseConfig( config );
00080         if( !disconnectServer( server )) {
00081             EQERROR << "Client::disconnectServer failed" << endl;
00082             return EXIT_FAILURE;
00083         }
00084         
00085         server = 0;
00086         return EXIT_SUCCESS;
00087     }
00088 
00089     void Client::run()
00090     {       
00091         // Run main loop
00092         while( config->isRunning( ) )
00093         {
00094             config->startFrame();
00095             config->finishFrame();
00096             
00097             if( !config->needsRedraw()) {
00098                 config->finishAllFrames();
00099             }
00100             
00101             config->handleEvents(); // process all pending events
00102         }               
00103     }
00104     
00105     bool Client::clientLoop()
00106     {
00107         while( true ) // TODO: implement SIGHUP handler to exit?
00108         {
00109             if( !eq::Client::clientLoop( ))
00110                 return false;
00111             EQINFO << "One configuration run successfully executed" << endl;
00112         }
00113         return true;
00114     }
00115     
00116 }
Generated on Mon Aug 10 18:58:31 2009 for Equalizer 0.9 by  doxygen 1.5.8