#include #include "structures.h" #include "AOPcalendar.h" #include "t_list.c" char *copy_string(char *st); struct term *t_null(); struct term *t_cons(struct term *t, struct term *list); struct term *t_object(struct term *l); struct term *t_next(struct term *l); int t_length(struct term *list); term_ptr term_template(char *c); term_ptr read_pipe(int pipe_id); void write_pipe(int pipe_id,term_ptr term); void userInsertAppointment(int year, int month, int day, dayAppP day_rec); void userDeleteAppointment(int year, int month, int day, dayAppP day_rec); void send_to_agent(term_ptr agent_term, term_ptr term); dayAppP apiGetDateAppointments(int year, int month, int day, int *counter); dayAppP apiGetAppointment(int year, int month, int day, int start); extern term_ptr default_scheduler; /* ============================================================ | convert_date() | |----------------------------------------------------------| | Params : 1) a dayApp structure | | Desc : converts the structure to the corresponding term| | | | Returns: the term | |==========================================================| */ term_ptr convert_date(dayAppP day) { int i; term_ptr temp; term_ptr message; if(day==NULL) { message=term_template("i"); message->functor=0; } else { message=term_template("iissnn6"); message->term_type=TUPLE; message->argument_array[0]->functor=day->start; message->argument_array[1]->functor=day->end; message->argument_array[2]->const_string=copy_string(day->title); message->argument_array[3]->const_string=copy_string(day->info); switch( day->status) { case MEETING: case BLOCKED: case PRIVATE: message->argument_array[4]->functor=day->status; break; default: message->argument_array[4]->functor=NEW; break; } free(message->argument_array[5]); message->argument_array[5]=t_null(); for(i=day->participants_count-1;i>=0;i--) { temp=term_template("s"); temp->const_string=copy_string(day->participants[i].alias); message->argument_array[5]=t_cons(temp,message->argument_array[5]); } } return message; } void userInsertAppointment(int year, int month, int day, dayAppP day_rec) { term_ptr message=term_template("iiin4"); message->functor=INSERT; message->argument_array[0]->functor=year; message->argument_array[1]->functor=month; message->argument_array[2]->functor=day; free(message->argument_array[3]); message->argument_array[3]=convert_date(day_rec); send_to_agent(default_scheduler,message); free_term(message); } void userScheduleAppointment(int year, int month, int day, dayAppP day_rec) { term_ptr message=term_template("iiin4"); message->functor=SCHEDULE; message->argument_array[0]->functor=year; message->argument_array[1]->functor=month; message->argument_array[2]->functor=day; free(message->argument_array[3]); message->argument_array[3]=convert_date(day_rec); send_to_agent(default_scheduler,message); free_term(message); } void userDeleteAppointment(int year, int month, int day, dayAppP day_rec) { term_ptr message=term_template("iiin4"); message->functor=DELETE; message->argument_array[0]->functor=year; message->argument_array[1]->functor=month; message->argument_array[2]->functor=day; free(message->argument_array[3]); message->argument_array[3]=convert_date(day_rec); send_to_agent(default_scheduler,message); free_term(message); } /* ============================================================ | unconvert_date() | |----------------------------------------------------------| | Params : 1) a term representing a date structure | | Desc : converts the term representation to struct representation | | | Returns: a struct | |==========================================================| */ dayAppP unconvert_date(term_ptr term) { int i; term_ptr temp; char *name; dayAppP day=(dayAppP)malloc(sizeof(struct dayApp)); term_ptr *arg=term->argument_array; day->start=arg[0]->functor; day->end=arg[1]->functor; strcpy(day->title,arg[2]->const_string); strcpy(day->info,arg[3]->const_string); day->status=arg[4]->functor; day->participants_count=t_length(arg[5]); if (day->participants_count>MAX_PARTICIPANTS) { printf("error: too many participants in meeting\n"); return NULL; } else { temp=arg[5]; for (i=0;iparticipants_count;i++) { name=t_object(temp)->const_string; if (strlen(name)>=40) { printf("Error: name too long\n"); return NULL; } else { strcpy(day->participants[i].alias,name); temp=t_next(temp); } } } return day; } /* ============================================================ | insert_appointment() | |----------------------------------------------------------| | Params : 1)a term representing an insert update | | Desc : converts the term to the right data structure, and calls the relevant api function |==========================================================| */ void insert_appointment(term_ptr term) { term_ptr agent=term->argument_array[0]; term_ptr *arg=term->argument_array[1]->argument_array; dayAppP day_rec; term_ptr reference=arg[0]; int year=arg[1]->functor; int month=arg[2]->functor; int day=arg[3]->functor; day_rec=unconvert_date(arg[4]); apiInsertAppointment(year,month,day,day_rec); send_to_agent(agent,reference); free_term(term); free(day_rec); } /* ============================================================ | delete_appointment() | |----------------------------------------------------------| | Params : 1)a term representing a delete update | | Desc : converts the term to the right data structure, and calls the relevant api function |==========================================================| */ void delete_appointment(term_ptr term) { term_ptr agent=term->argument_array[0]; term_ptr *arg=term->argument_array[1]->argument_array; dayAppP day_rec; term_ptr reference=arg[0]; int year=arg[1]->functor; int month=arg[2]->functor; int day=arg[3]->functor; day_rec=unconvert_date(arg[4]); apiDeleteAppointment(year,month,day,day_rec); send_to_agent(agent,reference); free_term(term); free(day_rec); } /* ============================================================ | enquire_appointment() | |----------------------------------------------------------| | Params : 1)a term representing an enquiry to the database| | Desc : replies to the enquiry | | | |==========================================================| */ void enquire_appointment(term_ptr term) { term_ptr message; term_ptr enquire_list(term_ptr,int,int,int); term_ptr enquire_start(term_ptr,int,int,int,int); term_ptr scheduler=term->argument_array[0]; term_ptr *arg=term->argument_array[1]->argument_array; term_ptr ref=arg[0]; int enquiry=arg[1]->functor; int year=arg[2]->functor; int month=arg[3]->functor; int day=arg[4]->functor; switch(enquiry) { case LIST: message=enquire_list(ref,year,month,day); break; case START: { int hour=arg[5]->functor; message=enquire_start(ref,year,month,day,hour); } break; } send_to_agent(scheduler,message); free_term(term); free_term(message); } term_ptr enquire_list(term_ptr ref,int year,int month,int day) { term_ptr e_list=t_null(); term_ptr message=term_template("nn2"); int i,counter; dayAppP answer=apiGetDateAppointments(year,month,day,&counter); for(i=0;iargument_array[0]); free(message->argument_array[1]); message->term_type=TUPLE; message->argument_array[0]=ref; message->argument_array[1]=e_list; return message; } term_ptr enquire_start(term_ptr ref,int year,int month,int day,int hour) { term_ptr appointment; term_ptr message=term_template("nn2"); int i,counter; dayAppP answer=apiGetAppointment(year,month,day,hour); appointment=convert_date(answer); free(answer); free(message->argument_array[0]); free(message->argument_array[1]); message->term_type=TUPLE; message->argument_array[0]=ref; message->argument_array[1]=appointment; return message; }