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.

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