Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

main.C

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000-2002, Jelle Kok, University of Amsterdam
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without 
00006 modification, are permitted provided that the following conditions are met:
00007 
00008 1. Redistributions of source code must retain the above copyright notice, this 
00009 list of conditions and the following disclaimer. 
00010 
00011 2. Redistributions in binary form must reproduce the above copyright notice, 
00012 this list of conditions and the following disclaimer in the documentation 
00013 and/or other materials provided with the distribution. 
00014 
00015 3. Neither the name of the University of Amsterdam nor the names of its 
00016 contributors may be used to endorse or promote products derived from this 
00017 software without specific prior written permission. 
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00020 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00021 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00022 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
00023 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00024 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
00025 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00026 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
00027 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00028 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 */
00049 #include "SenseHandler.h"
00050 #include "Player.h"
00051 #include "Parse.h"
00052 #include <string.h>   // needed for strcpy
00053 #include <pthread.h>  // needed for pthread_create
00054 #include <stdlib.h>   // needed for exit
00055 
00056 extern Logger Log; 
00057 void printOptions( );
00058 
00063 int main( int argc, char * argv[] )
00064 {
00065   pthread_t      listen, sense;
00066   ServerSettings ss;
00067   PlayerSettings cs;
00068 
00069   // define variables for command options and initialize with default values
00070   char     strTeamName[MAX_TEAM_NAME_LENGTH] = "UvA_Trilearn";
00071   int      iPort                             = ss.getPort();
00072   int      iMinLogLevel                      = 0;
00073   int      iMaxLogLevel                      = 0;  
00074   char     strHost[128]                      = "localhost";
00075   double   dVersion                          = 8.08;
00076   int      iMode                             = 0;
00077   char     strFormations[128]                = "formations.conf";
00078   int      iNr                               = 2;
00079   int      iReconnect                        = -1;
00080   bool     bInfo                             = false;
00081   bool     bSuppliedLogFile                  = false;
00082   ofstream os;
00083 
00084   // read in all the command options and change the associated variables
00085   // assume every two values supplied at prompt, form a duo
00086   char * str;
00087   for( int i = 1 ; i < argc ; i = i + 2  )
00088   {
00089     // help is only option that does not have to have an argument
00090     if( i + 1 >= argc && strncmp( argv[i], "-help", 3 ) != 0 )
00091     {
00092       cout << "Need argument for option: " << argv[i] << endl;
00093       exit( 0 );
00094     }
00095     // read a command option
00096     if( argv[i][0] == '-' && strlen( argv[i] ) > 1)
00097     {
00098       switch( argv[i][1] )
00099       {
00100         case '?':                                   // print help
00101           printOptions( );
00102           exit(0);
00103           break;
00104         case 'c':                                   // clientconf file
00105           if( cs.readValues( argv[i+1], ":" ) == false )
00106             cerr << "Error in reading client file: " << argv[i+1] << endl;
00107           break;
00108         case 'f':                                   // formations file
00109           strcpy( strFormations, argv[i+1] );
00110           break;
00111         case 'h':                                   // host server or help
00112           if( strlen( argv [i]) > 2 && argv[i][2] == 'e' )
00113           {
00114             printOptions( );
00115             exit(0);
00116           }
00117           else
00118             strcpy( strHost, argv[i+1] );
00119           break;
00120         case 'i':                                   // info 1 0
00121           str   = &argv[i+1][0];
00122           bInfo = (Parse::parseFirstInt( &str ) == 1 ) ? true : false ;
00123           break;
00124         case 'l':                                   // loglevel int[..int]
00125           str = &argv[i+1][0];
00126           iMinLogLevel = Parse::parseFirstInt( &str );
00127           while( iMinLogLevel != 0 )
00128           {
00129             if( *str == '.' || *str == '-') // '.' or '-' indicates range 
00130             {
00131               *str++;
00132               iMaxLogLevel = Parse::parseFirstInt( &str );            
00133               if( iMaxLogLevel == 0 ) iMaxLogLevel = iMinLogLevel;
00134               Log.addLogRange( iMinLogLevel, iMaxLogLevel );
00135             }
00136             else
00137               Log.addLogLevel( iMinLogLevel );
00138             iMinLogLevel = Parse::parseFirstInt( &str );                          
00139           }
00140           break;
00141         case 'm':                                   // mode int
00142           str = &argv[i+1][0];
00143           iMode = Parse::parseFirstInt( &str );
00144           break;
00145         case 'n':                                   // number in formation int
00146           str = &argv[i+1][0];
00147           iNr = Parse::parseFirstInt( &str );
00148           break;
00149         case 'o':                                   // output file log info
00150           os.open( argv[i+1] );
00151           bSuppliedLogFile = true;
00152           break;
00153         case 'p':                                   // port
00154           str = &argv[i+1][0];
00155           iPort = Parse::parseFirstInt( &str );
00156           break;
00157         case 'r':                                   // reconnect 1 0
00158           str = &argv[i+1][0];
00159           iReconnect = Parse::parseFirstInt( &str );
00160           break;
00161         case 's':                                   // serverconf file
00162           if( ss.readValues( argv[i+1], ":" ) == false )
00163             cerr << "Error in reading server file: " << argv[i+1] << endl;
00164           break;
00165         case 't':                                   // teamname name
00166           strcpy( strTeamName, argv[i+1] );
00167           break;
00168         case 'v':                                   // version version
00169           str = &argv[i+1][0];
00170           dVersion = Parse::parseFirstDouble( &str );
00171           break;
00172         default:
00173           cerr << "(main) Unknown command option: " << argv[i] << endl;
00174       }
00175     }
00176   }
00177 
00178 
00179   if( bInfo == true )
00180     cout << "team         : "  << strTeamName    << endl <<
00181             "port         : "  << iPort          << endl <<
00182             "host         : "  << strHost        << endl <<
00183             "version      : "  << dVersion       << endl <<
00184             "mode         : "  << iMode          << endl <<
00185             "playernr     : "  << iNr            << endl <<
00186             "reconnect    : "  << iReconnect     << endl ;
00187  
00188   if( bSuppliedLogFile == true )
00189     Log.setOutputStream( os );                   // initialize logger
00190   else
00191     Log.setOutputStream( cout );  
00192   Log.restartTimer( );
00193 
00194   Formations fs( strFormations, FT_433_OFFENSIVE, iNr-1 );// read formations file
00195   WorldModel wm( &ss, &cs, &fs );              // create worldmodel
00196   Connection c( strHost, iPort, MAX_MSG );     // make connection with server
00197   ActHandler a( &c, &wm, &ss );                // link actHandler and worldmodel
00198   SenseHandler s( &c, &wm, &ss, &cs );         // link senseHandler with wm
00199   Player bp( &a, &wm, &ss, &cs, &fs, strTeamName, dVersion, iReconnect );
00200                                                // create player
00201   pthread_create( &sense, NULL, sense_callback  , &s); // start listening
00202 
00203   if( iMode > 0 ) // only listen to standard input when not playing
00204     pthread_create( &listen, NULL, stdin_callback, &bp); 
00205 
00206   if( iMode == 0 )
00207     bp.mainLoop();
00208   else if( iMode == 1 )
00209     bp.test_only_update();
00210 
00211   c.disconnect();
00212   os.close();
00213 }
00214 
00217 void printOptions( )
00218 {
00219   cout << "Command options:"                                         << endl <<
00220    " c(lientconf) file     - use file as client conf file"           << endl <<
00221    " f(ormations) file     - file with formation info"               << endl <<
00222    " he(lp)                - print this information"                 << endl <<
00223    " h(ost) hostname       - host to connect with"                   << endl <<
00224    " i(nfo) 0/1            - print variables used to start"          << endl <<
00225    " l(oglevel) int[..int] - level of debug info"                    << endl <<
00226    " m(ode) int            - which mode to start up with"            << endl <<
00227    " n(umber) int          - player number in formation"             << endl <<
00228    " o(utput) file         - write log info to (screen is default)"  << endl <<
00229    " p(ort)                - port number to connect with"            << endl <<
00230    " r(econnect) int       - reconnect as player nr"                 << endl <<
00231    " s(erverconf) file     - use file as server conf file"           << endl <<
00232    " t(eamname) name       - name of your team"                      << endl;
00233 }

Generated on Tue Jul 2 10:18:52 2002 for UvA Trilearn 2002 by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001