highlighter application  1.1
HCE project utils : highlighter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
Application.cpp
Go to the documentation of this file.
1 
13 #include <msgpack.hpp>
14 #include <Poco/Timestamp.h>
15 #include <Poco/Exception.h>
16 #include <sstream>
17 
18 #include "Application.hpp"
20 #include "CRC64.h"
21 #include "ErrorStruct.hpp"
22 #include "InDataHighlight.hpp"
25 #include "HighlightComponent.hpp"
27 #include "HighlightJsonWrapper.hpp"
28 #include "Logger.hpp"
29 #include "SerializeCacheData.hpp"
30 
31 #define COMPONENTS_COUNT 4
32 
33 using namespace HCE;
34 using namespace HCE::component;
35 using namespace HCE::exception;
36 
37 namespace HCE
38 {
39  extern Poco::AutoPtr<Poco::Util::AbstractConfiguration> _config;
40 }
41 
43 
44 Application::Application():exitFlag(false), componentManagerPool(nullptr), crossThreadBufPtr(nullptr), contentsStoragePtr(nullptr)
45 {
46 }
47 
49 {
50 }
51 
53 {
55 }
56 
67 {
68  Poco::SharedPtr<Poco::ThreadPool> thPool = Poco::SharedPtr<Poco::ThreadPool>(new Poco::ThreadPool());
69  Poco::SharedPtr<ComponentBase> localComponent = nullptr;
70  Poco::SharedPtr<ComponentManager> localComponentManager = nullptr;
71  componentManagerPool = Poco::SharedPtr<ComponentManagerPoolIf>(new ComponentManagerPool());
72 
73  localComponentManager = Poco::SharedPtr<ComponentManager>(new ComponentManager());
74  localComponentManager->setThreadPool(thPool);
75 
76  for(unsigned int i = 0; i < COMPONENTS_COUNT; i++)
77  {
79  HighlightStringGenerator &highlightStringGenerator = (localComponent.cast<HighlightComponent>())->getHighlightStringGeneratorReference();
80  highlightStringGenerator.setHighlightStringSticker(new HighlightStringStickerSimple());
81  HCE::HighlightingAlgorithmsInit(highlightStringGenerator);
82  localComponentManager->addComponent(localComponent);
83  }
84  componentManagerPool->addManager(HCE::CT_HIGHLIGHT, localComponentManager, true);
85  localComponentManager->start();
86 }
87 
95 void Application::addWCache(unsigned long long &resCrc, std::string &wCacheBase64)
96 {
97  std::string wcacheString = HCE::decodeBase64(wCacheBase64);
98  if(wcacheString.length() > 0)
99  {
100  SerializeCacheData serializeCacheData;
101  msgpack::unpacked msg;
102  msgpack::unpack(&msg, wcacheString.c_str(), wcacheString.length());
103  msgpack::object obj = msg.get();
104  try
105  {
106  obj.convert<SerializeCacheData>(&serializeCacheData);
107  if(serializeCacheData.resCrc != resCrc)
108  {
109  std::cerr << ">>> Bad Resource cache CRC "<< std::endl;
110  }
111  else
112  {
113  ContentsStorageBase::WordPosPtrType wordPosPtr = ContentsStorageBase::WordPosPtrType(new std::vector<std::map<unsigned long long, std::vector<WordPos> > >());
114  (*wordPosPtr) = serializeCacheData.data;
115  (contentsStoragePtr.cast<ContentsStorageSimple>())->cacheAddElement(resCrc, wordPosPtr);
116  }
117  }
118  catch(msgpack::type_error &err)
119  {
120  std::cerr << ">>> Bad MSGPACK format "<< std::endl;
121  }
122  }
123 }
124 
125 void Application::extractWCache(msgpack::sbuffer &msgPackBuf, Poco::SharedPtr<OutDataStruct> outDataStructPtr)
126 {
127  std::string inBuff(msgPackBuf.data(), msgPackBuf.size());
128  std::string base64Buff = ((msgPackBuf.size() > 0 ) ? HCE::encodeBase64(inBuff) : "");
129  outDataStructPtr->addWcache(base64Buff);
130 }
131 
141 void Application::highlightComponentExecutor(Poco::SharedPtr<InDataStruct> inDataStructPtr, Poco::SharedPtr<OutDataStruct> outDataStructPtr)
142 {
143  Poco::SharedPtr<DataBase> outData = nullptr;
144  Poco::SharedPtr<InDataHighlight> inDataHighlight = nullptr;
145  CRC64 crc64;
146 // a) ------------------------------------------------------------------
147  crossThreadBufPtr = Poco::SharedPtr<CrossThreadBuf>(new CrossThreadBuf());
148  crossThreadBufPtr->setIsSendWordsMapBack(inDataStructPtr->getSendCacheBack());
149  for(unsigned int y = 0; y < inDataStructPtr->getContentsCount(); y++)
150  {
151  unsigned long long resCrc = 0llu;
152  CrossThreadBuf::ResElement inOutDataElem;
153  resCrc = crc64.calc(inDataStructPtr->getContent(y).c_str(), inDataStructPtr->getContent(y).length());
155  {
156  if(inDataStructPtr->getWcachesCount() > y)
157  {
158  addWCache(resCrc, inDataStructPtr->getWcache(y));
159  }
160  }
161  inOutDataElem.setInData(inDataStructPtr->getContent(y));
162  inOutDataElem.setOutData("");
163  inOutDataElem.setId(y);
164  inOutDataElem.setResCrc(resCrc);
165  crossThreadBufPtr->addInOutData(inOutDataElem);
166  }
167  unsigned int timeout = HCE::_config->getInt(HIGHLIGHT_TIMEOUT_MLS_NAME);
168  if(inDataStructPtr->getParamsCount() > 0 && inDataStructPtr->getParams(0).timeout != 0)
169  {
170  timeout = inDataStructPtr->getParams(0).timeout;
171  }
172  crossThreadBufPtr->setTimeout(timeout);
173 // b) ------------------------------------------------------------------
174  for(unsigned int y = 0; y < inDataStructPtr->getContentsCount(); y++)
175  {
176  inDataHighlight = Poco::SharedPtr<InDataHighlight>(new InDataHighlight(CT_HIGHLIGHT));
177  inDataHighlight->setInDataStructPtr(inDataStructPtr);
178  inDataHighlight->setContentsStorageBasePtr(contentsStoragePtr);
179  inDataHighlight->setCrossThreadBufPtr(crossThreadBufPtr);
180  try
181  {
182  componentManagerPool->addInData(inDataHighlight);
183  }
184  catch(ComponentExceptionBase &excp)
185  {
186  }
187  }
188 // c) ------------------------------------------------------------------
189 
190 // cppcheck-suppress variableScope
191  unsigned int tOut = 0;
192  while(true)
193  {
194  tOut = crossThreadBufPtr->getRestTimeout();
195  if(tOut > 0)
196  {
197  if(crossThreadBufPtr->isTaskFinished())
198  {
199  break;
200  }
201  crossThreadBufPtr->waitCv(tOut);
202  }
203  else
204  {
205  break;
206  }
207  }
208 
209  try
210  {
211  componentManagerPool->clearTaskQueue(CT_HIGHLIGHT);
212  }
213  catch(ComponentExceptionBase &excp)
214  {
215  }
216 // d) ------------------------------------------------------------------
217  unsigned int highlightCounterSum = 0;
218  crossThreadBufPtr->lock();
219  if(!crossThreadBufPtr->isTaskFinished())
220  {
222  }
223  for(unsigned int y = 0; y < crossThreadBufPtr->elemsCount(); y++)
224  {
225  CrossThreadBuf::ResElement &elem = crossThreadBufPtr->getElem(y);
226  std::string &outputStr = elem.getOutData();
227  outDataStructPtr->addContent(outputStr);
228  highlightCounterSum += elem.getHighlightCounter();
229  if(elem.getMsgPackBuff().size() > 0)
230  {
231  extractWCache(elem.getMsgPackBuff(), outDataStructPtr);
232  }
233  else
234  {
235  outDataStructPtr->addWcache("");
236  }
237  }
238  outDataStructPtr->setFoundCounter(highlightCounterSum);
239  outDataStructPtr->setHighlightedCounter(highlightCounterSum);
240  crossThreadBufPtr->unlock();
241  crossThreadBufPtr = nullptr;
242 }
243 
253 int Application::main(const std::vector<std::string>& args)
254 {
255  int ret = Poco::Util::Application::EXIT_OK;
256 // a) ------------------------------------------------------------------
258  {
261  contentsStoragePtr = Poco::SharedPtr<ContentsStorageBase>(new ContentsStorageSimple(HCE::_config->getInt(REFINES_COUNT_NAME)));
262  HighlightJsonWrapper highlightJsonWrapper;
263  Poco::Timestamp timer;
264  while(!exitFlag)
265  {
266  timer.update();
267  std::string incomeString;
268  Poco::SharedPtr<InDataStruct> inDataStructPtr = nullptr;
269  Poco::SharedPtr<OutDataStruct> outDataStructPtr = Poco::SharedPtr<OutDataStruct>(new OutDataStruct());
270  outDataString = "";
271 // b) ------------------------------------------------------------------
272  readData(incomeString);
273  if(!incomeString.empty())
274  {
275  try
276  {
277  inDataStructPtr = highlightJsonWrapper.inDataFromJsomString(incomeString);
278  }
279  catch(HighlightJsonException &excpt)
280  {
281  std::cerr << ">>> Error with parsing incoming JSON = ["<< excpt.what() << "]" << std::endl;
282  }
283  if(!inDataStructPtr.isNull() && inDataStructPtr->getParamsCount() > 0)
284  {
285  timer.update();
286 // c) ------------------------------------------------------------------
287  highlightComponentExecutor(inDataStructPtr, outDataStructPtr);
288  }
289  }
290  outDataStructPtr->setErrorCode(errorStruct.getErrorCode());
291  outDataStructPtr->setErrorMsg(errorStruct.resolveErrorString());
292  outDataStructPtr->setHighlightedCounter(outDataStructPtr->getHighlightedCounter());
293  outDataStructPtr->setFoundCounter(outDataStructPtr->getFoundCounter());
294  try
295  {
296 // d) -----------------------------------------------------------------
297  highlightJsonWrapper.jsonStringFromOutData(outDataStructPtr, outDataString, timer.elapsed());
298  }
299  catch(HighlightJsonException &excpt)
300  {
301  std::cerr << ">>> Error with creating outgoing JSON String = "<< std::endl;
302  }
303  std::cout << outDataString << std::endl;
304  break;
305  }
306  }
307  else
308  {
309  ret = Poco::Util::Application::EXIT_CONFIG;
310  }
311  return ret;
312 }
313 
314 void Application::readData(std::string &incomeString)
315 {
316  std::stringbuf sBuff;
317  std::cin.get(sBuff, BREAK_SYMBOL);
318  incomeString = sBuff.str();
319 }
320 
327 {
328  bool ret = true;
329  try
330  {
333  }
334  catch(const Poco::NotFoundException& exception)
335  {
336  Poco::Message msg("Application",exception.displayText() + ServerApplication::ADDITION_INFORM_STRING_NOT_FOUND, Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);
338  ret = false;
339  }
340  return ret;
341 }
342 
346 // cppcheck-suppress unusedFunction
347 void Application::handleHelp(const std::string& name,const std::string& value)
348 {
349  this->_showHelpInfo = true;
350  Poco::Util::HelpFormatter helpFormatter(options());
351  helpFormatter.setCommand(commandName());
352  helpFormatter.setUsage("OPTIONS");
353  helpFormatter.setHeader("Some options that demonstrates some of the features of the Highlighter application");
354  helpFormatter.format(std::cout);
355  ::_exit(Poco::Util::Application::EXIT_OK);
356 }
357 
361 // cppcheck-suppress unusedFunction
362 void Application::handleVersion(const std::string& name,const std::string& value)
363 {
364  std::string appVersion = std::string(APP_VERSION) + " " + std::string(__DATE__) + " " + std::string(__TIME__);
365  std::cout << appVersion << std::endl;
366  ::_exit(Poco::Util::Application::EXIT_OK);
367 }