worldsim/include/worldcontroller.h

00001 /********************************************************************************
00002  *  WorldSim -- library for robot simulations                                   *
00003  *  Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifdef FARSA_USE_YARP_AND_ICUB
00021 
00022 #ifndef WORLDCONTROLLER_H
00023 #define WORLDCONTROLLER_H
00024 
00025 #include "worldsimconfig.h"
00026 #include "world.h"
00027 #include "yarpobject.h"
00028 #include <QThread>
00029 #include <QHash>
00030 #include <QString>
00031 #include <QStringList>
00032 #include <QQueue>
00033 
00034 namespace farsa {
00035 
00036 class WorldController;
00037 class ServerWorldController;
00039 typedef QHash<QString, yarp::os::Bottle (ServerWorldController::*)(const yarp::os::Bottle&)> cmdHandlerHash;
00040 
00051 class FARSA_WSIM_API ServerWorldController : public QThread {
00052     Q_OBJECT
00053 public:
00055     ServerWorldController( WorldController* ctrl );
00057     ~ServerWorldController();
00059     void applyQueuedCommands();
00060 protected:
00062     virtual void run();
00063 private:
00065     yarp::os::Bottle startCmd( const yarp::os::Bottle& );
00067     yarp::os::Bottle pauseCmd( const yarp::os::Bottle& );
00069     yarp::os::Bottle stopCmd( const yarp::os::Bottle& );
00071     yarp::os::Bottle advanceCmd( const yarp::os::Bottle& );
00073     yarp::os::Bottle makeCmd( const yarp::os::Bottle& );
00075     yarp::os::Bottle setCmd( const yarp::os::Bottle& );
00077     yarp::os::Bottle setWorldCmd( const yarp::os::Bottle& cmd );
00079     yarp::os::Bottle setObjectCmd( const yarp::os::Bottle& cmd, WObject* object );
00081     WorldController* wctrl;
00085     cmdHandlerHash asyn_cmds;
00091     cmdHandlerHash syn_cmds;
00093     QQueue<yarp::os::Bottle> queue_cmds;
00094 };
00095 
00106 class FARSA_WSIM_API WorldController : public YarpObject {
00107     Q_OBJECT
00108 public:
00110     WorldController( World* world, QString name );
00112     ~WorldController();
00114     virtual void update();
00116     yarp::os::Port* inPort() {
00117         return inport;
00118     };
00120     yarp::os::Port* outPort() {
00121         return outport;
00122     };
00123 private:
00125     ServerWorldController* srv;
00127     yarp::os::Port* inport;
00129     yarp::os::Port* outport;
00130 };
00131 
00132 } // end namespace farsa
00133 
00134 #endif
00135 
00136 #endif // FARSA_USE_YARP_AND_ICUB