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

ActHandler.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 
00048 #include "ActHandler.h"
00049 
00050 #ifndef WIN32
00051   #include <poll.h>     // poll
00052   #include <sys/poll.h> // poll
00053 #endif
00054 #include <signal.h>     // SIGALARM
00055 
00056 ActHandler* ACT; 
00064 #ifdef WIN32
00065 extern void CALLBACK sigalarmHandler(UINT , UINT , DWORD , DWORD , DWORD )
00066 #else
00067 extern void sigalarmHandler( int i )
00068 #endif
00069 {
00070   Log.logFromSignal( 2, "alarm handler!!" );
00071   ACT->sendCommands( );
00072 }
00073 
00079 ActHandler::ActHandler( Connection *c, WorldModel *wm, ServerSettings *ss )
00080 {
00081   connection          = c;
00082   SS                  = ss;
00083   WM                  = wm;
00084 
00085   m_iMultipleCommands = 0;
00086   ACT                 = this; // needed to let signal call method from class
00087 }
00088 
00090 void ActHandler::emptyQueue( )
00091 {
00092   m_queueOneCycleCommand.commandType = CMD_ILLEGAL;
00093   for( int i = 0; i < CMD_MAX_COMMANDS - 1 ; i ++ )
00094     m_queueMultipleCommands[i].commandType = CMD_ILLEGAL;
00095   m_iMultipleCommands=0;
00096 }
00097 
00100 bool ActHandler::isQueueEmpty()
00101 {
00102   return m_queueOneCycleCommand.commandType == CMD_ILLEGAL &&
00103          m_iMultipleCommands                == 0;
00104 }
00105 
00114 bool ActHandler::sendCommands( )
00115 {
00116   static Time timeLastSent = -1;
00117   bool        bNoOneCycle  = false;
00118   char        strCommand[MAX_MSG];
00119   strCommand[0]            = '\0';
00120 
00121   if( WM->getCurrentTime() == timeLastSent )
00122   {
00123     Log.logFromSignal( 2, " already sent message; don't send" );
00124     return false;
00125   }
00126 
00127   if(  WM->isQueuedActionPerformed()    == false &&   // don't send action when
00128      m_queueOneCycleCommand.commandType != CMD_CATCH && // previous one is not
00129      WM->isFullStateOn() == false )                     // fullstate sense not
00130   {                                                     // not processed yet
00131     Log.logFromSignal( 2, " previous message not processed yet; don't send" );
00132     return false;                                    // except with catch since
00133   }                                                  // too important
00134 
00135   // make string of primary action and put it in 'strCommand' variable
00136   bool bReturn = m_queueOneCycleCommand.getCommandString( strCommand, SS );
00137 
00138   if( bReturn == false )
00139     cerr << WM->getCurrentCycle() << ", " <<  WM->getPlayerNumber() << " " 
00140          << "Acthandler::failed to create primary command string" << endl;
00141 
00142   if( strCommand[0] == '\0' )
00143   {
00144     bNoOneCycle = true;
00145     Log.logFromSignal( 2, " no primary action in queue" );
00146   }
00147 
00148   // make string of all other commands and add them to the end of 'strCommand'
00149   for( int i = 0; i < m_iMultipleCommands ; i ++ )
00150   {
00151     bReturn = m_queueMultipleCommands[i].getCommandString(
00152                                          &strCommand[strlen(strCommand)], SS );
00153     if( bReturn == false )
00154       cerr << WM->getCurrentCycle() << ", " <<  WM->getPlayerNumber() << " "
00155          << "Acthandler::failed to create secondary command string " <<
00156         m_queueMultipleCommands[i].commandType <<  endl;
00157   }
00158 
00159   char strComm[MAX_SAY_MSG];
00160   strcpy( strComm, WM->getCommunicationString() );
00161   if( strlen( strComm ) != 0 )
00162   {
00163     sprintf( &strCommand[strlen(strCommand)], "(say \"%s\")", strComm );
00164     WM->setCommunicationString( "" );
00165   }
00166 
00167   // send the string to the server (example string: (dash 100)(turn_neck -19))
00168   if( strCommand[0] != '\0' )
00169   {
00170     timeLastSent        = WM->getCurrentTime();
00171     connection->sendMessage( strCommand );
00172     Log.logFromSignal( 2, " send queued action to server: %s", strCommand);
00173   }
00174   else
00175   {
00176     Log.logFromSignal( 2, " no action in queue??" );
00177     return false;
00178   }
00179 
00180   if( ! bNoOneCycle ) // if primary action was send, place it at end of array
00181     m_queueMultipleCommands[m_iMultipleCommands++] = m_queueOneCycleCommand;
00182 
00183   // let worldmodel know which commands were sent to the server
00184   WM->processQueuedCommands( m_queueMultipleCommands, m_iMultipleCommands );
00185   m_iMultipleCommands = 0;
00186 
00187   // decrease amount of times primary action still has to be sent, if 0 delete
00188   // it, furthermore set number of multiple commands to zero
00189   if( --m_queueOneCycleCommand.iTimes  == 0 )
00190     m_queueOneCycleCommand.commandType = CMD_ILLEGAL;
00191 
00192   for( int i = 0; i < CMD_MAX_COMMANDS; i++ )
00193     m_queueMultipleCommands[i].commandType = CMD_ILLEGAL;
00194 
00195   return true;
00196 }
00197 
00198 
00201 SoccerCommand ActHandler::getPrimaryCommand(  )
00202 {
00203   return m_queueOneCycleCommand;
00204 }
00205 
00211 bool ActHandler::putCommandInQueue( SoccerCommand command )
00212 {
00213   int i;
00214   bool bOverwritten = false;
00215 
00216   if( command.commandType == CMD_ILLEGAL )
00217     return false;
00218   if( SoccerTypes::isPrimaryCommand( command.commandType ) )
00219     m_queueOneCycleCommand = command;           // overwrite primary command
00220   else                                          // non-primary command
00221   {
00222     for( i = 0; i < m_iMultipleCommands ; i ++ )
00223       if( m_queueMultipleCommands[i].commandType == command.commandType )
00224       {
00225         m_queueMultipleCommands[i] = command;   // if command already in queue
00226         bOverwritten = true;                    // overwrite it
00227       }
00228 
00229     // 1 less to save space for primary command
00230     if( bOverwritten == false && m_iMultipleCommands == CMD_MAX_COMMANDS-1 )
00231     {
00232       cerr << "(ActHandler::putCommandInQueue) too many commands" << endl;
00233       return false;
00234     }
00235     if( bOverwritten == false  ) // add it when command was not yet in queue
00236       m_queueMultipleCommands[m_iMultipleCommands++] = command;
00237   }
00238 
00239   return true;
00240 }
00241 
00247 bool ActHandler::sendCommand( SoccerCommand soc )
00248 {
00249   char strCommand[MAX_MSG];
00250   soc.getCommandString( strCommand, SS );
00251   return sendMessage( strCommand );
00252 }
00253 
00259 bool ActHandler::sendMessage( char * str )
00260 {
00261   emptyQueue( );
00262 #ifdef WIN32
00263   Sleep( SS->getSimulatorStep() );
00264 #else
00265   poll( 0, 0, SS->getSimulatorStep() );
00266 #endif
00267 
00268   bool bReturn = connection->sendMessage( str );
00269   Log.logFromSignal( 2, " send message to server and wait: %s", str);
00270 
00271 #ifdef WIN32
00272   Sleep( SS->getSimulatorStep() );
00273 #else
00274   poll( 0, 0, SS->getSimulatorStep() );
00275 #endif
00276   return bReturn;
00277 }
00278 
00284 bool ActHandler::sendCommandDirect( SoccerCommand soc )
00285 {
00286   char strCommand[MAX_MSG];
00287   if( soc.commandType == CMD_ILLEGAL )
00288     return false;
00289   soc.getCommandString( strCommand, SS );
00290   return sendMessageDirect( strCommand );
00291 }
00292 
00296 bool ActHandler::sendMessageDirect( char * str )
00297 {
00298   bool bReturn = connection->sendMessage( str );
00299   Log.logFromSignal( 2, " send message to server directly: %s", str);
00300   return bReturn;
00301 }
00302 

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