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.

179 lines
6.1 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. m_pMyCurrentJob = (void*) tJOB;
  56. memset( tJOB, 0 , sizeof(IM_JOB));
  57. tJOB->dbID = ptr->item_id;
  58. tJOB->type = ptr->type;
  59. tJOB->width = ptr->width;
  60. tJOB->height = ptr->height;
  61. memcpy( tJOB->md5, ptr->md5, 32 );
  62. tKernel->enqueueJob( tJOB );
  63. };
  64. };
  65. free( ptr );
  66. };
  67. return true;
  68. };
  69. bool IM_ClientHandler::packetHandler( protocolPackage *Package ){
  70. if( Package ) {
  71. if( Package->m_pData ) {
  72. if (Package->m_nSize > 3 ) {
  73. if( Package->m_pData[Package->m_nSize-4] == 13 ) Package->m_pData[Package->m_nSize-4] = 0;
  74. if( Package->m_pData[Package->m_nSize-3] == 10 ) Package->m_pData[Package->m_nSize-3] = 0;
  75. }
  76. Package->m_pData[Package->m_nSize-2] = 0;
  77. Package->m_pData[Package->m_nSize-1] = 0;
  78. //LOG( (CSTR) Package->m_pData );
  79. try{
  80. XMLNode node1 = XMLNode::parseString( (const char *)Package->m_pData, "root");
  81. if( !node1.isEmpty() ){
  82. LOG( node1.getName() );
  83. if( strstr(node1.getName(),"ClockSync")!=NULL ){
  84. #ifdef __linux__
  85. unsigned long long t0, t1, t2, t3;
  86. #else
  87. unsigned __int64 t0, t1, t2, t3;
  88. #endif
  89. XMLNode node3 = node1.getChildNode("t0", 0);
  90. XMLNode node4 = node1.getChildNode("t1", 0);
  91. XMLNode node5 = node1.getChildNode("t2", 0);
  92. /*
  93. t3 = getSHDTimeValue();
  94. #ifdef __linux__
  95. t0 = atoll(node3.getText(0));
  96. t1 = atoll(node4.getText(0));
  97. t2 = atoll(node5.getText(0));
  98. #else
  99. t0 = _atoi64(node3.getText(0));
  100. t1 = _atoi64(node4.getText(0));
  101. t2 = _atoi64(node5.getText(0));
  102. #endif
  103. int offset = ((t1-t0)+(t2-t3))/2;
  104. int latency = (t3-t0)-(t2-t1);
  105. int jagajaga = offset+latency;
  106. //if( 0 & correctSHTime( jagajaga ) ){
  107. // LOG("Time is Synced");
  108. // g_iTimeDiff = 0;
  109. //} else {
  110. // LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
  111. g_iTimeDiff = jagajaga;
  112. //};
  113. */
  114. };
  115. };
  116. } catch( exception e ){
  117. };
  118. try{
  119. Json::Value root;
  120. Json::Reader reader;
  121. if( !reader.parse( (char *)Package->m_pData, root ) ){
  122. LOG_ERROR( "The package data is nor a valid JSON data nor valid XML data" );
  123. return true;
  124. };
  125. if( root.size() ){
  126. Json::Value tVal;
  127. tVal = root["item_id"];
  128. if( tVal != NULL ){
  129. int itemID = atoi( tVal.asString().c_str() ) ;
  130. if( itemID > 0 ) {
  131. IM_ITEM_DATA *tData = m_pDBA->getItemDataById( itemID );
  132. if( tData ) return handleItem( tData );
  133. };
  134. };
  135. tVal = root["item_md5"];
  136. if( tVal != NULL ){
  137. if( strlen( tVal.asString().c_str())== 32 ){
  138. IM_ITEM_DATA *tData = m_pDBA->getItemDataByMd5( (CSTR) tVal.asString().c_str() );
  139. if( tData ) return handleItem( tData );
  140. };
  141. };
  142. };
  143. } catch ( exception e ){
  144. };
  145. };
  146. };
  147. return true;
  148. };
  149. void IM_ClientHandler::dispatchEvent( basicEvent *Evt ){
  150. if( Evt!= NULL ) {
  151. if( Evt->m_eClass == EVT_CLASS_IMAGE ){
  152. if( Evt->m_eType == EVT_READY ){
  153. IM_RESULT *tData = (IM_RESULT*) Evt->m_pEvtData;
  154. if( tData!= NULL ) {
  155. if( tData->pJOB == m_pMyCurrentJob ) {
  156. //handle finished Job
  157. };
  158. };
  159. };
  160. };
  161. };
  162. };
  163. #undef LOG
  164. #undef LOG_ERROR