Tuesday, July 28, 2009

Project Help?

The question is:


I want to write a program on Client-server program using OLTP i.e., Online transaction processing Using Centralized solution, but I want to write a program on Fifo. what changes I have to make in the program to get a Fifo program.


%26lt;"OLTP" a client-server simulation%26gt;


File Name:server_oltp.c


Discription:Compile %26amp; Run as DEAMON PROCESS.





#include "oltp.h" /*This file contains all the header files needed for both Clint and Server*/





#define LOG_MSG_LEN 100





/*function prototypes*/


void send_chan(int,OLTP_MSG_BUFF);


void send_reply(int,int,int,OLTP_MSG_BUFF );


int is_acc(int,int);


void start_logging(void);


int chan_gen(int);


void dump_log(int);





/*Globle Variables*/


int nof_clints=0,dead_clint,log_d,clint_list...


char log_msg[LOG_MSG_LEN];





main(int argc,char *argv[])


{


int i,j,semd,msgd,mdf_d,ret,off,server_pid;


char def_choise[6],time_ch[30];


time_t local_time;


OLTP_MSG_BUFF msg_clint,msg_reply;


SB_ACC sb_acc;


struct msqid_ds msg_stat;


if(argc == 1)


{


printf("USAGE: %s install | start | stop\n",argv[0]);


exit(0);


}


if(strcmp(argv[1],"install")==0)


{


system("clear");


printf("\n##### WELCOME TO OLTP SERVER INSTALLATION.........press[X] to EXIT:");


i=getchar();


if(i == 'X' || i == 'x')


{


printf("\n\t\t\tABORTED BY USER ! !\n");


exit(0);


}


printf("\n\t\t\tCREATING Master Database File in ./MDF.dbase...");


mdf_d=open("./MDF.dbase",O_WRONLY | O_CREAT | O_EXCL,0666);


if(mdf_d %26lt; 0)


{


printf("\nMAY BE SERVER IS ALLREADY INSTALLED....remove MDF.dat then TRY AGAIN ! !\n");


exit(0);


}


printf("\n\t\t\tCREATING ADMINSTRATOR ACCOUNT........");


printf("\n\t\t\tadminstrator phone number:");


scanf("%d",%26amp;sb_acc.acc_ph);


off=sizeof(struct SB_ACC);


sb_acc.acc_no=1;sb_acc.balence=1000;


write(mdf_d,(char*)(%26amp;sb_acc),off);


close(mdf_d);


printf("\n\t\t\tSERVER INSTALLED SUSCCESSFULLY.......");


printf("\n\t\t\tNow You can start the server....[OK]\n");


getchar();


exit(0);


}


if(strcmp(argv[1],"stop")==0)


{


if((semd=semget(OLTP_SEMID,1,IPC_CRE... %26lt; 0)


{


printf("\n\t\t\tsemget() error......Server stop failed ! !\n");


exit(2);


}


semctl(semd,0,IPC_RMID,0);


if((msgd=msgget(OLTP_MSGID,IPC_CREAT... %26lt; 0)


{


printf("\n\t\t\tmsgget() error......Server stop failed ! !\n");


exit(2);


}


msgctl(msgd,IPC_RMID,%26amp;msg_stat);


exit(0);


}


if(strcmp(argv[1],"start")==0)


{


mdf_d=open("./MDF.dbase",O_RDWR);


if(mdf_d %26lt; 0)


{


printf("\n\t\t\tFailed open Master Database File........Server start failed ! !\n");


exit(2);


}


msgd=msgget(OLTP_MSGID,IPC_CREAT | IPC_EXCL);


semd=semget(OLTP_SEMID,1,IPC_CREAT | IPC_EXCL | 0666);


if(semd == 0 || msgd == 0)


{


printf("\n\t\t\tsemget() OR msgget() error.........Server start failed ! !\n");


}


if((j=semctl(semd,0,SETVAL,1)) %26lt; 0)


{


printf("\n\t\t\tsemctl() error......Server start failed ! !\n");


exit(5);


}


j=semctl(semd,0,GETVAL,0);





}


else


{


printf("USAGE: %s install/start/stop\n",argv[0]);


exit(1);


}





log_d=open("./OLTP_server.log",O_TRUN... | O_WRONLY | O_CREAT | O_SYNC);





system("clear");


sleep(1);


server_pid=fork();


if(server_pid %26lt; 0)


{


printf("\n\t\t\tfork() error.........Server start failed ! !\n");


exit(7);


}


if(server_pid %26gt; 0)


{





printf("\n\t\t\tServer started successfully with PID [%d]......\n",server_pid);


time(%26amp;local_time);


ctime_r(%26amp;local_time,time_ch);


printf("\n\t\t\tON:%s",time_ch);


printf("\n\t\t\t............server logs in ./OLTP_server.log\n");


printf("\n\t\t\t............USE $tail -f ./OLTP_server.log to see the logs [OK]\n");


exit(0);


}


close(1);


if(dup(log_d) %26lt; 0)


{


char err[44]="dup() error.........Server start failed ! !\n";


write(2,(char*)(err),44);


exit(9);


}


close(2);close(0);


setpgrp();


start_logging();


while(1)


{


ret=msgrcv(msgd,(struct msgbuf*)(%26amp;msg_clint),MSG_LEN,MASTER_CHAN ,IPC_NOWAIT);


if(ret %26gt; 0)


{


dump_log(sprintf(log_msg,"-----%26gt;Got chan_req from %d\n",msg_clint.pid));


send_chan(msgd,msg_clint);


}


for(i=0;i%26lt;=nof_clints;i++)


{


ret=msgrcv(msgd,(struct msgbuf*)(%26amp;msg_clint),MSG_LEN,i+2,IPC_NOW...


if(ret %26gt;= 0) send_reply(semd,msgd,mdf_d,msg_clint);


}


}/*infinite loop..hence deamon process*/





}





/*This function is to send a uniqe channel number to clients who request through MASTER CHANNEL*/


void send_chan(int msgd,OLTP_MSG_BUFF msg_clint)


{


int chan_no;


OLTP_MSG_BUFF msg_reply;


chan_no=chan_gen(msg_clint.pid);


if(chan_no %26gt; 0) nof_clints=nof_clints+1;


msg_reply.req_rep_info=chan_no;


msg_reply.mtype=msg_clint.pid;


dump_log(sprintf(log_msg,"-----%26gt;Sendi... chan %d to %d\n",msg_reply.req_rep_info,msg_clint.p...


msgsnd(msgd,(struct msgbuf*)(%26amp;msg_reply),MSG_LEN,0);


return;


}


/*End of function*/





/*This Function is to send reply to the client processes.The reply is sent through a que with client pid as


messege type.client PID as message type is only for the simplicity...In actual OLTP we should stick for


some other method*/


void send_reply(int semd,int msgd,int mdf_d,OLTP_MSG_BUFF msg_clint)


{


int i,off,new_acc_no,stat_mdf,how_many_acc;


time_t local_time;


char time_ch[30];


OLTP_MSG_BUFF msg_reply;


SB_ACC sb_acc;


struct msqid_ds msg_stat;


off=sizeof(struct SB_ACC);


switch(msg_clint.req_type)


{


case 1:


stat_mdf=lseek(mdf_d,0,SEEK_END);


how_many_acc=stat_mdf/off;


new_acc_no=how_many_acc+1;


sb_acc.acc_ph=msg_clint.req_rep_inf...


sb_acc.acc_no=new_acc_no;


sb_acc.balence=1000;


dump_log(sprintf(log_msg,"TRANSACTI... ON [%d]--%26gt;New acc[%d] is OPENED ! ! Whose Acc_ph[%d]\n",msg_clint.pid,sb_acc.acc_n...


write(mdf_d,(char *)(%26amp;sb_acc),sizeof(struct SB_ACC));


msg_reply.req_rep_info=sb_acc.acc_n...


break;


case 2:


lseek(mdf_d,0,0);


if(is_acc(mdf_d,msg_clint.acc_no) != 0)


{


msg_reply.req_rep_info=-2;


break;


}


off=(msg_clint.acc_no-1)*off;


lseek(mdf_d,off,0);


read(mdf_d,(char*)(%26amp;sb_acc),sizeof(... SB_ACC));


sb_acc.acc_ph=msg_clint.req_rep_inf...


lseek(mdf_d,off,0);


write(mdf_d,(char*)(%26amp;sb_acc),sizeof... SB_ACC));


msg_reply.req_rep_info=sb_acc.acc_p...


dump_log(sprintf(log_msg,"TRANSACTI... ON [%d]--%26gt;Acc no[%d] NOW have[%d] phone_no\n",msg_clint.pid,sb_acc.acc_no,...


break;





case 3:


lseek(mdf_d,0,0);


if(is_acc(mdf_d,msg_clint.acc_no) != 0)


{


msg_reply.req_rep_info=-3;


break;


}


off=(msg_clint.acc_no-1)*off;


lseek(mdf_d,off,0);


read(mdf_d,(char*)(%26amp;sb_acc),sizeof(... SB_ACC));


lseek(mdf_d,0,0);


msg_reply.acc_no=sb_acc.acc_ph;


msg_reply.req_rep_info=sb_acc.balen...


dump_log(sprintf(log_msg,"ACCOUNT [%d] INFORMATION IS SENT TO [%d]\n",sb_acc.acc_no,msg_clint.pid));


break;





case 4:


lseek(mdf_d,0,0);


if(is_acc(mdf_d,msg_clint.acc_no) != 0)


{


msg_reply.req_rep_info=-4;


break;


}


off=(msg_clint.acc_no-1)*off;


lseek(mdf_d,off,0);


read(mdf_d,(char*)(%26amp;sb_acc),sizeof(... SB_ACC));


sb_acc.balence=sb_acc.balence+msg_c...


lseek(mdf_d,off,0);


write(mdf_d,(char*)(%26amp;sb_acc),sizeof... SB_ACC));


msg_reply.req_rep_info=sb_acc.balen...


dump_log(sprintf(log_msg,"TRANSACTI... ON [%d]--%26gt;After debiting acc no %d now have %d balence no\n",msg_clint.pid,sb_acc.acc_no,sb_acc...


break;





case 5:


lseek(mdf_d,0,0);


if(is_acc(mdf_d,msg_clint.acc_no) != 0)


{


msg_reply.req_rep_info=-5;


break;


}


off=(msg_clint.acc_no-1)*off;


lseek(mdf_d,off,0);


read(mdf_d,(char*)(%26amp;sb_acc),sizeof(... SB_ACC));


if((sb_acc.balence-msg_clint.req_re... %26lt; 1000)


{


msg_reply.req_rep_info=-55;


break;


}


sb_acc.balence=sb_acc.balence-msg_c...


lseek(mdf_d,off,0);


write(mdf_d,(char*)(%26amp;sb_acc),sizeof... SB_ACC));


msg_reply.req_rep_info=sb_acc.balen...


dump_log(sprintf(log_msg,"TRANSACTI... ON [%d]--%26gt;After withdra acc no %d now have %d balence no\n",msg_clint.pid,sb_acc.acc_no,sb_acc...


break;





case 6:


clint_list[msg_clint.req_rep_info-2...


nof_clints=nof_clints-1;


msg_reply.req_rep_info=BYE;


dump_log(sprintf(log_msg,"-----%26gt;Cli... %d with PID[%d] LOGGED OUT ! !\n",msg_clint.req_rep_info,msg_clint.pi...


break;





case 7:


if(msg_clint.req_rep_info != OLTP_ADMIN_KEY)


{


dump_log(sprintf(log_msg,"-----%26gt;%d AUTHENTIFICATION FAILED ! !\n",msg_clint.pid));


msg_reply.req_rep_info=-7;


break;


}


dump_log(sprintf(log_msg,"-----%26gt;%d AS ADMINSTRATOR IS GOING TO SHUTTINGDOWN THE SERVER ! !\n",msg_clint.pid));


msg_reply.mtype=msg_clint.pid;


msg_reply.req_rep_info=AUTH_ADMIN;


msgsnd(msgd,(struct msgbuf*)(%26amp;msg_reply),MSG_LEN,0);


dump_log(sprintf(log_msg,"-----%26gt;OLT... SERVER DOWN\n"));


time(%26amp;local_time);


ctime_r(%26amp;local_time,time_ch);


dump_log(sprintf(log_msg,"-----%26gt;ON %s",time_ch));


dump_log(sprintf(log_msg,"#########... ANY QUESTIONS TO %26lt;mdshreenath@yahoo.com%26gt; ###############\n"));


close(log_d);close(mdf_d);


sleep(5);


semctl(semd,0,IPC_RMID,0);


msgctl(msgd,IPC_RMID,%26amp;msg_stat);


exit(0);


default:


printf("wrong CHOISE ! ! !\n");


return;





}


msg_reply.mtype=msg_clint.pid;


if((msgsnd(msgd,(struct msgbuf*)(%26amp;msg_reply),MSG_LEN,0)) %26lt; 0)


{


printf("msgsnd() error.........Failed to send REPLY ! !\n");


exit(6);


};








}





/*This function is to check for valid account number*/


int is_acc(int mdf_d,int acc_no)


{


int off,stat_mdf,how_many_acc;


SB_ACC acc;


off=sizeof(struct SB_ACC);


stat_mdf=lseek(mdf_d,0,SEEK_END);


how_many_acc=stat_mdf/off;


if(acc_no %26gt; how_many_acc) return(-1);


return(0);


}





/*This function dumps some innitial messeges to log file*/


void start_logging(void)


{


time_t local_time;


char time_ch[30];


dump_log(sprintf(log_msg,"###########... LOG FILE GENERATED BY OLTP SERVER #####################\n"));


time(%26amp;local_time);


ctime_r(%26amp;local_time,time_ch);


dump_log(sprintf(log_msg,"-----%26gt;ON %s",time_ch));


dump_log(sprintf(log_msg,"-----%26gt;Serve... started successfully.......with PID [%d]\n",getpid()));


return;


}


/*This function is to get a uniq channel number*/





int chan_gen(int pid)


{


int i;


for(i=0;i%26lt;MAX_NO_CLINTS;i++)


{


if(clint_list[i] == 0)


{


clint_list[i]=pid;


return(i+2);/*because chan 1 is for MASTER*/


}


}


return(-1);


}





/*This function is to dump logs to log file */


void dump_log(int size)


{


if(size %26lt; 0) return;


write(log_d,log_msg,size);


return;


}


/*Physical End*/

Project Help?
Join a programming forum or newsgroup.


No comments:

Post a Comment