hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCEFileBuilder.cpp
Go to the documentation of this file.
1 #include <sstream>
2 #include <Poco/File.h>
3 #include <Poco/Logger.h>
4 
5 #include "DRCEFileStream.hpp"
6 #include "DRCEMessageConst.hpp"
7 #include "DRCEError.hpp"
9 
10 #include "DRCEFileBuilder.hpp"
11 
12 namespace HCE
13 {
14 namespace drce
15 {
16 //-----------------------------------------------------------------------------
17 FileBuilder::FileBuilder(CustomMessage& message_, const std::string& dir_)
18 : message(message_), dir(dir_)
19 {
20 }
21 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
23 DataFileBuilder::DataFileBuilder(CustomMessage& message_, const std::string& dir_)
24 : inherited(message_, dir_)
25 {
26 }
27 //-----------------------------------------------------------------------------
28 void DataFileBuilder::build(const DRCEResultData& resultData) throw (Poco::Exception)
29 {
30  if (resultData.getItemsCount()!=1)
31  throw Poco::Exception(message(message_const::EMPTY_OBJECT_INSTANCE), ERROR_BAD_DATA_CONTENT);
32 
33  std::string json = makeJson(resultData);
34  if (!json.empty() && resultData.getDataItem(0).getRequestId())
35  {
36  Poco::File dataFile(FileStream::getDataFileName(dir, resultData.getDataItem(0).getRequestId()));
37  if (dataFile.exists())
38  dataFile.remove();
39 
40  FileStream fileStream(dataFile.path(), FileStream::write);
41  fileStream << json;
42  }
43 }
44 //-----------------------------------------------------------------------------
45 std::string DataFileBuilder::makeJson(const DRCEResultData& resultData_) throw (Poco::Exception)
46 {
47  std::string json;
48  DRCEResultData resultData = resultData_;
49  DRCEResultDataSerializator resultSerializator(resultData);
50  if (!resultSerializator.serialize(json))
51  throw Poco::Exception(resultSerializator.getErrorMsg(), resultSerializator.getErrorCode());
52 
53  return json;
54 }
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 StatusFileBuilder::StatusFileBuilder(CustomMessage& message_, const std::string& dir_)
58 : inherited(message_, dir_)
59 {
60 }
61 //-----------------------------------------------------------------------------
62 void StatusFileBuilder::build(const DRCEResultData& resultData) throw (Poco::Exception)
63 {
64  if (resultData.getItemsCount()!=1)
65  throw Poco::Exception(message(message_const::EMPTY_OBJECT_INSTANCE), ERROR_BAD_DATA_CONTENT);
66 
67  std::string json = getStatusJson(resultData);
68  if (!json.empty() && resultData.getDataItem(0).getRequestId())
69  FileStream(FileStream::getStatusFileName(dir, resultData.getDataItem(0).getRequestId()), FileStream::write) << json;
70 }
71 //-----------------------------------------------------------------------------
72 std::string StatusFileBuilder::getStatusJson(const DRCEResultData& resultData) throw (Poco::Exception)
73 {
74  DRCEResultData localResultData;
75  for (size_t i=0;i<resultData.getItemsCount();++i)
76  {
77  DRCEResultDataItem resultDataItem(resultData.getDataItem(i));
78  cleanup(resultDataItem);
79  localResultData.addDataItem(std::forward<DRCEResultDataItem>(resultDataItem));
80  }
81  return makeJson(localResultData);
82 }
83 //-----------------------------------------------------------------------------
84 void StatusFileBuilder::cleanup(DRCEResultDataItem& resultDataItem)
85 {
86  resultDataItem.setStdoutStream("");
87  resultDataItem.setStderrStream("");
88  dynamic_cast<DRCEFilesList&>(resultDataItem).clear();
89  for (size_t i=0;i<resultDataItem.getSubtasksCount();++i)
90  {
91  DRCEResultDataItem resultItem(resultDataItem.getSubtaskItem(i));
92  cleanup(resultItem);
93  resultDataItem.setSubtaskItem(i, std::forward<DRCEResultDataItem>(resultItem));
94  }
95 }
96 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
98 RequestFileBuilder::RequestFileBuilder(CustomMessage& message_, const std::string& dir_)
99 : inherited(message_, dir_)
100 {
101 }
102 //-----------------------------------------------------------------------------
103 void RequestFileBuilder::build(DRCEInputJsonMessage& inputJsonMessage) throw (Poco::Exception)
104 {
105  std::string resultJson;
106  if (!inputJsonMessage.serialize(resultJson))
107  throw Poco::Exception(inputJsonMessage.getErrorMsg(), inputJsonMessage.getErrorCode());
108 
109  if (!resultJson.empty() && inputJsonMessage.getRequestId())
110  {
111  Poco::File requestFile(FileStream::getRequestFileName(dir, inputJsonMessage.getRequestId()));
112  if (requestFile.exists())
113  requestFile.remove();
114 
115  FileStream fileStream(requestFile.path(), FileStream::write);
116  fileStream << resultJson;
117  }
118 }
119 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
121 } // end namespace drce
122 } // end namespace HCE