?? sensehandler.c
字號:
/*Copyright (c) 2000-2002, Jelle Kok, University of AmsterdamAll rights reserved.Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the University of Amsterdam nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*//*! \file SenseHandler.C<pre><b>File:</b> SenseHandler.C<b>Project:</b> Robocup Soccer Simulation Team: UvA Trilearn<b>Authors:</b> Jelle Kok<b>Created:</b> 28/11/2000<b>Last Revision:</b> $ID$<b>Contents:</b> This file contains the class SenseHandler that is used to process the information coming from the server.<hr size=2><h2><b>Changes</b></h2><b>Date</b> <b>Author</b> <b>Comment</b>28/11/2000 Jelle Kok Initial version created</pre>*/#include "SenseHandler.h"#include "ActHandler.h" // sigalarmHandler#include "Parse.h"#include <signal.h> // needed for SIGALARM#include <string.h> // needed for strlen#include <stdio.h> // needed for printf#include <iostream> // needed for cout/******************************************************************************//********************** CLASS SENSEHANDLER ************************************//******************************************************************************//*! This function is needed to start the Sense Thread (thread that continually waits for input and parses the input). This function is needed since it is not possible to call a method from a class using a thread. So this function calls handleMessagesFromServer from the SenseHandler class. \param v pointer to a SenseHandler class.*/void* sense_callback( void *v ){ Log.log( 1, "Starting to listen for server messages" ); SenseHandler* s = (SenseHandler*)v; s->handleMessagesFromServer( ); return NULL;}/*! Constructor for the SenseHandler. It needs a reference to a connection and a reference to a worldmodel. \param c Connection from which input is received \param wm WorldModel to which new information will be sent for processing \param ss ServerSettings that contain the parameters used by the server \param ps PlayerSettings that determine how to interact with messages. */SenseHandler::SenseHandler( Connection *c, WorldModel *wm, ServerSettings *ss, PlayerSettings *ps ){ connection = c; SS = ss; PS = ps; WM = wm; iSimStep = SS->getSimulatorStep()*1000; iTimeSignal = (int)(iSimStep*0.85); struct sigaction sigact; sigact.sa_flags = SA_RESTART; // primitives (recvfrom) should not be unblocked sigact.sa_handler = (void (*)(int))sigalarmHandler; sigaction( SIGALRM, &sigact, NULL ); // set timer signal to indicate when ActHandler should sent commands to the // server, this structure will later be filled with exact timing values itv.it_interval.tv_sec = 0; itv.it_interval.tv_usec = 0; itv.it_value.tv_sec = 0; itv.it_value.tv_usec = 0;}/*! This is the main routine of this class. It loops forever (till the thread is destroyed) and receives and parses the incoming messages. */void SenseHandler::handleMessagesFromServer( ){ char strBuf[MAX_MSG]; int i=0; while( 1 ) { strBuf[0]='\0'; if( i != -1 ) // if no error i = connection->receiveMessage( strBuf, MAX_MSG ); // get message if( strBuf[0] != '\0' ) // if not empty analyzeMessage( strBuf ); // parse message }}/*! This method sets the time signal. This is the time that should be waited before the next action should be sent to the server. As soon as a sense message arrives this method is called. Using the information from the member variable 'iTriCounter' which denotes when the see message will arrive in this cycle (0=first half, 1=2nd half, 2=no see, all for the default view frequency) the timer is set. The values that denote the fraction of the simulation step that is waited are all defined in PlayerSettings, such that they can be easily changed. */void SenseHandler::setTimeSignal( ){ if( WM->getAgentViewFrequency() == 1.0 ) // VA_NORMAL AND VQ_HIGH (default) { if( iTriCounter % 3 == 0 ) // see will arrive first half cycle { iTimeSignal = (int)(iSimStep * PS->getFractionWaitSeeBegin() ); iTriCounter = 0; } else if( iTriCounter % 3 == 1 ) // see will arrive 2nd half of cycle { iTimeSignal = (int)(iSimStep * PS->getFractionWaitSeeEnd() ); } else // no see will arrive iTimeSignal = (int)(iSimStep * PS->getFractionWaitNoSee( ) ); } else if( WM->getAgentViewFrequency() == 2.0 ) // VA_WIDE AND VQ_HIGH { if( iTriCounter % 3 == 0 ) // see will arrive { iTimeSignal = (int)(iSimStep * PS->getFractionWaitSeeEnd() ); iTriCounter = 0; } else // no see will arrive iTimeSignal = (int)(iSimStep * PS->getFractionWaitNoSee() ); } else // VA_NARROW AND VQ_HIGH iTimeSignal = (int)(iSimStep * PS->getFractionWaitSeeEnd() ); iTriCounter++; itv.it_value.tv_usec = iTimeSignal; setitimer( ITIMER_REAL, &itv, NULL );}/*! This method analyzes the type of the incoming message and calls the message that corresponds to this message. \param strMsg message that should be parsed. \return bool indicating whether the message was parsed or not */bool SenseHandler::analyzeMessage( char *strMsg ){ switch( strMsg[1] ) { case 'c': return analyzeChangePlayerTypeMessage( strMsg ); // ( c hange_ case 'o': // ( o k if( strlen(strMsg) > 14 && strMsg[4] == 'c' && strMsg[10] == 'b' ) analyzeCheckBall( strMsg ); // (ok check_ball return true; case 's': { switch( strMsg[3] ) { case 'e': if( strMsg[5] == 'g' ) return analyzeSeeGlobalMessage ( strMsg ); // (se e_g else return analyzeSeeMessage ( strMsg ); // (se e case 'n': return analyzeSenseMessage ( strMsg ); // (se n se case 'r': return analyzeServerParamMessage( strMsg ); // (se r ver_param default : break; } } case 'i': return analyzeInitMessage ( strMsg ); // ( i nit case 'h': return analyzeHearMessage ( strMsg ); // ( h ear case 'p': return ( strMsg[8] == 't') ? analyzePlayerTypeMessage ( strMsg ) // (player_ t ype : analyzePlayerParamMessage( strMsg ); // (player_ p aram case 'e': printf("%s", strMsg); // ( error default: cerr << "(" << WM->getCurrentTime() << ", " << WM->getPlayerNumber() << ") (SenseHandler::analyzeMessage) " << "ignored message: " << strMsg << endl; return false; } return false;}/*! This method analyzes a see Message. It gets the time from the message and tries to synchronize with the server. Then the message is stored in the world model, which processes it when it performs an update. \return bool indicating whether the message was parsed correctly. */bool SenseHandler::analyzeSeeMessage( char *strMsg ){ // store the message for later processing strcpy( WM->strLastSeeMessage, strMsg ); Log.logWithTime( 2, " incoming see message" ); Log.logWithTime( 2, " %s",strMsg ); if( WM->getRelativeDistance( OBJECT_BALL ) < SS->getVisibleDistance() ) Log.logWithTime( 560, "%s", WM->strLastSeeMessage ); Time time = WM->getTimeLastSenseMessage(); int iTime = Parse::parseFirstInt( &strMsg ); // get the time if( time.getTime() != iTime ) { cerr << "(SenseHandler:analyzeSeeMessage) see with different time as sense:" << time.getTime() << " vs. " << iTime << endl; return false; } // count number of see message in this cycle if( WM->getTimeLastSeeMessage() == time ) m_iSeeCounter++; else m_iSeeCounter = 1; // do nothing with second see, since it adds nothings if( m_iSeeCounter >= 2 ) { Log.logWithTime( 4, "second see message in cycle; do nothing " ); return true; } // reset the send pattern when previous cycle(s) no see arrived if( WM->getAgentViewFrequency() == 1.0 && // VA_NORMAL; previous cycle no see time.getTimeDifference( WM->getTimeLastSeeMessage() )== 2 ) iTriCounter = 1; // see will arive in 2nd half in next cycle else if( WM->getAgentViewFrequency() == 2.0 && // VA_WIDE; two cycles no see time.getTimeDifference( WM->getTimeLastSeeMessage() ) == 3 ) iTriCounter = 1; // no see will arrive next two cycles WM->setTimeLastSeeMessage( time ); // set time of last see message // this will activate main thread return true;}/*! This method analyzes a see Message. All information from the different objects that is stored in a see message is send to worldmodel. A see message looks like(see 0 ((g r) 64.1 13) ((f r t) 65.4 -16) ....
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -