Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

simulationserver.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: simulationserver.cpp,v 1.4 2004/12/22 15:58:26 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #include "simulationserver.h"
00023 #include "simcontrolnode.h"
00024 #include <oxygen/monitorserver/monitorserver.h>
00025 #include <oxygen/sceneserver/sceneserver.h>
00026 #include <oxygen/gamecontrolserver/gamecontrolserver.h>
00027 #include <zeitgeist/logserver/logserver.h>
00028 #include <signal.h>
00029 
00030 using namespace oxygen;
00031 using namespace zeitgeist;
00032 using namespace salt;
00033 using namespace std;
00034 using namespace boost;
00035 
00036 bool SimulationServer::mExit = false;
00037 
00038 void SimulationServer::CatchSignal(int sig_num)
00039 {
00040     if (sig_num == SIGINT)
00041         {
00042             signal(SIGINT, CatchSignal);
00043             SimulationServer::mExit = true;
00044             std::cout << "(SimulationServer) caught SIGINT. exiting.\n";
00045         }
00046 }
00047 
00048 SimulationServer::SimulationServer() : Node()
00049 {
00050     mSimTime      = 0.0f;
00051     mSimStep      = 0.2f;
00052     mAutoTime     = true;
00053     mCycle        = 0;
00054     mSumDeltaTime = 0;
00055     mArgC = 0;
00056     mArgV = 0;
00057 
00058     signal(SIGINT, CatchSignal);
00059 }
00060 
00061 SimulationServer::~SimulationServer()
00062 {
00063 }
00064 
00065 void SimulationServer::OnLink()
00066 {
00067     mMonitorServer = shared_dynamic_cast<MonitorServer>
00068         (GetCore()->Get("/sys/server/monitor"));
00069 
00070     if (mMonitorServer.get() == 0)
00071     {
00072         GetLog()->Error()
00073             << "(SimulationServer) ERROR: MonitorServer not found.\n";
00074     }
00075 
00076     mGameControlServer = shared_dynamic_cast<GameControlServer>
00077         (GetCore()->Get("/sys/server/gamecontrol"));
00078 
00079     if (mGameControlServer.get() == 0)
00080     {
00081         GetLog()->Error()
00082             << "(SimulationServer) ERROR: GameControlServer not found.\n";
00083     }
00084 
00085     mSceneServer = shared_dynamic_cast<SceneServer>
00086         (GetCore()->Get("/sys/server/scene"));
00087 
00088     if (mSceneServer.get() == 0)
00089     {
00090         GetLog()->Error()
00091             << "(SimulationServer) ERROR: SceneServer not found.\n";
00092     }
00093 }
00094 
00095 
00096 void SimulationServer::OnUnlink()
00097 {
00098     mMonitorServer.reset();
00099     mGameControlServer.reset();
00100     mSceneServer.reset();
00101 }
00102 
00103 void SimulationServer::Quit()
00104 {
00105     mExit = true;
00106 }
00107 
00108 int SimulationServer::GetArgC()
00109 {
00110     return mArgC;
00111 }
00112 
00113 char** SimulationServer::GetArgV()
00114 {
00115     return mArgV;
00116 }
00117 
00118 float SimulationServer::GetTime()
00119 {
00120     return mSimTime;
00121 }
00122 
00123 float SimulationServer::GetSimStep()
00124 {
00125     return mSimStep;
00126 }
00127 
00128 void SimulationServer::SetSimStep(float deltaTime)
00129 {
00130     mSimStep = deltaTime;
00131 }
00132 
00133 float SimulationServer::GetSumDeltaTime()
00134 {
00135     return mSumDeltaTime;
00136 }
00137 
00138 void SimulationServer::SetAutoTimeMode(bool set)
00139 {
00140     mAutoTime = set;
00141 }
00142 
00143 bool SimulationServer::GetAutoTimeMode()
00144 {
00145     return mAutoTime;
00146 }
00147 
00148 int SimulationServer::GetCycle()
00149 {
00150     return mCycle;
00151 }
00152 
00153 bool SimulationServer::InitControlNode(const std::string& className, const std::string& name)
00154 {
00155     shared_ptr<SimControlNode> importer
00156         = shared_dynamic_cast<SimControlNode>(GetCore()->New(className));
00157 
00158     if (importer.get() == 0)
00159         {
00160             GetLog()->Error() << "(SimulationServer) ERROR: "
00161                               << "Unable to create '" << className << "'\n";
00162             return false;
00163         }
00164 
00165     importer->SetName(name);
00166     AddChildReference(importer);
00167 
00168     GetLog()->Normal()
00169         << "(SimulationServer) SimControlNode '"
00170         << name << "' registered\n";
00171 
00172     return true;
00173 }
00174 
00175 shared_ptr<SimControlNode>
00176 SimulationServer::GetControlNode(const string& controlName)
00177 {
00178     shared_ptr<SimControlNode> ctrNode =
00179         shared_dynamic_cast<SimControlNode>(GetChild(controlName));
00180 
00181     if (ctrNode.get() == 0)
00182         {
00183             GetLog()->Normal()
00184                 << "(SimulationServer) SimControlNode '"
00185                 << controlName << "' not found\n";
00186         }
00187 
00188     return ctrNode;
00189 }
00190 
00191 void SimulationServer::AdvanceTime(float deltaTime)
00192 {
00193     mSumDeltaTime += deltaTime;
00194 }
00195 
00196 void SimulationServer::Step()
00197 {
00198     if (
00199         (mSceneServer.get() == 0) ||
00200         (mGameControlServer.get() == 0)
00201         )
00202         {
00203             return;
00204         }
00205 
00206     if (mSimStep > 0)
00207         {
00208             // world is stepped in discrete steps
00209             while (mSumDeltaTime >= mSimStep)
00210                 {
00211                     mSceneServer->Update(mSimStep);
00212                     mGameControlServer->Update(mSimStep);
00213                     mSumDeltaTime -= mSimStep;
00214                     mSimTime += mSimStep;
00215                 }
00216         } else
00217             {
00218                 // simulate passed time in one single step
00219                 mSceneServer->Update(mSumDeltaTime);
00220                 mGameControlServer->Update(mSimStep);
00221                 mSimTime += mSumDeltaTime;
00222                 mSumDeltaTime = 0;
00223             }
00224 }
00225 
00226 void SimulationServer::ControlEvent(EControlEvent event)
00227 {
00228     for (
00229          TLeafList::iterator iter=begin();
00230          iter != end();
00231          ++iter
00232          )
00233         {
00234             shared_ptr<SimControlNode> ctrNode =
00235                 shared_dynamic_cast<SimControlNode>(*iter);
00236 
00237             if (ctrNode.get() == 0)
00238                 {
00239                     continue;
00240                 }
00241 
00242             switch (event)
00243                 {
00244                 case CE_Init :
00245                     ctrNode->InitSimulation();
00246                     break;
00247 
00248                 case CE_Done :
00249                     ctrNode->DoneSimulation();
00250                     break;
00251 
00252                 case CE_StartCycle :
00253                     ctrNode->StartCycle();
00254                     break;
00255 
00256                 case CE_SenseAgent :
00257                     ctrNode->SenseAgent();
00258                     break;
00259 
00260                 case CE_ActAgent :
00261                     ctrNode->ActAgent();
00262                     break;
00263 
00264                 case CE_EndCycle :
00265                     ctrNode->EndCycle();
00266                     break;
00267 
00268                 default:
00269                     GetLog()->Error()
00270                         << "(SimulationServer) ERROR: unknown control event "
00271                         << event << "\n";
00272                     return;
00273                 }
00274         }
00275 }
00276 
00277 void SimulationServer::Run(int argc, char** argv)
00278 {
00279     GetLog()->Normal() << "(SimulationServer) entering runloop\n";
00280 
00281     // cache argc and argv, to make it accessible for registerd
00282     // SimControlNodes
00283     mArgC = argc;
00284     mArgV = argv;
00285 
00286     ControlEvent(CE_Init);
00287 
00288     while (! mExit)
00289         {
00290             ++mCycle;
00291 
00292             ControlEvent(CE_StartCycle);
00293             ControlEvent(CE_SenseAgent);
00294             ControlEvent(CE_ActAgent);
00295 
00296             if (mAutoTime)
00297                 {
00298                     AdvanceTime(mSimStep);
00299                 }
00300             Step();
00301 
00302             ControlEvent(CE_EndCycle);
00303         }
00304 
00305     ControlEvent(CE_Done);
00306 
00307     mArgC = 0;
00308     mArgV = 0;
00309 
00310     GetLog()->Normal()
00311         << "(SimulationServer) leaving runloop at t="
00312         << mSimTime << "\n";
00313 }
00314 
00315 shared_ptr<GameControlServer> SimulationServer::GetGameControlServer()
00316 {
00317     return mGameControlServer;
00318 }
00319 
00320 shared_ptr<MonitorServer> SimulationServer::GetMonitorServer()
00321 {
00322     return mMonitorServer;
00323 }
00324 
00325 shared_ptr<SceneServer> SimulationServer::GetSceneServer()
00326 {
00327     return mSceneServer;
00328 }
00329 
00330 

Generated on Thu Apr 6 15:25:40 2006 for rcssserver3d by  doxygen 1.4.4