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

mainCoach.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000-2003, 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 */
00030 
00050 #include"ActHandler.h"
00051 #include"SenseHandler.h"
00052 #include"BasicCoach.h"
00053 #include"Parse.h"
00054 
00055 #include<string.h>   // strcpy
00056 #ifdef WIN32
00057   #include <windows.h>  // needed for CreateThread
00058 #else
00059   #include <pthread.h>  // needed for pthread_create
00060 #endif
00061 #include<stdlib.h>   // exit
00062 
00063 extern Logger Log; 
00064 void printOptions( );
00065 
00071 int main( int argc, char * argv[] )
00072 {
00073 #ifdef WIN32
00074   HANDLE         listen, sense;
00075 #else
00076   pthread_t      listen, sense;
00077 #endif
00078   ServerSettings ss;
00079   PlayerSettings cs;
00080 
00081   // define variables for command options and initialize with default values
00082   char     strTeamName[MAX_TEAM_NAME_LENGTH] = "UvA_Trilearn";
00083   int      iPort                             = 6002;
00084   int      iMinLogLevel                      = 0 ;
00085   int      iMaxLogLevel                      = 0;
00086   char     strHost[128]                      = "localhost";
00087   double   dVersion                          = 9.3;
00088   int      iMode                             = 0;
00089   char     strFormations[128]                = "formations.conf";
00090   int      iNr                               = 0;
00091   int      iReconnect                        = -1;
00092   bool     bInfo                             = false;
00093   bool     bSuppliedLogFile                  = false;
00094   ofstream os;
00095 
00096   // read in all the command options and change the associated variables
00097   // assume every two values supplied at prompt, form a duo
00098   char * str;
00099   for( int i = 1 ; i < argc ; i = i + 2  )
00100   {
00101     // help is only option that does not have to have an argument
00102     if( i + 1 >= argc && strncmp( argv[i], "-help", 3 ) != 0 )
00103     {
00104       cout << "Need argument for option: " << argv[i] << endl;
00105       exit( 0 );
00106     }
00107     // read a command option
00108     if( argv[i][0] == '-' && strlen( argv[i] ) > 1)
00109     {
00110       switch( argv[i][1] )
00111       {
00112         case 'h':                                   // host server or help
00113           if( strlen( argv [i]) > 2 && argv[i][2] == 'e' )
00114           {
00115             printOptions( );
00116             exit(0);
00117           }
00118           else
00119             strcpy( strHost, argv[i+1] );
00120           break;
00121         case 'f':                                   // formations file
00122           strcpy( strFormations, argv[i+1] );
00123           break;
00124         case 'c':                                   // clientconf file
00125           if( cs.readValues( argv[i+1], ":" ) == false )
00126             cerr << "Error in reading client file: " << argv[i+1] << endl;
00127           break;
00128         case 'i':                                   // info 1 0
00129           str   = &argv[i+1][0];
00130           bInfo = (Parse::parseFirstInt( &str ) == 1 ) ? true : false ;
00131           break;
00132         case 'l':                                   // loglevel int[..int]
00133           str = &argv[i+1][0];
00134           iMinLogLevel = Parse::parseFirstInt( &str );
00135           while( iMinLogLevel != 0 )
00136           {
00137             if( *str == '.' ) // '.' indicates range of levels
00138             {
00139               iMaxLogLevel = Parse::parseFirstInt( &str );
00140               if( iMaxLogLevel == 0 ) iMaxLogLevel = iMinLogLevel;
00141               Log.addLogRange( iMinLogLevel, iMaxLogLevel );
00142             }
00143             else
00144               Log.addLogLevel( iMinLogLevel );
00145             iMinLogLevel = Parse::parseFirstInt( &str );
00146           }
00147           break;
00148         case 'm':                                   // mode int
00149           str = &argv[i+1][0];
00150           iMode = Parse::parseFirstInt( &str );
00151           break;
00152         case 'o':                                   // output file log info
00153           os.open( argv[i+1] );
00154           bSuppliedLogFile = true;
00155           break;
00156         case 'p':                                   // port
00157           str = &argv[i+1][0];
00158           iPort = Parse::parseFirstInt( &str );
00159           break;
00160         case 's':                                   // serverconf file
00161           if( ss.readValues( argv[i+1], ":" ) == false )
00162             cerr << "Error in reading server file: " << argv[i+1] << endl;
00163           break;
00164         case 't':                                   // teamname name
00165           strcpy( strTeamName, argv[i+1] );
00166           break;
00167         case 'v':                                   // version version
00168           str = &argv[i+1][0];
00169           dVersion = Parse::parseFirstDouble( &str );
00170           break;
00171         default:
00172           cerr << "(main) Unknown command option: " << argv[i] << endl;
00173       }
00174     }
00175   }
00176 
00177 
00178   if( bInfo == true )
00179   cout << "team         : "  << strTeamName    << endl <<
00180           "port         : "  << iPort          << endl <<
00181           "host         : "  << strHost        << endl <<
00182           "version      : "  << dVersion       << endl <<
00183           "min loglevel : "  << iMinLogLevel   << endl <<
00184           "max loglevel : "  << iMaxLogLevel   << endl <<
00185           "mode         : "  << iMode          << endl <<
00186           "playernr     : "  << iNr            << endl <<
00187           "reconnect    : "  << iReconnect     << endl ;
00188 
00189   if( bSuppliedLogFile == true )
00190     Log.setOutputStream( os );                   // initialize logger
00191   else
00192     Log.setOutputStream( cout );
00193   Log.restartTimer( );
00194   Formations fs( strFormations, (FormationT)cs.getInitialFormation(), iNr );
00195                                                // read formations file
00196   WorldModel wm( &ss, &cs, &fs );              // create worldmodel
00197   Connection c( strHost, iPort, MAX_MSG );     // make connection with server
00198   ActHandler a( &c, &wm, &ss );                // link actHandler and WM
00199   SenseHandler s( &c, &wm, &ss, &cs );         // link senseHandler with wm
00200   bool isTrainer = (iPort == ss.getCoachPort()) ? true : false;
00201   BasicCoach bp( &a, &wm, &ss, strTeamName, dVersion, isTrainer );
00202                                                // link acthandler and WM
00203 
00204 #ifdef WIN32
00205   DWORD id1;
00206   sense = CreateThread(NULL, 0, &sense_callback, &s, 0, &id1);
00207   if (sense == NULL)
00208   {
00209       cerr << "create thread error" << endl;
00210       return false;
00211   }
00212 #else
00213   pthread_create( &sense, NULL, sense_callback  , &s); // start listening
00214 #endif
00215 
00216   if( iMode > 0 && iMode < 9 )       // only listen to sdtdin when not playing
00217 #ifdef WIN32
00218   {
00219     DWORD id2;
00220     listen = CreateThread(NULL, 0, &stdin_callback, &bp, 0, &id2);
00221     if ( listen == NULL)
00222     {
00223         cerr << "create thread error" << endl;
00224         return false;
00225     }
00226   }
00227 #else
00228     pthread_create( &listen, NULL, stdin_callback, &bp);
00229 #endif
00230 
00231   if( iMode == 0 )
00232     bp.mainLoopNormal();
00233 
00234   c.disconnect();
00235   os.close();
00236 }
00237 
00238 
00241 void printOptions( )
00242 {
00243   cout << "Command options:"                                        << endl <<
00244    " c(lientconf) file     - use file as client conf file"           << endl <<
00245    " f(ormations) file     - file with formation info"               << endl <<
00246    " he(lp)                - print this information"                 << endl <<
00247    " h(ost) hostname       - host to connect with"                   << endl <<
00248    " i(nfo) 0/1            - print variables used to start"          << endl <<
00249    " l(oglevel) int[..int] - level of debug info"                    << endl <<
00250    " m(ode) int            - which mode to start up with"            << endl <<
00251    " o(utput) file         - write log info to (screen is default)"  << endl <<
00252    " p(ort)                - port number to connect with"            << endl <<
00253    " s(erverconf) file     - use file as server conf file"           << endl <<
00254    " t(eamname) name       - name of your team"                      << endl;
00255 }
00256 
00257 

Generated on Fri Nov 7 11:45:40 2003 for UvA Trilearn 2003 Base Code by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001