/*********************************************************** * INTERPRETER WINDOWS RUTINES * ************************************************************ * Description: * * * * Created by: avrami tzur * * At: Mon Aug 3 10:52:06 1992 * ***********************************************************/ /* INCLUDES */ #include "winlib.h" #include "AOPcomm.h" #include "network.h" #include # include /* DECLARATIONS */ int startWinProcess(char *user_name); int SendToWindow(int type, module_ptr agent, term_pointer term); void readWinPipe(int pipe_id); void EndWinProcess(); int loadAgentFile(char *file_name); void preLoadAgents(); static void WinProcessMsg(int msg_type, module_ptr agent, term_pointer term, char *data); void WriteWinPipe(int msg_type, module_ptr agent, term_pointer term, int data_length, void *data); static void ErrSys(char *msg); struct term *t_null(); struct term *t_cons(struct term *t, struct term *list); extern int GetTermFunctor(term_pointer term); extern char *GetTermString(term_pointer term); extern int *GetTermValue(term_pointer term); extern term_pointer GetTermArgument(term_pointer term, int arg_num); extern char *TermToString(term_pointer term); #define READ_PIPE 0 #define WRITE_PIPE 1 static int read_pipe; static int write_pipe; static int pid = -1; extern int errno; /* ============================================================ | ErrSys() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ static void ErrSys(char *msg) { printf("Error: %s\n", msg); EndWinProcess(); } /* ============================================================ | startWinProcess() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ int startWinProcess(char *user_name) { int win_pipe[2]; int inter_pipe[2]; char c_read_pipe[10], c_write_pipe[10]; /* Open the pipes. */ if(pipe(win_pipe) < 0) return(0); if(pipe(inter_pipe) < 0) { close(win_pipe[READ_PIPE]); close(win_pipe[WRITE_PIPE]); return(-1); } /* Convert pipes number to strings. */ sprintf(c_read_pipe, "%d", win_pipe[READ_PIPE]); sprintf(c_write_pipe, "%d", inter_pipe[WRITE_PIPE]); /* Start the window process. */ if((pid = fork()) < 0) { /* Fork failed. */ close(win_pipe[READ_PIPE]); close(win_pipe[WRITE_PIPE]); close(inter_pipe[READ_PIPE]); close(inter_pipe[WRITE_PIPE]); return(-1); } if(pid == 0) { /* Child process. */ close(inter_pipe[READ_PIPE]); close(win_pipe[WRITE_PIPE]); execlp("window", "window", c_read_pipe, c_write_pipe, user_name, (char *) 0); } else { /* Parent process. */ close(inter_pipe[WRITE_PIPE]); close(win_pipe[READ_PIPE]); read_pipe = inter_pipe[READ_PIPE]; write_pipe = win_pipe[WRITE_PIPE]; fcntl(read_pipe, F_SETFL, O_NDELAY); /* Register the pipe. */ registerPipe(read_pipe, &readWinPipe); } return(1); } /* ============================================================ | EndWinProcess() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ void EndWinProcess() { WriteWinPipe(WIN_QUIT_REQUEST, NULL, NULL, 0, NULL); /* Unregister the pipe. */ unregisterPipe(read_pipe); /* Close the pipe. */ close(read_pipe); close(write_pipe); /* Exit the interpreter. */ exit_interpreter(); } /* ============================================================ | ReadWinPipe() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ void readWinPipe(int pipe_id) { int n; int header_size = sizeof(win_msg_header); win_msg_header header; char *buffer; if((n = read(read_pipe, (char *)&header, header_size)) > 0){ if (n!= header_size) ErrSys(" Window pipe - header read"); buffer = (char *)malloc(header.data_length); n = read(read_pipe, buffer, header.data_length); if(n != header.data_length ) ErrSys(" Window pipe - data read"); WinProcessMsg(header.msg_type, header.agent, header.term, buffer); free(buffer); } } /* ============================================================ | WriteWinPipe() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ void WriteWinPipe(int msg_type, module_ptr agent, term_pointer term, int data_length, void *data) { int header_size = sizeof(win_msg_header); char *buffer = (char *)malloc(header_size + data_length); win_msg_header *header = (win_msg_header *)buffer; char *ptr1 = (char *)(buffer + header_size); header->msg_type = msg_type; header->agent = agent; header->term = term; header->data_length = data_length; strncpy(ptr1, (char *)data, data_length); write(write_pipe, buffer, header_size + data_length); } /* ============================================================ | WinProcessMsg() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : Note: If you want to keep any passed data you | | must make a copy of it! | | | | Returns: | |==========================================================| */ static void WinProcessMsg(int msg_type, module_ptr agent, term_pointer term, char *data) { switch(msg_type){ case WIN_INFORM_OK_REPLY:{ /* Should be WIN_INFORM_ACKNOWLDGE */ struct term **args; term_pointer reply_term; /* Reply on inform request. */ if(agent != NULL) { /* Inform message came from an agent. */ /* Make the x_window_acknowldge term. */ reply_term = maketerm(NONVAR, X_WINDOW_ACKNOWLEDGE,1, NULL,maketermarray(1)); reply_term->argument_array[0] = term; send_message(INFORM, agent->agent_functor, USER, reply_term); } break; } case WIN_QUESTION_OK_REPLY:{ struct term **args; term_pointer reply_term; /* Reply on inform request. */ /* Make the x_window_acknowldge term. */ reply_term = maketerm(NONVAR, X_WINDOW_REPLY,2,NULL,maketermarray(2)); reply_term->argument_array[0] = term; reply_term->argument_array[1] = maketerm(NONVAR, YES,0,NULL,NULL); send_message(INFORM, agent->agent_functor, USER, reply_term); break; } case WIN_QUESTION_CANCEL_REPLY:{ struct term **args; term_pointer reply_term; /* Reply on inform request. */ /* Make the x_window_acknowldge term. */ reply_term = maketerm(NONVAR, X_WINDOW_REPLY,2,NULL,maketermarray(2)); reply_term->argument_array[0] = term; reply_term->argument_array[1] = maketerm(NONVAR, NO,0,NULL,NULL); send_message(INFORM, agent->agent_functor, USER, reply_term); break; } case WIN_INTERVAL_OK_REPLY:{ term_pointer reply_term; int *temp = (int *)data; int i; int counter=0; term_pointer reply_list=t_null();/* initiate to empty list*/ /* Reply on inform request. */ /* Count number of free slots */ for(i=0;i<9;i++) if(!temp[i]) counter++; counter = 0; /* Make the free slots tuple */ for(i=8;i>=0;i--) if(!temp[i]){ reply_list=t_cons(maketerm(INTEGER, i+8,0,NULL,NULL), reply_list); } /* Make the x_window_reply term. */ reply_term = maketerm(NONVAR, X_WINDOW_REPLY, 2, NULL, maketermarray(2)); reply_term->argument_array[0] = term; reply_term->argument_array[1] = reply_list; send_message(INFORM, agent->agent_functor, USER, reply_term); break; } case WIN_INTERVAL_CANCEL_REPLY:{ int counter=0; term_pointer reply_term; reply_term = maketerm(NONVAR, X_WINDOW_REPLY,2,NULL,maketermarray(2)); reply_term->argument_array[0] = term; reply_term->argument_array[1] = maketerm(TUPLE, 0,counter,NULL,NULL); send_message(INFORM, agent->agent_functor, USER, reply_term); break; } case WIN_PROCESS_TERM:{ struct clause *cl; if((cl=parse_interactive_input(data)) == NULL) return; send_message(cl->head->functor, cl->head->argument_array[0]->functor, functor_string("user"), cl->head->argument_array[1]); break; } case WIN_LOAD_AGENT:{ /* Load agent file */ if(!loadAgentFile(data)){ char buffer[100]; strcpy(buffer, "Are you sure that the file exists?"); WriteWinPipe(WIN_INFORM_REQUEST, NULL, NULL, strlen(buffer)+1, buffer); } break; } case WIN_QUIT_REQUEST:{ /* The window process like to quit */ EndWinProcess(); } } } /* ============================================================ | 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 ( fgets(temp, 100, stream) ) { /* Trim the new-line charecter. */ temp[strlen(temp)-1] = '\0'; loadAgentFile(temp); } fclose(stream); } }