You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
8.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "IM_ClientHandler.h"
  2. #include "IM_Kernel.h"
  3. #include "../../lib/components/base/basicFunctionality.h"
  4. #include "../../lib/data/json/json_tool.h"
  5. #include "../../lib/data/protocol/xmlParser.h"
  6. #include <strings.h>
  7. #include <json/value.h>
  8. #include <json/writer.h>
  9. #include <json/reader.h>
  10. #define LOG( X ) m_pLog->Log( "NCH", this, X )
  11. #ifdef DEBUG
  12. #define LOG_ERROR( X ) {\
  13. char _tBfrErrMSG234[256];\
  14. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  15. m_pLog->Log( "NCH", this, _LOG_ERROR, _tBfrErrMSG234 );\
  16. }
  17. #else
  18. #define LOG_ERROR( X ) m_pLog->Log( "NCH", this, _LOG_ERROR, X );
  19. #endif
  20. // Constructor implementation
  21. IM_ClientHandler::IM_ClientHandler( int SOCKET, networkServiceEngine *ptr ) : networkClientHandler( SOCKET, ptr ), IM_ClientProtocol(){
  22. m_pLog = basicLogCollection::getInstance();
  23. m_pDBA = IM_DataBaseAdapter::getInstance();
  24. m_pMyCurrentJob = NULL;
  25. if( create() ){
  26. m_pPort = new serverPort( SOCKET );
  27. if( m_pPort != NULL ){
  28. setTransport( m_pPort );
  29. #ifdef DEBUG
  30. LOG("Port Created OK!");
  31. #endif
  32. if( m_pPort->create() ){
  33. LOG("Port Init OK!");
  34. } else LOG_ERROR("Not Connected!");
  35. } else LOG_ERROR("Port Not Created!");
  36. } else LOG_ERROR( "Protocol Parser Creation Failed!");
  37. };
  38. // Destructor implementation
  39. IM_ClientHandler::~IM_ClientHandler(){
  40. if( m_pPort != NULL ) delete( m_pPort );
  41. if( m_pMyCurrentJob ) free( (IM_JOB *)m_pMyCurrentJob );
  42. };
  43. bool IM_ClientHandler::getStatus( void ){
  44. if( m_pPort!= NULL ) {
  45. return m_pPort->getStatus();
  46. };
  47. return false;
  48. };
  49. bool IM_ClientHandler::handleItem( IM_ITEM_DATA *ptr ){
  50. if( ptr ) {
  51. if( m_pEngine != NULL ) {
  52. IM_Kernel *tKernel = (IM_Kernel *)m_pEngine;
  53. IM_JOB *tJOB;
  54. tJOB = (IM_JOB*)malloc(sizeof(IM_JOB));
  55. if( tJOB ) {
  56. if( m_pMyCurrentJob ) {
  57. LOG("CLEANNING UNCLEAN JOB!");
  58. //AnyWay Clean Memory that was Used!
  59. free((IM_JOB *)m_pMyCurrentJob);
  60. };
  61. m_pMyCurrentJob = (void*) tJOB;
  62. memset( tJOB, 0 , sizeof(IM_JOB));
  63. tJOB->dbID = ptr->item_id;
  64. tJOB->image_type = ptr->type;
  65. tJOB->job_type = ptr->process_type;
  66. tJOB->width = ptr->width;
  67. tJOB->height = ptr->height;
  68. memcpy( tJOB->md5, ptr->md5, 32 );
  69. tKernel->enqueueJob( tJOB );
  70. };
  71. };
  72. free( ptr );
  73. };
  74. return true;
  75. };
  76. bool IM_ClientHandler::packetHandler( protocolPackage *Package ){
  77. if( Package ) {
  78. if( Package->m_pData ) {
  79. if (Package->m_nSize > 3 ) {
  80. if( Package->m_pData[Package->m_nSize-4] == 13 ) Package->m_pData[Package->m_nSize-4] = 0;
  81. if( Package->m_pData[Package->m_nSize-3] == 10 ) Package->m_pData[Package->m_nSize-3] = 0;
  82. }
  83. Package->m_pData[Package->m_nSize-2] = 0;
  84. Package->m_pData[Package->m_nSize-1] = 0;
  85. LOG( (CSTR) Package->m_pData );
  86. try{
  87. XMLNode node1 = XMLNode::parseString( (const char *)Package->m_pData, "root");
  88. if( !node1.isEmpty() ){
  89. LOG( node1.getName() );
  90. if( strstr(node1.getName(),"ClockSync")!=NULL ){
  91. #ifdef __linux__
  92. unsigned long long t0, t1, t2, t3;
  93. #else
  94. unsigned __int64 t0, t1, t2, t3;
  95. #endif
  96. XMLNode node3 = node1.getChildNode("t0", 0);
  97. XMLNode node4 = node1.getChildNode("t1", 0);
  98. XMLNode node5 = node1.getChildNode("t2", 0);
  99. /*
  100. t3 = getSHDTimeValue();
  101. #ifdef __linux__
  102. t0 = atoll(node3.getText(0));
  103. t1 = atoll(node4.getText(0));
  104. t2 = atoll(node5.getText(0));
  105. #else
  106. t0 = _atoi64(node3.getText(0));
  107. t1 = _atoi64(node4.getText(0));
  108. t2 = _atoi64(node5.getText(0));
  109. #endif
  110. int offset = ((t1-t0)+(t2-t3))/2;
  111. int latency = (t3-t0)-(t2-t1);
  112. int jagajaga = offset+latency;
  113. //if( 0 & correctSHTime( jagajaga ) ){
  114. // LOG("Time is Synced");
  115. // g_iTimeDiff = 0;
  116. //} else {
  117. // LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
  118. g_iTimeDiff = jagajaga;
  119. //};
  120. */
  121. };
  122. };
  123. } catch( exception e ){
  124. LOG_ERROR("Some Error in XML Parser!");
  125. };
  126. // JSON Parsing
  127. try{
  128. Json::Value root;
  129. Json::Reader reader;
  130. if( !reader.parse( (char *)Package->m_pData, root ) ){
  131. LOG_ERROR( "The package data is nor a valid JSON data nor valid XML data" );
  132. return true;
  133. };
  134. if( root.size() ){
  135. IM_ITEM_DATA * tData = NULL;
  136. Json::Value tVal;
  137. tVal = root["item_id"];
  138. if( tVal != NULL ){
  139. int itemID = atoi( tVal.asString().c_str() ) ;
  140. if( itemID > 0 ) {
  141. tData = m_pDBA->getItemDataById( itemID );
  142. if( tData!= NULL ) tData->process_type = IM_PROC_TYPE_DNN;
  143. };
  144. };
  145. tVal = root["item_md5"];
  146. if( tVal != NULL ){
  147. if( strlen( tVal.asString().c_str())== 32 ){
  148. tData = m_pDBA->getItemDataByMd5( (CSTR) tVal.asString().c_str() );
  149. if( tData!= NULL ) tData->process_type = IM_PROC_TYPE_DNN;
  150. };
  151. };
  152. tVal = root["process_type"];
  153. if( tVal != NULL ){
  154. if( tData!= NULL ) tData->process_type = atoi( tVal.asString().c_str());
  155. };
  156. if( tData!= NULL ){
  157. return handleItem( tData );
  158. } else {
  159. m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"ERROR\"}\n", 19 );
  160. }
  161. };
  162. } catch ( exception e ){
  163. LOG_ERROR("Some Error in JSON Parser!");
  164. m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"ERROR\"}\n", 19 );
  165. };
  166. };
  167. };
  168. return true;
  169. };
  170. bool IM_ClientHandler::dispatchEvent( EVENT_CONTAINER *Evt ){
  171. if( Evt!= NULL ) {
  172. if( Evt->m_eClass == EVT_CLASS_IMAGE ){
  173. if( Evt->m_eType == EVT_READY ){
  174. IM_RESULT *tData = (IM_RESULT*) Evt->m_pEvtData;
  175. if( tData!= NULL ) {
  176. if( tData->pJOB == m_pMyCurrentJob ) {
  177. LOG("JOB Finished Sucessfully!");
  178. for( int i=0; i < tData->nBoxCount; i++ ){
  179. m_pDBA->insertItemROI(
  180. tData->pJOB->dbID,
  181. tData->boxes[i].classID,
  182. tData->boxes[i].weight,
  183. tData->boxes[i].x,
  184. tData->boxes[i].y,
  185. tData->boxes[i].width,
  186. tData->boxes[i].height
  187. );
  188. };
  189. //handle finished Job
  190. free( (IM_JOB *)m_pMyCurrentJob );
  191. m_pMyCurrentJob = NULL;
  192. free( (IM_CLASS_BOUNDARY_BOX *)tData->boxes );
  193. free( (IM_RESULT *)Evt->m_pEvtData );
  194. Evt->m_pEvtData = NULL;
  195. m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"OK\"}\n", 16 );
  196. };
  197. } else {
  198. LOG("JOB Finished Without Detection!");
  199. free( (IM_JOB *)m_pMyCurrentJob );
  200. m_pMyCurrentJob = NULL;
  201. m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"ERROR\"}\n", 19 );
  202. };
  203. };
  204. };
  205. return true;
  206. };
  207. return false;
  208. };
  209. #undef LOG
  210. #undef LOG_ERROR