Poll.c keeps track of the events that are to be notified. More...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <mysql/mysql.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
Go to the source code of this file.
Data Structures | |
struct | date |
Functions | |
void | daemon_init () |
daemon_init() initializes the program as a daemon in the background | |
void | readdata (int d1, int m1, int y1, int d2, int m2, int y2) |
readdata fills in values for date structure variables | |
long int | diff_date () |
diff_date calculates the difference of days between two dates | |
void | handler (int sig) |
handler is the function to receive a sytem signal from the GUI Interface | |
int | months (char m[5]) |
months returns the vaue in integer given a month | |
void | display_row () |
void | databaseconnect () |
databaseconnect estabilishes a connection to the database | |
void | get_time (int *h, int *m, int *s, char dt[15], char day[5], int *dat, int *mnt, int *y) |
get_time returns values of system date & time | |
void | conv_date_integer (char dt[15], int *dat, int *mnt, int *yr) |
conv_date_integer converts a given date into its corresponding integer parts | |
int | time_comp (int h1, int m1, int h2, int m2) |
time_comp given two times it checks if time one is greater than time two or not | |
long int | query () |
query fires queries into the database to find out about any forthcoming events | |
int | main (int argc, char *argv[]) |
Main function from where execution starts. | |
Variables | |
MYSQL * | connection1 |
MYSQL_RES * | result |
MYSQL_ROW | sqlrow |
int | g_flag = 0 |
int | mnts [13] = {0,31,28,31,30,31,30,31,31,30,31,30,31} |
date | fd |
date | sd |
date | temp |
Poll.c keeps track of the events that are to be notified.
Definition in file poll.c.
void conv_date_integer | ( | char | dt[15], | |
int * | dat, | |||
int * | mnt, | |||
int * | yr | |||
) |
conv_date_integer converts a given date into its corresponding integer parts
dt | The date to be converted into integer parts | |
dat | Pointer where day of dt will be returned as integer | |
dat | Pointer where month of dt will be returned as integer | |
dat | Pointer where year of dt will be returned as integer |
void daemon_init | ( | ) |
daemon_init() initializes the program as a daemon in the background
Definition at line 20 of file poll.c.
References pid.
Referenced by main().
00021 { 00022 int i; 00023 pid_t pid; 00024 char path[512]; 00025 if((pid=fork())!=0) 00026 { 00027 exit(0); 00028 } 00029 setsid(); 00030 signal(SIGHUP,SIG_IGN); 00031 if((pid=fork())!=0) 00032 { 00033 exit(0); 00034 } 00035 getcwd(path,512); 00036 chdir(path); 00037 umask(0); 00038 for(i=0;i<64;i++) 00039 { 00040 close(i); 00041 } 00042 }
void databaseconnect | ( | ) |
databaseconnect estabilishes a connection to the database
void |
Definition at line 209 of file poll.c.
References connection1.
Referenced by main().
00210 { 00211 char server[30],dbuser[30],dbpassword[30],dbname[30]; 00212 strcpy(server,"localhost"); 00213 strcpy(dbuser,"alarm_user"); 00214 strcpy(dbpassword,"alarm_password"); 00215 strcpy(dbname,"alarm"); 00216 00217 connection1 = mysql_init (NULL); 00218 if (!connection1) 00219 { 00220 fprintf (stderr, "MySQL initialization failed\n"); 00221 exit(EXIT_FAILURE); 00222 } 00223 00224 connection1 = mysql_real_connect (connection1, server,dbuser,dbpassword,dbname, 0, NULL, 0); 00225 00226 if (connection1) 00227 { 00228 printf ("Connection successful\n"); 00229 } 00230 else 00231 { 00232 printf ("Connection failed\n"); 00233 return; 00234 } 00235 }
long int diff_date | ( | ) |
diff_date calculates the difference of days between two dates
void |
Definition at line 121 of file poll.c.
References date::dd, date::mm, mnts, and date::yy.
Referenced by query().
00122 { 00123 long int diff=0; 00124 int maxday; 00125 while(temp.dd!=sd.dd || temp.mm!=sd.mm || temp.yy!=sd.yy) 00126 { 00127 if(temp.yy%100==0) 00128 { 00129 if(temp.yy%400==0) 00130 mnts[2]=29; 00131 } 00132 else if(temp.yy%4==0) 00133 mnts[2]=29; 00134 maxday=mnts[temp.mm]; 00135 mnts[2]=28; 00136 if(temp.mm==12 && temp.dd>=31) 00137 { 00138 temp.yy++; 00139 temp.mm=1; 00140 temp.dd=1; 00141 diff++; 00142 } 00143 else if(temp.dd>=maxday && temp.mm!=12) 00144 { 00145 temp.mm++; 00146 temp.dd=1; 00147 diff++; 00148 } 00149 else 00150 { 00151 temp.dd++; 00152 diff++; 00153 } 00154 00155 00156 00157 } 00158 return diff; 00159 }
void display_row | ( | ) |
Definition at line 192 of file poll.c.
References connection1, and sqlrow.
00193 { 00194 unsigned int field_count; 00195 field_count = 0; 00196 printf("\ndisplay_row()<"); 00197 while (field_count < mysql_field_count (connection1)) 00198 { 00199 printf ("%s ", sqlrow[field_count]); 00200 field_count++; 00201 } 00202 printf (">\n"); 00203 }
void get_time | ( | int * | h, | |
int * | m, | |||
int * | s, | |||
char | dt[15], | |||
char | day[5], | |||
int * | dat, | |||
int * | mnt, | |||
int * | y | |||
) |
get_time returns values of system date & time
h | Pointer variable where hour will be returned | |
m | Pointer variable where min will be returned | |
s | Pointer variable where secs will be returned | |
dt | Pointer where system date will be returned | |
day | Pointer where system day of the week will be returned | |
dat | Pointer variable where day of month will be returned as integer | |
mnt | Pointer variable where month will be returned as integer | |
y | Pointer variable where year will be returned as integer |
Definition at line 248 of file poll.c.
Referenced by main(), and query().
00249 { 00250 time_t now; 00251 int n,dd,mnth,yr; 00252 char fields[5][15],str[50]; 00253 char *pch=NULL; 00254 00255 n=0; 00256 time(&now); 00257 strcpy(str,ctime(&now)); 00258 pch = strtok (str," "); 00259 while (pch != NULL) 00260 { 00261 strcpy(fields[n++],pch); 00262 pch = strtok (NULL," "); 00263 } 00264 *h=atoi(fields[3]); 00265 *m=atoi(fields[3]+3); 00266 *s=atoi(fields[3]+6); 00267 00268 dd=atoi(fields[2]); 00269 mnth=months(fields[1]); 00270 yr=atoi(fields[4]); 00271 00272 *dat=dd; 00273 *y=yr; 00274 *mnt=mnth; 00275 00276 strcpy(day,fields[0]); 00277 00278 if(dd<10 && mnth>9) 00279 sprintf(dt,"%d-%d-0%d",yr,mnth,dd); 00280 else if(dd>9 && mnth<10) 00281 sprintf(dt,"%d-0%d-%d",yr,mnth,dd); 00282 else if(dd<9 && mnth<10) 00283 sprintf(dt,"%d-0%d-0%d",yr,mnth,dd); 00284 else 00285 sprintf(dt,"%d-%d-%d",yr,mnth,dd); 00286 00287 printf("\nfn:get_time() system time:<%d:%d:%d>system date:<%s>sytem day:<%s>sys date:<%d/%d/%d>\n",*h,*m,*s,dt,day,*dat,*mnt,*y); 00288 }
void handler | ( | int | sig | ) |
int main | ( | int | argc, | |
char * | argv[] | |||
) |
Main function from where execution starts.
argc | number of command line arguments | |
argv | command line arguments itself |
Definition at line 597 of file poll.c.
References a, b, connection1, daemon_init(), databaseconnect(), g_flag, get_time(), handler(), and query().
00598 { 00599 daemon_init(); 00600 00601 int ret=-1; 00602 int cur_hr,cur_min,cur_sec,a,b,c; 00603 int hr=23,min=60; 00604 long int diff,diff_time; 00605 char comm[200],d[15],e[5]; 00606 FILE *fp; 00607 00608 databaseconnect();//connecting to the database 00609 00610 signal(SIGUSR1,handler); 00611 00612 while(g_flag!=1) 00613 { 00614 printf("Processing....Awake!!\n"); 00615 diff=query();//calling the query function to find out nearest time 00616 printf("\nDiff inside main=%ld\n",diff); 00617 00618 if(diff>=0) 00619 { 00620 printf("Put to sleep %ld seconds",diff); 00621 ret=sleep(diff); 00622 } 00623 00624 printf("\nret=%d\n",ret); 00625 00626 if(ret==0) 00627 { 00628 printf("\nExecuting normal/duplicate query\n"); 00629 00630 fp=fopen("t","r"); 00631 while(fscanf(fp," %[^\n]",comm)>0) 00632 { 00633 //printf("%s\n",comm); 00634 system(comm); 00635 } 00636 00637 fclose(fp); 00638 system("rm -f t"); 00639 g_flag=0; 00640 ret=-1; 00641 00642 } 00643 else if(ret==-1 && diff==-1) 00644 { 00645 printf("Sleeping till end of the day..!!\n"); 00646 get_time(&cur_hr,&cur_min,&cur_sec,d,e,&a,&b,&c); 00647 if(cur_sec==0) 00648 diff_time=((hr-cur_hr)*60*60)+((min-cur_min)*60); 00649 else if(cur_sec!=0) 00650 diff_time=((hr-cur_hr)*60*60)+((min-(cur_min+1))*60)+(60-cur_sec); 00651 sleep(diff_time); 00652 g_flag=0; 00653 } 00654 else 00655 { 00656 printf("Sleep interrupted..!!\n"); 00657 g_flag=0; 00658 ret=-1; 00659 continue; 00660 } 00661 } 00662 mysql_close(connection1); 00663 return 0; 00664 }
int months | ( | char | m[5] | ) |
months returns the vaue in integer given a month
m | The month for which the integer value is to be returned |
Definition at line 175 of file poll.c.
00176 { 00177 if(strcmp(m,"Jan")==0)return 1; 00178 else if(strcmp(m,"Feb")==0)return 2; 00179 else if(strcmp(m,"Mar")==0)return 3; 00180 else if(strcmp(m,"Apr")==0)return 4; 00181 else if(strcmp(m,"May")==0)return 5; 00182 else if(strcmp(m,"Jun")==0)return 6; 00183 else if(strcmp(m,"Jul")==0)return 7; 00184 else if(strcmp(m,"Aug")==0)return 8; 00185 else if(strcmp(m,"Sep")==0)return 9; 00186 else if(strcmp(m,"Oct")==0)return 10; 00187 else if(strcmp(m,"Nov")==0)return 11; 00188 else if(strcmp(m,"Dec")==0)return 12; 00189 else return -1; 00190 }
long int query | ( | ) |
query fires queries into the database to find out about any forthcoming events
void |
Definition at line 333 of file poll.c.
References connection1, conv_date_integer(), diff_date(), display_row(), g_event, g_hour, g_min, get_time(), readdata(), result, sqlrow, and time_comp().
Referenced by create_and_fill_model(), create_connection(), main(), match_answer(), and view_onRowActivated().
00334 { 00335 int g_hour,g_min,g_o,g_d,g_w,g_m,g_y,g_eo,g_em,g_ey,g_curmnth,g_curyr; 00336 int return_value,hr,min,sec,flg=0,dat,mnt,yr,flg1=0; 00337 int db_dat,db_mnt,db_yr,r; 00338 00339 long int diff=-1; 00340 long int diff_dt=0; 00341 00342 char g_dt[15],g_day[5],g_event[100],g_ed[15],g_ew[15],g_currdate[15],g_curday[5]; 00343 00344 char dt[15],day[5],qry[500],line[500]; 00345 FILE *fp; 00346 00347 fp=fopen("t","w"); 00348 00349 get_time(&hr,&min,&sec,dt,day,&dat,&mnt,&yr); 00350 strcpy(g_currdate,dt); 00351 g_curmnth=mnt; 00352 g_curyr=yr; 00353 strcpy(g_curday,day); 00354 00355 sprintf(qry,"update ers set dt='%s' where dt < '%s' and d=1",dt,dt); /*Daily Event*/ 00356 return_value=mysql_query(connection1,qry); 00357 if(return_value) 00358 printf("Update Failed!!"); 00359 00360 sprintf(qry,"select * from ers where ((eo=0 and o=1) or ('%s'>ed and d=1) or ('%s'>ew and w=1 and day='%s') or (%d>em and m=1) or (%d>ey and y=1)) and ('%s'>=dt) order by ed,hour,min",dt,dt,day,mnt,yr,dt); 00361 printf("\nSelect Query : %s",qry); 00362 00363 return_value = mysql_query (connection1,qry); 00364 00365 if (return_value) 00366 { 00367 printf ("select failed as : %s\n", mysql_error (connection1)); 00368 } 00369 else 00370 { 00371 result = mysql_use_result(connection1); 00372 if (result) 00373 { 00374 while ((sqlrow = mysql_fetch_row (result))) 00375 { 00376 display_row(); 00377 diff=0; 00378 flg=0; 00379 flg1=0; 00380 diff_dt=0; 00381 00382 strcpy(g_dt,sqlrow[0]); 00383 strcpy(g_day,sqlrow[1]); 00384 g_hour=atoi(sqlrow[2]); 00385 g_min=atoi(sqlrow[3]); 00386 strcpy(g_event,sqlrow[4]); 00387 g_o=atoi(sqlrow[5]); 00388 g_d=atoi(sqlrow[6]); 00389 g_w=atoi(sqlrow[7]); 00390 g_m=atoi(sqlrow[8]); 00391 g_y=atoi(sqlrow[9]); 00392 g_eo=atoi(sqlrow[10]); 00393 strcpy(g_ed,sqlrow[11]); 00394 strcpy(g_ew,sqlrow[12]); 00395 g_em=atoi(sqlrow[13]); 00396 g_ey=atoi(sqlrow[14]); 00397 00398 sprintf(line,"./popup %s %s %d %d %d %d %d %d %d %d %s %s %d %d %s %d %d %s %d %s",g_dt,g_day,g_hour,g_min,g_o,g_d,g_w,g_m,g_y,g_eo,g_ed,g_ew,g_em,g_ey,g_currdate,g_curmnth,g_curyr,g_curday,1,g_event); 00399 printf("%s\n",line); 00400 00401 if(g_o==1) /*Once Event working fine*/ 00402 { 00403 conv_date_integer(g_dt,&db_dat,&db_mnt,&db_yr); 00404 readdata(db_dat,db_mnt,db_yr,dat,mnt,yr); 00405 temp=fd; 00406 diff_dt=diff_date(); 00407 printf("\nOnce difference=%ld\n",diff_dt); 00408 if(diff_dt>0) 00409 { 00410 printf("\n<Backlog 'once' event>\n"); 00411 flg1=1; 00412 system(line); 00413 } 00414 else if(diff_dt==0)/*same day backlog*/ 00415 { 00416 r=time_comp(g_hour,g_min,hr,min); 00417 if(r==2) 00418 { 00419 printf("\n<Backlog 'once' event same day>\n"); 00420 flg1=1; 00421 system(line); 00422 } 00423 } 00424 } 00425 if(g_d==1) /*Daily event ed-systemdate*/ 00426 { 00427 conv_date_integer(g_ed,&db_dat,&db_mnt,&db_yr); 00428 readdata(db_dat,db_mnt,db_yr,dat,mnt,yr); 00429 temp=fd; 00430 diff_dt=diff_date(); 00431 printf("\nDaily difference=%ld\n",diff_dt); 00432 if(diff_dt==1) 00433 { 00434 r=time_comp(g_hour,g_min,hr,min); 00435 if(r==2) 00436 { 00437 printf("\n<Backlog 'daily' event same day>\n"); 00438 flg1=1; 00439 system(line); 00440 } 00441 } 00442 } 00443 if(g_w==1) 00444 { 00445 conv_date_integer(g_ew,&db_dat,&db_mnt,&db_yr); 00446 readdata(db_dat,db_mnt,db_yr,dat,mnt,yr); 00447 temp=fd; 00448 diff_dt=diff_date(); 00449 printf("\nWeekly difference=%ld\n",diff_dt); 00450 if(diff_dt>7) 00451 { 00452 printf("\n<Backlog 'weekly' event>\n"); 00453 flg1=1; 00454 system(line); 00455 } 00456 else if(diff_dt==1) 00457 { 00458 r=time_comp(g_hour,g_min,hr,min); 00459 if(r==2) 00460 { 00461 printf("\n<Backlog 'weekly' event same day>\n"); 00462 flg1=1; 00463 system(line); 00464 } 00465 } 00466 } 00467 if(g_m==1) 00468 { 00469 if(g_em==12) 00470 diff_dt=mnt; 00471 else 00472 diff_dt=mnt-g_em; 00473 printf("\nMonthly difference=%ld\n",diff_dt); 00474 if(diff_dt>1) 00475 { 00476 printf("\n<Backlog 'monthly' event>\n"); 00477 flg1=1; 00478 system(line); 00479 } 00480 else if(diff_dt==1) 00481 { 00482 r=time_comp(g_hour,g_min,hr,min); 00483 if(r==2) 00484 { 00485 printf("\n<Backlog 'monthly' event same day>\n"); 00486 flg1=1; 00487 system(line); 00488 } 00489 } 00490 } 00491 if(g_y==1) 00492 { 00493 diff_dt=yr-g_ey; 00494 printf("\nYearly difference=%ld\n",diff_dt); 00495 if(diff_dt>1) 00496 { 00497 printf("\n<Backlog 'yearly' event>\n"); 00498 flg1=1; 00499 system(line); 00500 } 00501 else if(diff_dt==1) 00502 { 00503 r=time_comp(g_hour,g_min,hr,min); 00504 if(r==2) 00505 { 00506 printf("\n<Backlog 'yearly' event same day>\n"); 00507 flg1=1; 00508 system(line); 00509 } 00510 } 00511 } 00512 00513 printf("\nquery():flg1=%d\n",flg1); 00514 00515 if(flg1==0) 00516 { 00517 printf("\nNORMAL EVENT\n"); 00518 if(g_hour==hr) 00519 diff=((g_min-min)*60); 00520 else if(g_hour>hr) 00521 diff=((g_hour-hr)*60*60)+((g_min-min)*60); 00522 00523 diff-=sec; 00524 00525 printf("\nquery():diff=%ld\n",diff); 00526 00527 if(diff>0) 00528 { 00529 flg=1; 00530 printf("\nFiring Normal Query into file\n"); 00531 sprintf(line,"./popup %s %s %d %d %d %d %d %d %d %d %s %s %d %d %s %d %d %s %d %s",g_dt,g_day,g_hour,g_min,g_o,g_d,g_w,g_m,g_y,g_eo,g_ed,g_ew,g_em,g_ey,g_currdate,g_curmnth,g_curyr,g_curday,2,g_event); 00532 fprintf(fp,"%s\n",line); 00533 break; 00534 } 00535 } 00536 } 00537 if (mysql_errno (connection1)) 00538 { 00539 printf ("Error occurred while retrieving data : %s\n", mysql_error (connection1)); 00540 } 00541 } 00542 } 00543 00544 if(flg==1) 00545 { 00546 int l_hour,l_min,l_o,l_d,l_w,l_m,l_y,l_eo,l_em,l_ey,l_curmnth,l_curyr; 00547 char l_dt[15],l_day[5],l_event[100],l_ed[15],l_ew[15],l_currdate[15],l_curday[5]; 00548 00549 while ((sqlrow = mysql_fetch_row (result))) 00550 { 00551 l_hour=atoi(sqlrow[2]); 00552 l_min=atoi(sqlrow[3]); 00553 if(g_hour==l_hour && g_min==l_min) 00554 { 00555 display_row(); 00556 strcpy(l_dt,sqlrow[0]); 00557 strcpy(l_day,sqlrow[1]); 00558 strcpy(l_event,sqlrow[4]); 00559 l_o=atoi(sqlrow[5]); 00560 l_d=atoi(sqlrow[6]); 00561 l_w=atoi(sqlrow[7]); 00562 l_m=atoi(sqlrow[8]); 00563 l_y=atoi(sqlrow[9]); 00564 l_eo=atoi(sqlrow[10]); 00565 strcpy(l_ed,sqlrow[11]); 00566 strcpy(l_ew,sqlrow[12]); 00567 l_em=atoi(sqlrow[13]); 00568 l_ey=atoi(sqlrow[14]); 00569 00570 strcpy(l_currdate,g_currdate); 00571 l_curmnth=g_curmnth; 00572 l_curyr=g_curyr; 00573 strcpy(l_curday,g_curday); 00574 00575 sprintf(line,"./popup %s %s %d %d %d %d %d %d %d %d %s %s %d %d %s %d %d %s %d %s",l_dt,l_day,l_hour,l_min,l_o,l_d,l_w,l_m,l_y,l_eo,l_ed,l_ew,l_em,l_ey,l_currdate,l_curmnth,l_curyr,l_curday,2,l_event); 00576 printf("\nFiring additional normal query with same time into file\n"); 00577 fprintf(fp,"%s\n",line); 00578 } 00579 } 00580 } 00581 00582 mysql_free_result(result); 00583 fclose(fp); 00584 if(flg==1) 00585 return diff; 00586 else if(flg1==1) 00587 return -1; 00588 else 00589 return -1; 00590 }
void readdata | ( | int | d1, | |
int | m1, | |||
int | y1, | |||
int | d2, | |||
int | m2, | |||
int | y2 | |||
) |
readdata fills in values for date structure variables
d1 | Day value for first date structure fd | |
m1 | Month value for first date structure fd | |
y1 | Year value for first date structure fd | |
d2 | Day value for first date structure sd | |
m2 | Month value for first date structure sd | |
Year | Day value for first date structure sd |
Definition at line 72 of file poll.c.
References date::dd, date::mm, and date::yy.
Referenced by query().
00073 { 00074 if(y2>y1) 00075 { 00076 fd.dd=d1;fd.mm=m1;fd.yy=y1; 00077 sd.dd=d2;sd.mm=m2;sd.yy=y2; 00078 } 00079 else if(y1>y2) 00080 { 00081 fd.dd=d2;fd.mm=m2;fd.yy=y2; 00082 sd.dd=d1;sd.mm=m1;sd.yy=y1; 00083 } 00084 else if(y1==y2) 00085 { 00086 if(m2>m1) 00087 { 00088 fd.dd=d1;fd.mm=m1;fd.yy=y1; 00089 sd.dd=d2;sd.mm=m2;sd.yy=y2; 00090 } 00091 else if(m1>m2) 00092 { 00093 fd.dd=d2;fd.mm=m2;fd.yy=y2; 00094 sd.dd=d1;sd.mm=m1;sd.yy=y1; 00095 } 00096 else if(m1==m2) 00097 { 00098 if(d2>d1) 00099 { 00100 fd.dd=d1;fd.mm=m1;fd.yy=y1; 00101 sd.dd=d2;sd.mm=m2;sd.yy=y2; 00102 } 00103 else if(d1>d2) 00104 { 00105 fd.dd=d2;fd.mm=m2;fd.yy=y2; 00106 sd.dd=d1;sd.mm=m1;sd.yy=y1; 00107 } 00108 else if(d1==d2) 00109 { 00110 fd.dd=d1;fd.mm=m1;fd.yy=y1; 00111 sd.dd=d2;sd.mm=m2;sd.yy=y2; 00112 } 00113 } 00114 } 00115 }
int time_comp | ( | int | h1, | |
int | m1, | |||
int | h2, | |||
int | m2 | |||
) |
time_comp given two times it checks if time one is greater than time two or not
Definition at line 312 of file poll.c.
Referenced by query().
MYSQL* connection1 |
int mnts[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31} |
Definition at line 50 of file poll.c.
Referenced by diff_date(), and week_calc().