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.

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