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.

302 lines
9.5 KiB

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