void TVSearchMgr::UnInit() { dxreport("TVSearchMgr::~UnInit Begin >>>\n"); m_pNotifyProxy = NULL; if(m_pTVSearchMgrThread != NULL ) { m_pTVSearchMgrThread->stop(); delete m_pTVSearchMgrThread; m_pTVSearchMgrThread = NULL; } m_listDVBService.clear(); ClearSearchers(); destroy_search(); tvdevice_close(m_hTvDevice); dxreport("TVSearchMgr::~UnInit End <<<\n"); }
/*main function*/ int main(int argc, char *argv[]) { search *mysearch; /*ptr to a structure holding the search request */ remote *rmt; Client_para *para; pthread_t id; int err, rmt_flag; char *temp; time_type t_start, t_end; double tdiff; /*initialization*/ err = 0; rmt_flag = 0; mysearch = NULL; init_search(&mysearch); /* process all the command line switches */ opterr = 0; /* prevent getopt() from printing error messages */ scan_opt_search(argc, argv, mysearch); /*move the getopt to command_util.h to shorten the main */ /*build the shift table*/ build_shifttable(mysearch); /* * if there is no argument in list of files * directly go to stdin */ if (optind >= argc) { search_stream(stdin, NULL, mysearch, NULL); return 0; } get_time(&t_start); /* process the list of files*/ for (; optind < argc; optind++) { /* if it a remote search */ temp = argv[optind]; /* if it is remote search and has been successfully parsed*/ if ((rmt = scan_remote_search(temp, &rmt_flag)) != NULL) { if ((para = (Client_para*) malloc(sizeof(Client_para))) == NULL) { perror("malloc"); continue; } para->mysearch = mysearch; para->rmt = rmt; /* spawn a thread the perform remote search*/ // increment the stack count pthread_mutex_lock(&(mysearch->lock)); err = pthread_create(&id, NULL, client_agent, (void*) para); mysearch->stk_count++; if (err != 0) { fprintf(stderr, "Pthread_create of client\n"); free(para); mysearch->stk_count--; pthread_mutex_unlock(&(mysearch->lock)); continue; } pthread_mutex_unlock(&(mysearch->lock)); continue; } else if (rmt_flag == 1) { /* if a : used to appear there but has not been parsed, neglect it */ continue; } if (strcmp(temp, STREAM_REDIRECT) == 0) /* "-" redirect the io to stdin*/ { search_stream(stdin, NULL, mysearch, NULL); continue; } search_given(temp, mysearch); } /* wait all the thread to be done*/ while (mysearch->stk_count != 0) { pthread_cond_wait(&(mysearch->ready), &(mysearch->lock)); } get_time(&t_end); tdiff = time_diff(&t_start, &t_end); /* print the statistical result*/ print_stat(stdout, &(mysearch->statistics), tdiff); destroy_search(mysearch); return 0; } /* main */
/* the agent is now a separate thread */ void * server_agent(void *params) { int client_fd, n, errcode; enum header_types type; unsigned int len; char to_search[MAX_SEARCH_STR]; char remote_obj[REMOTE_NAME_MAX]; time_type t_start, t_end; double tdiff; msg_one msg1; search *mysearch; Statistics statistic; /* we are now successfully connected to a remote client */ client_fd = ((struct thread_params *) params)->client_fd; fprintf(stderr, "Starting Agent fd: %d, thread id: %lu \n", client_fd, (unsigned long) pthread_self()); /* do some initialization*/ memset(&statistic, 0, sizeof(Statistics)); errcode = pthread_detach(pthread_self()); if (errcode != 0) { fprintf(stderr, "pthread_detach server agent: %s\n", strerror(errcode)); } pthread_once(&init_done, thread_init); get_time(&t_start); len = sizeof(msg_one); /* first message should be the file name */ if ((n = our_recv_message(client_fd, &type, &len, &msg1)) < 0) return NULL; if (type != OPTION_PARAMETER) return NULL; /*firstly build the search object */ mysearch = build_search(&msg1); mysearch->client_fd = client_fd; /* then print the search options */ print_search_para(client_fd, mysearch); /* receive the second message*/ len = MAX_SEARCH_STR; if ((n = our_recv_message(client_fd, &type, &len, to_search)) < 0) return NULL; if (type != TO_SEARCH) return NULL; len = strlen(to_search); if ((mysearch->search_pattern = (char*) malloc(len + 1)) == NULL) { perror("malloc"); free(params); return NULL; } strncpy(mysearch->search_pattern, to_search, len+1); /*build its own shift table if needed */ build_shifttable(mysearch); /* receive the second message*/ len = REMOTE_NAME_MAX; if ((n = our_recv_message(client_fd, &type, &len, remote_obj)) < 0) return NULL; if (type != REMOTE_NAME) return NULL; /* do the search job */ search_given(remote_obj, mysearch); /* send the statistics message 6*/ /*wait for directory search to be finished if it is on the server side*/ while (mysearch->stk_count != 0) { pthread_cond_wait(&(mysearch->ready), &(mysearch->lock)); } len = sizeof(Statistics); /* make a copy of little/big endian adapted statistical structure*/ update_statistics_sock(&statistic, &mysearch->statistics); trans_stat2send(&statistic); if (our_send_message(mysearch->client_fd, STATISTICS_MSG, len, &statistic) != 0) { fprintf(stderr, "Fail to send statistics\n"); return NULL; } get_time(&t_end); tdiff = time_diff(&t_start, &t_end); fprintf(stderr, "Search Statistics: client fd %u, thread id %lu\n", mysearch->client_fd, (unsigned long) pthread_self()); print_stat(stderr, &mysearch->statistics, tdiff); destroy_search(mysearch); fprintf(stderr, "Terminating Agent fd: %d, thread id: %lu \n", client_fd, (unsigned long) pthread_self()); free(params); return NULL; } /* server_agent */