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

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

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