Esempio n. 1
0
void* msg_processor(void* parameter)
{
	fd_set readfds;
	char buf[BUFLEN] = {0};
	int numbytes = 0;

	fd_addr* client_p = (fd_addr*)parameter;

	if(!login(client_p))
		return;

	FD_ZERO(&readfds);

		write(client_p->fd," ----------------------------------------------\n", BUFLEN);
		write(client_p->fd," *                 Welcome                    *\n",BUFLEN);
		write(client_p->fd," *    Input \"Quit\" or \"quit\" to quit a chat   *\n", BUFLEN);
		write(client_p->fd," ----------------------------------------------\n",BUFLEN);
	while(1)
	{
		write(client_p->fd, "\n\t*********************************\n", BUFLEN);
		write(client_p->fd,   "\t* (1) Going to chatroom		*\n", BUFLEN);
		write(client_p->fd,   "\t* (2) Going to private chat	*\n", BUFLEN);
		write(client_p->fd,   "\t* (3) Exit			*\n", BUFLEN);
		write(client_p->fd,   "\t*********************************\n", BUFLEN);
		write(client_p->fd, "\n\tInput you chioce:\n", BUFLEN);

		FD_SET(client_p->fd, &readfds);
		//select((client_p->fd) + 1, &readfds, NULL, NULL, NULL);
		//if(FD_ISSET(client_p->fd, &readfds))
		{
			memset(buf, 0, BUFLEN);
			pthread_mutex_lock(&sock_mutex);
			if((numbytes = recv(client_p->fd, buf, BUFLEN, 0)) == 1)
			{
				perror("recv in send_all_user");
				close(client_p->fd);
				return;
			}
			pthread_mutex_unlock(&sock_mutex);
			buf[--numbytes] = '\0';  //buf recv have '\n'
#ifdef DEBUG_VERSION
			printf("recv:%s\n",buf);
#endif
	
			if(strlen(buf) != 1)
			{
				write(buf, "select error!!!\n", BUFLEN);
				sleep(1);
				continue;
			}

			if(buf[0] == '1')
			{
				printf("%s add to room\n", inet_ntoa(client_p->addr.sin_addr));
				sendto_all(">>>>>>>>>>>>>>come in>>>>>>>>>>>>>", client_p);

				client_p->room_flag = 1;
				chat_with_all(client_p);
				sleep(1);
			}

			else if(buf[0] == '2')
			{
				//TODO SEND TO A USER
				//......
				//chat_with_one(client_p, );
				//write(client_p->fd, "this service is in building...\n", BUFLEN);
				client_p->room_flag = 2;
				chat_with_one(client_p);
				sleep(1);
			}
			else if(buf[0] == '3')
			{
				write(client_p->fd, "EXIT", BUFLEN);
				close(client_p->fd);
				pthread_mutex_lock(&client_mutex);
				FD_ADDR_CLR(client_p);
				pthread_mutex_unlock(&client_mutex);
				return NULL;
			}
		}

		FD_CLR(client_p->fd, &readfds);
	}
	close(client_p->fd);
	pthread_mutex_lock(&client_mutex);
	FD_ADDR_CLR(client_p);
	pthread_mutex_unlock(&client_mutex);
	return NULL ;
}
Esempio n. 2
0
/*handle send*/
void *send_handle(void *threadarg){
    //char func='0';
    struct send_thread_data *send_data;
    char func[2];
    memset(&func,0,sizeof(func));
    send_data=(struct send_thread_data *)threadarg;
    //system("clear");
    while(1){
        //printf("Hello,%s\n",send_data->send_name);
        //printf("Function:\n");
        //printf("1.chat with one person;\n");
        //printf("2.chat with all the people online;\n");
        //printf("3.show all the people online;\n");
        //printf("4.logout;\n");
		memset(&func,0,sizeof(func));
		wclear(local_win_down);
        wclear(local_win_up);
        wborder(local_win_up,'|','|','-','-','+','+','+','+');
        wborder(local_win_down,'|',' ','-',' ','+',' ','+',' ');
        mvwprintw(local_win_right,1,1,"Hello,%s",send_data->send_name);
        mvwprintw(local_win_right,2,1,"Function");
        mvwprintw(local_win_right,3,1,"1.chat with one");
        mvwprintw(local_win_right,4,1,"2.chat with all");
        mvwprintw(local_win_right,5,1,"3.update namelist");
        mvwprintw(local_win_right,6,1,"4.logout");
        mvwprintw(local_win_right,7,1,"input  to return");
        mvwprintw(local_win_right,8,1,"=control+B");
		wrefresh(local_win_right);
        mvwprintw(local_win_down,1,1,"fuc:");
        mvwscanw(local_win_down,1,6,"%2s%*[^\n]",func);
        wrefresh(local_win_down);
        switch(func[0]){
            case '1': {//printf("chat name: %s\n",send_data->send_name);
                       //wclear(local_win_down); 
                       //mvwprintw(local_win_down,1,1,"chat name:%s\n",send_data->send_name);
                       //wrefresh(local_win_down);
                       chat_with_one(send_data);
                       break;
                      }
            case '2': {chat_with_all(send_data);break;}
            case '3': {show_user_list(send_data);break;}
            case '4': {
                        logout(send_data);
                        wclear(local_win_down);
                        wclear(local_win_left);
                        wclear(local_win_up);
                        wclear(local_win_right);
                        delwin(local_win_down);
                        delwin(local_win_up);
                        delwin(local_win_right);
                        delwin(local_win_left);
                        endwin();
                        exit(0);
                        break;}
            default : {
                    //printf("input error\n");
                wclear(local_win_down);
                wborder(local_win_down,'|',' ','-',' ','+',' ','+',' ');
                mvwprintw(local_win_down,2,1,"input error");   
                wrefresh(local_win_down); 
				getchar();
            }
        }
    }
}