Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "graspit_dbase_tasks/dbTaskDispatcher.h"
00036
00037 #include <sstream>
00038 #include <cassert>
00039
00040 #include "DBPlanner/ros_database_manager.h"
00041 #include "graspitGUI.h"
00042 #include "graspit_db_model.h"
00043 #include "debug.h"
00044
00045 #include "graspit_dbase_tasks/dbTask.h"
00046
00047 namespace graspit_dbase_tasks {
00048
00049 DBTaskDispatcher::DBTaskDispatcher() : mDBMgr(NULL) , mCurrentTask(NULL), mStatus(READY)
00050 {
00051
00052 mMaxTasks = -1;
00053 mCompletedTasks = 0;
00054 }
00055
00056 DBTaskDispatcher::~DBTaskDispatcher()
00057 {
00058 if (mCurrentTask) {
00059
00060 DBGA("Dispatcher: deleting current task on cleanup");
00061 delete mCurrentTask;
00062 }
00063 delete mDBMgr;
00064 }
00065
00066 int DBTaskDispatcher::connect(std::string host, int port, std::string username,
00067 std::string password, std::string database)
00068 {
00069 delete mDBMgr;
00070 std::ostringstream port_str;
00071 port_str << port;
00072
00073
00074 mDBMgr = new db_planner::RosDatabaseManager(host, port_str.str(), username, password, database, NULL, NULL);
00075
00076 GeomGraspitDBModelAllocator* allocator = new GeomGraspitDBModelAllocator(mDBMgr);
00077 mDBMgr->SetModelAllocator(allocator);
00078
00079 if (!mDBMgr->isConnected()) {
00080 DBGA("DBase operator: Connection failed");
00081 delete mDBMgr; mDBMgr = NULL;
00082 return -1;
00083 }
00084 return 0;
00085 }
00086
00087 int DBTaskDispatcher::init(int argc, char **argv)
00088 {
00089
00090 return connect("10.0.0.81",5432,"willow","willow","household_objects");
00091 }
00092
00101 void DBTaskDispatcher::startNewTask()
00102 {
00103 assert(!mCurrentTask);
00104
00105 if (mMaxTasks >= 0 && mCompletedTasks >= mMaxTasks) {
00106 mStatus = DONE;
00107 return;
00108 }
00109 db_planner::TaskRecord rec;
00110 if (!mDBMgr->AcquireNextTask(&rec, mAcceptedTaskTypes)) {
00111 DBGA("Dispatcher: error reading next task");
00112 mStatus = ERROR;
00113 return;
00114 }
00115
00116 if (rec.taskType.empty()) {
00117 DBGA("Dispatcher: no tasks to be executed");
00118 mStatus = NO_TASK;
00119 return;
00120 }
00121
00122 std::map<std::string,DBTaskCreator*>::iterator it = mTaskCreators.find(rec.taskType);
00123 if (it==mTaskCreators.end())
00124 {
00125 DBGA("Dispatcher: no creator available for task type: " << rec.taskType);
00126 mStatus = ERROR;
00127 return;
00128 }
00129
00130 mCurrentTask = (*(it->second))(this, mDBMgr,rec);
00131 if (!mCurrentTask) {
00132 DBGA("Dispatcher: could not start task of type: " << rec.taskType);
00133 mStatus = ERROR;
00134 return;
00135 }
00136
00137 mCurrentTask->start();
00138 }
00139
00151 void DBTaskDispatcher::checkCurrentTask()
00152 {
00153 assert(mCurrentTask);
00154 switch (mCurrentTask->getStatus()) {
00155 case DBTask::RUNNING:
00156 DBGP("Task running");
00157 mStatus = RUNNING;
00158 mCurrentTask->mainLoop();
00159 break;
00160 case DBTask::ERROR:
00161 DBGP("Task error");
00162 mStatus = READY;
00163 if (!mDBMgr->SetTaskStatus(mCurrentTask->getRecord().taskId,"ERROR")) {
00164 DBGA("Dispatcher: error marking completed task");
00165 mStatus = ERROR;
00166 }
00167 delete mCurrentTask; mCurrentTask = NULL;
00168 break;
00169 case DBTask::DONE:
00170 DBGP("Task done");
00171 mStatus = READY;
00172 mCompletedTasks++;
00173 if (!mDBMgr->SetTaskStatus(mCurrentTask->getRecord().taskId,"COMPLETED")) {
00174 DBGA("Dispatcher: error marking completed task");
00175 mStatus = ERROR;
00176 }
00177 delete mCurrentTask; mCurrentTask = NULL;
00178 break;
00179 default:
00180 DBGA("Dispatcher: Unknown task state");
00181 mStatus = ERROR;
00182 }
00183 }
00184
00190 int DBTaskDispatcher::mainLoop()
00191 {
00192 if(mCurrentTask) {
00193 checkCurrentTask();
00194 }
00195 if (mStatus == READY) {
00196 startNewTask();
00197 }
00198 switch(mStatus) {
00199 case DONE:
00200 graspItGUI->exitMainLoop();
00201 return 1;
00202 case ERROR:
00203 graspItGUI->exitMainLoop();
00204 return 1;
00205 case NO_TASK:
00206 graspItGUI->exitMainLoop();
00207 return 0;
00208 case RUNNING:
00209 break;
00210 case READY:
00211 break;
00212 }
00213 return 0;
00214 }
00215
00216 }