static int search_prompt(char *withdef, char *withoutdef) { char buffer[80]; int len; if (!get_str(last_search ? withdef : withoutdef, buffer, TRUE)) return 0; /* user break */ if (!last_search && !*buffer) { strcpy (message, "Search aborted."); return 0; } if (!*buffer) { len = last_search->len; } else { len = parse_quoted (buffer); if (len == -1) { display_beep(); strcpy (message, "Invalid escape sequence in search string"); return 0; } if (last_search) free_search(last_search); last_search = build_search (buffer, len); } return 1; }
/* the agent is now a separate thread */ void * server_agent(void *params) { int client_fd, n, errcode; enum header_types type; unsigned int len; char to_search[MAX_SEARCH_STR]; char remote_obj[REMOTE_NAME_MAX]; time_type t_start, t_end; double tdiff; msg_one msg1; search *mysearch; Statistics statistic; /* we are now successfully connected to a remote client */ client_fd = ((struct thread_params *) params)->client_fd; fprintf(stderr, "Starting Agent fd: %d, thread id: %lu \n", client_fd, (unsigned long) pthread_self()); /* do some initialization*/ memset(&statistic, 0, sizeof(Statistics)); errcode = pthread_detach(pthread_self()); if (errcode != 0) { fprintf(stderr, "pthread_detach server agent: %s\n", strerror(errcode)); } pthread_once(&init_done, thread_init); get_time(&t_start); len = sizeof(msg_one); /* first message should be the file name */ if ((n = our_recv_message(client_fd, &type, &len, &msg1)) < 0) return NULL; if (type != OPTION_PARAMETER) return NULL; /*firstly build the search object */ mysearch = build_search(&msg1); mysearch->client_fd = client_fd; /* then print the search options */ print_search_para(client_fd, mysearch); /* receive the second message*/ len = MAX_SEARCH_STR; if ((n = our_recv_message(client_fd, &type, &len, to_search)) < 0) return NULL; if (type != TO_SEARCH) return NULL; len = strlen(to_search); if ((mysearch->search_pattern = (char*) malloc(len + 1)) == NULL) { perror("malloc"); free(params); return NULL; } strncpy(mysearch->search_pattern, to_search, len+1); /*build its own shift table if needed */ build_shifttable(mysearch); /* receive the second message*/ len = REMOTE_NAME_MAX; if ((n = our_recv_message(client_fd, &type, &len, remote_obj)) < 0) return NULL; if (type != REMOTE_NAME) return NULL; /* do the search job */ search_given(remote_obj, mysearch); /* send the statistics message 6*/ /*wait for directory search to be finished if it is on the server side*/ while (mysearch->stk_count != 0) { pthread_cond_wait(&(mysearch->ready), &(mysearch->lock)); } len = sizeof(Statistics); /* make a copy of little/big endian adapted statistical structure*/ update_statistics_sock(&statistic, &mysearch->statistics); trans_stat2send(&statistic); if (our_send_message(mysearch->client_fd, STATISTICS_MSG, len, &statistic) != 0) { fprintf(stderr, "Fail to send statistics\n"); return NULL; } get_time(&t_end); tdiff = time_diff(&t_start, &t_end); fprintf(stderr, "Search Statistics: client fd %u, thread id %lu\n", mysearch->client_fd, (unsigned long) pthread_self()); print_stat(stderr, &mysearch->statistics, tdiff); destroy_search(mysearch); fprintf(stderr, "Terminating Agent fd: %d, thread id: %lu \n", client_fd, (unsigned long) pthread_self()); free(params); return NULL; } /* server_agent */