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.

299 lines
9.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
5 years ago
​
​
6 years ago
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseModuleFactory.h"
  2. #include "rgooseModuleZLMedia.h"
  3. #include "rgooseModuleUSBRelay.h"
  4. #include "rgooseModuleKafka.h"
  5. #include "rgooseModuleFlyHunter.h"
  6. #include "rgooseModuleSSDP.h"
  7. #include "rgooseModuleOnvifObserver.h"
  8. #include "rgooseModuleEmbedVideo.h"
  9. #ifdef MEM_DEBUG
  10. #include "../alg/generic/heapDebug.h"
  11. #endif
  12. #define LOG( X ) m_pLog->Log( "MFAC", this, X )
  13. #define LOG_ERROR( X ) m_pLog->Log( "MFAC", this, _LOG_ERROR, X )
  14. argusModuleFactory * argusModuleFactory::_instance = 0;
  15. /**
  16. * Returns the single instance of the object.
  17. */
  18. argusModuleFactory* argusModuleFactory::getInstance() {
  19. // check if the instance has been created yet
  20. if (0 == _instance) {
  21. // if not, then create it
  22. _instance = new argusModuleFactory;
  23. }
  24. // return the single instance
  25. return _instance;
  26. };
  27. argusModuleFactory::argusModuleFactory(){
  28. m_pLog = basicLogCollection::getInstance();
  29. m_pDBC = argusDBC::getInstance();
  30. };
  31. argusModuleFactory::~argusModuleFactory(){
  32. if( m_vKernelModules.size() > 0 ){
  33. for( vectorKernelModules::iterator index = m_vKernelModules.begin(); index < m_vKernelModules.end(); index++ ) deleteModule( (*index) );
  34. m_vKernelModules.clear();
  35. };
  36. _instance = NULL;
  37. };
  38. bool argusModuleFactory::loadModules( uint32_t ServerID ){
  39. if(m_pDBC == NULL) return false;
  40. m_nServerID = ServerID;
  41. DBC_INT_LIST *tList = NULL;
  42. tList = m_pDBC->listModules( m_nServerID );
  43. if( tList != NULL ) {
  44. if( tList->size() ) {
  45. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) createModule( *idx );
  46. tList->clear();
  47. }
  48. #ifdef DEBUG
  49. else {
  50. LOG("No Modules Defined");
  51. }
  52. #endif
  53. delete( tList );
  54. return true;
  55. } else {
  56. LOG_ERROR("Cannot Fetch Modules from DB!");
  57. };
  58. return false;
  59. };
  60. argusKernelModule *argusModuleFactory::createModule( int id ){
  61. argusKernelModule *tVal = NULL;
  62. if( m_pDBC != NULL ){
  63. DBC_MODULE_DATA *tData = NULL;
  64. tData = m_pDBC->getModuleData( id );
  65. if( tData!= NULL ) {
  66. switch( tData->type ) {
  67. case ARGUS_MODULE_ZLMEDIA:
  68. #ifdef DEBUG
  69. LOG("Creating ZLMedia Connector Module");
  70. #endif
  71. tVal = new argusModuleZLMedia( tData->module_id );
  72. break;
  73. case ARGUS_MODULE_DCBUSB:
  74. #ifdef DEBUG
  75. LOG("Creating Dry Contact USB Module");
  76. #endif
  77. tVal = new argusModuleUSBRelay( tData->module_id );
  78. break;
  79. case ARGUS_MODULE_KAFKA:
  80. #ifdef DEBUG
  81. LOG("Creating KAFKA Module");
  82. #endif
  83. tVal = new argusModuleKafka( tData->module_id );
  84. break;
  85. case ARGUS_MODULE_FLYHUNTER:
  86. #ifdef DEBUG
  87. LOG("Creating Fly Hunter Module");
  88. #endif
  89. tVal = new argusModuleFlyHunter( tData->module_id );
  90. break;
  91. case ARGUS_MODULE_SSDP_ANONCE:
  92. #ifdef DEBUG
  93. LOG("Creating SSDP Announcer Module");
  94. #endif
  95. tVal = new argusModuleSSDPAnnouncer( tData->module_id );
  96. break;
  97. case ARGUS_MODULE_ONVIF_OBSERVER:
  98. #ifdef DEBUG
  99. LOG("Creating OnVif Observer Module");
  100. #endif
  101. tVal = new argusModuleOnvifObserver( tData->module_id );
  102. break;
  103. case ARGUS_MODULE_EMBED_VIDEO:
  104. #ifdef DEBUG
  105. LOG("Creating Embed Video Module");
  106. #endif
  107. tVal = new argusModuleEmbedVideo( tData->module_id );
  108. break;
  109. };
  110. free(tData);
  111. } else {
  112. LOG_ERROR("No Module Data Fetched!");
  113. };
  114. } else {
  115. LOG_ERROR("No Database Object Assigned!");
  116. };
  117. if( tVal != NULL ){
  118. tVal->startService();
  119. m_vKernelModules.push_back( tVal );
  120. };
  121. return tVal;
  122. };
  123. void argusModuleFactory::deleteModule( argusKernelModule *M ){
  124. if( M ) {
  125. switch( M->getType() ) {
  126. case ARGUS_MODULE_ZLMEDIA: delete( (argusModuleZLMedia *) M ); break;
  127. case ARGUS_MODULE_DCBUSB: delete( (argusModuleUSBRelay*) M ); break;
  128. case ARGUS_MODULE_KAFKA: delete( (argusModuleKafka*) M ); break;
  129. case ARGUS_MODULE_FLYHUNTER: delete( (argusModuleFlyHunter*) M ); break;
  130. case ARGUS_MODULE_SSDP_ANONCE: delete( (argusModuleSSDPAnnouncer*) M ); break;
  131. case ARGUS_MODULE_ONVIF_OBSERVER: delete( (argusModuleOnvifObserver*) M ); break;
  132. case ARGUS_MODULE_EMBED_VIDEO: delete( (argusModuleEmbedVideo*) M ); break;
  133. };
  134. };
  135. };
  136. void argusModuleFactory::deleteModule( int id ){
  137. if( m_vKernelModules.size() > id ) {
  138. deleteModule( m_vKernelModules.at( id ) );
  139. m_vKernelModules.erase( m_vKernelModules.begin() + id );
  140. m_vKernelModules.shrink_to_fit();
  141. };
  142. };
  143. uint32_t argusModuleFactory::getModulesCount( ARGUS_MODULE_TYPE t ){
  144. uint32_t cnt = 0;
  145. if( m_vKernelModules.size() ) {
  146. for( int i=0; i < m_vKernelModules.size(); i++ ) {
  147. if( m_vKernelModules.at(i)->getType() == t ) cnt++;
  148. };
  149. };
  150. return cnt;
  151. };
  152. uint32_t argusModuleFactory::fetchModules( ARGUS_MODULE_TYPE t, argusKernelModule**&RA ){
  153. uint32_t cnt = getModulesCount( t );
  154. RA = NULL;
  155. if( cnt > 0 ) {
  156. RA = ( argusKernelModule** ) calloc( cnt, sizeof( argusKernelModule* ) );
  157. if( RA!= NULL ) {
  158. if( m_vKernelModules.size() ) {
  159. uint32_t idx = 0;
  160. for( int i=0; i < m_vKernelModules.size(); i++ ) {
  161. if( m_vKernelModules.at(i)->getType() == t ) {
  162. RA[idx] = m_vKernelModules.at(i);
  163. idx++;
  164. };
  165. };
  166. };
  167. } else cnt = 0;
  168. };
  169. return cnt;
  170. };
  171. argusKernelModule *argusModuleFactory::getModule( uint32_t id ){
  172. argusKernelModule *RetVal = NULL;
  173. if( m_vKernelModules.size() ) {
  174. for( vectorKernelModules::iterator idx = m_vKernelModules.begin(); idx < m_vKernelModules.end(); idx++ ){
  175. if( (*idx)->getId() == id ) {
  176. RetVal = (*idx);
  177. break;
  178. };
  179. };
  180. };
  181. return RetVal;
  182. };
  183. argusKernelModule *argusModuleFactory::getModule( ARGUS_MODULE_TYPE t, uint32_t id ){
  184. argusKernelModule *RetVal = NULL;
  185. if( m_vKernelModules.size() ) {
  186. for( vectorKernelModules::iterator idx = m_vKernelModules.begin(); idx < m_vKernelModules.end(); idx++ ){
  187. if( id ){
  188. if( ( (*idx)->getType() == t ) && ( (*idx)->getId() == id ) ){
  189. RetVal = (*idx);
  190. break;
  191. };
  192. } else {
  193. if( (*idx)->getType() == t ){
  194. RetVal = (*idx);
  195. break;
  196. };
  197. };
  198. };
  199. };
  200. return RetVal;
  201. };
  202. void argusModuleFactory::validate( void ){
  203. if(m_pDBC == NULL) return;
  204. DBC_INT_LIST *tList = NULL;
  205. #ifdef MEM_DEBUG
  206. size_t memStart, memEnd, memDiff;
  207. char MEM_MSG[256];
  208. memStart = getCurrentRSS();
  209. #endif
  210. if( m_vKernelModules.size() ) {
  211. DBC_INT_LIST tL;
  212. for( int cm = 0; cm < m_vKernelModules.size(); cm++ ) tL.push_back( m_vKernelModules.at(cm)->getId() );
  213. tList = m_pDBC->listNewModules( m_nServerID, &tL );
  214. tL.clear();
  215. } else {
  216. tList = m_pDBC->listNewModules( m_nServerID, NULL );
  217. };
  218. if( tList != NULL ) {
  219. #ifdef DEBUG
  220. LOG("New Modules Exists!");
  221. #endif
  222. for( int index=0; index< tList->size(); index++) createModule(tList->at(index));
  223. tList->clear();
  224. delete( tList );
  225. };
  226. #ifdef MEM_DEBUG
  227. memEnd = getCurrentRSS();
  228. memDiff = memEnd - memStart;
  229. sprintf(MEM_MSG,"MEM START: %d MEM DIF1: %d", memStart, memDiff );
  230. LOG( MEM_MSG );
  231. #endif
  232. if( m_vKernelModules.size() ) {
  233. //Get Full List
  234. //tList = NULL;
  235. tList = m_pDBC->listModules( m_nServerID );
  236. if( tList != NULL ) {
  237. int cm = 0;
  238. while( cm < m_vKernelModules.size() ) {
  239. //check if module removed
  240. bool tRemoved = true;
  241. for( int lm = 0; lm < tList->size(); lm++ ) {
  242. if( tList->at(lm) == m_vKernelModules.at( cm )->getId() ) {
  243. tRemoved = false;
  244. break;
  245. };
  246. };
  247. if( !tRemoved ) {
  248. // validate module
  249. m_vKernelModules.at( cm )->validate();
  250. cm++;
  251. } else {
  252. #ifdef DEBUG
  253. LOG("Delete Module!");
  254. #endif
  255. deleteModule( cm );
  256. };
  257. };
  258. tList->clear();
  259. delete( tList );
  260. } else {
  261. #ifdef DEBUG
  262. LOG("All Modules Must be Deleted!");
  263. #endif
  264. while( m_vKernelModules.size() ) deleteModule( 0 );
  265. };
  266. } else {
  267. #ifdef DEBUG
  268. LOG("No Modules to Validate!");
  269. #endif
  270. };
  271. #ifdef MEM_DEBUG
  272. memEnd = getCurrentRSS();
  273. memDiff = memEnd - memStart;
  274. sprintf(MEM_MSG,"MEM START: %d MEM DIF2: %d", memStart, memDiff );
  275. LOG( MEM_MSG );
  276. #endif
  277. };
  278. #undef LOG
  279. #undef LOG_ERROR