00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00048 #include "ActHandler.h"
00049
00050 #ifndef WIN32
00051 #include <poll.h>
00052 #include <sys/poll.h>
00053 #endif
00054 #include <signal.h>
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;
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 &&
00128 m_queueOneCycleCommand.commandType != CMD_CATCH &&
00129 WM->isFullStateOn() == false )
00130 {
00131 Log.logFromSignal( 2, " previous message not processed yet; don't send" );
00132 return false;
00133 }
00134
00135
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
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
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 )
00181 m_queueMultipleCommands[m_iMultipleCommands++] = m_queueOneCycleCommand;
00182
00183
00184 WM->processQueuedCommands( m_queueMultipleCommands, m_iMultipleCommands );
00185 m_iMultipleCommands = 0;
00186
00187
00188
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;
00220 else
00221 {
00222 for( i = 0; i < m_iMultipleCommands ; i ++ )
00223 if( m_queueMultipleCommands[i].commandType == command.commandType )
00224 {
00225 m_queueMultipleCommands[i] = command;
00226 bOverwritten = true;
00227 }
00228
00229
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 )
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