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
00043 #include "ActHandler.h"
00044
00045 #include <poll.h>
00046 #include <sys/poll.h>
00047 #include <signal.h>
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;
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 &&
00116 m_queueOneCycleCommand.commandType != CMD_CATCH)
00117 {
00118 Log.logWithTime( 2, " previous message not processed yet; don't send" );
00119 return false;
00120 }
00121
00122
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
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 )
00146 m_queueMultipleCommands[m_iMultipleCommands++] = m_queueOneCycleCommand;
00147
00148
00149 WM->processQueuedCommands( m_queueMultipleCommands, m_iMultipleCommands );
00150
00151
00152
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;
00177 else
00178 {
00179 for( i = 0; i < m_iMultipleCommands ; i ++ )
00180 if( m_queueMultipleCommands[i].commandType == command.commandType )
00181 {
00182 m_queueMultipleCommands[i] = command;
00183 bOverwritten = true;
00184 }
00185
00186
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 )
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