/*********************************************************** * sc_link * ************************************************************ * Description: the c_module for linking the interpreter with the scheduler process * * * Created by: kave eshghi * * At: Thu Nov 19 17:56:56 1992 * ***********************************************************/ /* INCLUDES */ #include "k_winlib.h" #include "AOPcomm.h" #include "network.h" #include # include /* DECLARATIONS */ int register_module(char *module_name); int register_function(int module_number, char *function_name, function_type function, void *data); void register_generic(int module_number, generic_function function); #define INPUT_PIPE 0 #define OUTPUT_PIPE 1 void read_scheduler_pipe(int pipe_id); term_ptr read_pipe(int pipe_id); void write_pipe(int pipe_id,term_ptr term); void handle_scheduler_term(term_ptr term); void read_scheduler_pipe(int pipe_id); struct term *reconstruct_term( char *buffer); char *term_image(struct term *t); static int input_pipe; static int output_pipe; static int pid = -1; extern int errno; extern char *user_name; /* ============================================================ | startSchProcess() | |----------------------------------------------------------| |==========================================================| */ int startSchProcess(char *user_name) { int win_pipe[2]; int inter_pipe[2]; char c_input_pipe[10], c_output_pipe[10]; /* Open the pipes. */ if(pipe(win_pipe) < 0) return(0); if(pipe(inter_pipe) < 0) { close(win_pipe[INPUT_PIPE]); close(win_pipe[OUTPUT_PIPE]); return(-1); } /* Convert pipes number to strings. */ sprintf(c_input_pipe, "%d", win_pipe[INPUT_PIPE]); sprintf(c_output_pipe, "%d", inter_pipe[OUTPUT_PIPE]); /* Start the scheduler process. */ if((pid = fork()) < 0) {/* fork failed */ close(win_pipe[INPUT_PIPE]); close(win_pipe[OUTPUT_PIPE]); close(inter_pipe[INPUT_PIPE]); close(inter_pipe[OUTPUT_PIPE]); return(-1); } if(pid == 0) { /* child process */ close(inter_pipe[INPUT_PIPE]); close(win_pipe[OUTPUT_PIPE]); execlp("AOPcal", "AOPcal", c_input_pipe, c_output_pipe, user_name, (char *) 0); } else { /* parent process */ close(inter_pipe[OUTPUT_PIPE]); close(win_pipe[INPUT_PIPE]); input_pipe = inter_pipe[INPUT_PIPE]; output_pipe = win_pipe[OUTPUT_PIPE]; fcntl(input_pipe, F_SETFL, O_NDELAY); /* Register the pipe. */ registerPipe(input_pipe, &read_scheduler_pipe); } return(1); } /* ============================================================ | EndSchProcess() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ void EndSchProcess() { term_ptr exit_term; term_ptr *args; args=maketermarray(2); args[0]=maketerm(NONVAR,SCHEDULER,0,NULL,NULL); args[1]=maketerm(NONVAR,QUIT_REQUEST,0,NULL,NULL); exit_term=maketerm(TUPLE,0,2,NULL,args); write_pipe(output_pipe,exit_term); /* Unregister the pipe. */ unregisterPipe(input_pipe); /* Close the pipe. */ close(input_pipe); close(output_pipe); } void read_scheduler_pipe(int pipe_id) { term_ptr pipe_term=read_pipe(pipe_id); handle_scheduler_term(pipe_term); free_term(pipe_term); } extern int message_trace; void write_scheduler_pipe(term_ptr term) { if (message_trace) { printf("message to calendar window\n"); pt(term); } write_pipe(output_pipe,term); } void handle_scheduler_term(term_ptr term) { if (term==NULL) return; else { int recepient=term->argument_array[0]->functor; term_ptr content=copy_term(term->argument_array[1]); send_message(INFORM,recepient,CALENDAR,content); free_term(content); } } void scheduler_transmit(module_ptr agent,term_ptr term) { term_ptr *args=maketermarray(2); term_ptr transmit_term; args[0]=maketerm(NONVAR,agent->agent_functor,0,NULL,NULL); args[1]=copy_term(term); transmit_term=maketerm(TUPLE,0,2,NULL,args); write_scheduler_pipe(transmit_term); free_term(transmit_term); } int module_functor; void scheduler_init(int request_number) { int i; if (request_number==MODULE_INIT) { module_functor=register_module("sc_link"); startSchProcess(user_name); register_generic(module_functor,scheduler_transmit); } else if(request_number==MODULE_EXIT) EndSchProcess(); }