📄 save_daemon.cpp
字号:
while (ptr1) { if(difftime(local_mktime(&ptr2->starttime),local_mktime(&ptr1->starttime))>0) { ptr2 = ptr1; } ptr1=ptr1->next; continue; } return ptr2; }void inserttotasklist(TASK_ITEM * item){ struct task *ptr1=task_head,* ptr2=NULL; if(task_head==NULL){ task_head=item; return; } // (1)find until ptr1->channel >= item->channel for(;ptr1&&ptr1->channel<item->channel;ptr2=ptr1,ptr1=ptr1->next); if(!ptr1){ ptr2->next=item; return; } else if(ptr1->channel>item->channel){ item->next=ptr1; if(ptr1==task_head) task_head=item; else ptr2->next=item; return; } else{ //ptr1->channel==item->channel // (2) until ptr1->stoptime >= item->starttime, so item is before ptr1 or combined with it for( ; ptr1 && ptr1->channel==item->channel && difftime( local_mktime(&(ptr1->stoptime)), local_mktime(&(item->starttime)) )<0; ptr2=ptr1,ptr1=ptr1->next ); if(ptr1&&ptr1->channel==item->channel){ // (3) ptr1->stop>=item->start && ptr1->start<=item->stop, so combined it if(difftime(local_mktime(&ptr1->starttime),local_mktime(&item->stoptime))<=0) { //(4) combine: select the minimum starttime if(difftime(local_mktime(&ptr1->starttime),local_mktime(&item->starttime))>0) ptr1->starttime=item->starttime; if(difftime(local_mktime(&ptr1->stoptime),local_mktime(&item->stoptime))<0) ptr1->stoptime=item->stoptime; // (5) combine ptr1 forward : ptr2=ptr1; ptr1=ptr1->next; while( ptr1 && ( ptr2->channel==ptr1->channel ) && difftime(local_mktime(&(ptr2->stoptime)),local_mktime(&(ptr1->starttime)))>=0 ){ if( difftime(local_mktime(&ptr2->stoptime), local_mktime(&ptr1->stoptime)) < 0) { ptr2->stoptime=ptr1->stoptime; break; // find a more large stoptime } ptr2->next=ptr1->next; free(ptr1); ptr1=ptr2->next; }//end of while; // (6) combine ptr1 backward : because item1->start > ptr2->stop, so ptr1->start > ptr2->stop } else { //(7) ptr1->stop>=item->start && ptr1->start>item->stop, so insert it item->next = ptr1; if (ptr1==task_head) task_head=item; else ptr2->next = item; } // end if...else... }// end if in same channel else{ // in this channel, item have the largest starttime, ptr2, ptr1 are sure no NULL item->next=ptr1; ptr2->next=item;// if(ptr1==task_head)// task_head=item; } } return;} /* read one line from file, and get a task item */int getataskfromfile(FILE *fp,struct task * item){//年 月 日 时:分 年 月 日 时:分 频道号 频道ip 频道port char tempstr[30]="", *pstr; fscanf(fp, "%s", tempstr); if(!strcmp(tempstr,"")) return 0; item->starttime.tm_year=atoi(tempstr)-1900; fscanf(fp, "%s", tempstr); item->starttime.tm_mon=atoi(tempstr)-1; fscanf(fp, "%s", tempstr); item->starttime.tm_mday=atoi(tempstr); fscanf(fp, "%s", tempstr); pstr = strchr(tempstr, ':'); if (pstr==NULL) return FALSE; *pstr++='\0'; item->starttime.tm_hour=atoi(tempstr); item->starttime.tm_min=atoi(pstr); // add timezone support item->starttime.tm_gmtoff = global_tm_gmtoff; item->starttime.tm_zone = global_tm_zone; fscanf(fp, "%s", tempstr); item->stoptime.tm_year=atoi(tempstr)-1900; fscanf(fp, "%s", tempstr); item->stoptime.tm_mon=atoi(tempstr)-1; fscanf(fp, "%s", tempstr); item->stoptime.tm_mday=atoi(tempstr); fscanf(fp, "%s", tempstr); pstr = strchr(tempstr, ':'); if (pstr==NULL) return FALSE; *pstr++='\0'; item->stoptime.tm_hour=atoi(tempstr); item->stoptime.tm_min=atoi(pstr); // add timezone support item->stoptime.tm_gmtoff = global_tm_gmtoff; item->stoptime.tm_zone = global_tm_zone; fscanf(fp, "%s", tempstr); item->channel=atoi(tempstr); fscanf(fp, "%s", tempstr); strncpy(item->channel_ip, tempstr, 16); fscanf(fp, "%s", tempstr); item->channel_port=atoi(tempstr); item->next=NULL; return 1;}/********************************************* * pthread list function *********************************************/PTHREAD_ITEM *addto_pthreadlist(TASK_ITEM *task_ptr){ PTHREAD_ITEM *ptr1=pthread_header, *ptr2=NULL, *ptr3; if(pthread_header==NULL){ pthread_header=(PTHREAD_ITEM*)new char[sizeof(PTHREAD_ITEM)]; pthread_header->starttime=task_ptr->starttime; pthread_header->stoptime=task_ptr->stoptime; pthread_header->channel=task_ptr->channel; strcpy(pthread_header->channel_ip,task_ptr->channel_ip); pthread_header->channel_port=task_ptr->channel_port; fillfilename(pthread_header); pthread_header->next=NULL; return pthread_header; } for( ; ptr1 && difftime( local_mktime(&(ptr1->stoptime)), local_mktime(&(task_ptr->stoptime)))<0; ptr2=ptr1, ptr1=ptr1->next); if(ptr1==pthread_header){ ptr1=(PTHREAD_ITEM*)new char[sizeof(PTHREAD_ITEM)]; ptr1->starttime=task_ptr->starttime; ptr1->stoptime=task_ptr->stoptime; ptr1->channel=task_ptr->channel; strcpy(ptr1->channel_ip,task_ptr->channel_ip); ptr1->channel_port=task_ptr->channel_port; fillfilename(ptr1); ptr1->next=pthread_header; pthread_header=ptr1; return ptr1; } else{ ptr3=(PTHREAD_ITEM*)new char[sizeof(PTHREAD_ITEM)]; ptr3->starttime=task_ptr->starttime; ptr3->stoptime=task_ptr->stoptime; ptr3->channel=task_ptr->channel; strcpy(ptr3->channel_ip,task_ptr->channel_ip); ptr3->channel_port=task_ptr->channel_port; fillfilename(ptr3); ptr2->next=ptr3; ptr3->next=ptr1; return ptr3; } return NULL; } void fillfilename(PTHREAD_ITEM* ptr){ sprintf(ptr->filename,"%sch%d_%d_%d_%d_%d_%d--%d_%d_%d_%d_%d", filepath, ptr->channel, ptr->starttime.tm_year+1900, ptr->starttime.tm_mon+1, ptr->starttime.tm_mday, ptr->starttime.tm_hour, ptr->starttime.tm_min, ptr->stoptime.tm_year+1900, ptr->stoptime.tm_mon+1, ptr->stoptime.tm_mday, ptr->stoptime.tm_hour, ptr->stoptime.tm_min );}time_t local_mktime(struct tm *timeptr){ struct tm temp; time_t local_tm; temp = *timeptr; local_tm = mktime(&temp); return local_tm;} static void output_task_list(TASK_ITEM *pheader){ FILE *fp = fopen(fname_task, "wb"); TASK_ITEM *pItem=pheader; if ( fp==NULL ) return; while(pItem) { fprintf(fp, "%d/%d/%d/%d:%d ", pItem->starttime.tm_year+1900, pItem->starttime.tm_mon+1, pItem->starttime.tm_mday, pItem->starttime.tm_hour, pItem->starttime.tm_min); fprintf(fp, "%d/%d/%d/%d:%d ", pItem->stoptime.tm_year+1900, pItem->stoptime.tm_mon+1, pItem->stoptime.tm_mday, pItem->stoptime.tm_hour, pItem->stoptime.tm_min); fprintf(fp, "%d %s %d\n", pItem->channel, pItem->channel_ip, pItem->channel_port); pItem = pItem->next; } fclose(fp);}static void output_pthread_list(PTHREAD_ITEM *pheader){ FILE *fp = fopen(fname_thread, "wb"); PTHREAD_ITEM *pItem=pheader; if ( fp==NULL ) return; while(pItem) { fprintf(fp, "%d/%d/%d/%d:%d ", pItem->starttime.tm_year+1900, pItem->starttime.tm_mon+1, pItem->starttime.tm_mday, pItem->starttime.tm_hour, pItem->starttime.tm_min); fprintf(fp, "%d/%d/%d/%d:%d ", pItem->stoptime.tm_year+1900, pItem->stoptime.tm_mon+1, pItem->stoptime.tm_mday, pItem->stoptime.tm_hour, pItem->stoptime.tm_min); fprintf(fp, "%d %s %d %s\n", pItem->channel, pItem->channel_ip, pItem->channel_port, pItem->filename); pItem = pItem->next; } fclose(fp);}static void DoCommand( int fd ) { TASK_ITEM *task_ptr, *prev_task; PTHREAD_ITEM *pthread_ptr, *prev_pthread; time_t tm; SAVE_CMD save_command; int length=0, sizeof_SAVE_CMD=sizeof(SAVE_CMD); struct sockaddr from; socklen_t fromlen = sizeof(struct sockaddr); length = recvfrom(fd, &save_command, sizeof_SAVE_CMD, 0, &from, &fromlen); if ( length!=sizeof_SAVE_CMD ) { dbg_print1(COMMON_LOG, "receive unexpected command, length %d\n", length); return; } if ( save_command.length != sizeof_SAVE_CMD ) { dbg_print1(COMMON_LOG, "receive unexpected command, command length %d\n", save_command.length); return; } if ( debug_level&COMMON_LOG ) dumpcommand(&save_command); // add timezone support save_command.starttime.tm_gmtoff = global_tm_gmtoff; save_command.starttime.tm_zone = global_tm_zone; save_command.stoptime.tm_gmtoff = global_tm_gmtoff; save_command.stoptime.tm_zone = global_tm_zone; switch (save_command.type) { case ADD_ONE_TASK: time(&tm); if(difftime(local_mktime(&(save_command.stoptime)),tm)<0) {// outdated item save_command.type = NACK_TASK_THREAD; } else { task_ptr=(struct task *)new char[sizeof(struct task)]; memset(task_ptr, 0, sizeof(TASK_ITEM)); task_ptr->starttime = save_command.starttime; task_ptr->stoptime = save_command.stoptime; task_ptr->channel = save_command.channel; strncpy(task_ptr->channel_ip, save_command.channel_ip, 16); task_ptr->channel_port = save_command.channel_port; task_ptr->next = 0; inserttotasklist(task_ptr); save_command.type = ACK_TASK_THREAD; change_task_list=TRUE; } break; case DEL_ONE_TASK: task_ptr = task_head; while (task_ptr) { if ( task_ptr->channel!=save_command.channel ) { prev_task=task_ptr; task_ptr = task_ptr->next; continue; } if ( task_ptr->channel_port!=save_command.channel_port ) { prev_task=task_ptr; task_ptr = task_ptr->next; continue; } if ( strncmp(task_ptr->channel_ip, save_command.channel_ip,16) ) { prev_task=task_ptr; task_ptr = task_ptr->next; continue; } if ( compare_tm(&(task_ptr->starttime), &(save_command.starttime))==FALSE ) { prev_task=task_ptr; task_ptr = task_ptr->next; continue; } if ( compare_tm(&(task_ptr->stoptime), &(save_command.stoptime))==FALSE ) { prev_task=task_ptr; task_ptr = task_ptr->next; continue; } break; } if ( task_ptr==NULL ) save_command.type = NACK_TASK_THREAD; else if ( task_ptr==task_head ) { task_head = task_head->next; delete task_ptr; save_command.type = ACK_TASK_THREAD; change_task_list=TRUE; } else { prev_task->next = task_ptr->next; delete task_ptr; save_command.type = ACK_TASK_THREAD; change_task_list=TRUE; } break; case DEL_ONE_THREAD: pthread_ptr = pthread_header; while (pthread_ptr) { if ( pthread_ptr->channel!=save_command.channel ) { prev_pthread=pthread_ptr; pthread_ptr = pthread_ptr->next; continue; } if ( pthread_ptr->channel_port!=save_command.channel_port ) { prev_pthread=pthread_ptr; pthread_ptr = pthread_ptr->next; continue; } if ( strncmp(pthread_ptr->filename,save_command.filename,256) ) { prev_pthread=pthread_ptr; pthread_ptr = pthread_ptr->next; continue; } break; } if ( pthread_ptr==NULL ) save_command.type = NACK_TASK_THREAD; else if ( pthread_ptr==pthread_header ) { pthread_header = pthread_header->next; main_saveThread_Quit(pthread_ptr); delete pthread_ptr; save_command.type = ACK_TASK_THREAD; change_pthread_list=TRUE; } else { prev_pthread->next = pthread_ptr->next; delete pthread_ptr; save_command.type = ACK_TASK_THREAD; change_pthread_list=TRUE; } break; case DEL_ALL_TASK: while ( task_head ) { task_ptr = task_head; task_head = task_head->next; delete task_ptr; } save_command.type = ACK_TASK_THREAD; change_task_list=TRUE; break; case DEL_ALL_THREAD: while ( pthread_header ) { pthread_ptr = pthread_header; pthread_header = pthread_header->next; main_saveThread_Quit(pthread_ptr); delete pthread_ptr; } save_command.type = ACK_TASK_THREAD; change_pthread_list=TRUE; break; default: dbg_print1(COMMON_LOG, "unexpected command \n", save_command.type); return; } length = sendto(fd, &save_command, sizeof_SAVE_CMD, 0, &from, fromlen); if ( length!=sizeof_SAVE_CMD ) { dbg_print0(COMMON_LOG, "ack send error\n"); } return;}static int compare_tm(struct tm *p1, struct tm *p2 ){ if (p1==NULL || p2==NULL) return FALSE; if (p1->tm_year!=p2->tm_year) return FALSE; if (p1->tm_mon!=p2->tm_mon) return FALSE; if (p1->tm_mday!=p2->tm_mday) return FALSE; if (p1->tm_hour!=p2->tm_hour) return FALSE; if (p1->tm_min!=p2->tm_min) return FALSE; return TRUE;}static void dumpcommand(SAVE_CMD *cmd){ char str[512], *next=str; switch ( cmd->type ) { case ADD_ONE_TASK: next += sprintf(str, "Add one task, length=%d, channel=%d, ip=%.16s, port=%.4d, ", cmd->length, cmd->channel, cmd->channel_ip, cmd->channel_port); next += sprintf(next, "starttime=%d/%d/%d/%d:%d ", cmd->starttime.tm_year+1900, cmd->starttime.tm_mon+1, cmd->starttime.tm_mday, cmd->starttime.tm_hour, cmd->starttime.tm_min); next += sprintf(next, "stoptime=%d/%d/%d/%d:%d\n", cmd->stoptime.tm_year+1900, cmd->stoptime.tm_mon+1, cmd->stoptime.tm_mday, cmd->stoptime.tm_hour, cmd->stoptime.tm_min); break; case DEL_ONE_TASK: next += sprintf(str, "Del one task, length=%d, channel=%d, ip=%.16s, port=%.4d, ", cmd->length, cmd->channel, cmd->channel_ip, cmd->channel_port); next += sprintf(next, "starttime=%d/%d/%d/%d:%d ", cmd->starttime.tm_year+1900, cmd->starttime.tm_mon+1, cmd->starttime.tm_mday, cmd->starttime.tm_hour, cmd->starttime.tm_min); next += sprintf(next, "stoptime=%d/%d/%d/%d:%d\n", cmd->stoptime.tm_year+1900, cmd->stoptime.tm_mon+1, cmd->stoptime.tm_mday, cmd->stoptime.tm_hour, cmd->stoptime.tm_min); break; case DEL_ONE_THREAD: next += sprintf(str, "Del one thread, length=%d, channel=%d, ip=%.16s, port=%.4d, ", cmd->length, cmd->channel, cmd->channel_ip, cmd->channel_port); next += sprintf(next, "filename=%s\n", cmd->filename); break; case DEL_ALL_TASK: sprintf(str, "Del all task\n"); break; case DEL_ALL_THREAD: sprintf(str, "Del all thread\n"); break; default: dbg_print0(COMMON_LOG, "unexpected command\n"); return; } dbg_print1(COMMON_LOG, "%s", str); return;}static void dumptaskitem(TASK_ITEM *task_ptr){ char str[512], *next = str; if ( task_ptr==NULL ) { dbg_print0(COMMON_LOG, "dump an NULL task_item"); return; } next += sprintf(str, "one item:"); next += sprintf(next, "starttime %d %d %d %d:%d ", task_ptr->starttime.tm_year+1900, task_ptr->starttime.tm_mon+1, task_ptr->starttime.tm_mday, task_ptr->starttime.tm_hour, task_ptr->starttime.tm_min); next += sprintf(next, "stoptime %d %d %d %d:%d ", task_ptr->stoptime.tm_year+1900, task_ptr->stoptime.tm_mon+1, task_ptr->stoptime.tm_mday, task_ptr->stoptime.tm_hour, task_ptr->stoptime.tm_min); next += sprintf(next, "channel %d ip %s port %d\n", task_ptr->channel, task_ptr->channel_ip, task_ptr->channel_port); dbg_print1(COMMON_LOG, "%s", str); return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -