$search
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Willow Garage nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 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 //hard-coded for now 00052 mMaxTasks = -1; 00053 mCompletedTasks = 0; 00054 } 00055 00056 DBTaskDispatcher::~DBTaskDispatcher() 00057 { 00058 if (mCurrentTask) { 00059 //this should not really happen 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 //careful: we pass a null for the grasp allocator as we don't know yet which hand we'll be using 00074 mDBMgr = new db_planner::RosDatabaseManager(host, port_str.str(), username, password, database, NULL, NULL); 00075 //use the special allocator for models that get geometry directly from the database 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 //in the future, we'll get the connection params from arguments 00090 return connect("10.0.0.81",5432,"willow","willow","household_objects"); 00091 } 00092 00101 void DBTaskDispatcher::startNewTask() 00102 { 00103 assert(!mCurrentTask); 00104 // check if we have completed the max number of tasks 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 //task type 0 is reserved for no task to do 00116 if (rec.taskType.empty()) { 00117 DBGA("Dispatcher: no tasks to be executed"); 00118 mStatus = NO_TASK; 00119 return; 00120 } 00121 //check if we have a creator for tasks of this type 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 //create the new task 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 //start the next task 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 } //namespace