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

ActHandler.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 */
00043 #include "ActHandler.h"
00044 
00045 #include <poll.h>       // poll
00046 #include <sys/poll.h>   // poll
00047 #include <signal.h>     // SIGALARM
00048 
00049 ActHandler* ACT; 
00057 extern void sigalarmHandler( int i )
00058 {
00059   ACT->sendCommands( );
00060 }
00061 
00067 ActHandler::ActHandler( Connection *c, WorldModel *wm, ServerSettings *ss )
00068 {
00069   connection          = c;
00070   SS                  = ss;
00071   WM                  = wm;
00072 
00073   m_iMultipleCommands = 0;
00074   ACT                 = this; // needed to let signal call method from class
00075 }
00076 
00078 void ActHandler::emptyQueue( )
00079 {
00080   m_queueOneCycleCommand.commandType = CMD_ILLEGAL;
00081   for( int i = 0; i < MAX_COMMANDS - 1 ; i ++ )
00082     m_queueMultipleCommands[i].commandType = CMD_ILLEGAL;
00083   m_iMultipleCommands=0;
00084 }
00085 
00088 bool ActHandler::isQueueEmpty()
00089 {
00090   return m_queueOneCycleCommand.commandType == CMD_ILLEGAL &&
00091          m_iMultipleCommands                == 0;
00092 }
00093 
00102 bool ActHandler::sendCommands( )
00103 {
00104   static Time timeLastSent = -1;
00105   bool        bNoOneCycle  = false;
00106   char        strCommand[MAX_MSG];
00107   strCommand[0]            = '\0';
00108 
00109   if( WM->getCurrentTime() == timeLastSent )
00110   {
00111     Log.logWithTime( 2, " already sent message; don't send" );
00112     return false;
00113   }
00114 
00115   if(WM->isQueuedActionPerformed()    == false &&    // don't send action when
00116      m_queueOneCycleCommand.commandType != CMD_CATCH) // previous one is not
00117   {                                                   // not processed yet
00118     Log.logWithTime( 2, " previous message not processed yet; don't send" );
00119     return false;                                     // except with catch since
00120   }                                                   // too important
00121 
00122   // make string of primary action and send it to server (if there was any)
00123   m_queueOneCycleCommand.getCommandString( strCommand, SS );
00124   if( strCommand[0] != '\0' )
00125   {
00126     connection->sendMessage( strCommand );
00127     Log.logWithTime( 2, " send queued action to server: %s", strCommand);
00128   }
00129   else
00130   {
00131     bNoOneCycle = true;
00132     Log.logWithTime( 2, " no primary action in queue" );
00133   }
00134 
00135   // make strings of all other commands and send them to server
00136   for( int i = 0; i < m_iMultipleCommands ; i ++ )
00137   {
00138     m_queueMultipleCommands[i].getCommandString( strCommand, SS );
00139     if( strCommand[0] != '\0' )
00140     {
00141       connection->sendMessage( strCommand );
00142       Log.logWithTime( 2, " send queued action to server: %s", strCommand);
00143     }
00144   }
00145   if( ! bNoOneCycle ) // if primary action was send, put it at end of array
00146     m_queueMultipleCommands[m_iMultipleCommands++] = m_queueOneCycleCommand;
00147 
00148   // let worldmodel know which commands were sent to the server
00149   WM->processQueuedCommands( m_queueMultipleCommands, m_iMultipleCommands );
00150 
00151   // decrease amount of times primary action still has to be sent, if 0 delete
00152   // it, furthermore set number of multiple commands to zero
00153   if( --m_queueOneCycleCommand.iTimes  == 0 )
00154     m_queueOneCycleCommand.commandType = CMD_ILLEGAL;
00155 
00156   for( int i = 0; i < MAX_COMMANDS; i++ )
00157     m_queueMultipleCommands[i].commandType = CMD_ILLEGAL;
00158   m_iMultipleCommands = 0;
00159   timeLastSent        = WM->getCurrentTime();
00160   return true;
00161 }
00162 
00168 bool ActHandler::putCommandInQueue( SoccerCommand command )
00169 {
00170   int i = 0;
00171   bool bOverwritten = false;
00172   
00173   if( command.commandType == CMD_ILLEGAL )
00174     return false;
00175   if( SoccerTypes::isPrimaryCommand( command.commandType ) )
00176     m_queueOneCycleCommand = command;           // overwrite primary command
00177   else                                          // non-primary command
00178   {
00179     for( i = 0; i < m_iMultipleCommands ; i ++ )
00180       if( m_queueMultipleCommands[i].commandType == command.commandType )
00181       {
00182         m_queueMultipleCommands[i] = command;   // if command already in queue
00183         bOverwritten = true;                    // overwrite it
00184       }
00185 
00186     // 1 less to save space for primary command
00187     if( bOverwritten == false && m_iMultipleCommands == MAX_COMMANDS-1 ) 
00188     {
00189       cerr << "(ActHandler::putCommandInQueue) too many commands" << endl;
00190       return false;
00191     }
00192     if( bOverwritten == false  ) // add it when command was not yet in queue
00193       m_queueMultipleCommands[m_iMultipleCommands++] = command;          
00194   }
00195 
00196   return true;
00197 }
00198 
00204 bool ActHandler::sendCommand( SoccerCommand soc )
00205 {
00206   char strCommand[MAX_MSG];
00207   soc.getCommandString( strCommand, SS );
00208   return sendMessage( strCommand );
00209 }
00210 
00216 bool ActHandler::sendMessage( char * str )
00217 {
00218   emptyQueue( );
00219   poll( 0, 0, SS->getSimulatorStep() );
00220 
00221   bool bReturn = connection->sendMessage( str );
00222   Log.logWithTime( 2, " send message to server and wait: %s", str);
00223 
00224   poll( 0, 0, SS->getSimulatorStep() );
00225   return bReturn;
00226 }
00227 
00233 bool ActHandler::sendCommandDirect( SoccerCommand soc )
00234 {
00235   char strCommand[MAX_MSG];
00236   soc.getCommandString( strCommand, SS );
00237   return sendMessageDirect( strCommand );
00238 }
00239 
00243 bool ActHandler::sendMessageDirect( char * str )
00244 {
00245   bool bReturn = connection->sendMessage( str );
00246   Log.logWithTime( 2, " send message to server directly: %s", str);
00247   return bReturn;
00248 }
00249 

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