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,2001, 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 */
00045 #include "SenseHandler.h"
00046 #include "Player.h"
00047 #include "Parse.h"
00048 #include <string.h>   // needed for strcpy
00049 #include <pthread.h>  // needed for pthread_create
00050 #include <stdlib.h>   // needed for exit
00051 
00052 extern Logger Log; 
00053 void   printOptions( );
00054 
00059 int main( int argc, char * argv[] )
00060 {
00061   pthread_t      listen, sense;
00062   ServerSettings ss;
00063   PlayerSettings ps;
00064 
00065   // define variables for command options and initialize with default values
00066   char     strTeamName[MAX_TEAM_NAME_LENGTH] = "UvA_Trilearn";
00067   int      iPort                             = ss.getPort();
00068   int      iMinLogLevel                      = 0;
00069   int      iMaxLogLevel                      = 0;
00070   char     strHost[128]                      = "localhost";
00071   double   dVersion                          = 7.04;
00072   int      iMode                             = 0;
00073   char     strFormations[128]                = "formations.conf";
00074   int      iNr                               = 1;
00075   int      iReconnect                        = -1;
00076   bool     bInfo                             = false;
00077   bool     bSuppliedLogFile                  = false;
00078   ofstream os;
00079 
00080   // read in all the command options and change the associated variables
00081   // assume every two values supplied at prompt, form a duo
00082   char * str;
00083   for( int i = 1 ; i < argc ; i = i + 2  )
00084   {
00085     // help is only option that does not have to have an argument
00086     if( i + 1 >= argc && strncmp( argv[i], "-help", 3 ) != 0 )
00087     {
00088       cout << "Need argument for option: " << argv[i] << endl;
00089       exit( 0 );
00090     }
00091     // read a command option
00092     if( argv[i][0] == '-' && strlen( argv[i] ) > 1)
00093     {
00094       switch( argv[i][1] )
00095       {
00096         case '?':                                   // print help
00097           printOptions( );
00098           exit(0);
00099           break;
00100         case 'c':                                   // clientconf file
00101           if( ps.readValues( argv[i+1], ":" ) == false )
00102             cerr << "Error in reading client file: " << argv[i+1] << endl;
00103           break;
00104         case 'f':                                   // formations file
00105           strcpy( strFormations, argv[i+1] );
00106           break;
00107         case 'h':                                   // host server or help
00108           if( strlen( argv [i]) > 2 && argv[i][2] == 'e' )
00109           {
00110             printOptions( );
00111             exit(0);
00112           }
00113           else
00114             strcpy( strHost, argv[i+1] );
00115           break;
00116         case 'i':                                   // info 1 0
00117           str   = &argv[i+1][0];
00118           bInfo = (Parse::parseFirstInt( &str ) == 1 ) ? true : false ;
00119           break;
00120         case 'l':                                   // loglevel int[..int]
00121           str = &argv[i+1][0];
00122           iMinLogLevel = iMaxLogLevel = Parse::parseFirstInt( &str );
00123           iMaxLogLevel = Parse::parseFirstInt( &str );
00124           if( iMaxLogLevel == 0 && iMinLogLevel != 0 )
00125             iMaxLogLevel = iMinLogLevel; // if no max is supplied
00126           break;
00127         case 'm':                                   // mode int
00128           str = &argv[i+1][0];
00129           iMode = Parse::parseFirstInt( &str );
00130           break;
00131         case 'n':                                   // number in formation int
00132           str = &argv[i+1][0];
00133           iNr = Parse::parseFirstInt( &str );
00134           break;
00135         case 'o':                                   // output file log info
00136           os.open( argv[i+1] );
00137           bSuppliedLogFile = true;
00138           break;
00139         case 'p':                                   // port
00140           str = &argv[i+1][0];
00141           iPort = Parse::parseFirstInt( &str );
00142           break;
00143         case 'r':                                   // reconnect 1 0
00144           str = &argv[i+1][0];
00145           iReconnect = Parse::parseFirstInt( &str );
00146           break;
00147         case 's':                                   // serverconf file
00148           if( ss.readValues( argv[i+1], ":" ) == false )
00149             cerr << "Error in reading server file: " << argv[i+1] << endl;
00150           break;
00151         case 't':                                   // teamname name
00152           strcpy( strTeamName, argv[i+1] );
00153           break;
00154         case 'v':                                   // version version
00155           str = &argv[i+1][0];
00156           dVersion = Parse::parseFirstDouble( &str );
00157           break;
00158         default:
00159           cerr << "(main) Unknown command option: " << argv[i] << endl;
00160       }
00161     }
00162   }
00163 
00164 
00165   if( bInfo == true )
00166     cout << "team         : "  << strTeamName    << endl <<
00167             "port         : "  << iPort          << endl <<
00168             "host         : "  << strHost        << endl <<
00169             "version      : "  << dVersion       << endl <<
00170             "min loglevel : "  << iMinLogLevel   << endl <<
00171             "max loglevel : "  << iMaxLogLevel   << endl <<
00172             "mode         : "  << iMode          << endl <<
00173             "playernr     : "  << iNr            << endl <<
00174             "reconnect    : "  << iReconnect     << endl ;
00175  
00176   if( bSuppliedLogFile == true )
00177     Log.setOutputStream( os );                   // initialize logger
00178   else
00179     Log.setOutputStream( cout );  
00180   Log.setMinLogLevel( iMinLogLevel );
00181   Log.setMaxLogLevel( iMaxLogLevel );
00182   Log.restartTimer( );
00183   
00184   Formations fs( strFormations, FT_INITIAL, iNr-1 );// read formations file
00185   WorldModel wm( &ss, &ps );                   // create worldmodel
00186   Connection c( strHost, iPort, MAX_MSG );     // make connection with server
00187   ActHandler a( &c, &wm, &ss );                // link actHandler and worldmodel
00188   SenseHandler s( &c, &wm, &ss, &ps );         // link senseHandler with wm
00189   Player p( &a, &wm, &ss, &ps, &fs, strTeamName, dVersion, iReconnect );
00190                                                // create player
00191 
00192   pthread_create( &sense, NULL, sense_callback  , &s); // start listening
00193 
00194   if( iMode > 0)    // only listen to standard input when not playing match
00195     pthread_create( &listen, NULL, stdin_callback, &p); 
00196 
00197   if( iMode == 0 )
00198     p.mainLoop();
00199   else if( iMode == 1 )
00200     p.test_only_update();
00201 
00202   c.disconnect();
00203   os.close();
00204 }
00205 
00208 void printOptions( )
00209 {
00210   cout << "Command options:"                                         << endl <<
00211    " c(lientconf) file     - use file as client conf file"           << endl <<
00212    " f(ormations) file     - file with formation info"               << endl <<
00213    " he(lp)                - print this information"                 << endl <<
00214    " h(ost) hostname       - host to connect with"                   << endl <<
00215    " i(nfo) 0/1            - print variables used to start"          << endl <<
00216    " l(oglevel) int[..int] - level of debug info"                    << endl <<
00217    " m(ode) int            - which mode to start up with"            << endl <<
00218    " n(umber) int          - player number in formation"             << endl <<
00219    " o(utput) file         - write log info to (screen is default)"  << endl <<
00220    " p(ort)                - port number to connect with"            << endl <<
00221    " r(econnect) int       - reconnect as player nr"                 << endl <<
00222    " s(erverconf) file     - use file as server conf file"           << endl <<
00223    " t(eamname) name       - name of your team"                      << endl;
00224 }

Generated on Thu Mar 7 00:37:42 2002 for UvA Trilearn 2001 by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001