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.

150 lines
4.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "IM_Kernel.h"
  2. #include "IM_ClientHandler.h"
  3. #define LOG( X ) m_pLog->Log( "KRNL", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG234[256];\
  7. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  12. #endif
  13. IM_Kernel *IM_Kernel::_instance = NULL;
  14. IM_Kernel *IM_Kernel::getInstance(){
  15. if( _instance == NULL ) {
  16. _instance = new IM_Kernel;
  17. };
  18. return _instance;
  19. };
  20. // Constructor implementation
  21. inline IM_Kernel::IM_Kernel( config_setting_t *Settings ) : networkServiceEngine(), basicThread(){
  22. m_pLog = basicLogCollection::getInstance();
  23. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" );
  24. m_pEvtHeap = basicEventHeap::getInstance();
  25. m_bExitCause = 0;
  26. m_pWorker = NULL;
  27. m_pRecognizer = NULL;
  28. m_pDBA = NULL;
  29. m_pActiveJob = NULL;
  30. };
  31. // Destructor implementation
  32. inline IM_Kernel::~IM_Kernel(){
  33. waitThreadEnd( true );
  34. if( m_pWorker ) delete( m_pWorker );
  35. if( m_pRecognizer ) delete( m_pRecognizer );
  36. if( m_pDBA ) delete( m_pDBA );
  37. _instance = NULL;
  38. };
  39. bool IM_Kernel::start(){
  40. m_pDBA = IM_DataBaseAdapter::getInstance( m_pSettings );
  41. if( m_pDBA ==NULL ) {
  42. LOG_ERROR("Error Creating DataBase Adapter!");
  43. return false;
  44. };
  45. m_pWorker = new IM_DnnWorker( m_pSettings );
  46. if( m_pWorker ) {
  47. if( !m_pWorker->create() ){
  48. LOG_ERROR( "DNN Worker INIT Failure!" );
  49. return false;
  50. };
  51. } else LOG_ERROR( "DNN Worker Create Failure!" );
  52. m_pRecognizer = new IM_DlibWorker( m_pSettings );
  53. if( m_pRecognizer ) {
  54. if( !m_pRecognizer->create() ){
  55. LOG_ERROR( "DLib Worker INIT Failure!" );
  56. return false;
  57. };
  58. } else LOG_ERROR( "DLib Worker Create Failure!" );
  59. return init();
  60. };
  61. bool IM_Kernel::stop( bool Force ){
  62. waitThreadEnd( Force );
  63. return true;
  64. };
  65. networkClientHandler *IM_Kernel::attachNewClient( int SocketID, void *ptp ){
  66. IM_ClientHandler *tVal;
  67. LOG("BEFORE NEW CLIENT");
  68. tVal = new IM_ClientHandler( SocketID, this );
  69. LOG("AFTER NEW CLIENT");
  70. return (networkClientHandler *) tVal;
  71. };
  72. bool IM_Kernel::detachClient( networkClientHandler *ptr ){
  73. delete( (IM_ClientHandler *) ptr );
  74. return true;
  75. };
  76. void IM_Kernel::m_fThreadProc( void ){
  77. m_bThreadStatus = TH_WORKING;
  78. while( m_bThreadStatus < TH_ENDED ) {
  79. if( m_qJOBS.size() ) {
  80. if( m_pWorker ){
  81. m_pActiveJob = m_qJOBS.front();
  82. m_qJOBS.pop();
  83. switch( m_pActiveJob->job_type ) {
  84. case IM_PROC_TYPE_FACE:
  85. if( !m_pRecognizer->enqueueJob( m_pActiveJob ) ) {
  86. #ifdef DEBUG
  87. LOG("Not Inserted Job Must generate EVT");
  88. #endif
  89. EVENT_CONTAINER *tEVT;
  90. tEVT = (EVENT_CONTAINER *)malloc(sizeof(EVENT_CONTAINER));
  91. if( tEVT ) {
  92. memset(tEVT,0,sizeof(EVENT_CONTAINER));
  93. tEVT->m_eClass = EVT_CLASS_IMAGE;
  94. tEVT->m_eType = EVT_READY;
  95. m_pEvtHeap->broadcast( tEVT );
  96. } else {
  97. LOG_ERROR( "Failure to Emmit EVENT!");
  98. };
  99. };
  100. break;
  101. default:
  102. if( !m_pWorker->enqueueJob( m_pActiveJob ) ) {
  103. #ifdef DEBUG
  104. LOG("Not Inserted Job Must generate EVT");
  105. #endif
  106. EVENT_CONTAINER *tEVT;
  107. tEVT = (EVENT_CONTAINER *)malloc(sizeof(EVENT_CONTAINER));
  108. if( tEVT ) {
  109. memset(tEVT,0,sizeof(EVENT_CONTAINER));
  110. tEVT->m_eClass = EVT_CLASS_IMAGE;
  111. tEVT->m_eType = EVT_READY;
  112. m_pEvtHeap->broadcast( tEVT );
  113. } else {
  114. LOG_ERROR( "Failure to Emmit EVENT!");
  115. };
  116. };
  117. };
  118. } else LOG_ERROR( "DNN Worker Not Created!" );
  119. };
  120. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  121. };
  122. };
  123. bool IM_Kernel::enqueueJob( IM_JOB *ptr ){
  124. if( m_pWorker ) {
  125. if( ptr!= NULL ) {
  126. #ifdef DEBUG
  127. LOG( "New Job Enqueued" );
  128. #endif
  129. m_qJOBS.push( ptr );
  130. };
  131. } else LOG_ERROR( "DNN Worker Not Created!" );
  132. return false;
  133. };
  134. #undef LOG
  135. #undef LOG_ERROR