Update of /cvsroot/simspark/simspark/spark/plugin/sparkmonitor In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv26634 Modified Files: Tag: projectx Makefile.am export.cpp Added Files: Tag: projectx sparkmonitorlogfileserver.cpp sparkmonitorlogfileserver.h sparkmonitorlogfileserver_c.cpp Log Message: added sparkmonitorlogfileserver --- NEW FILE: sparkmonitorlogfileserver_c.cpp --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: sparkmonitorlogfileserver_c.cpp,v 1.1.2.1 2007/07/18 23:08:50 fruit Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "sparkmonitorlogfileserver.h" using namespace std; using namespace oxygen; FUNCTION(SparkMonitorLogFileServer, setFileName) { string inName; if ( (in.GetSize() != 1) || (! in.GetValue(in[0], inName)) ) { return false; } obj->SetFileName(inName); return true; } FUNCTION(SparkMonitorLogFileServer, setStepDelay) { int inDelay; if ( (in.GetSize() != 1) || (! in.GetValue(in[0], inDelay)) ) { return false; } obj->SetStepDelay(inDelay); return true; } FUNCTION(SparkMonitorLogFileServer, pauseMode) { if (in.GetSize() != 0) { return false; } obj->Pause(); return true; } FUNCTION(SparkMonitorLogFileServer, stepForward) { if (in.GetSize() != 0) { return false; } obj->ForwardStep(); return true; } FUNCTION(SparkMonitorLogFileServer, stepBackward) { if (in.GetSize() != 0) { return false; } obj->BackwardStep(); return true; } FUNCTION(SparkMonitorLogFileServer, playBackward) { if (in.GetSize() != 0) { return false; } obj->BackwardPlayback(); return true; } void CLASS(SparkMonitorLogFileServer)::DefineClass() { DEFINE_BASECLASS(oxygen/SimControlNode); DEFINE_FUNCTION(setFileName); DEFINE_FUNCTION(pauseMode); DEFINE_FUNCTION(stepForward); DEFINE_FUNCTION(stepBackward); DEFINE_FUNCTION(playBackward); DEFINE_FUNCTION(setStepDelay); } --- NEW FILE: sparkmonitorlogfileserver.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef SPARK_SPARKMONITORLOGFILESERVER_H #define SPARK_SPARKMONITORLOGFILESERVER_H #include <sfsexp/sexp.h> #include <zeitgeist/class.h> #include <oxygen/simulationserver/simcontrolnode.h> #include <oxygen/sceneserver/sceneserver.h> #include <oxygen/sceneserver/scene.h> #include <oxygen/sceneserver/sceneimporter.h> #include <oxygen/gamecontrolserver/predicate.h> #include <fstream> #include <stack> class SparkMonitorLogFileServer : public oxygen::SimControlNode { public: SparkMonitorLogFileServer(); virtual ~SparkMonitorLogFileServer(); /** called once when the simulation is started */ virtual void InitSimulation(); /** called once before the simulation is shut down */ virtual void DoneSimulation(); /** called when a new simulation cycle starts, before the simulation is stepped */ virtual void StartCycle(); /** set the log file name */ void SetFileName(std::string fileName){ mLogfileName = fileName; } /** pause the log player */ void Pause(){ mPause = !mPause; } /** go to the next step */ void ForwardStep(){ mForwardStep = true; } /** go to the previous step */ void BackwardStep(); /** backward play the log file back */ void BackwardPlayback(); /** set the length of delay between steps */ void SetStepDelay(int delay){ mStepDelay = delay; } protected: /** parses a received message */ void ParseMessage(const std::string& msg); void ParseCustomPredicates(sexp_t* sexp, oxygen::PredicateList& pList); /** parses the given s-expression into a ParameterList and transfers it to a registered user monitor */ void ParseCustomPredicates(sexp_t* sexp); virtual void OnLink(); virtual void OnUnlink(); protected: /** cached reference to the SceneServer */ boost::shared_ptr<oxygen::SceneServer> mSceneServer; /** cached reference to the current active scene */ boost::shared_ptr<oxygen::Scene> mActiveScene; /** chached reference to the scene importer */ boost::shared_ptr<oxygen::SceneImporter> mSceneImporter; /** the root node of the managed scene */ boost::shared_ptr<oxygen::BaseNode> mManagedScene; /** the logfile name */ std::string mLogfileName; /** the logfile */ std::ifstream mLog; /** the pause state of the log player */ bool mPause; /** go to the next step in the log file */ bool mForwardStep; /** line numbers storage */ std::stack<unsigned> linePositions; /** the length of delay between seteps */ int mStepDelay; bool mBackwardPlayback; /** cached reference to the script server */ boost::shared_ptr<zeitgeist::ScriptServer> mScriptServer; }; DECLARE_CLASS(SparkMonitorLogFileServer); #endif // SPARKMONITORLOGFILESERVER_H Index: Makefile.am =================================================================== RCS file: /cvsroot/simspark/simspark/spark/plugin/sparkmonitor/Makefile.am,v retrieving revision 1.1 retrieving revision 1.1.4.1 diff -C2 -d -r1.1 -r1.1.4.1 *** Makefile.am 13 Dec 2005 21:48:50 -0000 1.1 --- Makefile.am 18 Jul 2007 23:08:50 -0000 1.1.4.1 *************** *** 8,12 **** sparkmonitorclient.h \ sparkmonitorclient.cpp \ ! sparkmonitorclient_c.cpp sparkmonitor_la_LDFLAGS = -module -version-info 0:0:0 -L${top_srcdir}/utility/sfsexp/ --- 8,15 ---- sparkmonitorclient.h \ sparkmonitorclient.cpp \ ! sparkmonitorclient_c.cpp \ ! sparkmonitorlogfileserver.h \ ! sparkmonitorlogfileserver.cpp \ ! sparkmonitorlogfileserver_c.cpp sparkmonitor_la_LDFLAGS = -module -version-info 0:0:0 -L${top_srcdir}/utility/sfsexp/ --- NEW FILE: sparkmonitorlogfileserver.cpp --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "sparkmonitorlogfileserver.h" #include <oxygen/monitorserver/custommonitor.h> #include <zeitgeist/logserver/logserver.h> #include <zeitgeist/scriptserver/scriptserver.h> #include <netinet/in.h> #include <rcssnet/exception.hpp> #include <cerrno> using namespace oxygen; using namespace zeitgeist; using namespace rcss::net; using namespace salt; using namespace boost; using namespace std; SparkMonitorLogFileServer::SparkMonitorLogFileServer() : SimControlNode() { mPause = false; mForwardStep = false; mStepDelay = 0; mBackwardPlayback = false; } SparkMonitorLogFileServer::~SparkMonitorLogFileServer() { } void SparkMonitorLogFileServer::OnLink() { mScriptServer = GetCore()->GetScriptServer(); // setup SceneServer reference mSceneServer = shared_dynamic_cast<SceneServer> (GetCore()->Get("/sys/server/scene")); if (mSceneServer.get() == 0) { GetLog()->Error() << "(SparkMonitor) ERROR: SceneServer not found\n"; } } void SparkMonitorLogFileServer::OnUnlink() { mSceneServer.reset(); if (mManagedScene.get() != 0) { mManagedScene->UnlinkChildren(); mManagedScene.reset(); } } void SparkMonitorLogFileServer::InitSimulation() { // get the SceneImporter mSceneImporter = shared_dynamic_cast<SceneImporter> (GetCore()->Get("/sys/server/scene/RubySceneImporter")); if (mSceneImporter.get() == 0) { GetLog()->Error() << "(SparkMonitorLogFileServer) ERROR: cannot create" << " a RubySceneImporter instance\n"; } mLog.open(mLogfileName.c_str()); if (! mLog.is_open()) { GetLog()->Error() << "(SparkMonitorLogFileServer) ERROR: cannot open" << " the log file\n"; exit(1); } } void SparkMonitorLogFileServer::DoneSimulation() { mActiveScene.reset(); mSceneImporter.reset(); mLog.close(); } void SparkMonitorLogFileServer::StartCycle() { if (mPause && !mForwardStep) { return; } if (mBackwardPlayback) { if (linePositions.size() < 3) return; linePositions.pop(); linePositions.pop(); mLog.seekg( linePositions.top() ); } string msg; linePositions.push( mLog.tellg() ); std::getline(mLog,msg); if (msg.size() != 0) { ParseMessage(msg); } usleep(mStepDelay); mForwardStep = false; } void SparkMonitorLogFileServer::ParseCustomPredicates(sexp_t* sexp, PredicateList& pList) { if ( (sexp == 0) || (sexp->ty != SEXP_VALUE) ) { return; } Predicate& pred = pList.AddPredicate(); pred.name = sexp->val; sexp = sexp->next; while (sexp != 0) { if (sexp->ty == SEXP_VALUE) { pred.parameter.AddValue(sexp->val); } sexp = sexp->next; } } void SparkMonitorLogFileServer::ParseCustomPredicates(sexp_t* sexp) { // ( (name param1 param2 ...) (name param1 param2 ...) ... ) if (sexp == 0) { return; } // get list of registered CustomMonitor objects TLeafList customList; ListChildrenSupportingClass<CustomMonitor>(customList); customList.push_back(GetCore()->Get("/sys/server/simulation/SparkMonitorClient/SoccerMonitor")); if (customList.empty()) { return; } // parse predicates PredicateList pList; sexp = sexp->list; while (sexp != 0) { if (sexp->ty == SEXP_LIST) { sexp_t* sPred = sexp->list; ParseCustomPredicates(sPred,pList); } sexp = sexp->next; } // pass predicates to all registered CustomMonitor objects for ( TLeafList::iterator iter = customList.begin(); iter != customList.end(); ++iter ) { shared_static_cast<CustomMonitor>((*iter)) ->ParseCustomPredicates(pList); } } void SparkMonitorLogFileServer::ParseMessage(const string& msg) { if ( (mSceneServer.get() == 0) || (mSceneImporter.get() == 0) ) { return; } mActiveScene = mSceneServer->GetActiveScene(); if (mActiveScene.get() == 0) { return; } if (mManagedScene.get() == 0) { mManagedScene = shared_dynamic_cast<BaseNode> (GetCore()->New("oxygen/BaseNode")); mActiveScene->AddChildReference(mManagedScene); } // parse s-expressions; we expect a leading list of custom // predicates followed by the RubySceneGraph expressions /** ( (predicate parameter ...) (predicate parameter ...) ... ) (RubySceneGraph Header) (RubySceneGraph Body) ) **/ char* msgBuf = const_cast<char*>(msg.c_str()); pcont_t* pcont = init_continuation(msgBuf); sexp_t* sexp_custom = iparse_sexp(msgBuf,msg.size(),pcont); if (sexp_custom == 0) { destroy_sexp(sexp_custom); destroy_continuation(pcont); return; } ParseCustomPredicates(sexp_custom); mSceneImporter->ParseScene(string(pcont->lastPos), mManagedScene, shared_ptr<ParameterList>()); destroy_sexp(sexp_custom); destroy_continuation(pcont); } void SparkMonitorLogFileServer::BackwardStep() { if (linePositions.size() < 3) return; linePositions.pop(); linePositions.pop(); mLog.seekg( linePositions.top() ); mForwardStep = true; } void SparkMonitorLogFileServer::BackwardPlayback() { mBackwardPlayback = !mBackwardPlayback; mPause = false; } Index: export.cpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/plugin/sparkmonitor/export.cpp,v retrieving revision 1.1 retrieving revision 1.1.4.1 diff -C2 -d -r1.1 -r1.1.4.1 *** export.cpp 13 Dec 2005 21:48:50 -0000 1.1 --- export.cpp 18 Jul 2007 23:08:50 -0000 1.1.4.1 *************** *** 22,25 **** --- 22,26 ---- #include "sparkmonitor.h" #include "sparkmonitorclient.h" + #include "sparkmonitorlogfileserver.h" #include <zeitgeist/zeitgeist.h> *************** *** 29,31 **** --- 30,33 ---- ZEITGEIST_EXPORT(SparkMonitor); ZEITGEIST_EXPORT(SparkMonitorClient); + ZEITGEIST_EXPORT(SparkMonitorLogFileServer); ZEITGEIST_EXPORT_END() |