NCBI C++ ToolKit
app_task_service.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_FRAMEWORK___APP_TASK_SERVICE__HPP
2 #define GUI_FRAMEWORK___APP_TASK_SERVICE__HPP
3 
4 /* $Id: app_task_service.hpp 31807 2014-11-17 19:32:54Z katargir $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Andrey Yazhuk
30  *
31  * File Description:
32  *
33  */
34 
35 /** @addtogroup GUI_FRAMEWORK
36 *
37 * @{
38 */
39 
40 #include <corelib/ncbistd.hpp>
41 #include <util/sync_queue.hpp>
42 
46 
48 
50 
51 
52 class IEventLogService;
53 class IStatusBarService;
54 class CTaskDescr;
55 
57 {
58 public:
59  enum EEventId {
60  eUpdate
61  };
62 
64 };
65 
66 ///////////////////////////////////////////////////////////////////////////////
67 /// CAppTaskService - Application Task Service.
68 
70  public CObjectEx,
71  public CEventHandler,
72  public IService,
74 {
76 public:
78 
79  enum EConsts {
80  eInvalidTaskID = -1
81  };
82 
84  typedef vector<TTaskRef> TTaskRefVec;
85 
86  class CTaskRecord : public CObject
87  {
88  public:
90  : m_StartTime(CTime(CTime::eCurrent).GetTimeT())
91  {
92  }
93 
95  time_t m_StartTime; // executing start
96  };
98  typedef vector<TRecordRef> TRecRefVec;
99 
100 public:
102  virtual ~CAppTaskService();
103 
104  /// @name IService implementation
105  /// @{
106  virtual void InitService();
107 
108  /// Cancels all backgrounded tasks, removes pending tasks and blocks
109  /// processing of incoming requests and events.
110  virtual void ShutDownService();
111 
112  void PreShutDownService();
113 
114  /// @}
115 
116  /// @name IServiceLocatorConsumer interface implementation
117  /// @{
118  virtual void SetServiceLocator(IServiceLocator* srv_locator);
119  /// @}
120 
121  /// Add a task to the queue. The task will be executed on the main UI
122  /// thread at some point in future. Tasks are executed according to FIFO
123  /// principle.
124  void AddTask(IAppTask& task);
125 
126  /// Places a request to cancel a backgrounded task. It is guaranteed that
127  /// Task Manager will not further execute the task on the main thread,
128  /// however subcomponents of the task may still continue to run on a
129  /// background thread.
130  ///
131  /// @param task - task to cancel
132  /// @param throw_on_error - flag to throw an exception on error or
133  /// return false
134  /// @return true if taks was canceled
135  ///
136  bool CancelTask(IAppTask& task, bool throw_on_error=true);
137 
138  /// This function shall be called in the the application idle function, it
139  /// polls the Task Queue and executes pending tasks.
140  bool IdleCallback();
141 
142  /// Inspection interface - supposed to be used from the main UI thread only
143  void GetPendingTasks(TRecRefVec& tasks);
144  void GetBackgroundedTasks(TRecRefVec& tasks);
145 
146  /// returns the number of running and backgrounded tasks
147  int GetRunningTasksCount(bool vis_only = true);
148 
149 protected:
150  void x_OnWakeUpSignal(CEvent* event);
151  void x_OnTaskCanceled(CEvent* event);
152  void x_OnStatusUpdate(CEvent* event);
153 
154  void x_ExecuteNextTaskInQueue();
155 
156  void x_LogTask(IEventRecord::EType type,
157  const string& prefix, IAppTask& task,
158  const string& postfix, const string& details = kEmptyStr);
159 
160  void x_ReportTaskFailure(IAppTask& task, const string& err_msg, const string& details);
161 
162 protected:
163 
166 
167 protected:
170 
172 
174 
177 
178  IAppTask* m_RunningTask; // currently executed task
179 };
180 
181 
182 ///////////////////////////////////////////////////////////////////////////////
183 /// CAppTaskServiceEvent
185  public CEvent
186 {
187 public:
188  enum EType {
189  eInvalid = -1,
192  eStatusUpdate
193  };
194 
196  : CEvent(eEvent_Message, type), m_Task(&task) {}
197 
198  CIRef<IAppTask> GetTask() { return m_Task; }
199 
200 protected:
202 };
203 
204 ///////////////////////////////////////////////////////////////////////////////
205 /// CAppTaskServiceException
208 {
209 public:
210  enum EErrCode {
213  eFatalError /// unknown tragic error
214  };
216 };
217 
218 
220 
221 /* @} */
222 
223 #endif // GUI_FRAMEWORK___APP_TASK_SERVICE__HPP
CAppTaskServiceEvent.
CAppTaskServiceException.
CAppTaskService - Application Task Service.
CEventHandler.
CEvent - generic event implementation TODO TODO - Attachments.
Definition: event.hpp:86
CMutex –.
Definition: ncbimtx.hpp:749
CObjectEx –.
Definition: ncbiobj.hpp:2531
CObject –.
Definition: ncbiobj.hpp:180
CRef –.
Definition: ncbiobj.hpp:618
CTime –.
Definition: ncbitime.hpp:296
IAppTask.
Definition: app_task.hpp:83
IEventLogService - records application events.
IServiceLocatorConsumer - classes that need IServiceLocator should implement this interface.
Definition: service.hpp:103
IServiceLocator - an abstract mechanism for locating services.
Definition: service.hpp:71
IService an abstraction that represents an application component providing specific functional capa...
Definition: service.hpp:56
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define EXCEPTION_VIRTUAL_BASE
Do not use virtual base classes in exception declaration at all, because in this case derived class s...
Definition: ncbiexpt.hpp:1388
IEventLogService * m_EventLogService
IAppTask * m_RunningTask
TRecordMap m_BackgoundedTasks
CAppTaskServiceEvent(IAppTask &task, EType type)
CIRef< IAppTask > TTaskRef
IAppTask::ETaskState TTaskState
vector< TRecordRef > TRecRefVec
ETaskState
List of task states defining the task management FSM.
Definition: app_task.hpp:87
CRef< CTaskRecord > TRecordRef
virtual void SetServiceLocator(IServiceLocator *locator)=0
virtual void InitService()=0
vector< TTaskRef > TTaskRefVec
CSyncQueue< TRecordRef > TQueue
virtual void ShutDownService()=0
IServiceLocator * m_ServiceLocator
CIRef< IAppTask > GetTask()
map< TTaskRef, TRecordRef > TRecordMap
CIRef< IAppTask > m_Task
NCBI_EXCEPTION_DEFAULT(CAppTaskServiceException, CException)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
#define NCBI_GUIFRAMEWORK_EXPORT
Definition: gui_export.h:509
static const char * prefix[]
Definition: pcregrep.c:405
Definition: type.c:6
Definition of synchronized queue (CSyncQueue template) and templates related to it.
Modified on Wed Jun 19 16:59:50 2024 by modify_doxy.py rev. 669887