/* INCLUDES */ #include "k_winlib.h" #include "AOPcomm.h" #include "network.h" #include # include #define INPUT_PIPE 0 #define OUTPUT_PIPE 1 void read_window_pipe(int pipe_id); term_ptr read_pipe(int pipe_id); void write_pipe(int pipe_id,term_ptr term); void handle_window_term(term_ptr term); void read_window_pipe(int pipe_id); void EndWinProcess(); int unregisterPipe(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; /* ============================================================ | ErrSys() | |----------------------------------------------------------| |==========================================================| */ static void ErrSys(char *msg) { printf("Error: %s\n", msg); EndWinProcess(); } /* ============================================================ | startWinProcess() | |----------------------------------------------------------| |==========================================================| */ int startWinProcess(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 window 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("k_window", "k_window", 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_window_pipe); } return(1); } /* ============================================================ | EndWinProcess() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ void EndWinProcess(module_ptr agent) { term_ptr exit_term; term_ptr *args; args=maketermarray(2); args[0]=maketerm(NONVAR,agent->agent_functor,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); /* Exit the interpreter. */ exit_interpreter(); } void read_window_pipe(int pipe_id) { term_ptr pipe_term=read_pipe(pipe_id); handle_window_term(pipe_term); free_term(pipe_term); } void write_window_pipe(term_ptr term) { write_pipe(output_pipe,term); } void handle_window_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,X_WINDOW,content); free_term(content); } } /* ============================================================ | loadAgentFile() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Thu Sep 17 15:33:49 1992 | |==========================================================| */ int loadAgentFile(char *file_name) { module_ptr t1; int ret=0; char buffer[100]; t1 = read_module(file_name); if(t1 != NULL) { /* File loaded. */ /* Register the agent. */ if(!gethostname(buffer, 100)) { sendNetMessage(PACKET_TYPE_REGISTER, t1->name, buffer,"","", "", 0); ret=1; } else { printf("Can not get host name\n"); } } return(ret); } /* ============================================================ | preLoadAgent() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Thu Sep 17 16:11:42 1992 | |==========================================================| */ void preLoadAgents() { FILE *stream; struct stat dir_status; char temp[100]; if(stat(AGENTS_FILE,&dir_status) == -1){ /* No agent file. */ printf("Can not find %s\n",AGENTS_FILE); } else { /* Open the file. */ if ((stream = fopen(AGENTS_FILE, "r")) == NULL) { ErrSys("Unable to open file"); } while ( fscanf(stream,"%s",temp)!=EOF) { loadAgentFile(temp); } fclose(stream); } }