comparison Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp @ 2616:2f3007bf0708 jobs

event queues in Lua, serialization of sequence of operations
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 May 2018 12:25:37 +0200
parents 25225f0b4f33
children 1232922c8793
comparison
equal deleted inserted replaced
2614:3200223f9ade 2616:2f3007bf0708
40 namespace Orthanc 40 namespace Orthanc
41 { 41 {
42 class SequenceOfOperationsJob::Operation : public boost::noncopyable 42 class SequenceOfOperationsJob::Operation : public boost::noncopyable
43 { 43 {
44 private: 44 private:
45 size_t index_;
45 JobOperationValues originalInputs_; 46 JobOperationValues originalInputs_;
46 JobOperationValues workInputs_; 47 JobOperationValues workInputs_;
47 std::auto_ptr<IJobOperation> operation_; 48 std::auto_ptr<IJobOperation> operation_;
48 std::list<Operation*> nextOperations_; 49 std::list<Operation*> nextOperations_;
49 size_t currentInput_; 50 size_t currentInput_;
50 51
51 public: 52 public:
52 Operation(IJobOperation* operation) : 53 Operation(size_t index,
53 operation_(operation), 54 IJobOperation* operation) :
54 currentInput_(0) 55 index_(index),
56 operation_(operation),
57 currentInput_(0)
55 { 58 {
56 if (operation == NULL) 59 if (operation == NULL)
57 { 60 {
58 throw OrthancException(ErrorCode_NullPointer); 61 throw OrthancException(ErrorCode_NullPointer);
59 } 62 }
83 currentInput_ = 0; 86 currentInput_ = 0;
84 } 87 }
85 88
86 void AddNextOperation(Operation& other) 89 void AddNextOperation(Operation& other)
87 { 90 {
91 if (other.index_ <= index_)
92 {
93 throw OrthancException(ErrorCode_InternalError);
94 }
95
88 if (currentInput_ != 0) 96 if (currentInput_ != 0)
89 { 97 {
90 // Cannot add input after processing has started 98 // Cannot add input after processing has started
91 throw OrthancException(ErrorCode_BadSequenceOfCalls); 99 throw OrthancException(ErrorCode_BadSequenceOfCalls);
92 } 100 }
137 } 145 }
138 } 146 }
139 147
140 currentInput_ += 1; 148 currentInput_ += 1;
141 } 149 }
150
151 void Serialize(Json::Value& target) const
152 {
153 target = Json::objectValue;
154 operation_->Serialize(target["Operation"]);
155 originalInputs_.Serialize(target["OriginalInputs"]);
156 workInputs_.Serialize(target["WorkInputs"]);
157
158 Json::Value tmp = Json::arrayValue;
159 for (std::list<Operation*>::const_iterator it = nextOperations_.begin();
160 it != nextOperations_.end(); ++it)
161 {
162 tmp.append(static_cast<int>((*it)->index_));
163 }
164
165 target["NextOperations"] = tmp;
166 }
142 }; 167 };
143 168
144 169
145 // Invoked from constructors 170 SequenceOfOperationsJob::SequenceOfOperationsJob() :
146 void SequenceOfOperationsJob::Setup() 171 done_(false),
147 { 172 current_(0),
148 done_ = false; 173 trailingTimeout_(boost::posix_time::milliseconds(1000))
149 current_ = 0; 174 {
150 trailingTimeout_ = boost::posix_time::milliseconds(1000);
151 } 175 }
152 176
153 177
154 SequenceOfOperationsJob::~SequenceOfOperationsJob() 178 SequenceOfOperationsJob::~SequenceOfOperationsJob()
155 { 179 {
158 if (operations_[i] != NULL) 182 if (operations_[i] != NULL)
159 { 183 {
160 delete operations_[i]; 184 delete operations_[i];
161 } 185 }
162 } 186 }
187 }
188
189
190 void SequenceOfOperationsJob::SetDescription(const std::string& description)
191 {
192 boost::mutex::scoped_lock lock(mutex_);
193 description_ = description;
163 } 194 }
164 195
165 196
166 void SequenceOfOperationsJob::Register(IObserver& observer) 197 void SequenceOfOperationsJob::Register(IObserver& observer)
167 { 198 {
187 if (IsDone()) 218 if (IsDone())
188 { 219 {
189 throw OrthancException(ErrorCode_BadSequenceOfCalls); 220 throw OrthancException(ErrorCode_BadSequenceOfCalls);
190 } 221 }
191 222
192 that_.operations_.push_back(new Operation(operation)); 223 size_t index = that_.operations_.size();
224
225 that_.operations_.push_back(new Operation(index, operation));
193 that_.operationAdded_.notify_one(); 226 that_.operationAdded_.notify_one();
194 227
195 return that_.operations_.size() - 1; 228 return index;
196 } 229 }
197 230
198 231
199 void SequenceOfOperationsJob::Lock::AddInput(size_t index, 232 void SequenceOfOperationsJob::Lock::AddInput(size_t index,
200 const JobOperationValue& value) 233 const JobOperationValue& value)
322 void SequenceOfOperationsJob::GetPublicContent(Json::Value& value) 355 void SequenceOfOperationsJob::GetPublicContent(Json::Value& value)
323 { 356 {
324 boost::mutex::scoped_lock lock(mutex_); 357 boost::mutex::scoped_lock lock(mutex_);
325 358
326 value["CountOperations"] = static_cast<unsigned int>(operations_.size()); 359 value["CountOperations"] = static_cast<unsigned int>(operations_.size());
360 value["Description"] = description_;
361 }
362
363
364 void SequenceOfOperationsJob::GetInternalContent(Json::Value& value)
365 {
366 boost::mutex::scoped_lock lock(mutex_);
367
368 value = Json::arrayValue;
369 for (size_t i = 0; i < operations_.size(); i++)
370 {
371 Json::Value operation = Json::objectValue;
372 operations_[i]->Serialize(operation);
373 value.append(operation);
374 }
327 } 375 }
328 } 376 }