hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCEFileExtractor.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <Poco/File.h>
5 
6 #include "DRCEFileStream.hpp"
7 #include "DRCEMessageConst.hpp"
8 #include "DRCEError.hpp"
11 #include "DRCEFileExtractor.hpp"
12 
13 namespace HCE
14 {
15 namespace drce
16 {
17 //-----------------------------------------------------------------------------
18 DRCEResultDataItem DataFileExtractor::extract(const std::string& dataFileName) throw (Poco::Exception)
19 {
20  Poco::File file(dataFileName);
21  if (!file.exists())
22  throw Poco::Exception(message(message_const::NOT_EXISTS_DATA_FILE, dataFileName), ERROR_READ_DATA_FILE);
23 
24  FileStream dataFile(dataFileName, FileStream::read);
25  if (!dataFile.isOpen())
26  {
27  std::stringstream errMsg;
28  FILE* pFile = fopen(dataFileName.c_str(), "r");
29  if (pFile==NULL)
30  errMsg << message(message_const::ERROR_READ_DATA_FILE, dataFileName) << " (" << strerror(errno) << ")";
31  else
32  fclose(pFile);
33 
34  throw Poco::Exception(errMsg.str(), ERROR_READ_DATA_FILE);
35  }
36 
37  DRCEResultData resultData;
38  std::string dataFileData;
39  dataFile >> dataFileData;
40 
41  if (!dataFileData.empty())
42  {
43  DRCEResultDataSerializator resultSerializator(resultData);
44  if (!resultSerializator.unserialize(dataFileData))
45  throw Poco::Exception(resultSerializator.getErrorMsg(), resultSerializator.getErrorCode());
46 
47  if (resultData.getItemsCount()!=1)
48  throw Poco::Exception(message(message_const::BAD_FILE_CONTENT, dataFileName), ERROR_BAD_DATA_CONTENT);
49  }
50  else
51  {
52  DRCEResultDataItem resultDataItem;
53  resultDataItem.setErrorCode(ERROR_BAD_DATA_CONTENT);
54  resultDataItem.setErrorMessage(message(message_const::DATA_FILE_IS_EMPTY, dataFileName));
55  resultDataItem.setState(static_cast<unsigned int>(DRCETaskRequest::TaskState::UNDEFINED));
56  resultData.addDataItem(std::forward<DRCEResultDataItem>(resultDataItem));
57  }
58  return resultData.getDataItem(0);
59 }
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 DRCEInputJsonMessage RequestFileExtractor::extract(const std::string& requestFileName) throw (Poco::Exception)
63 {
64  Poco::File file(requestFileName);
65  if (!file.exists())
66  throw Poco::Exception(message(message_const::NOT_EXISTS_REQUEST_FILE, requestFileName), ERROR_READ_REQUEST_FILE);
67 
68  FileStream requestFile(requestFileName, FileStream::read);
69  if (!requestFile.isOpen())
70  {
71  std::stringstream errMsg;
72  FILE* pFile = fopen(requestFileName.c_str(), "r");
73  if (pFile==NULL)
74  errMsg << "Errno: " << strerror(errno) << std::endl;
75  else
76  fclose(pFile);
77 
78  errMsg << message(message_const::ERROR_READ_REQUEST_FILE, requestFileName);
79  throw Poco::Exception(errMsg.str(), ERROR_READ_REQUEST_FILE);
80  }
81 
82  std::string requestFileData;
83  requestFile >> requestFileData;
84 
85  if (requestFileData.empty())
86  throw Poco::Exception(message(message_const::EMPTY_FILE_CONTENT, requestFileName), ERROR_BAD_DATA_CONTENT);
87 
88  DRCEInputJsonMessage inputJsonMessage(requestFileData);
89  if (inputJsonMessage.isError())
90  throw Poco::Exception(message(message_const::BAD_FILE_CONTENT, requestFileName)+
91  message(message_const::ERROR, inputJsonMessage.getErrorMsg()), inputJsonMessage.getErrorCode());
92 
93  return inputJsonMessage;
94 }
95 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
97 } // end namespace drce
98 } // end namespace HCE