hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCEListAllTasks.cpp
Go to the documentation of this file.
1 #include <Poco/JSON/JSON.h>
2 #include <Poco/JSON/Stringifier.h>
3 #include <Poco/JSON/Parser.h>
4 #include <Poco/JSON/JSONException.h>
5 #include <Poco/Dynamic/Var.h>
6 #include <sys/types.h>
7 #include <dirent.h>
8 #include "string.h"
9 #include "errno.h"
10 
11 #include <Poco/File.h>
12 #include <Poco/Path.h>
13 
14 #include "DRCEError.hpp"
15 #include "DRCEMessageConst.hpp"
16 #include "DRCEJsonMessageConst.hpp"
17 #include "DRCEFileStream.hpp"
18 #include "DRCEListAllTasks.hpp"
19 
20 namespace HCE
21 {
22 namespace drce
23 {
24 //-----------------------------------------------------------------------------
26 : taskId(0), infoData(""), timeModified(0)
27 {
28 }
29 //-----------------------------------------------------------------------------
31 : taskId(0), infoData(""), timeModified(0)
32 {
33  (*this) = rhs;
34 }
35 //-----------------------------------------------------------------------------
37 : taskId(0), infoData(""), timeModified(0)
38 {
39  (*this) = std::forward<TaskInfoData>(rhs);
40 }
41 //-----------------------------------------------------------------------------
43 {
44  if (this != &rhs)
45  {
46  taskId = rhs.taskId;
47  infoData = rhs.infoData;
49  }
50  return *this;
51 }
52 //-----------------------------------------------------------------------------
54 {
55  if (this != &rhs)
56  {
57  taskId = std::move(rhs.taskId);
58  infoData = std::move(rhs.infoData);
59  timeModified = std::move(rhs.timeModified);
60  }
61  return *this;
62 }
63 //-----------------------------------------------------------------------------
65 {
66  taskId = 0;
67  infoData.clear();
68  timeModified = 0;
69 }
70 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
74 {
75 }
76 //-----------------------------------------------------------------------------
77 bool DRCEListAllTasks::serialize(std::string& json)
78 {
79  _isError = false;
81  errorMsg.clear();
82  try
83  {
84  Poco::JSON::Array::Ptr pItems = new Poco::JSON::Array();
85  if (pItems.isNull())
86  throw Poco::Exception(message_const::objectInstanceWasNotCreated);
87 
88  for (size_t i=0;i<getItemsCount();++i)
89  {
90  Poco::JSON::Object::Ptr pItemElem = new Poco::JSON::Object();
91 
92  if (!pItemElem.isNull())
93  {
94  TaskInfoData taskInfoData(getItem(i));
95  pItemElem->set(drce_json_message_const::taskId, taskInfoData.taskId);
96  pItemElem->set(drce_json_message_const::info, taskInfoData.infoData);
97  pItemElem->set(drce_json_message_const::modified, taskInfoData.timeModified);
98  pItems->add(pItemElem);
99  }
100  }
101 
102  std::stringstream ostr;
103  Poco::JSON::Stringifier::stringify(pItems, ostr);
104  json = ostr.str();
105  }
106  catch(std::exception& e)
107  {
108  _isError = true;
109  errorMsg = e.what();
111  }
112  return !_isError;
113 }
114 //-----------------------------------------------------------------------------
115 bool DRCEListAllTasks::unserialize(const std::string& json)
116 {
117  clear();
118  _isError = false;
120  errorMsg.clear();
121 
122  try
123  {
124  Poco::JSON::Parser parser;
125  Poco::Dynamic::Var res = parser.parse(json);
126  Poco::JSON::Array::Ptr pItems = res.extract<Poco::JSON::Array::Ptr>();
127  if (!pItems.isNull())
128  {
129  for (size_t i=0;i<pItems->size();++i)
130  {
131  Poco::JSON::Object::Ptr pItemElem = pItems->getObject(i);
132  if (!pItemElem.isNull())
133  {
134  TaskInfoData taskInfoData;
135 
136  Poco::Dynamic::Var tmp = pItemElem->get(drce_json_message_const::taskId);
137  taskInfoData.taskId = convertVarToNumeric<unsigned int>(tmp, 0);
138 
139  tmp = pItemElem->get(drce_json_message_const::info);
140  if (tmp.isString())
141  taskInfoData.infoData = tmp.convert<std::string>();
142 
143  tmp = pItemElem->get(drce_json_message_const::modified);
144  taskInfoData.timeModified = convertVarToNumeric<time_t>(tmp, 0);
145 
146  addItem(std::forward<TaskInfoData>(taskInfoData));
147  }
148  }
149  }
150  }
151  catch(Poco::JSON::JSONException& e)
152  {
153  _isError = true;
154  errorMsg = e.message();
156  }
157  catch(Poco::Exception& e)
158  {
159  _isError = true;
160  errorMsg = e.message();
161  errorCode = e.code();
162  }
163  catch(std::exception& e)
164  {
165  _isError = true;
166  errorMsg = e.what();
168  }
169  return !_isError;
170 }
171 //-----------------------------------------------------------------------------
172 bool DRCEListAllTasks::rebuild(const std::string& sourceDir)
173 {
174  clear();
175  _isError = false;
177  errorMsg.clear();
178  try
179  {
180  Poco::File sources(sourceDir);
181  if (sources.exists() && sources.isDirectory())
182  {
183  std::vector<std::string> files;
184  sources.list(files);
185 
186  for (const std::string& fileName : files)
187  {
188  Poco::Path path(sourceDir);
189  path.append(fileName);
190 
191  if (FileStream::isStatusFileName(path.toString()))
192  {
193  unsigned int taskId = 0;
194  std::istringstream(path.getFileName()) >> taskId;
195 
196  if (taskId)
197  {
198  std::string fileContent;
199  FileStream fileStream(path.toString(), FileStream::read);
200  fileStream >> fileContent;
201  fileStream.close();
202 
203  TaskInfoData taskInfoData;
204  taskInfoData.taskId = taskId;
205  taskInfoData.infoData = fileContent;
206  taskInfoData.timeModified = Poco::File(path).getLastModified().epochTime();
207 
208  addItem(std::forward<TaskInfoData>(taskInfoData));
209  }
210  }
211  }
212  }
213  }
214  catch(Poco::Exception& e)
215  {
216  _isError = true;
218  errorMsg = e.message();
219  }
220  catch(std::exception& e)
221  {
222  _isError = true;
224  errorMsg = e.what();
225  }
226  return !_isError;
227 }
228 //-----------------------------------------------------------------------------
229 std::string DRCEListAllTasks::toString(void)
230 {
231  std::string json;
232  serialize(json);
233  return json;
234 }
235 //-----------------------------------------------------------------------------
236 //-----------------------------------------------------------------------------
237 }// end namespace drce
238 } // end namespace HCE