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.

324 lines
10 KiB

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