void ProcessCommander::onConnectionAwaitingCommand(ServerConnectionPtr serverConnection) { switch(currentTask()) { case ReadQualityTablesTask: continueReadQualityTablesTask(serverConnection); break; case ReadAntennaTablesTask: continueReadAntennaTablesTask(serverConnection); break; case ReadBandTablesTask: continueReadBandTablesTask(serverConnection); break; case ReadDataRowsTask: continueReadDataRowsTask(serverConnection); break; case WriteDataRowsTask: continueWriteDataRowsTask(serverConnection); break; case NoTask: handleIdleConnection(serverConnection); break; default: throw std::runtime_error("Unknown task"); } }
void TasksHandler::execute() { BaseTask *t = currentTask(); switch (t->status()) { case TASK_RUN: // the current task is to be started or still running, // execute it. t->exec(); break; case TASK_PAUSE: // current task is pausing, it should be pushed to the // back of the task queue again; at the same time, a new // task is retrieved t->start(); addTask(t); retrieveNextTask(); break; case TASK_ERROR: case TASK_STOP: // current task has been finished, it should be removed // and then retrieve a new task. However, in Naboo viewer // we need wait for document host remove the aborted task removeCurrentTask(); retrieveNextTask(); break; default: break; } }
/* * Constructor */ CTaskManager::CTaskManager() : _RunningTask (""), _TaskQueue (""), _DoneTaskQueue ("") { _IsTaskRunning = false; _ThreadRunning = true; CSynchronized<string>::CAccessor currentTask(&_RunningTask); currentTask.value () = ""; _Thread = IThread::create(this); _Thread->start(); _ChangePriorityCallback = NULL; }
void module_video_camera::worker() { CvCapture* capture = cvCreateCameraCapture(0);//cvCaptureFromCAM(0); if(!capture || currentTask() != INITIALIZE_CAPTURE){ message = "module||ERROR! Cannot initialize camera!!"; addTask(CLEANUP_CAPTURE); return; } else message = ""; IplImage *frame; Tasks task = INITIALIZE_CAPTURE; while( task != TERMINATE_CAPTURE ){ // Fetch a frame if its asked for / couldnt fetch a previous frame / is the first time. if( task == FETCH_FRAME || task == IGNORE_FRAME || task == INITIALIZE_CAPTURE ){ frame = cvQueryFrame(capture); if(frame){ if( !m_bufferReady ) initializeBuffers(frame->width,frame->height, frame->depth, frame->nChannels); cvConvertImage(frame,m_buffer[nextPage()], CV_CVTIMG_SWAP_RB); addTask(CONSUME_FRAME); } else addTask(IGNORE_FRAME); } usleep(10); task = currentTask(); } //Cleanup cvReleaseCapture(&capture); freeBuffers(); addTask(CLEANUP_CAPTURE); }
// Manage TaskQueue void CTaskManager::run(void) { IRunnable *runnableTask; float priorityTask = 0.f; while(_ThreadRunning) { { CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue); if(acces.value().empty()) { runnableTask = NULL; } else { // Update task priorities changeTaskPriority (); // Get the best task list<CWaitingTask> &taskList = acces.value(); list<CWaitingTask>::iterator ite = taskList.begin(); list<CWaitingTask>::iterator bestIte = ite; while (ite != taskList.end()) { if (ite->Priority < bestIte->Priority) bestIte = ite; // Next task; ite++; } _IsTaskRunning = true; runnableTask = bestIte->Task; priorityTask = bestIte->Priority; taskList.erase (bestIte); } } if(runnableTask) { { CSynchronized<string>::CAccessor currentTask(&_RunningTask); string temp; runnableTask->getName(temp); currentTask.value () = temp + " " + toString (priorityTask); } runnableTask->run(); { CSynchronized<string>::CAccessor currentTask(&_RunningTask); CSynchronized<deque<string> >::CAccessor doneTask(&_DoneTaskQueue); doneTask.value().push_front (currentTask.value ()); currentTask.value () = ""; if (doneTask.value().size () > NLMISC_DONE_TASK_SIZE) doneTask.value().resize (NLMISC_DONE_TASK_SIZE); } _IsTaskRunning = false; } else { sleepTask(); } } CBigFile::getInstance().currentThreadFinished(); _ThreadRunning = true; }