NCBI C++ ToolKit
event_translator.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: event_translator.cpp 33449 2015-07-27 17:47:12Z katargir $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  *
31  */
32 
33 
34 #include <ncbi_pch.hpp>
35 
37 
38 
40 
41 ///////////////////////////////////////////////////////////////////////////////
42 /// CAppJobNotification
43 
45 : CEvent(eEvent_Message, eStateChanged),
46  m_JobID(job_id),
47  m_State(state)
48 {
49 }
50 
51 
53 : CEvent(eEvent_Message, eStateChanged),
54  m_JobID(job_id),
55  m_State(IAppJob::eCompleted),
56  m_Result(result)
57 {
58 }
59 
60 
62 : CEvent(eEvent_Message, eStateChanged),
63  m_JobID(job_id),
64  m_State(IAppJob::eFailed),
65  m_Error(&error)
66 {
67 }
68 
69 
71 : CEvent(eEvent_Message, eProgress),
72  m_JobID(job_id),
73  m_State(IAppJob::eRunning),
74  m_Progress(&progress)
75 {
76 }
77 
78 
79 
80 ///////////////////////////////////////////////////////////////////////////////
81 /// CAppJobEventTranslator
82 
84 : m_TargetEventHandler(target),
85  m_Dispatcher(NULL)
86 {
87  m_IsWeakTarget = true;
88  CWeakObject* oweak = dynamic_cast<CWeakObject*>(&target);
89  // We need to convert all event handling classes to be weak-ptr compatible (derive from CObjectEx)
90  _ASSERT(oweak);
91  if (!oweak) {
92  m_IsWeakTarget = false;
93  }
94  else
95  {
96  try {
97  m_TargetWeakPtr = &target;
98  } catch (CObjectException& ) {
99  m_IsWeakTarget = false;
101  }
102  }
103 }
104 
105 
107 {
108  m_Dispatcher = &disp;
109 }
110 
112 {
113  // Event handler supports weak pointer interface?
114  if (m_IsWeakTarget) {
116  if (weh.NotNull()) {
117  weh->Post(evt);
118  }
119  }
120  else {
122  }
123 }
124 
126 {
127  if(!m_Dispatcher) return;
128  switch(new_state)
129  {
130  case IAppJob::eCompleted:
131  {
132  CRef<CObject> res(job_rec ? job_rec->m_Job->GetResult().GetPointer() : NULL);
133  CRef<CEvent> evt(new CAppJobNotification(job_rec->m_ID, res.GetPointer()));
134  x_NotifyObservers(evt);
135  break;
136  }
137  case IAppJob::eFailed:
138  {
139  CConstIRef<IAppJobError> err(job_rec ? job_rec->m_Job->GetError().GetPointer() : NULL);
140  if (err) {
141  CRef<CEvent> evt(new CAppJobNotification(job_rec->m_ID, *err));
142  x_NotifyObservers(evt);
143  }
144  else {
145  ERR_POST("Job failed -- NULL error job_id= " << job_rec->m_ID);
146  CRef<CEvent> evt(new CAppJobNotification(job_rec->m_ID, new_state));
147  x_NotifyObservers(evt);
148  }
149  break;
150  }
151  case IAppJob::eSuspended:
152  case IAppJob::eRunning:
153  if( m_Dispatcher->x_IsCanceled(job_rec->m_ID) ) break;
154  // intentional fall through
155 
156  case IAppJob::eCanceled:
157  {
158  CRef<CEvent> evt(new CAppJobNotification(job_rec->m_ID, new_state));
159  x_NotifyObservers(evt);
160  break;
161  }
162  default:
163  _ASSERT(false);
164  }
165 }
166 
167 
168 
170 {
171  if(m_Dispatcher) {
173  if(job_rec) {
174  if(job_rec->ActiveProgress()) {
175  pr = job_rec->m_Progress;
176  } else {
177  pr = job_rec->m_Job->GetProgress();
178  }
179  }
180  if(pr) {
181  CRef<CEvent> evt(new CAppJobNotification(job_rec->m_ID, *pr));
182  x_NotifyObservers(evt);
183  } else {
184  LOG_POST(Error << "CAppJobEventTranslator::OnJobProgress() - "
185  << " progress object is missing!");
186  }
187  }
188 }
189 
190 
CAppJobDispatcher.
CAppJobNotification Notification send by CAppJobEventTranslator.
CEventHandler.
CEvent - generic event implementation TODO TODO - Attachments.
Definition: event.hpp:86
CObjectException –.
Definition: ncbiobj.hpp:74
CObject –.
Definition: ncbiobj.hpp:180
CWeakObject –.
Definition: ncbiobj.hpp:2443
IAppJobError.
Definition: app_job.hpp:65
IAppJobProgress.
Definition: app_job.hpp:50
IAppJob.
Definition: app_job.hpp:82
#define NULL
Definition: ncbistd.hpp:225
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
bool x_IsCanceled(int job_id) const
CEventHandler & m_TargetEventHandler
void SetDispatcher(CAppJobDispatcher &disp)
CAppJobNotification(TJobID job_id, TJobState state)
CAppJobNotification.
void OnJobStateChanged(CAppJobDispatcher::SJobRecord *job_rec, TJobState new_state)
CAppJobEventTranslator(CEventHandler &target)
CAppJobEventTranslator.
CWeakIRef< CEventHandler > m_TargetWeakPtr
void Post(CRef< CEvent > evt, EDispatch disp_how=eDispatch_Default, int pool_name=ePool_Default)
Handles an event asynchronously (process and/or dispatch).
EJobState
Job states (describe FSM)
Definition: app_job.hpp:86
void OnJobProgress(CAppJobDispatcher::SJobRecord *job_rec)
CConstIRef< IAppJobProgress > m_Progress
CAppJobDispatcher::TJobID TJobID
virtual void x_NotifyObservers(CRef< CEvent > &evt)
CAppJobDispatcher * m_Dispatcher
@ eCanceled
Definition: app_job.hpp:91
@ eCompleted
Definition: app_job.hpp:89
@ eRunning
Definition: app_job.hpp:88
@ eSuspended
Definition: app_job.hpp:92
@ eFailed
Definition: app_job.hpp:90
bool NotNull(void) const THROWS_NONE
Check if pointer is not null – same effect as NotEmpty().
Definition: ncbiobj.hpp:744
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
TRefType Lock(void) const
Lock the object and return reference to it.
Definition: ncbiobj.hpp:2864
void Reset(void)
Reset the containing pointer to null.
Definition: ncbiobj.hpp:2722
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
SJobRecord describes a Job registered in Dispatcher.
#define _ASSERT
else result
Definition: token2.c:20
Modified on Mon Apr 22 04:04:34 2024 by modify_doxy.py rev. 669887