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
231 lines
8.5 KiB
#include "IM_ClientHandler.h"
|
|
#include "IM_Kernel.h"
|
|
|
|
#include "../../lib/components/base/basicFunctionality.h"
|
|
#include "../../lib/data/json/json_tool.h"
|
|
#include "../../lib/data/protocol/xmlParser.h"
|
|
|
|
#include <strings.h>
|
|
#include <json/value.h>
|
|
#include <json/writer.h>
|
|
#include <json/reader.h>
|
|
|
|
|
|
|
|
#define LOG( X ) m_pLog->Log( "CH", this, X )
|
|
#ifdef DEBUG
|
|
#define LOG_ERROR( X ) {\
|
|
char _tBfrErrMSG234[256];\
|
|
sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
|
|
m_pLog->Log( "CH", this, _LOG_ERROR, _tBfrErrMSG234 );\
|
|
}
|
|
#else
|
|
#define LOG_ERROR( X ) m_pLog->Log( "CH", this, _LOG_ERROR, X );
|
|
#endif
|
|
|
|
// Constructor implementation
|
|
IM_ClientHandler::IM_ClientHandler( int SOCKET, networkServiceEngine *ptr ) : networkClientHandler( SOCKET, ptr ), IM_ClientProtocol(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pDBA = IM_DataBaseAdapter::getInstance();
|
|
m_pMyCurrentJob = NULL;
|
|
LOG("New Client!");
|
|
if( create() ){
|
|
m_pPort = new serverPort( SOCKET );
|
|
if( m_pPort != NULL ){
|
|
setTransport( m_pPort );
|
|
#ifdef DEBUG
|
|
LOG("Port Created OK!");
|
|
#endif
|
|
if( m_pPort->create() ){
|
|
LOG("Port Init OK!");
|
|
} else LOG_ERROR("Not Connected!");
|
|
} else LOG_ERROR("Port Not Created!");
|
|
} else LOG_ERROR( "Protocol Parser Creation Failed!");
|
|
};
|
|
|
|
// Destructor implementation
|
|
IM_ClientHandler::~IM_ClientHandler(){
|
|
if( m_pPort != NULL ) delete( m_pPort );
|
|
if( m_pMyCurrentJob ) free( (IM_JOB *)m_pMyCurrentJob );
|
|
};
|
|
|
|
bool IM_ClientHandler::getStatus( void ){
|
|
if( m_pPort!= NULL ) {
|
|
return m_pPort->getStatus();
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool IM_ClientHandler::handleItem( IM_ITEM_DATA *ptr ){
|
|
if( ptr ) {
|
|
if( m_pEngine != NULL ) {
|
|
IM_Kernel *tKernel = (IM_Kernel *)m_pEngine;
|
|
IM_JOB *tJOB;
|
|
tJOB = (IM_JOB*)malloc(sizeof(IM_JOB));
|
|
if( tJOB ) {
|
|
if( m_pMyCurrentJob ) {
|
|
LOG("CLEANNING UNCLEAN JOB!");
|
|
//AnyWay Clean Memory that was Used!
|
|
free((IM_JOB *)m_pMyCurrentJob);
|
|
};
|
|
m_pMyCurrentJob = (void*) tJOB;
|
|
memset( tJOB, 0 , sizeof(IM_JOB));
|
|
tJOB->dbID = ptr->item_id;
|
|
tJOB->image_type = ptr->type;
|
|
tJOB->job_type = ptr->process_type;
|
|
tJOB->width = ptr->width;
|
|
tJOB->height = ptr->height;
|
|
memcpy( tJOB->md5, ptr->md5, 32 );
|
|
tKernel->enqueueJob( tJOB );
|
|
};
|
|
};
|
|
free( ptr );
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool IM_ClientHandler::packetHandler( protocolPackage *Package ){
|
|
if( Package ) {
|
|
if( Package->m_pData ) {
|
|
if (Package->m_nSize > 3 ) {
|
|
if( Package->m_pData[Package->m_nSize-4] == 13 ) Package->m_pData[Package->m_nSize-4] = 0;
|
|
if( Package->m_pData[Package->m_nSize-3] == 10 ) Package->m_pData[Package->m_nSize-3] = 0;
|
|
}
|
|
Package->m_pData[Package->m_nSize-2] = 0;
|
|
Package->m_pData[Package->m_nSize-1] = 0;
|
|
LOG( (CSTR) Package->m_pData );
|
|
try{
|
|
XMLNode node1 = XMLNode::parseString( (const char *)Package->m_pData, "root");
|
|
if( !node1.isEmpty() ){
|
|
LOG( node1.getName() );
|
|
if( strstr(node1.getName(),"ClockSync")!=NULL ){
|
|
#ifdef __linux__
|
|
unsigned long long t0, t1, t2, t3;
|
|
#else
|
|
unsigned __int64 t0, t1, t2, t3;
|
|
#endif
|
|
XMLNode node3 = node1.getChildNode("t0", 0);
|
|
XMLNode node4 = node1.getChildNode("t1", 0);
|
|
XMLNode node5 = node1.getChildNode("t2", 0);
|
|
/*
|
|
t3 = getSHDTimeValue();
|
|
#ifdef __linux__
|
|
t0 = atoll(node3.getText(0));
|
|
t1 = atoll(node4.getText(0));
|
|
t2 = atoll(node5.getText(0));
|
|
#else
|
|
t0 = _atoi64(node3.getText(0));
|
|
t1 = _atoi64(node4.getText(0));
|
|
t2 = _atoi64(node5.getText(0));
|
|
#endif
|
|
int offset = ((t1-t0)+(t2-t3))/2;
|
|
int latency = (t3-t0)-(t2-t1);
|
|
int jagajaga = offset+latency;
|
|
//if( 0 & correctSHTime( jagajaga ) ){
|
|
// LOG("Time is Synced");
|
|
// g_iTimeDiff = 0;
|
|
//} else {
|
|
// LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
|
|
g_iTimeDiff = jagajaga;
|
|
//};
|
|
*/
|
|
};
|
|
};
|
|
} catch( exception e ){
|
|
LOG_ERROR("Some Error in XML Parser!");
|
|
};
|
|
// JSON Parsing
|
|
try{
|
|
Json::Value root;
|
|
Json::Reader reader;
|
|
if( !reader.parse( (char *)Package->m_pData, root ) ){
|
|
LOG_ERROR( "The package data is nor a valid JSON data nor valid XML data" );
|
|
return true;
|
|
};
|
|
if( root.size() ){
|
|
IM_ITEM_DATA * tData = NULL;
|
|
Json::Value tVal;
|
|
|
|
tVal = root["item_id"];
|
|
|
|
if( tVal != NULL ){
|
|
int itemID = atoi( tVal.asString().c_str() ) ;
|
|
if( itemID > 0 ) {
|
|
tData = m_pDBA->getItemDataById( itemID );
|
|
if( tData!= NULL ) tData->process_type = IM_PROC_TYPE_DNN;
|
|
};
|
|
};
|
|
|
|
tVal = root["item_md5"];
|
|
if( tVal != NULL ){
|
|
if( strlen( tVal.asString().c_str())== 32 ){
|
|
tData = m_pDBA->getItemDataByMd5( (CSTR) tVal.asString().c_str() );
|
|
if( tData!= NULL ) tData->process_type = IM_PROC_TYPE_DNN;
|
|
};
|
|
};
|
|
|
|
tVal = root["process_type"];
|
|
if( tVal != NULL ){
|
|
if( tData!= NULL ) tData->process_type = atoi( tVal.asString().c_str());
|
|
};
|
|
|
|
if( tData!= NULL ){
|
|
return handleItem( tData );
|
|
} else {
|
|
m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"ERROR\"}\n", 19 );
|
|
}
|
|
};
|
|
} catch ( exception e ){
|
|
LOG_ERROR("Some Error in JSON Parser!");
|
|
m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"ERROR\"}\n", 19 );
|
|
};
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool IM_ClientHandler::dispatchEvent( EVENT_CONTAINER *Evt ){
|
|
if( Evt!= NULL ) {
|
|
if( Evt->m_eClass == EVT_CLASS_IMAGE ){
|
|
if( Evt->m_eType == EVT_READY ){
|
|
IM_RESULT *tData = (IM_RESULT*) Evt->m_pEvtData;
|
|
if( tData!= NULL ) {
|
|
if( tData->pJOB == m_pMyCurrentJob ) {
|
|
LOG("JOB Finished Sucessfully!");
|
|
for( int i=0; i < tData->nBoxCount; i++ ){
|
|
m_pDBA->insertItemROI(
|
|
tData->pJOB->dbID,
|
|
tData->boxes[i].classID,
|
|
tData->boxes[i].weight,
|
|
tData->boxes[i].x,
|
|
tData->boxes[i].y,
|
|
tData->boxes[i].width,
|
|
tData->boxes[i].height
|
|
);
|
|
};
|
|
//handle finished Job
|
|
free( (IM_JOB *)m_pMyCurrentJob );
|
|
m_pMyCurrentJob = NULL;
|
|
free( (IM_CLASS_BOUNDARY_BOX *)tData->boxes );
|
|
free( (IM_RESULT *)Evt->m_pEvtData );
|
|
Evt->m_pEvtData = NULL;
|
|
m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"OK\"}\n", 16 );
|
|
};
|
|
} else {
|
|
LOG("JOB Finished Without Detection!");
|
|
free( (IM_JOB *)m_pMyCurrentJob );
|
|
m_pMyCurrentJob = NULL;
|
|
m_pPort->sendData( (uint8_t *) "{\"STATUS\":\"ERROR\"}\n", 19 );
|
|
};
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
|
|
|