Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Tools/Process.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file Process.cpp
    00003 *
    00004 * Implementation of class Process.
    00005 */
    00006
    00007 #define USE_GETMESSAGEIDNAME
    00008 #include "Tools/Process.h"
    00009 #include "Tools/Debugging/Debugging.h"
    00010
    00011 GT_GLOBAL unsigned long* processFrameNumberPtr;
    00012
    00013 Process::Process(MessageQueue& debugIn, MessageQueue& debugOut)
    00014 : debugIn(debugIn), debugOut(debugOut)
    00015 , maxRunTime(0), minRunTime(0xffffffff), averageRunTime(0), frameCounter(0)
    00016 {
    00017   // cannot initialize debugIn/debugOut here, because they have not yet been constructed
    00018   // But there addresses can already be stored
    00019   // Note: this initialization is only temporary
    00020   initDebugging(&debugOut.out, &debugKeyTable, &debugRequestTable);
    00021   initStreamHandler( &streamHandler);
    00022   initDebugDataTable( &debugDataTable);
    00023 #ifdef NEWDEBUGGING
    00024   initDrawingManager(&drawingManager);
    00025 #endif
    00026   setRobotConfiguration(&robotConfiguration);
    00027   setPlayer(&player);
    00028
    00029   // read the player.cfg and robot.cfg
    00030   player.load();
    00031   getRobotConfiguration().load();
    00032
    00033   initialized = false;
    00034   frameNumber = 0;
    00035   processFrameNumberPtr = & frameNumber;
    00036 }
    00037
    00038 int Process::processMain()
    00039 {
    00040   unsigned long time = SystemCall::getCurrentSystemTime();
    00041   if(!initialized)
    00042   {
    00043     // Now we can be in a different thread, so repeat initialization
    00044     initDebugging(&debugOut.out, &debugKeyTable, &debugRequestTable);
    00045     initStreamHandler(&streamHandler);
    00046     initDebugDataTable( &debugDataTable);
    00047 #ifdef NEWDEBUGGING
    00048     initDrawingManager(&drawingManager);
    00049 #endif
    00050     processFrameNumberPtr = & frameNumber;
    00051
    00052     setRobotConfiguration(&robotConfiguration);
    00053     setPlayer(&player);
    00054
    00055     debugIn.setPlayerForNewMessages(getPlayer()); // This is not needed except in the Win32 version of OVirtualRobotComm
    00056
    00057     init();
    00058     initialized = true;
    00059   }
    00060   // team color can change, so set it always
    00061   debugOut.setPlayerForNewMessages(getPlayer());
    00062
    00063 #ifndef NDEBUG
    00064   debugIn.handleAllMessages(*this);
    00065   debugIn.clear();
    00066   debugKeyTable.activate();
    00067 #endif
    00068
    00069 int toReturn = this->main();
    00070
    00071 #ifndef NDEBUG
    00072   if(this->debugRequestTable.poll)
    00073   {
    00074     if(this->debugRequestTable.pollCounter++ > 10)
    00075     {
    00076       this->debugRequestTable.poll = false;
    00077       OUTPUT(idDebugResponse, text, "pollingFinished");
    00078     }
    00079   }
    00080 #endif
    00081   DEBUG_RESPONSE("automated requests:ModuleSolutionTable",
    00082     int numOfModules = 0;
    00083     for(int moduleIndex = 0; moduleIndex < SolutionRequest::numOfModules; moduleIndex++)
    00084       if(moduleHandler.pGetModuleSelector((SolutionRequest::ModuleID)moduleIndex) != 0)
    00085         ++numOfModules;
    00086     getDebugOut().bin << numOfModules;
    00087   for(int moduleIndex = 0; moduleIndex < SolutionRequest::numOfModules; moduleIndex++)
    00088   {
    00089     if(moduleHandler.pGetModuleSelector((SolutionRequest::ModuleID)moduleIndex) != 0)
    00090     {
    00091       getDebugOut().bin << SolutionRequest::getModuleName((SolutionRequest::ModuleID)moduleIndex);
    00092       getDebugOut().bin << SolutionRequest::getNumOfSolutions((SolutionRequest::ModuleID)moduleIndex);
    00093       for(int solutionIndex = 0;  solutionIndex < SolutionRequest::getNumOfSolutions((SolutionRequest::ModuleID)moduleIndex); solutionIndex++)
    00094       {
    00095         getDebugOut().bin << SolutionRequest::getModuleSolutionName((SolutionRequest::ModuleID)moduleIndex, (SolutionRequest::ModuleSolutionID)solutionIndex);
    00096       }
    00097       getDebugOut().bin << SolutionRequest::getModuleSolutionName((SolutionRequest::ModuleID)moduleIndex, SolutionRequest::getDefaultSolution((SolutionRequest::ModuleID)moduleIndex));
    00098       getDebugOut().bin << SolutionRequest::getModuleSolutionName((SolutionRequest::ModuleID)moduleIndex, moduleHandler.getSelectedSolution((SolutionRequest::ModuleID)moduleIndex));
    00099       getDebugOut().bin << SolutionRequest::getModuleCategoryName((SolutionRequest::ModuleID)moduleIndex);
    00100     }
    00101   }
    00102   getDebugOut().finishMessage(idModuleSolutionTable);
    00103   );
    00104
    00105   time = SystemCall::getTimeSince(time);
    00106   if (time > maxRunTime)
    00107   {
    00108     maxRunTime = time;
    00109   }
    00110   if (time < minRunTime)
    00111   {
    00112     minRunTime = time;
    00113   }
    00114   averageRunTime = averageRunTime*frameCounter + time;
    00115   frameCounter++;
    00116   averageRunTime /= frameCounter;
    00117   return toReturn;
    00118 }
    00119
    00120 bool Process::handleMessage(InMessage& message)
    00121 {
    00122   switch (message.getMessageID())
    00123   {
    00124   case idDebugKeyTable:
    00125     message.bin >> debugKeyTable;
    00126     return true;
    00127   case idDebugRequest:
    00128     {
    00129       DebugRequest debugRequest;
    00130       message.bin >> debugRequest;
    00131       debugRequestTable.addRequest(debugRequest);
    00132       return true;
    00133     }
    00134   case idSolutionRequest:
    00135     {
    00136       SolutionRequest request;
    00137       message.bin >> request;
    00138       moduleHandler.selectSolutions(request);
    00139     }
    00140     return true;
    00141   default:
    00142     if (moduleHandler.handleMessage(message))
    00143     {
    00144 //      OUTPUT(idText,text,"Process::handleDebugMessage : Handled "<< getMessageIDName(message.getMessageID()));
    00145       return true;
    00146     }
    00147     else
    00148     {
    00149       OUTPUT(idText,text,"Process::handleDebugMessage : Unhandled debug message ("<< getMessageIDName(message.getMessageID()) << ")");
    00150       return false;
    00151     }
    00152   }
    00153 }
    00154
    00155 unsigned long Process::getFrameNumber()
    00156 {
    00157   return *processFrameNumberPtr;
    00158 }